Skip to content

Commit 812cd5a

Browse files
committed
[cr checker]: fix copyright detection for year ranges
1 parent 8a95cc5 commit 812cd5a

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

cr_checker/tests/test_cr_checker.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,19 @@ def test_process_files_detects_duplicate_header(tmp_path):
443443

444444
assert results["duplicate_copyright"] == 1
445445
assert results["no_copyright"] == 0
446+
447+
448+
# test that has_duplicate_copyright detects two headers with different year ranges
449+
def test_has_duplicate_copyright_detects_different_year_ranges(tmp_path):
450+
cr_checker = load_cr_checker_module()
451+
test_file = tmp_path / "file.py"
452+
header_template = load_template("py")
453+
header1 = header_template.format(year="2026", author="Author")
454+
header2 = header_template.format(year="2024-2026", author="Author")
455+
test_file.write_text(header1 + header2 + "some content\n", encoding="utf-8")
456+
457+
result = cr_checker.has_duplicate_copyright(
458+
test_file, header_template, False, "utf-8", 0
459+
)
460+
461+
assert result is True

cr_checker/tool/cr_checker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def has_copyright(path, template, use_mmap, encoding, offset, config=None):
396396
if BORDER_FILL_PATTERN.search(stripped_line):
397397
regex_parts.append(line_to_flexible_regex(line))
398398
else:
399-
formatted = line.format(year=r"\\d\{4\}", author=r"\.\*")
399+
formatted = line.format(year=r"\\d\{4\}\(-\\d\{4\}\)\?", author=r"\.\*")
400400
regex_parts.append(convert_bre_to_regex(formatted))
401401
template_regex = "".join(regex_parts) + "\n?"
402402

@@ -431,7 +431,7 @@ def has_duplicate_copyright(path, template, use_mmap, encoding, offset):
431431
if BORDER_FILL_PATTERN.search(stripped_line):
432432
regex_parts.append(line_to_flexible_regex(line))
433433
else:
434-
formatted = line.format(year=r"\\d\{4\}", author=r"\.\*")
434+
formatted = line.format(year=r"\\d\{4\}\(-\\d\{4\}\)\?", author=r"\.\*")
435435
regex_parts.append(convert_bre_to_regex(formatted))
436436
template_regex = "\n?".join(regex_parts)
437437

0 commit comments

Comments
 (0)