From c43d489679ff85644ea14c82f1b88e9068eff7ca Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Thu, 26 Sep 2024 14:38:47 +0200 Subject: [PATCH 1/3] Fix a globbing cornercase 'directory-*/file' did not correctly glob previously. The glob simply did not apply. Signed-off-by: Carmen Bianca BAKKER --- src/reuse/global_licensing.py | 3 +++ tests/test_global_licensing.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/src/reuse/global_licensing.py b/src/reuse/global_licensing.py index 3ca566578..2c95af813 100644 --- a/src/reuse/global_licensing.py +++ b/src/reuse/global_licensing.py @@ -352,6 +352,7 @@ class AnnotationsItem: def __attrs_post_init__(self) -> None: def translate(path: str) -> str: + # pylint: disable=too-many-branches blocks = [] escaping = False globstar = False @@ -372,6 +373,8 @@ def translate(path: str) -> str: blocks.append(r".*") elif char == "/": if not globstar: + if prev_char == "*": + blocks.append("[^/]*") blocks.append("/") escaping = False else: diff --git a/tests/test_global_licensing.py b/tests/test_global_licensing.py index 2a415426f..5dc73441a 100644 --- a/tests/test_global_licensing.py +++ b/tests/test_global_licensing.py @@ -257,6 +257,11 @@ def test_asterisk_asterisk(self): assert item.matches("src/foo.py") assert item.matches(".foo/bar") + def test_asterisk_slash(self): + """Handle asterisk slash.""" + item = AnnotationsItem(paths=[r"*/file"]) + assert item.matches("foo/file") + def test_escape_asterisk(self): """Handle escape asterisk.""" item = AnnotationsItem(paths=[r"\*.py"]) From b760c61103b6cefafeaf4a52fbc16cc091d2c343 Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Thu, 26 Sep 2024 14:39:57 +0200 Subject: [PATCH 2/3] Small improvements to globbing - Don't use the ambiguous r"\\". - Add a missing test. Signed-off-by: Carmen Bianca BAKKER --- src/reuse/global_licensing.py | 2 +- tests/test_global_licensing.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/reuse/global_licensing.py b/src/reuse/global_licensing.py index 2c95af813..18fef001f 100644 --- a/src/reuse/global_licensing.py +++ b/src/reuse/global_licensing.py @@ -361,7 +361,7 @@ def translate(path: str) -> str: if char == "\\": if prev_char == "\\" and escaping: escaping = False - blocks.append(r"\\") + blocks.append("\\\\") else: escaping = True elif char == "*": diff --git a/tests/test_global_licensing.py b/tests/test_global_licensing.py index 5dc73441a..175623916 100644 --- a/tests/test_global_licensing.py +++ b/tests/test_global_licensing.py @@ -298,6 +298,7 @@ def test_escape_escape_asterisk(self): """Handle escape escape asterisk.""" item = AnnotationsItem(paths=[r"\\*.py"]) assert item.matches(r"\foo.py") + assert not item.matches(r"foo.py") def test_asterisk_asterisk_asterisk(self): """Handle asterisk asterisk asterisk.""" From 0f5a87d6fad0cc74834e65a9fe6012c9922fc1d3 Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Thu, 26 Sep 2024 14:42:43 +0200 Subject: [PATCH 3/3] Add change log entry Signed-off-by: Carmen Bianca BAKKER --- changelog.d/fixed/glob.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelog.d/fixed/glob.md diff --git a/changelog.d/fixed/glob.md b/changelog.d/fixed/glob.md new file mode 100644 index 000000000..dac4d9525 --- /dev/null +++ b/changelog.d/fixed/glob.md @@ -0,0 +1,2 @@ +- Fixed the globbing of a single asterisk succeeded by a slash (e.g. + `directory-*/foo.py`). The glob previously did nothing. (#1078)