-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_config_parser.py
More file actions
115 lines (86 loc) · 5.05 KB
/
test_config_parser.py
File metadata and controls
115 lines (86 loc) · 5.05 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
from __future__ import annotations
from pathlib import Path
import pytest
from codeflash.code_utils.config_parser import ALL_CONFIG_FILES, PYPROJECT_TOML_CACHE, parse_config_file
@pytest.fixture(autouse=True)
def clear_caches() -> None:
PYPROJECT_TOML_CACHE.clear()
ALL_CONFIG_FILES.clear()
PYPROJECT_TOML_CONTENT = """\
[tool.codeflash]
module-root = "src/python"
tests-root = "tests"
"""
CODEFLASH_TOML_CONTENT = """\
[tool.codeflash]
module-root = "src/main/java"
tests-root = "src/test/java"
"""
class TestParseConfigFileTomlPriority:
def test_codeflash_toml_preferred_over_pyproject_when_same_dir(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
(tmp_path / "pyproject.toml").write_text(PYPROJECT_TOML_CONTENT, encoding="utf-8")
(tmp_path / "codeflash.toml").write_text(CODEFLASH_TOML_CONTENT, encoding="utf-8")
(tmp_path / "src" / "main" / "java").mkdir(parents=True)
(tmp_path / "src" / "test" / "java").mkdir(parents=True)
(tmp_path / "src" / "python").mkdir(parents=True)
(tmp_path / "tests").mkdir(parents=True)
monkeypatch.chdir(tmp_path)
config, config_path = parse_config_file()
assert config_path == tmp_path / "codeflash.toml"
assert config["module_root"] == str((tmp_path / "src" / "main" / "java").resolve())
assert config["tests_root"] == str((tmp_path / "src" / "test" / "java").resolve())
def test_only_pyproject_toml_still_works(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
(tmp_path / "pyproject.toml").write_text(PYPROJECT_TOML_CONTENT, encoding="utf-8")
(tmp_path / "src" / "python").mkdir(parents=True)
(tmp_path / "tests").mkdir(parents=True)
monkeypatch.chdir(tmp_path)
config, config_path = parse_config_file()
assert config_path == tmp_path / "pyproject.toml"
assert config["module_root"] == str((tmp_path / "src" / "python").resolve())
def test_only_codeflash_toml_still_works(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
(tmp_path / "codeflash.toml").write_text(CODEFLASH_TOML_CONTENT, encoding="utf-8")
(tmp_path / "src" / "main" / "java").mkdir(parents=True)
(tmp_path / "src" / "test" / "java").mkdir(parents=True)
monkeypatch.chdir(tmp_path)
config, config_path = parse_config_file()
assert config_path == tmp_path / "codeflash.toml"
assert config["module_root"] == str((tmp_path / "src" / "main" / "java").resolve())
def test_closer_codeflash_toml_preferred_over_parent_pyproject(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
# pyproject.toml in parent, codeflash.toml in child (closer to CWD)
(tmp_path / "pyproject.toml").write_text(PYPROJECT_TOML_CONTENT, encoding="utf-8")
(tmp_path / "src" / "python").mkdir(parents=True)
(tmp_path / "tests").mkdir(parents=True)
child = tmp_path / "subproject"
child.mkdir()
(child / "codeflash.toml").write_text(CODEFLASH_TOML_CONTENT, encoding="utf-8")
(child / "src" / "main" / "java").mkdir(parents=True)
(child / "src" / "test" / "java").mkdir(parents=True)
monkeypatch.chdir(child)
config, config_path = parse_config_file()
assert config_path == child / "codeflash.toml"
assert config["module_root"] == str((child / "src" / "main" / "java").resolve())
def test_closer_pyproject_preferred_over_parent_codeflash_toml(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
# codeflash.toml in parent, pyproject.toml in child (closer to CWD)
(tmp_path / "codeflash.toml").write_text(CODEFLASH_TOML_CONTENT, encoding="utf-8")
(tmp_path / "src" / "main" / "java").mkdir(parents=True)
(tmp_path / "src" / "test" / "java").mkdir(parents=True)
child = tmp_path / "subproject"
child.mkdir()
(child / "pyproject.toml").write_text(PYPROJECT_TOML_CONTENT, encoding="utf-8")
(child / "src" / "python").mkdir(parents=True)
(child / "tests").mkdir(parents=True)
monkeypatch.chdir(child)
config, config_path = parse_config_file()
assert config_path == child / "pyproject.toml"
assert config["module_root"] == str((child / "src" / "python").resolve())
def test_explicit_config_file_path_bypasses_discovery(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
(tmp_path / "pyproject.toml").write_text(PYPROJECT_TOML_CONTENT, encoding="utf-8")
(tmp_path / "codeflash.toml").write_text(CODEFLASH_TOML_CONTENT, encoding="utf-8")
(tmp_path / "src" / "main" / "java").mkdir(parents=True)
(tmp_path / "src" / "test" / "java").mkdir(parents=True)
(tmp_path / "src" / "python").mkdir(parents=True)
(tmp_path / "tests").mkdir(parents=True)
monkeypatch.chdir(tmp_path)
config, config_path = parse_config_file(config_file_path=tmp_path / "pyproject.toml")
assert config_path == tmp_path / "pyproject.toml"
assert config["module_root"] == str((tmp_path / "src" / "python").resolve())