Skip to content

Commit

Permalink
Merge pull request #983 from carmenbianca/test-asterisk
Browse files Browse the repository at this point in the history
Add some tests for asterisks
  • Loading branch information
carmenbianca committed May 11, 2024
2 parents 7ab8124 + 8d40de9 commit 0830a2f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/reuse/global_licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,7 @@ def translate(path: str) -> str:
globstar = True
blocks.append(r".*")
elif char == "/":
if globstar:
pass
else:
globstar = False
if not globstar:
blocks.append("/")
escaping = False
else:
Expand All @@ -371,6 +368,8 @@ def translate(path: str) -> str:
globstar = False
escaping = False
prev_char = char
if prev_char == "*" and not globstar:
blocks.append(r"[^/]*")
result = "".join(blocks)
return f"^({result})$"

Expand Down
25 changes: 25 additions & 0 deletions tests/test_global_licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ def test_only_in_dir(self):
assert item.matches("src/foo.py")
assert not item.matches("src/other/foo.py")

def test_asterisk(self):
"""Match everything in local directory."""
item = AnnotationsItem(paths=["*"])
assert item.matches("foo.py")
assert item.matches(".gitignore")
assert not item.matches("src/foo.py")
assert not item.matches(".foo/bar")

def test_asterisk_asterisk(self):
"""Match everything."""
item = AnnotationsItem(paths=["**"])
assert item.matches("foo.py")
assert item.matches(".gitignore")
assert item.matches("src/foo.py")
assert item.matches(".foo/bar")

def test_escape_asterisk(self):
"""Handle escape asterisk."""
item = AnnotationsItem(paths=[r"\*.py"])
Expand Down Expand Up @@ -288,6 +304,15 @@ def test_escape_a(self):
assert item.matches(r"a")
assert not item.matches(r"\a")

def test_middle_asterisk(self):
"""See what happens if the asterisk is in the middle of the path."""
item = AnnotationsItem(paths=["foo*bar"])
assert item.matches("foobar")
assert item.matches("foo2bar")
assert not item.matches("foo")
assert not item.matches("bar")
assert not item.matches("foo/bar")

def test_multiple_paths(self):
"""Match one of multiple files."""
item = AnnotationsItem(paths=["*.py", "*.js", "README"])
Expand Down

0 comments on commit 0830a2f

Please sign in to comment.