-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_config_java_validation.py
More file actions
151 lines (124 loc) · 6.77 KB
/
test_config_java_validation.py
File metadata and controls
151 lines (124 loc) · 6.77 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
from __future__ import annotations
from typing import TYPE_CHECKING
from codeflash.code_utils.config_java_validation import infer_java_module_root, validate_java_module_resolution
if TYPE_CHECKING:
from pathlib import Path
class TestValidateJavaModuleResolution:
def test_valid_maven_project(self, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pom.xml").write_text("<project/>", encoding="utf-8")
src_root = project_root / "src" / "main" / "java"
pkg_dir = src_root / "com" / "example"
pkg_dir.mkdir(parents=True)
source_file = pkg_dir / "Foo.java"
source_file.write_text("package com.example;\npublic class Foo {}", encoding="utf-8")
valid, error = validate_java_module_resolution(source_file, project_root, src_root)
assert valid is True
assert error == ""
def test_source_does_not_exist(self, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pom.xml").write_text("<project/>", encoding="utf-8")
source_file = project_root / "src" / "main" / "java" / "Missing.java"
valid, error = validate_java_module_resolution(source_file, project_root, project_root)
assert valid is False
assert "does not exist" in error
def test_source_outside_project_root(self, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pom.xml").write_text("<project/>", encoding="utf-8")
outside_file = tmp_path / "outside" / "Foo.java"
outside_file.parent.mkdir(parents=True)
outside_file.write_text("public class Foo {}", encoding="utf-8")
valid, error = validate_java_module_resolution(outside_file, project_root, project_root)
assert valid is False
assert "outside the project root" in error
def test_no_build_config(self, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
source_file = project_root / "Foo.java"
source_file.write_text("public class Foo {}", encoding="utf-8")
valid, error = validate_java_module_resolution(source_file, project_root, project_root)
assert valid is False
assert "No build configuration" in error
def test_source_outside_module_root(self, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pom.xml").write_text("<project/>", encoding="utf-8")
module_root = project_root / "src" / "main" / "java"
module_root.mkdir(parents=True)
# Source file is in the project root, not in module root
source_file = project_root / "Foo.java"
source_file.write_text("public class Foo {}", encoding="utf-8")
valid, error = validate_java_module_resolution(source_file, project_root, module_root)
assert valid is False
assert "outside the module root" in error
def test_package_declaration_mismatch(self, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pom.xml").write_text("<project/>", encoding="utf-8")
src_root = project_root / "src" / "main" / "java"
wrong_dir = src_root / "com" / "bar"
wrong_dir.mkdir(parents=True)
source_file = wrong_dir / "Foo.java"
source_file.write_text("package com.foo;\npublic class Foo {}", encoding="utf-8")
valid, error = validate_java_module_resolution(source_file, project_root, src_root)
assert valid is False
assert "does not match directory structure" in error
def test_no_package_declaration(self, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pom.xml").write_text("<project/>", encoding="utf-8")
src_root = project_root / "src" / "main" / "java"
src_root.mkdir(parents=True)
source_file = src_root / "Foo.java"
source_file.write_text("public class Foo {}", encoding="utf-8")
valid, error = validate_java_module_resolution(source_file, project_root, src_root)
assert valid is True
assert error == ""
def test_gradle_project(self, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "build.gradle").write_text("apply plugin: 'java'", encoding="utf-8")
src_root = project_root / "src" / "main" / "java"
pkg_dir = src_root / "com" / "example"
pkg_dir.mkdir(parents=True)
source_file = pkg_dir / "Foo.java"
source_file.write_text("package com.example;\npublic class Foo {}", encoding="utf-8")
valid, error = validate_java_module_resolution(source_file, project_root, src_root)
assert valid is True
assert error == ""
class TestInferJavaModuleRoot:
def test_infers_standard_maven_layout(self, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pom.xml").write_text("<project/>", encoding="utf-8")
src_root = project_root / "src" / "main" / "java"
src_root.mkdir(parents=True)
source_file = src_root / "com" / "example" / "Foo.java"
source_file.parent.mkdir(parents=True)
source_file.write_text("package com.example;\npublic class Foo {}", encoding="utf-8")
result = infer_java_module_root(source_file, project_root)
assert result.resolve() == src_root.resolve()
def test_falls_back_to_project_root(self, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pom.xml").write_text("<project/>", encoding="utf-8")
# No src/main/java, no alternative source dirs with .java files
source_file = project_root / "Foo.java"
source_file.write_text("public class Foo {}", encoding="utf-8")
result = infer_java_module_root(source_file, project_root)
assert result.resolve() == project_root.resolve()
def test_detects_project_root_from_pom(self, tmp_path: Path) -> None:
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pom.xml").write_text("<project/>", encoding="utf-8")
src_root = project_root / "src" / "main" / "java"
src_root.mkdir(parents=True)
source_file = src_root / "com" / "example" / "Foo.java"
source_file.parent.mkdir(parents=True)
source_file.write_text("package com.example;\npublic class Foo {}", encoding="utf-8")
# Don't pass project_root — let it detect from pom.xml
result = infer_java_module_root(source_file, project_root=None)
assert result.resolve() == src_root.resolve()