Skip to content

Commit

Permalink
Add: single-year flag to command pontos-update-header
Browse files Browse the repository at this point in the history
To enable using only creation year for header license.
  • Loading branch information
llugin authored and greenbonebot committed Aug 13, 2024
1 parent 139faba commit b7a3bd0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pontos/updateheader/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,15 @@ def parse_args(args: Optional[Sequence[str]] = None) -> Namespace:
help="Do a cleanup: Remove lines from outdated header format",
)

parser.add_argument(
"--single-year",
action="store_true",
default=False,
help=(
"If set, will not update license with from-to years format "
"(YYYY-YYYY) if it has single (only creation) year format (YYYY). "
"Default is %(default)s."
),
)

return parser.parse_args(args)
9 changes: 8 additions & 1 deletion pontos/updateheader/updateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _get_modified_year(f: Path) -> str:
try:
ret = Git().log("-1", "--date=format:%Y", str(f), format="%ad")[0]
except IndexError:
raise PontosError(f"Empty \"git log -1\" output for {f}.")
raise PontosError(f'Empty "git log -1" output for {f}.')

return ret

Expand Down Expand Up @@ -154,6 +154,7 @@ def update_file(
company: str,
*,
cleanup: bool = False,
single_year: bool = False,
) -> None:
"""Function to update the header of the given file
Expand Down Expand Up @@ -213,6 +214,10 @@ def update_file(
or copyright_match.modification_year
and copyright_match.modification_year < year
):
if single_year:
# In case of single year updating the license with modification date doesn't make sense.
# Changing the existing license header with created-modified years to single year is not supported.
return
copyright_term = (
f"SPDX-FileCopyrightText: "
f"{copyright_match.creation_year}"
Expand Down Expand Up @@ -314,6 +319,7 @@ def main(args: Optional[Sequence[str]] = None) -> None:
changed: bool = parsed_args.changed
quiet: bool = parsed_args.quiet
cleanup: bool = parsed_args.cleanup
single_year: bool = parsed_args.single_year

if quiet:
term: Union[NullTerminal, RichTerminal] = NullTerminal()
Expand Down Expand Up @@ -370,6 +376,7 @@ def main(args: Optional[Sequence[str]] = None) -> None:
license_id,
company,
cleanup=cleanup,
single_year=single_year,
)
except (FileNotFoundError, UnicodeDecodeError, ValueError):
continue
Expand Down
44 changes: 44 additions & 0 deletions tests/updateheader/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,26 @@ def test_update_create_header(self, mock_stdout):
expected_header, test_file.read_text(encoding="utf-8")
)

@patch("sys.stdout", new_callable=StringIO)
def test_update_create_header_single_year(self, mock_stdout):
year = "1995"
license_id = "AGPL-3.0-or-later"

expected_header = HEADER.format(date="1995") + "\n\n"

with temp_file(name="test.py", change_into=True) as test_file:
update_file(
test_file, year, license_id, self.company, single_year=True
)
ret = mock_stdout.getvalue()
self.assertEqual(
f"{test_file}: Added license header.\n",
ret,
)
self.assertEqual(
expected_header, test_file.read_text(encoding="utf-8")
)

@patch("sys.stdout", new_callable=StringIO)
def test_update_header_in_file(self, mock_stdout):
year = "2021"
Expand Down Expand Up @@ -318,6 +338,28 @@ def test_update_header_in_file(self, mock_stdout):
test_file.read_text(encoding="utf-8"),
)

@patch("sys.stdout", new_callable=StringIO)
def test_update_header_in_file_single_year(self, mock_stdout):
year = "2021"
license_id = "AGPL-3.0-or-later"

header = HEADER.format(date="2020")
with temp_file(
content=header, name="test.py", change_into=True
) as test_file:
update_file(
test_file,
year,
license_id,
self.company,
single_year=True,
)

self.assertIn(
"# SPDX-FileCopyrightText: 2020 Greenbone AG",
test_file.read_text(encoding="utf-8"),
)

@patch("sys.stdout", new_callable=StringIO)
def test_update_header_ok_in_file(self, mock_stdout):
year = "2021"
Expand Down Expand Up @@ -510,6 +552,7 @@ def test_defaults(self):
self.assertEqual(args.files, ["foo.txt"])
self.assertIsNone(args.directories)
self.assertIsNone(args.exclude_file)
self.assertFalse(args.single_year)
self.assertFalse(args.cleanup)

def test_files_and_directories_mutual_exclusive(self):
Expand Down Expand Up @@ -569,6 +612,7 @@ def test_main_never_happen(self, argparser_mock, mock_stdout):
self.args.log_file = None
self.args.quiet = False
self.args.cleanup = False
self.args.single_year = False

argparser_mock.return_value = self.args

Expand Down

0 comments on commit b7a3bd0

Please sign in to comment.