Skip to content

Commit

Permalink
Verify generated headers can be parsed back
Browse files Browse the repository at this point in the history
This is just another test to ensure robustness
  • Loading branch information
emzeat committed Nov 27, 2023
1 parent ce27d67 commit 11ef630
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion license_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def patterns(style=None):
(Style.BATCH_STYLE,
r"REM(?P<authors>.+?)@LICENSE_HEADER_START@(?P<license>.+?)@LICENSE_HEADER_END@(?:.*?)REM\r?\n(?!REM)(?P<body>.*)"),
(Style.BATCH_STYLE,
r"REM(?P<authors>.+?)All rights reserved\.(?P<license>.+?)^(REM)?\r?\n(?!REM)(?P<body>.*)"),
r"REM(?P<authors>.+?)All rights reserved\.(?P<license>.+?)^(?P<body>(?!REM)(.*)|$)"),
(Style.BATCH_STYLE,
r"::(?P<authors>.+?)@LICENSE_HEADER_START@(?P<license>.+?)@LICENSE_HEADER_END@(?:.*?)::\r?\n(?!::)(?P<body>.*)"),
(Style.BATCH_STYLE,
Expand Down Expand Up @@ -262,6 +262,9 @@ def __init__(self, name: str, year_from: int = None,
def __repr__(self) -> str:
return f"({self.name} {self.year_from}-{self.year_to})"

def __eq__(self, other: object) -> bool:
return self.name == other.name and self.year_from == other.year_from and self.year_to == other.year_to


class GitRepo:
"""Git repository object"""
Expand Down Expand Up @@ -444,6 +447,8 @@ def __init__(self, file: pathlib.PurePath = None, contents: str = None):
if contents is None:
with open(file, 'r', encoding='utf-8', newline='') as file_obj:
contents = file_obj.read()
if isinstance(file, str):
file = pathlib.Path(file)
# style is determined from the extension, if unknown we try a second attempt using the contents below
self.style = Style.from_suffix(file.suffix)
if self.style == Style.UNKNOWN:
Expand Down
27 changes: 27 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,9 @@ def test_generator(self):
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
Expand All @@ -709,6 +712,9 @@ def test_generator(self):
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
Expand All @@ -720,6 +726,9 @@ def test_generator(self):
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
Expand All @@ -731,6 +740,9 @@ def test_generator(self):
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
Expand All @@ -742,6 +754,9 @@ def test_generator(self):
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
Expand All @@ -753,6 +768,9 @@ def test_generator(self):
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
Expand All @@ -764,6 +782,9 @@ def test_generator(self):
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
Expand All @@ -776,6 +797,9 @@ def test_generator(self):
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
Expand All @@ -788,6 +812,9 @@ def test_generator(self):
try:
with open(BASE / 'test' / filename, 'r') as expected:
self.assertEqual(expected.read(), output)
parsed = license_tools.ParsedHeader(
contents=output, file=filename)
self.assertListEqual(authors, parsed.authors)
except:
with open(BASE / 'test' / filename, 'w') as expected:
expected.write(output)
Expand Down

0 comments on commit 11ef630

Please sign in to comment.