Skip to content

Commit 8b64b39

Browse files
committed
remove patch and use actual function call
1 parent 9048d38 commit 8b64b39

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

tests/code_utils/test_code_utils.py

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@
99

1010
from codeflash.code_utils.code_utils import custom_addopts
1111

12+
def test_custom_addopts_modifies_and_restores_dotini_file(tmp_path: Path) -> None:
13+
"""Verify that custom_addopts correctly modifies and then restores a pytest.ini file."""
14+
# Create a dummy pytest.ini file
15+
config_file = tmp_path / ".pytest.ini"
16+
original_content = "[pytest]\naddopts = -v --cov=./src -n auto\n"
17+
config_file.write_text(original_content)
18+
19+
# Use patch to mock get_all_closest_config_files
20+
os.chdir(tmp_path)
21+
with custom_addopts():
22+
# Check that the file is modified inside the context
23+
modified_content = config_file.read_text()
24+
config = configparser.ConfigParser()
25+
config.read_string(modified_content)
26+
modified_addopts = config.get("pytest", "addopts", fallback="")
27+
assert modified_addopts == "-v"
28+
29+
# Check that the file is restored after exiting the context
30+
restored_content = config_file.read_text()
31+
assert restored_content.strip() == original_content.strip()
1232

1333
def test_custom_addopts_modifies_and_restores_ini_file(tmp_path: Path) -> None:
1434
"""Verify that custom_addopts correctly modifies and then restores a pytest.ini file."""
@@ -18,14 +38,14 @@ def test_custom_addopts_modifies_and_restores_ini_file(tmp_path: Path) -> None:
1838
config_file.write_text(original_content)
1939

2040
# Use patch to mock get_all_closest_config_files
21-
with patch("codeflash.code_utils.code_utils.get_all_closest_config_files", return_value=[config_file]):
22-
with custom_addopts():
23-
# Check that the file is modified inside the context
24-
modified_content = config_file.read_text()
25-
config = configparser.ConfigParser()
26-
config.read_string(modified_content)
27-
modified_addopts = config.get("pytest", "addopts", fallback="")
28-
assert modified_addopts == "-v"
41+
os.chdir(tmp_path)
42+
with custom_addopts():
43+
# Check that the file is modified inside the context
44+
modified_content = config_file.read_text()
45+
config = configparser.ConfigParser()
46+
config.read_string(modified_content)
47+
modified_addopts = config.get("pytest", "addopts", fallback="")
48+
assert modified_addopts == "-v"
2949

3050
# Check that the file is restored after exiting the context
3151
restored_content = config_file.read_text()
@@ -45,13 +65,13 @@ def test_custom_addopts_modifies_and_restores_toml_file(tmp_path: Path) -> None:
4565
config_file.write_text(original_content)
4666

4767
# Use patch to mock get_all_closest_config_files
48-
with patch("codeflash.code_utils.code_utils.get_all_closest_config_files", return_value=[config_file]):
49-
with custom_addopts():
50-
# Check that the file is modified inside the context
51-
modified_content = config_file.read_text()
52-
modified_data = tomlkit.parse(modified_content)
53-
modified_addopts = modified_data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("addopts", "")
54-
assert modified_addopts == "-v"
68+
os.chdir(tmp_path)
69+
with custom_addopts():
70+
# Check that the file is modified inside the context
71+
modified_content = config_file.read_text()
72+
modified_data = tomlkit.parse(modified_content)
73+
modified_addopts = modified_data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("addopts", "")
74+
assert modified_addopts == "-v"
5575

5676
# Check that the file is restored after exiting the context
5777
restored_content = config_file.read_text()
@@ -65,11 +85,11 @@ def test_custom_addopts_handles_no_addopts(tmp_path: Path) -> None:
6585
original_content = "[pytest]\n# no addopts here\n"
6686
config_file.write_text(original_content)
6787

68-
with patch("codeflash.code_utils.code_utils.get_all_closest_config_files", return_value=[config_file]):
69-
with custom_addopts():
70-
# The file should not be modified
71-
content_inside_context = config_file.read_text()
72-
assert content_inside_context == original_content
88+
os.chdir(tmp_path)
89+
with custom_addopts():
90+
# The file should not be modified
91+
content_inside_context = config_file.read_text()
92+
assert content_inside_context == original_content
7393

7494
# The file should remain unchanged
7595
content_after_context = config_file.read_text()
@@ -79,8 +99,8 @@ def test_custom_addopts_handles_no_relevant_files(tmp_path: Path) -> None:
7999
"""Ensure custom_addopts runs without error when no config files are found."""
80100
# No config files created in tmp_path
81101

82-
with patch("codeflash.code_utils.code_utils.get_all_closest_config_files", return_value=[]):
83-
# This should execute without raising any exceptions
84-
with custom_addopts():
85-
pass
102+
os.chdir(tmp_path)
103+
# This should execute without raising any exceptions
104+
with custom_addopts():
105+
pass
86106
# No assertions needed, the test passes if no exceptions were raised

0 commit comments

Comments
 (0)