From d7a7084abeed0c3c88c6852864e67ada3c87c4cf Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Tue, 16 Jan 2024 23:23:19 +0100 Subject: [PATCH] Update multiple licence year ends in one run - close #94 --- pre_commit_hooks/insert_license.py | 5 +++-- tests/insert_license_test.py | 14 ++++++++++++++ .../LICENSE_with_multiple_year_ranges.txt | 5 +++++ .../module_with_multiple_stale_years_in_license.py | 8 ++++++++ .../module_with_multiple_years_in_license.py | 8 ++++++++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/resources/LICENSE_with_multiple_year_ranges.txt create mode 100644 tests/resources/module_with_multiple_stale_years_in_license.py create mode 100644 tests/resources/module_with_multiple_years_in_license.py diff --git a/pre_commit_hooks/insert_license.py b/pre_commit_hooks/insert_license.py index 42a01b1..19c9929 100644 --- a/pre_commit_hooks/insert_license.py +++ b/pre_commit_hooks/insert_license.py @@ -396,14 +396,15 @@ def try_update_year_range( :return: source file contents and a flag indicating update """ current_year = datetime.now().year + changed = False for i in range(license_header_index, license_header_index + license_length): updated = try_update_year( src_file_content[i], src_filepath, current_year, introduce_range=True ) if updated: src_file_content[i] = updated - return src_file_content, True - return src_file_content, False + changed = True + return src_file_content, changed def license_found( diff --git a/tests/insert_license_test.py b/tests/insert_license_test.py index 2972849..85f2ef4 100644 --- a/tests/insert_license_test.py +++ b/tests/insert_license_test.py @@ -425,6 +425,20 @@ def _convert_line_ending(file_path, new_line_endings): ), ), ), + product( + ("LICENSE_with_multiple_year_ranges.txt",), + ("\n",), + ( + ( + "module_with_multiple_stale_years_in_license.py", + "#", + "module_with_multiple_years_in_license.py", + "", + True, + ["--use-current-year"], + ), + ), + ), ), ), ) diff --git a/tests/resources/LICENSE_with_multiple_year_ranges.txt b/tests/resources/LICENSE_with_multiple_year_ranges.txt new file mode 100644 index 0000000..01fca7f --- /dev/null +++ b/tests/resources/LICENSE_with_multiple_year_ranges.txt @@ -0,0 +1,5 @@ +Copyright (C) 2016-2017 Teela O'Malley + +Licensed under the Apache License, Version 2.0 (the "License"); + +@copyright 2012-2015 Teela O'Malley \ No newline at end of file diff --git a/tests/resources/module_with_multiple_stale_years_in_license.py b/tests/resources/module_with_multiple_stale_years_in_license.py new file mode 100644 index 0000000..cfeac1b --- /dev/null +++ b/tests/resources/module_with_multiple_stale_years_in_license.py @@ -0,0 +1,8 @@ +# Copyright (C) 2012-2015 Teela O'Malley +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# +# @copyright 2012-2015 Teela O'Malley + +import sys +sys.stdout.write("FOO") diff --git a/tests/resources/module_with_multiple_years_in_license.py b/tests/resources/module_with_multiple_years_in_license.py new file mode 100644 index 0000000..eaf5cdf --- /dev/null +++ b/tests/resources/module_with_multiple_years_in_license.py @@ -0,0 +1,8 @@ +# Copyright (C) 2012-2017 Teela O'Malley +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# +# @copyright 2012-2017 Teela O'Malley + +import sys +sys.stdout.write("FOO")