|
| 1 | +"""Tests for walking source code directories in search of relevant files""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from reccmp.dir import source_code_search, walk_source_dir |
| 5 | + |
| 6 | + |
| 7 | +def create_blank_file(p: Path): |
| 8 | + """Helper to make this action more idiomatic in the tests.""" |
| 9 | + p.write_text("") |
| 10 | + |
| 11 | + |
| 12 | +def test_walk_empty_dir(tmp_path_factory): |
| 13 | + """Empty directory returns no files.""" |
| 14 | + path = tmp_path_factory.mktemp("empty") |
| 15 | + |
| 16 | + files = list(walk_source_dir(path)) |
| 17 | + assert not files |
| 18 | + |
| 19 | + |
| 20 | +def test_walk_dir_with_irrelevant_files(tmp_path_factory): |
| 21 | + """Skip files that do not match our default filter on file extension.""" |
| 22 | + path = tmp_path_factory.mktemp("no_match") |
| 23 | + create_blank_file(path / "test.txt") |
| 24 | + create_blank_file(path / "sample.jpg") |
| 25 | + |
| 26 | + files = list(walk_source_dir(path)) |
| 27 | + assert not files |
| 28 | + |
| 29 | + |
| 30 | +def test_walk_dir_with_mixed_files(tmp_path_factory): |
| 31 | + """Return code files and skip non-matching files.""" |
| 32 | + path = tmp_path_factory.mktemp("mixed") |
| 33 | + create_blank_file(path / "test.txt") |
| 34 | + create_blank_file(path / "sample.jpg") |
| 35 | + create_blank_file(path / "game.cpp") |
| 36 | + create_blank_file(path / "game.hpp") |
| 37 | + |
| 38 | + files = list(walk_source_dir(path)) |
| 39 | + assert len(files) == 2 |
| 40 | + assert {f.name for f in files} == {"game.cpp", "game.hpp"} |
| 41 | + |
| 42 | + |
| 43 | +def test_walk_non_directory(tmp_path_factory): |
| 44 | + """Return nothing if the input path is not a directory.""" |
| 45 | + path = tmp_path_factory.mktemp("mixed") |
| 46 | + create_blank_file(path / "test.txt") |
| 47 | + create_blank_file(path / "sample.jpg") |
| 48 | + create_blank_file(path / "game.cpp") |
| 49 | + create_blank_file(path / "game.hpp") |
| 50 | + |
| 51 | + files = list(walk_source_dir(path / "game.cpp")) |
| 52 | + assert not files |
| 53 | + |
| 54 | + |
| 55 | +def test_walk_recursion(tmp_path_factory): |
| 56 | + """Should scan every subdirectory for matching files.""" |
| 57 | + path = tmp_path_factory.mktemp("recurse") |
| 58 | + create_blank_file(path / "game.cpp") |
| 59 | + (path / "x" / "y" / "z").mkdir(parents=True) |
| 60 | + create_blank_file(path / "x" / "hello.cpp") |
| 61 | + create_blank_file(path / "x" / "y" / "z" / "test.cpp") |
| 62 | + |
| 63 | + files = list(walk_source_dir(path)) |
| 64 | + assert {f.name for f in files} == {"game.cpp", "hello.cpp", "test.cpp"} |
| 65 | + |
| 66 | + |
| 67 | +def test_walk_recursion_disabled(tmp_path_factory): |
| 68 | + """Should not enter subdirectories if recursion is disabled.""" |
| 69 | + path = tmp_path_factory.mktemp("recurse") |
| 70 | + create_blank_file(path / "game.cpp") |
| 71 | + (path / "x" / "y" / "z").mkdir(parents=True) |
| 72 | + create_blank_file(path / "x" / "hello.cpp") |
| 73 | + create_blank_file(path / "x" / "y" / "z" / "test.cpp") |
| 74 | + |
| 75 | + files = list(walk_source_dir(path, recursive=False)) |
| 76 | + assert {f.name for f in files} == {"game.cpp"} |
| 77 | + |
| 78 | + |
| 79 | +def test_walk_absolute_paths(tmp_path_factory): |
| 80 | + """Returned paths should be absolute and include all subdirectories.""" |
| 81 | + path = tmp_path_factory.mktemp("recurse") |
| 82 | + create_blank_file(path / "game.cpp") |
| 83 | + (path / "x" / "y" / "z").mkdir(parents=True) |
| 84 | + create_blank_file(path / "x" / "hello.cpp") |
| 85 | + create_blank_file(path / "x" / "y" / "z" / "test.cpp") |
| 86 | + |
| 87 | + files = list(walk_source_dir(path)) |
| 88 | + for f in files: |
| 89 | + assert f.is_absolute() |
| 90 | + |
| 91 | + # For files in subdirectories, make sure each one is represented. |
| 92 | + # Check only the components relative to the base path. |
| 93 | + # We don't control where pytest creates the tmp directory. |
| 94 | + if f.name == "hello.cpp": |
| 95 | + assert "x" in f.relative_to(path).parts |
| 96 | + |
| 97 | + if f.name == "test.cpp": |
| 98 | + assert "x" in f.relative_to(path).parts |
| 99 | + assert "y" in f.relative_to(path).parts |
| 100 | + assert "z" in f.relative_to(path).parts |
| 101 | + |
| 102 | + |
| 103 | +def test_walk_mixed_case_extension(tmp_path_factory): |
| 104 | + """Should use case-insensitive match for file extensions.""" |
| 105 | + path = tmp_path_factory.mktemp("mixed_case") |
| 106 | + create_blank_file(path / "game.CPP") |
| 107 | + create_blank_file(path / "HELLO.C") |
| 108 | + create_blank_file(path / "HELLO.h") |
| 109 | + create_blank_file(path / "test.hpP") |
| 110 | + |
| 111 | + files = list(walk_source_dir(path)) |
| 112 | + assert {f.name.lower() for f in files} == { |
| 113 | + "game.cpp", |
| 114 | + "hello.c", |
| 115 | + "hello.h", |
| 116 | + "test.hpp", |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | +def test_search_mixed_files(tmp_path_factory): |
| 121 | + """Search behaves the same as walk_source_dir: return only matching code files.""" |
| 122 | + path = tmp_path_factory.mktemp("mixed") |
| 123 | + create_blank_file(path / "test.txt") |
| 124 | + create_blank_file(path / "sample.jpg") |
| 125 | + create_blank_file(path / "game.cpp") |
| 126 | + create_blank_file(path / "game.hpp") |
| 127 | + |
| 128 | + files = list(source_code_search(path)) |
| 129 | + assert {f.name for f in files} == {"game.cpp", "game.hpp"} |
| 130 | + |
| 131 | + |
| 132 | +def test_search_ignore_missing_dir(tmp_path_factory): |
| 133 | + """Search should return nothing if the search path does not exist.""" |
| 134 | + path = tmp_path_factory.mktemp("mixed") |
| 135 | + create_blank_file(path / "test.txt") |
| 136 | + create_blank_file(path / "sample.jpg") |
| 137 | + create_blank_file(path / "game.cpp") |
| 138 | + create_blank_file(path / "game.hpp") |
| 139 | + |
| 140 | + files = list(source_code_search(path / "x")) |
| 141 | + assert not files |
| 142 | + |
| 143 | + |
| 144 | +def test_search_input_order_ignored(tmp_path_factory): |
| 145 | + """The returned paths are always sorted by their lower-case string version, |
| 146 | + regardless of input order.""" |
| 147 | + path = tmp_path_factory.mktemp("order") |
| 148 | + create_blank_file(path / "game.cpp") |
| 149 | + create_blank_file(path / "Test.cpp") |
| 150 | + |
| 151 | + search_paths = (path / "Test.cpp", path / "game.cpp") |
| 152 | + files = list(source_code_search(search_paths)) |
| 153 | + assert [f.name for f in files] == ["game.cpp", "Test.cpp"] |
| 154 | + |
| 155 | + |
| 156 | +def test_search_ignore_irrelevant_file_even_if_targeted(tmp_path_factory): |
| 157 | + """The current behavior is to test each file's extension. |
| 158 | + This is true even if the input search path points directly at a file.""" |
| 159 | + path = tmp_path_factory.mktemp("mixed") |
| 160 | + create_blank_file(path / "test.txt") |
| 161 | + create_blank_file(path / "sample.jpg") |
| 162 | + create_blank_file(path / "game.cpp") |
| 163 | + create_blank_file(path / "game.hpp") |
| 164 | + |
| 165 | + search_paths = path / "test.text" |
| 166 | + files = list(source_code_search(search_paths)) |
| 167 | + assert not files |
| 168 | + |
| 169 | + |
| 170 | +def test_search_nested_dirs(tmp_path_factory): |
| 171 | + """Should return distinct files if any search paths overlap.""" |
| 172 | + path = tmp_path_factory.mktemp("recurse") |
| 173 | + create_blank_file(path / "game.cpp") |
| 174 | + (path / "x" / "y" / "z").mkdir(parents=True) |
| 175 | + create_blank_file(path / "x" / "hello.cpp") |
| 176 | + create_blank_file(path / "x" / "y" / "z" / "test.cpp") |
| 177 | + |
| 178 | + # Nested paths |
| 179 | + search_paths = (path, path / "x") |
| 180 | + files = list(source_code_search(search_paths)) |
| 181 | + assert len(files) == 3 |
| 182 | + |
| 183 | + |
| 184 | +def test_search_using_file(tmp_path_factory): |
| 185 | + """Using a file as our search path, the search function |
| 186 | + should return the path if the file exists and matches our criteria.""" |
| 187 | + path = tmp_path_factory.mktemp("mixed") |
| 188 | + create_blank_file(path / "test.txt") |
| 189 | + create_blank_file(path / "sample.jpg") |
| 190 | + create_blank_file(path / "game.cpp") |
| 191 | + create_blank_file(path / "game.hpp") |
| 192 | + |
| 193 | + files = list(source_code_search(path / "game.cpp")) |
| 194 | + assert [f.name for f in files] == ["game.cpp"] |
| 195 | + |
| 196 | + |
| 197 | +def test_search_using_file_not_exist(tmp_path_factory): |
| 198 | + """Search should return nothing if the search path does not exist. |
| 199 | + Using a file in this example.""" |
| 200 | + path = tmp_path_factory.mktemp("mixed") |
| 201 | + create_blank_file(path / "test.txt") |
| 202 | + create_blank_file(path / "sample.jpg") |
| 203 | + create_blank_file(path / "game.cpp") |
| 204 | + create_blank_file(path / "game.hpp") |
| 205 | + |
| 206 | + files = list(source_code_search(path / "hello.cpp")) |
| 207 | + assert not files |
| 208 | + |
| 209 | + |
| 210 | +def test_search_case_insensitive_order(tmp_path_factory): |
| 211 | + """Make sure path order is consistent across Posix/Windows runners. |
| 212 | + The difference is that paths are case-sensitive on Posix but not Windows. |
| 213 | + The expectation is that reccmp returns the same results regardless of platform. |
| 214 | + Metadata read from source code files could be overwritten if it is duplicated across files. |
| 215 | + """ |
| 216 | + path = tmp_path_factory.mktemp("mixed_case") |
| 217 | + create_blank_file(path / "game.CPP") |
| 218 | + create_blank_file(path / "HELLO.C") |
| 219 | + create_blank_file(path / "HELLO.h") |
| 220 | + create_blank_file(path / "test.hpP") |
| 221 | + |
| 222 | + files = list(source_code_search(path)) |
| 223 | + assert [f.name.lower() for f in files] == [ |
| 224 | + "game.cpp", |
| 225 | + "hello.c", |
| 226 | + "hello.h", |
| 227 | + "test.hpp", |
| 228 | + ] |
0 commit comments