Skip to content

Commit dcf9286

Browse files
committed
precommit pass
1 parent 10d323f commit dcf9286

File tree

2 files changed

+47
-53
lines changed

2 files changed

+47
-53
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ast
44
import os
5+
import re
56
import shutil
67
import site
78
from contextlib import contextmanager
@@ -12,11 +13,12 @@
1213
import tomlkit
1314

1415
from codeflash.cli_cmds.console import logger
16+
from codeflash.code_utils.config_parser import find_pyproject_toml
1517

1618

1719
@contextmanager
18-
def custom_addopts(pyproject_path: str = "pyproject.toml") -> None:
19-
pyproject_file = Path(pyproject_path)
20+
def custom_addopts() -> None:
21+
pyproject_file = find_pyproject_toml()
2022
original_content = None
2123

2224
try:
@@ -28,28 +30,24 @@ def custom_addopts(pyproject_path: str = "pyproject.toml") -> None:
2830

2931
# Backup original addopts
3032
original_addopts = data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("addopts", "")
31-
32-
# Set new addopts
33-
if "tool" not in data:
34-
data["tool"] = {}
35-
if "pytest" not in data["tool"]:
36-
data["tool"]["pytest"] = {}
37-
if "ini_options" not in data["tool"]["pytest"]:
38-
data["tool"]["pytest"]["ini_options"] = {}
39-
40-
data["tool"]["pytest"]["ini_options"]["addopts"] = [
41-
x for x in original_addopts if x not in ["-n", "-n auto", "auto"]
42-
]
43-
44-
# Write modified file
45-
with Path.open(pyproject_file, "w", encoding="utf-8") as f:
46-
f.write(tomlkit.dumps(data))
33+
# nothing to do if no addopts present
34+
if original_addopts != "":
35+
non_blacklist_plugin_args = re.sub(r"-n +\S+", "", " ".join(original_addopts)).split(" ")
36+
if non_blacklist_plugin_args != original_addopts:
37+
data["tool"]["pytest"]["ini_options"]["addopts"] = non_blacklist_plugin_args
38+
# Write modified file
39+
with Path.open(pyproject_file, "w", encoding="utf-8") as f:
40+
f.write(tomlkit.dumps(data))
4741

4842
yield
4943

5044
finally:
5145
# Restore original file
52-
if original_content and pyproject_file.exists():
46+
if (
47+
original_content
48+
and pyproject_file.exists()
49+
and tuple(original_addopts) not in {(), tuple(non_blacklist_plugin_args)}
50+
):
5351
with Path.open(pyproject_file, "w", encoding="utf-8") as f:
5452
f.write(original_content)
5553

codeflash/verification/test_runner.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ def execute_test_subprocess(
2323
cmd_list: list[str], cwd: Path, env: dict[str, str] | None, timeout: int = 600
2424
) -> subprocess.CompletedProcess:
2525
"""Execute a subprocess with the given command list, working directory, environment variables, and timeout."""
26-
logger.debug(f"executing test run with command: {' '.join(cmd_list)}")
27-
return subprocess.run(cmd_list, capture_output=True, cwd=cwd, env=env, text=True, timeout=timeout, check=False)
26+
with custom_addopts():
27+
logger.debug(f"executing test run with command: {' '.join(cmd_list)}")
28+
return subprocess.run(cmd_list, capture_output=True, cwd=cwd, env=env, text=True, timeout=timeout, check=False)
2829

2930

