Skip to content

Commit

Permalink
Change: Remove copyright_regex from update_header function
Browse files Browse the repository at this point in the history
The update_header function just expect a year, license and company name
at the end.
  • Loading branch information
bjoernricks committed Feb 26, 2024
1 parent a4312d3 commit 3d03b09
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
9 changes: 3 additions & 6 deletions pontos/updateheader/updateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,16 @@ def update_file(
year: str,
license_id: str,
company: str,
copyright_regex: re.Pattern,
cleanup_regexes: Optional[List[re.Pattern]] = None,
) -> None:
"""Function to update the header of the given file
Checks if header exists. If not it adds an header to that file, otherwise it
checks if year is up to date
"""

copyright_regex = _compile_copyright_regex(company=[company, OLD_COMPANY])

try:
with file.open("r+") as fp:
found = False
Expand Down Expand Up @@ -341,10 +343,6 @@ def main() -> None:
term.error("Specify files to update!")
sys.exit(1)

copyright_regex: re.Pattern = _compile_copyright_regex(
company=[parsed_args.company, OLD_COMPANY]
)

cleanup_regexes: Optional[List[re.Pattern]] = None
if cleanup:
cleanup_regexes = _compile_outdated_regex()
Expand All @@ -368,7 +366,6 @@ def main() -> None:
year,
license_id,
company,
copyright_regex,
cleanup_regexes,
)
except (FileNotFoundError, UnicodeDecodeError, ValueError):
Expand Down
26 changes: 3 additions & 23 deletions tests/updateheader/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#

import datetime
import re
import struct
from argparse import Namespace
from contextlib import redirect_stdout
Expand All @@ -16,14 +15,13 @@
from pontos.errors import PontosError
from pontos.terminal.terminal import ConsoleTerminal
from pontos.testing import temp_directory, temp_file
from pontos.updateheader.updateheader import _add_header as add_header
from pontos.updateheader.updateheader import (
OLD_COMPANY,
_compile_copyright_regex,
main,
parse_args,
update_file,
)
from pontos.updateheader.updateheader import _add_header as add_header
from pontos.updateheader.updateheader import (
_compile_outdated_regex as compile_outdated_regex,
)
Expand Down Expand Up @@ -74,12 +72,9 @@ def test_get_modified_year_error(self):


class FindCopyRightTestCase(TestCase):
def setUp(self):
def setUp(self) -> None:
self.company = "Greenbone AG"
self.regex = re.compile(
"(SPDX-FileCopyrightText:|[Cc]opyright).*?(19[0-9]{2}|20[0-9]{2}) "
f"?-? ?(19[0-9]{{2}}|20[0-9]{{2}})? ({self.company})"
)
self.regex = _compile_copyright_regex(self.company)

def test_find_copyright(self):
test_line = "# Copyright (C) 1995-2021 Greenbone AG"
Expand Down Expand Up @@ -193,11 +188,6 @@ def setUp(self):

self.path = Path(__file__).parent

self.regex = re.compile(
"(SPDX-FileCopyrightText:|[Cc]opyright).*?(19[0-9]{2}|20[0-9]{2}) "
f"?-? ?(19[0-9]{{2}}|20[0-9]{{2}})? ({self.company})"
)

@patch("sys.stdout", new_callable=StringIO)
def test_update_file_not_existing(self, mock_stdout):
year = "2020"
Expand All @@ -212,7 +202,6 @@ def test_update_file_not_existing(self, mock_stdout):
year,
license_id,
self.company,
copyright_regex=self.regex,
)

ret = mock_stdout.getvalue()
Expand All @@ -232,7 +221,6 @@ def test_update_file_wrong_license(self, mock_stdout):
year,
license_id,
self.company,
copyright_regex=self.regex,
)

ret = mock_stdout.getvalue()
Expand All @@ -253,7 +241,6 @@ def test_update_file_suffix_invalid(self, mock_stdout):
year,
license_id,
self.company,
copyright_regex=self.regex,
)

ret = mock_stdout.getvalue()
Expand All @@ -280,7 +267,6 @@ def test_update_file_binary_file(self, mock_stdout):
year,
license_id,
self.company,
copyright_regex=self.regex,
)

ret = mock_stdout.getvalue()
Expand All @@ -302,7 +288,6 @@ def test_update_create_header(self, mock_stdout):
year,
license_id,
self.company,
copyright_regex=self.regex,
)

ret = mock_stdout.getvalue()
Expand All @@ -328,7 +313,6 @@ def test_update_header_in_file(self, mock_stdout):
year,
license_id,
self.company,
copyright_regex=self.regex,
)

ret = mock_stdout.getvalue()
Expand Down Expand Up @@ -356,7 +340,6 @@ def test_update_header_ok_in_file(self, mock_stdout):
year,
license_id,
self.company,
copyright_regex=self.regex,
)

ret = mock_stdout.getvalue()
Expand Down Expand Up @@ -416,9 +399,6 @@ def test_cleanup_file(self):
year,
license_id,
company,
copyright_regex=_compile_copyright_regex(
["Greenbone AG", OLD_COMPANY]
),
cleanup_regexes=compiled_regexes,
)

Expand Down

0 comments on commit 3d03b09

Please sign in to comment.