Debian Codegraph @

tests/ignore_test.py

raw
1 #!/usr/bin/env python3
2 # Copyright: 2026, Maximiliano Curia <[email protected]>
3 #
4 # License: ISC
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 # .
9 # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 # PERFORMANCE OF THIS SOFTWARE.
16
17 """Contract of IgnoreMatcher.
18
19 Files-Excluded matching: an exact path pattern, a directory pattern excluding
20 its subtree via the ancestor walk, a glob, an unrelated path, and the empty-list
21 no-op. Gitignore matching: root and nested .gitignore files, directory patterns,
22 negation re-inclusion, and the use_gitignore=False gate.
23 """
24
25 from decopy.fstree.ignore import IgnoreMatcher
26
27
28 def _matcher(files_excluded):
29 return IgnoreMatcher(
30 use_gitignore=False, files_excluded=tuple(files_excluded), root="/nonexistent"
31 )
32
33
34 def _git_matcher(root, *, use_gitignore=True, files_excluded=()):
35 return IgnoreMatcher(
36 use_gitignore=use_gitignore,
37 files_excluded=tuple(files_excluded),
38 root=str(root),
39 )
40
41
42 def test_exact_path_pattern_matches_only_that_path():
43 matcher = _matcher(["foo/bar.c"])
44 assert matcher.is_ignored("foo/bar.c", is_dir=False)
45 assert not matcher.is_ignored("foo/bar.cx", is_dir=False)
46 assert not matcher.is_ignored("foo/baz.c", is_dir=False)
47
48
49 def test_directory_pattern_excludes_its_subtree():
50 matcher = _matcher(["vendor"])
51 assert matcher.is_ignored("vendor", is_dir=True)
52 assert matcher.is_ignored("vendor/lib/x.c", is_dir=False)
53 assert not matcher.is_ignored("vendored/x.c", is_dir=False)
54
55
56 def test_glob_pattern_matches():
57 matcher = _matcher(["a/*"])
58 assert matcher.is_ignored("a/b.c", is_dir=False)
59 assert matcher.is_ignored("a/b/c.c", is_dir=False)
60 assert not matcher.is_ignored("b/c.c", is_dir=False)
61
62
63 def test_unrelated_path_is_not_ignored():
64 matcher = _matcher(["foo/bar.c"])
65 assert not matcher.is_ignored("src/main.c", is_dir=False)
66
67
68 def test_empty_files_excluded_matches_nothing():
69 matcher = _matcher([])
70 assert not matcher.is_ignored("anything/at/all.c", is_dir=False)
71 assert not matcher.is_ignored("foo", is_dir=True)
72
73
74 def test_root_gitignore_file_and_dir_patterns(tmp_path):
75 matcher = _git_matcher(tmp_path)
76 matcher.add_gitignore("", ["*.log", "build/"])
77 assert matcher.is_ignored("a.log", is_dir=False)
78 assert not matcher.is_ignored("a.txt", is_dir=False)
79 assert matcher.is_ignored("build", is_dir=True)
80 assert matcher.is_ignored("build/x.o", is_dir=False)
81
82
83 def test_negation_reincludes(tmp_path):
84 matcher = _git_matcher(tmp_path)
85 matcher.add_gitignore("", ["*.log", "!keep.log"])
86 assert matcher.is_ignored("a.log", is_dir=False)
87 assert not matcher.is_ignored("keep.log", is_dir=False)
88
89
90 def test_nested_gitignore_only_affects_its_subtree(tmp_path):
91 matcher = _git_matcher(tmp_path)
92 matcher.add_gitignore("src", ["tmp/"])
93 assert matcher.is_ignored("src/tmp", is_dir=True)
94 assert matcher.is_ignored("src/tmp/x.c", is_dir=False)
95 assert not matcher.is_ignored("tmp", is_dir=True)
96
97
98 def test_use_gitignore_false_disables_gitignore_not_files_excluded(tmp_path):
99 matcher = _git_matcher(tmp_path, use_gitignore=False, files_excluded=["drop.c"])
100 matcher.add_gitignore("", ["*.log"])
101 assert not matcher.is_ignored("a.log", is_dir=False)
102 assert matcher.is_ignored("drop.c", is_dir=False)
103
104
105 def test_git_info_exclude_loaded_at_construction(tmp_path):
106 info = tmp_path / ".git" / "info"
107 info.mkdir(parents=True)
108 (info / "exclude").write_text("*.bak\n")
109 matcher = _git_matcher(tmp_path)
110 assert matcher.is_ignored("notes.bak", is_dir=False)
111 assert not matcher.is_ignored("notes.txt", is_dir=False)
112
113
114 def test_git_as_file_is_skipped(tmp_path):
115 (tmp_path / ".git").write_text("gitdir: /elsewhere\n")
116 matcher = _git_matcher(tmp_path)
117 assert not matcher.is_ignored("notes.bak", is_dir=False)