3031
def run_behavioral_tests(
@@ -98,26 +99,24 @@ def run_behavioral_tests(
9899

99100
blocklist_args = [f"-p no:{plugin}" for plugin in BEHAVIORAL_BLOCKLISTED_PLUGINS if plugin != "cov"]
100101

101-
with custom_addopts():
102-
results = execute_test_subprocess(
103-
coverage_cmd + common_pytest_args + blocklist_args + result_args + test_files,
104-
cwd=cwd,
105-
env=pytest_test_env,
106-
timeout=600,
107-
)
102+
results = execute_test_subprocess(
103+
coverage_cmd + common_pytest_args + blocklist_args + result_args + test_files,
104+
cwd=cwd,
105+
env=pytest_test_env,
106+
timeout=600,
107+
)
108108
logger.debug(
109109
f"Result return code: {results.returncode}, "
110110
f"{'Result stderr:' + str(results.stderr) if results.stderr else ''}"
111111
)
112112
else:
113113
blocklist_args = [f"-p no:{plugin}" for plugin in BEHAVIORAL_BLOCKLISTED_PLUGINS]
114-
with custom_addopts():
115-
results = execute_test_subprocess(
116-
pytest_cmd_list + common_pytest_args + blocklist_args + result_args + test_files,
117-
cwd=cwd,
118-
env=pytest_test_env,
119-
timeout=600, # TODO: Make this dynamic
120-
)
114+
results = execute_test_subprocess(
115+
pytest_cmd_list + common_pytest_args + blocklist_args + result_args + test_files,
116+
cwd=cwd,
117+
env=pytest_test_env,
118+
timeout=600, # TODO: Make this dynamic
119+
)
121120
logger.debug(
122121
f"""Result return code: {results.returncode}, {"Result stderr:" + str(results.stderr) if results.stderr else ""}"""
123122
)
@@ -195,13 +194,12 @@ def run_line_profile_tests(
195194
pytest_test_env["PYTEST_PLUGINS"] = "codeflash.verification.pytest_plugin"
196195
blocklist_args = [f"-p no:{plugin}" for plugin in BENCHMARKING_BLOCKLISTED_PLUGINS]
197196
pytest_test_env["LINE_PROFILE"] = "1"
198-
with custom_addopts():
199-
results = execute_test_subprocess(
200-
pytest_cmd_list + pytest_args + blocklist_args + result_args + test_files,
201-
cwd=cwd,
202-
env=pytest_test_env,
203-
timeout=600, # TODO: Make this dynamic
204-
)
197+
results = execute_test_subprocess(
198+
pytest_cmd_list + pytest_args + blocklist_args + result_args + test_files,
199+
cwd=cwd,
200+
env=pytest_test_env,
201+
timeout=600, # TODO: Make this dynamic
202+
)
205203
else:
206204
msg = f"Unsupported test framework: {test_framework}"
207205
raise ValueError(msg)
@@ -256,13 +254,12 @@ def run_benchmarking_tests(
256254
pytest_test_env = test_env.copy()
257255
pytest_test_env["PYTEST_PLUGINS"] = "codeflash.verification.pytest_plugin"
258256
blocklist_args = [f"-p no:{plugin}" for plugin in BENCHMARKING_BLOCKLISTED_PLUGINS]
259-
with custom_addopts():
260-
results = execute_test_subprocess(
261-
pytest_cmd_list + pytest_args + blocklist_args + result_args + test_files,
262-
cwd=cwd,
263-
env=pytest_test_env,
264-
timeout=600, # TODO: Make this dynamic
265-
)
257+
results = execute_test_subprocess(
258+
pytest_cmd_list + pytest_args + blocklist_args + result_args + test_files,
259+
cwd=cwd,
260+
env=pytest_test_env,
261+
timeout=600, # TODO: Make this dynamic
262+
)
266263
elif test_framework == "unittest":
267264
test_files = [file.benchmarking_file_path for file in test_paths.test_files]
268265
result_file_path, results = run_unittest_tests(
@@ -282,8 +279,7 @@ def run_unittest_tests(
282279
log_level = ["-v"] if verbose else []
283280
files = [str(file) for file in test_file_paths]
284281
output_file = ["--output-file", str(result_file_path)]
285-
with custom_addopts():
286-
results = execute_test_subprocess(
287-
unittest_cmd_list + log_level + files + output_file, cwd=cwd, env=test_env, timeout=600
288-
)
282+
results = execute_test_subprocess(
283+
unittest_cmd_list + log_level + files + output_file, cwd=cwd, env=test_env, timeout=600
284+
)
289285
return result_file_path, results

0 commit comments

Comments
 (0)