-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_config.py
More file actions
393 lines (321 loc) · 12.5 KB
/
test_config.py
File metadata and controls
393 lines (321 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
"""Tests for Java project configuration detection."""
from pathlib import Path
import pytest
from codeflash.languages.java.build_tools import BuildTool
from codeflash.languages.java.config import (
JavaProjectConfig,
_detect_test_deps_from_gradle,
_detect_test_framework,
detect_java_project,
get_test_class_pattern,
get_test_file_pattern,
is_java_project,
)
class TestIsJavaProject:
"""Tests for is_java_project function."""
def test_maven_project(self, tmp_path: Path):
"""Test detecting a Maven project."""
(tmp_path / "pom.xml").write_text("<project></project>")
assert is_java_project(tmp_path) is True
def test_gradle_project(self, tmp_path: Path):
"""Test detecting a Gradle project."""
(tmp_path / "build.gradle").write_text("plugins { id 'java' }")
assert is_java_project(tmp_path) is True
def test_gradle_kotlin_project(self, tmp_path: Path):
"""Test detecting a Gradle Kotlin DSL project."""
(tmp_path / "build.gradle.kts").write_text("plugins { java }")
assert is_java_project(tmp_path) is True
def test_java_files_only(self, tmp_path: Path):
"""Test detecting project with only Java files."""
src_dir = tmp_path / "src"
src_dir.mkdir()
(src_dir / "Main.java").write_text("public class Main {}")
assert is_java_project(tmp_path) is True
def test_not_java_project(self, tmp_path: Path):
"""Test non-Java directory."""
(tmp_path / "README.md").write_text("# Not a Java project")
assert is_java_project(tmp_path) is False
def test_empty_directory(self, tmp_path: Path):
"""Test empty directory."""
assert is_java_project(tmp_path) is False
class TestDetectJavaProject:
"""Tests for detect_java_project function."""
def test_detect_maven_with_junit5(self, tmp_path: Path):
"""Test detecting Maven project with JUnit 5."""
pom_content = """<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
"""
(tmp_path / "pom.xml").write_text(pom_content)
(tmp_path / "src" / "main" / "java").mkdir(parents=True)
(tmp_path / "src" / "test" / "java").mkdir(parents=True)
config = detect_java_project(tmp_path)
assert config is not None
assert config.build_tool == BuildTool.MAVEN
assert config.has_junit5 is True
assert config.group_id == "com.example"
assert config.artifact_id == "my-app"
assert config.java_version == "11"
def test_detect_maven_with_junit4(self, tmp_path: Path):
"""Test detecting Maven project with JUnit 4."""
pom_content = """<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>legacy-app</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
"""
(tmp_path / "pom.xml").write_text(pom_content)
(tmp_path / "src" / "main" / "java").mkdir(parents=True)
config = detect_java_project(tmp_path)
assert config is not None
assert config.has_junit4 is True
def test_detect_maven_with_testng(self, tmp_path: Path):
"""Test detecting Maven project with TestNG."""
pom_content = """<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>testng-app</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
"""
(tmp_path / "pom.xml").write_text(pom_content)
(tmp_path / "src" / "main" / "java").mkdir(parents=True)
config = detect_java_project(tmp_path)
assert config is not None
assert config.has_testng is True
def test_detect_gradle_project(self, tmp_path: Path):
"""Test detecting Gradle project."""
gradle_content = """
plugins {
id 'java'
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.0'
}
test {
useJUnitPlatform()
}
"""
(tmp_path / "build.gradle").write_text(gradle_content)
(tmp_path / "src" / "main" / "java").mkdir(parents=True)
(tmp_path / "src" / "test" / "java").mkdir(parents=True)
config = detect_java_project(tmp_path)
assert config is not None
assert config.build_tool == BuildTool.GRADLE
assert config.has_junit5 is True
def test_detect_from_test_files(self, tmp_path: Path):
"""Test detecting test framework from test file imports."""
(tmp_path / "pom.xml").write_text("<project></project>")
test_root = tmp_path / "src" / "test" / "java"
test_root.mkdir(parents=True)
# Create a test file with JUnit 5 imports
(test_root / "ExampleTest.java").write_text("""
package com.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ExampleTest {
@Test
void test() {}
}
""")
config = detect_java_project(tmp_path)
assert config is not None
assert config.has_junit5 is True
def test_detect_mockito(self, tmp_path: Path):
"""Test detecting Mockito dependency."""
pom_content = """<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>mock-app</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.3.0</version>
</dependency>
</dependencies>
</project>
"""
(tmp_path / "pom.xml").write_text(pom_content)
(tmp_path / "src" / "main" / "java").mkdir(parents=True)
config = detect_java_project(tmp_path)
assert config is not None
assert config.has_mockito is True
def test_detect_assertj(self, tmp_path: Path):
"""Test detecting AssertJ dependency."""
pom_content = """<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>assertj-app</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.0</version>
</dependency>
</dependencies>
</project>
"""
(tmp_path / "pom.xml").write_text(pom_content)
(tmp_path / "src" / "main" / "java").mkdir(parents=True)
config = detect_java_project(tmp_path)
assert config is not None
assert config.has_assertj is True
def test_detect_non_java_project(self, tmp_path: Path):
"""Test detecting non-Java directory."""
(tmp_path / "package.json").write_text('{"name": "js-project"}')
config = detect_java_project(tmp_path)
assert config is None
class TestJavaProjectConfig:
"""Tests for JavaProjectConfig dataclass."""
def test_config_fields(self, tmp_path: Path):
"""Test that all config fields are accessible."""
config = JavaProjectConfig(
project_root=tmp_path,
build_tool=BuildTool.MAVEN,
source_root=tmp_path / "src" / "main" / "java",
test_root=tmp_path / "src" / "test" / "java",
java_version="17",
encoding="UTF-8",
test_framework="junit5",
group_id="com.example",
artifact_id="my-app",
version="1.0.0",
has_junit5=True,
has_junit4=False,
has_testng=False,
has_mockito=True,
has_assertj=False,
)
assert config.build_tool == BuildTool.MAVEN
assert config.java_version == "17"
assert config.has_junit5 is True
assert config.has_mockito is True
class TestGetTestPatterns:
"""Tests for test pattern functions."""
def test_get_test_file_pattern(self, tmp_path: Path):
"""Test getting test file pattern."""
config = JavaProjectConfig(
project_root=tmp_path,
build_tool=BuildTool.MAVEN,
source_root=None,
test_root=None,
java_version=None,
encoding="UTF-8",
test_framework="junit5",
group_id=None,
artifact_id=None,
version=None,
)
pattern = get_test_file_pattern(config)
assert pattern == "*Test.java"
def test_get_test_class_pattern(self, tmp_path: Path):
"""Test getting test class pattern."""
config = JavaProjectConfig(
project_root=tmp_path,
build_tool=BuildTool.MAVEN,
source_root=None,
test_root=None,
java_version=None,
encoding="UTF-8",
test_framework="junit5",
group_id=None,
artifact_id=None,
version=None,
)
pattern = get_test_class_pattern(config)
assert "Test" in pattern
class TestDetectWithFixture:
"""Tests using the Java fixture project."""
@pytest.fixture
def java_fixture_path(self):
"""Get path to the Java fixture project."""
fixture_path = Path(__file__).parent.parent.parent / "test_languages" / "fixtures" / "java_maven"
if not fixture_path.exists():
pytest.skip("Java fixture project not found")
return fixture_path
def test_detect_fixture_project(self, java_fixture_path: Path):
"""Test detecting the fixture project."""
config = detect_java_project(java_fixture_path)
assert config is not None
assert config.build_tool == BuildTool.MAVEN
assert config.source_root is not None
assert config.test_root is not None
assert config.has_junit5 is True
class TestGradleSubmoduleDetection:
def test_detect_gradle_junit5_from_submodule(self, tmp_path: Path) -> None:
root = tmp_path.resolve()
# Root build.gradle with no test deps
(root / "build.gradle.kts").write_text("plugins { java }\n", encoding="utf-8")
(root / "settings.gradle.kts").write_text('include("submodule-a")\n', encoding="utf-8")
# Submodule with JUnit 5
sub = root / "submodule-a"
sub.mkdir()
(sub / "build.gradle.kts").write_text(
'dependencies { testImplementation("org.junit.jupiter:junit-jupiter:5.10.0") }\n'
"tasks.withType<Test> { useJUnitPlatform() }\n",
encoding="utf-8",
)
has_junit5, has_junit4, has_testng = _detect_test_deps_from_gradle(root)
assert has_junit5 is True
assert has_junit4 is False
assert has_testng is False
def test_detect_gradle_junit5_from_root(self, tmp_path: Path) -> None:
root = tmp_path.resolve()
(root / "build.gradle").write_text(
"dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0' }\n"
"test { useJUnitPlatform() }\n",
encoding="utf-8",
)
has_junit5, has_junit4, has_testng = _detect_test_deps_from_gradle(root)
assert has_junit5 is True
assert has_junit4 is False
assert has_testng is False
def test_default_to_junit5_when_nothing_detected(self, tmp_path: Path) -> None:
root = tmp_path.resolve()
# Empty Gradle project — no test deps anywhere
(root / "build.gradle.kts").write_text("plugins { java }\n", encoding="utf-8")
framework, has_junit5, has_junit4, has_testng = _detect_test_framework(root, BuildTool.GRADLE)
assert framework == "junit5"
assert has_junit5 is False
assert has_junit4 is False
assert has_testng is False