|
6 | 6 |
|
7 | 7 | from codeflash.languages.base import Language, LanguageSupport |
8 | 8 | from codeflash.languages.java.support import JavaSupport, get_java_support |
| 9 | +from codeflash.discovery.functions_to_optimize import get_files_for_language |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class TestJavaSupportProtocol: |
@@ -132,3 +133,73 @@ def test_discover_functions_from_fixture(self, support, java_fixture_path: Path) |
132 | 133 |
|
133 | 134 | functions = support.discover_functions(calculator_file) |
134 | 135 | assert len(functions) > 0 |
| 136 | + |
| 137 | + |
| 138 | +class TestJavaDirExcludes: |
| 139 | + """Tests that Java-specific directories are excluded from file discovery.""" |
| 140 | + |
| 141 | + @pytest.fixture |
| 142 | + def support(self): |
| 143 | + return get_java_support() |
| 144 | + |
| 145 | + def test_dir_excludes_contains_apidocs(self, support): |
| 146 | + """apidocs (generated Javadoc HTML) must not be walked during --all.""" |
| 147 | + assert "apidocs" in support.dir_excludes |
| 148 | + |
| 149 | + def test_dir_excludes_contains_javadoc(self, support): |
| 150 | + """javadoc directory must not be walked during --all.""" |
| 151 | + assert "javadoc" in support.dir_excludes |
| 152 | + |
| 153 | + def test_apidocs_js_files_not_discovered(self, tmp_path: Path): |
| 154 | + """JS files inside apidocs/ must not appear in Java file discovery.""" |
| 155 | + # Simulate a Java project with an apidocs directory that contains .js files |
| 156 | + src_dir = tmp_path / "src" / "main" / "java" |
| 157 | + src_dir.mkdir(parents=True) |
| 158 | + (src_dir / "Foo.java").write_text("public class Foo { public void bar() {} }") |
| 159 | + |
| 160 | + apidocs_dir = tmp_path / "apidocs" / "script-files" |
| 161 | + apidocs_dir.mkdir(parents=True) |
| 162 | + (apidocs_dir / "jquery-3.7.1.min.js").write_text("/* jQuery */") |
| 163 | + (apidocs_dir / "search.js").write_text("/* search */") |
| 164 | + |
| 165 | + files = get_files_for_language(tmp_path, language=Language.JAVA) |
| 166 | + file_names = [f.name for f in files] |
| 167 | + |
| 168 | + assert "Foo.java" in file_names |
| 169 | + assert "jquery-3.7.1.min.js" not in file_names |
| 170 | + assert "search.js" not in file_names |
| 171 | + |
| 172 | + def test_javadoc_files_not_discovered(self, tmp_path: Path): |
| 173 | + """Files inside javadoc/ must not appear in Java file discovery.""" |
| 174 | + src_dir = tmp_path / "src" |
| 175 | + src_dir.mkdir(parents=True) |
| 176 | + (src_dir / "Bar.java").write_text("public class Bar { public void baz() {} }") |
| 177 | + |
| 178 | + javadoc_dir = tmp_path / "javadoc" |
| 179 | + javadoc_dir.mkdir(parents=True) |
| 180 | + (javadoc_dir / "index.html").write_text("<html></html>") |
| 181 | + # Also create a .java file here (edge case: javadoc may contain source snippets) |
| 182 | + (javadoc_dir / "Snippet.java").write_text("public class Snippet {}") |
| 183 | + |
| 184 | + files = get_files_for_language(tmp_path, language=Language.JAVA) |
| 185 | + file_names = [f.name for f in files] |
| 186 | + |
| 187 | + assert "Bar.java" in file_names |
| 188 | + assert "Snippet.java" not in file_names |
| 189 | + |
| 190 | + def test_apidocs_excluded_in_all_mode(self, tmp_path: Path): |
| 191 | + """In --all mode (language=None), apidocs .js files must also be excluded.""" |
| 192 | + src_dir = tmp_path / "src" |
| 193 | + src_dir.mkdir(parents=True) |
| 194 | + (src_dir / "Hello.java").write_text("public class Hello { public void hi() {} }") |
| 195 | + |
| 196 | + apidocs_dir = tmp_path / "apidocs" |
| 197 | + apidocs_dir.mkdir(parents=True) |
| 198 | + (apidocs_dir / "jquery.min.js").write_text("/* jQuery */") |
| 199 | + |
| 200 | + # language=None simulates --all mode |
| 201 | + files = get_files_for_language(tmp_path, language=None) |
| 202 | + file_names = [f.name for f in files] |
| 203 | + |
| 204 | + assert "Hello.java" in file_names |
| 205 | + assert "jquery.min.js" not in file_names |
0 commit comments