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) diff --git a/src/reuse/global_licensing.py b/src/reuse/global_licensing.py index 3ca566578..18fef001f 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 @@ -360,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 == "*": @@ -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..175623916 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"]) @@ -293,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."""