Skip to content

Commit

Permalink
Easier testing for update-header main function
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks committed Feb 26, 2024
1 parent 91ce1f6 commit c31ef4d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 44 deletions.
8 changes: 4 additions & 4 deletions pontos/updateheader/updateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Optional, Union
from typing import Optional, Sequence, Union

from pontos.errors import PontosError
from pontos.git import Git
Expand Down Expand Up @@ -298,8 +298,8 @@ def _compile_copyright_regex(company: Union[str, list[str]]) -> re.Pattern:
return re.compile(rf"{c_str}.*? {d_str}?-? ?{d_str}? ({'|'.join(company)})")


def main() -> None:
parsed_args = parse_args()
def main(args: Optional[Sequence[str]] = None) -> None:
parsed_args = parse_args(args)
exclude_list = []
year: str = parsed_args.year
license_id: str = parsed_args.license_id
Expand Down Expand Up @@ -349,7 +349,7 @@ def main() -> None:
except PontosError:
term.warning(
f"{file}: Could not get date of last modification"
f" using git, using {year} instead."
f" via git, using {year} instead."
)

try:
Expand Down
69 changes: 29 additions & 40 deletions tests/updateheader/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,25 +463,17 @@ def setUp(self) -> None:
self.args = Namespace()
self.args.company = "Greenbone AG"

@patch("pontos.updateheader.updateheader.parse_args")
def test_main(self, argparser_mock):
self.args.year = "2021"
self.args.changed = False
self.args.license_id = "AGPL-3.0-or-later"
self.args.files = ["test.py"]
self.args.directories = None
self.args.verbose = 0
self.args.log_file = None
self.args.quiet = False
self.args.cleanup = False

argparser_mock.return_value = self.args

def test_main(self):
args = [
"--year",
"2021",
"--license",
"AGPL-3.0-or-later",
"--files",
"test.py",
]
with redirect_stdout(StringIO()):
code = True if not main() else False

# I have no idea how or why test main ...
self.assertTrue(code)
main(args)

@patch("sys.stdout", new_callable=StringIO)
@patch("pontos.updateheader.updateheader.parse_args")
Expand All @@ -508,33 +500,30 @@ def test_main_never_happen(self, argparser_mock, mock_stdout):
ret,
)

@patch("sys.stdout", new_callable=StringIO)
@patch("pontos.updateheader.updateheader.parse_args")
def test_update_file_changed(self, argparser_mock, mock_stdout):
self.args.year = "1995"
self.args.license_id = "AGPL-3.0-or-later"
self.args.changed = True
self.args.directories = None
self.args.verbose = 0
self.args.log_file = None
self.args.quiet = False
self.args.cleanup = False

argparser_mock.return_value = self.args
def test_update_file_changed_no_git(self):
args = [
"--changed",
"--year",
"1999",
"--files",
]

with temp_directory(change_into=True) as temp_dir:
with (
redirect_stdout(StringIO()) as out,
temp_directory(change_into=True) as temp_dir,
):
test_file = temp_dir / "test.py"
self.args.files = str(test_file)
args.append(str(test_file))

main()
main(args)

ret = mock_stdout.getvalue()
ret = out.getvalue()

self.assertIn(f"{test_file}", ret)
self.assertIn("Could not get date", ret)
self.assertIn("of last modification using git,", ret)
self.assertIn(f"using {self.args.year} instead.", ret)
self.assertIn("File is not existing.", ret)
self.assertIn(
"Could not get date of last modification via git, "
f"using 1999 instead.{test_file}: File is not existing.",
ret.replace("\n", ""),
)


class RemoveOutdatedLinesTestCase(TestCase):
Expand Down

0 comments on commit c31ef4d

Please sign in to comment.