Skip to content

Commit 6164edf

Browse files
committed
Added passing test for nested .gitignore
Refs simonw#40 - though I had hoped this test would fail.
1 parent bbbf5cf commit 6164edf

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

tests/test_files_to_prompt.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import os
22
import pytest
3+
import re
34

45
from click.testing import CliRunner
56

67
from files_to_prompt.cli import cli
78

89

10+
def filenames_from_cxml(cxml_string):
11+
"Return set of filenames from <source>...</source> tags"
12+
return set(re.findall(r"<source>(.*?)</source>", cxml_string))
13+
14+
915
def test_basic_functionality(tmpdir):
1016
runner = CliRunner()
1117
with tmpdir.as_cwd():
@@ -44,23 +50,44 @@ def test_ignore_gitignore(tmpdir):
4450
runner = CliRunner()
4551
with tmpdir.as_cwd():
4652
os.makedirs("test_dir")
53+
os.makedirs("test_dir/nested_include")
54+
os.makedirs("test_dir/nested_ignore")
4755
with open("test_dir/.gitignore", "w") as f:
4856
f.write("ignored.txt")
4957
with open("test_dir/ignored.txt", "w") as f:
5058
f.write("This file should be ignored")
5159
with open("test_dir/included.txt", "w") as f:
5260
f.write("This file should be included")
53-
54-
result = runner.invoke(cli, ["test_dir"])
61+
with open("test_dir/nested_include/included2.txt", "w") as f:
62+
f.write("This nested file should be included")
63+
with open("test_dir/nested_ignore/.gitignore", "w") as f:
64+
f.write("nested_ignore.txt")
65+
with open("test_dir/nested_ignore/nested_ignore.txt", "w") as f:
66+
f.write("This nested file should not be included")
67+
with open("test_dir/nested_ignore/actually_include.txt", "w") as f:
68+
f.write("This nested file should actually be included")
69+
70+
result = runner.invoke(cli, ["test_dir", "-c"])
5571
assert result.exit_code == 0
56-
assert "test_dir/ignored.txt" not in result.output
57-
assert "test_dir/included.txt" in result.output
58-
59-
result = runner.invoke(cli, ["test_dir", "--ignore-gitignore"])
60-
assert result.exit_code == 0
61-
assert "test_dir/ignored.txt" in result.output
62-
assert "This file should be ignored" in result.output
63-
assert "test_dir/included.txt" in result.output
72+
filenames = filenames_from_cxml(result.output)
73+
74+
assert filenames == {
75+
"test_dir/included.txt",
76+
"test_dir/nested_include/included2.txt",
77+
"test_dir/nested_ignore/actually_include.txt",
78+
}
79+
80+
result2 = runner.invoke(cli, ["test_dir", "-c", "--ignore-gitignore"])
81+
assert result2.exit_code == 0
82+
filenames2 = filenames_from_cxml(result2.output)
83+
84+
assert filenames2 == {
85+
"test_dir/included.txt",
86+
"test_dir/ignored.txt",
87+
"test_dir/nested_include/included2.txt",
88+
"test_dir/nested_ignore/nested_ignore.txt",
89+
"test_dir/nested_ignore/actually_include.txt",
90+
}
6491

6592

6693
def test_multiple_paths(tmpdir):

0 commit comments

Comments
 (0)