Skip to content

Commit

Permalink
Always resolve and exclude paths relative to project root
Browse files Browse the repository at this point in the history
Fixes #82
  • Loading branch information
amyreese committed Mar 27, 2023
1 parent 3aa126c commit 9a8dad8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
11 changes: 3 additions & 8 deletions trailrunner/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,11 @@ def walk(self, path: Path, *, excludes: Excludes = None) -> Iterator[Path]:

def gen(children: Iterable[Path], *, explicit: bool = False) -> Iterator[Path]:
for child in children:
if ignore.match_file(child):
self.EXCLUDED[child] = "matched ignore"
relative = child.resolve().relative_to(root)
if ignore.match_file(relative):
self.EXCLUDED[child] = "matched gitignore"
continue

if child.is_absolute():
relative = child.relative_to(root)
if ignore.match_file(relative):
self.EXCLUDED[child] = "matched relative ignore"
continue

if child.is_file() and (explicit or include.match_file(child)):
yield child

Expand Down
92 changes: 92 additions & 0 deletions trailrunner/tests/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,98 @@ def test_walk(self) -> None:
]
self.assertListEqual(expected, result)

def test_walk_with_gitignore_matching_parents(self) -> None:
"""
test gitignore that matches paths outside of project root
"""
root = self.td / "inner"
root.mkdir()
(root / "pyproject.toml").write_text("")
(root / "foo.py").write_text("")
(root / "something.py").write_text("")
(root / "inner").mkdir()
(root / "inner" / "voice.py").write_text("")

with self.subTest("without gitignore"):
runner = core.Trailrunner()
result = sorted(runner.walk(root))
expected = sorted(
[
root / "foo.py",
root / "something.py",
root / "inner" / "voice.py",
]
)
self.assertListEqual(expected, result)

(root / ".gitignore").write_text("something.py\n")

with self.subTest("exclude something"):
runner = core.Trailrunner()
result = sorted(runner.walk(root))
expected = sorted(
[
root / "foo.py",
root / "inner" / "voice.py",
]
)
self.assertListEqual(expected, result)

with self.subTest("exclude something (relative)"):
with cd(root):
runner = core.Trailrunner()
result = sorted(runner.walk(Path(".")))
expected = sorted(
[
Path("foo.py"),
Path("inner") / "voice.py",
]
)
self.assertListEqual(expected, result)

with self.subTest("exclude something (relative inner)"):
with cd(root / "inner"):
runner = core.Trailrunner()
result = sorted(runner.walk(Path(".")))
expected = sorted(
[
Path("voice.py"),
]
)
self.assertListEqual(expected, result)

(root / ".gitignore").write_text("inner/\n")

with self.subTest("exclude inner"):
runner = core.Trailrunner()
result = sorted(runner.walk(root))
expected = sorted(
[
root / "foo.py",
root / "something.py",
]
)
self.assertListEqual(expected, result)

with self.subTest("exclude inner (relative)"):
with cd(root):
runner = core.Trailrunner()
result = sorted(runner.walk(Path(".")))
expected = sorted(
[
Path("foo.py"),
Path("something.py"),
]
)
self.assertListEqual(expected, result)

with self.subTest("exclude inner (relative inner)"):
with cd(root / "inner"):
runner = core.Trailrunner()
result = sorted(runner.walk(Path(".")))
expected = sorted([])
self.assertListEqual(expected, result)

def test_run(self) -> None:
def get_posix(path: Path) -> str:
return path.as_posix()
Expand Down

0 comments on commit 9a8dad8

Please sign in to comment.