Skip to content

Commit 35c8d14

Browse files
committed
edit pyproject to remove blacklisted plugin parameters
1 parent e244b65 commit 35c8d14

File tree

3 files changed

+78
-31
lines changed

3 files changed

+78
-31
lines changed

codeflash/cli_cmds/cmd_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ def install_github_actions(override_formatter_check: bool = False) -> None: # n
537537
existing_api_key = None
538538
click.prompt(
539539
f"Next, you'll need to add your CODEFLASH_API_KEY as a secret to your GitHub repo.{LF}"
540-
f"Press Enter to open your repo's secrets page at {get_github_secrets_page_url(repo)}{LF}"
540+
f"Press Enter to open your repo's secrets page at {get_github_secrets_page_url(repo)} {LF}"
541541
f"Then, click 'New repository secret' to add your api key with the variable name CODEFLASH_API_KEY.{LF}"
542542
f"{'Here is your CODEFLASH_API_KEY: ' + existing_api_key + ' ' + LF}"
543543
if existing_api_key

codeflash/code_utils/code_utils.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,56 @@
44
import os
55
import shutil
66
import site
7+
from contextlib import contextmanager
78
from functools import lru_cache
89
from pathlib import Path
910
from tempfile import TemporaryDirectory
1011

12+
import tomlkit
13+
1114
from codeflash.cli_cmds.console import logger
1215

1316

17+
@contextmanager
18+
def custom_addopts(pyproject_path: str = "pyproject.toml") -> None:
19+
pyproject_file = Path(pyproject_path)
20+
original_content = None
21+
22+
try:
23+
# Read original file
24+
if pyproject_file.exists():
25+
with Path.open(pyproject_file, encoding="utf-8") as f:
26+
original_content = f.read()
27+
data = tomlkit.parse(original_content)
28+
29+
# Backup original addopts
30+
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))
47+
48+
yield
49+
50+
finally:
51+
# Restore original file
52+
if original_content and pyproject_file.exists():
53+
with Path.open(pyproject_file, "w", encoding="utf-8") as f:
54+
f.write(original_content)
55+
56+
1457
def encoded_tokens_len(s: str) -> int:
1558
"""Return the approximate length of the encoded tokens.
1659

codeflash/verification/test_runner.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import TYPE_CHECKING
77

88
from codeflash.cli_cmds.console import logger
9-
from codeflash.code_utils.code_utils import get_run_tmp_file
9+
from codeflash.code_utils.code_utils import custom_addopts, get_run_tmp_file
1010
from codeflash.code_utils.compat import IS_POSIX, SAFE_SYS_EXECUTABLE
1111
from codeflash.code_utils.config_consts import TOTAL_LOOPING_TIME
1212
from codeflash.code_utils.coverage_utils import prepare_coverage_files
@@ -97,24 +97,27 @@ def run_behavioral_tests(
9797
coverage_cmd.extend(shlex.split(pytest_cmd, posix=IS_POSIX)[1:])
9898

9999
blocklist_args = [f"-p no:{plugin}" for plugin in BEHAVIORAL_BLOCKLISTED_PLUGINS if plugin != "cov"]
100-
results = execute_test_subprocess(
101-
coverage_cmd + common_pytest_args + blocklist_args + result_args + test_files,
102-
cwd=cwd,
103-
env=pytest_test_env,
104-
timeout=600,
105-
)
100+
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+
)
106108
logger.debug(
107109
f"Result return code: {results.returncode}, "
108110
f"{'Result stderr:' + str(results.stderr) if results.stderr else ''}"
109111
)
110112
else:
111113
blocklist_args = [f"-p no:{plugin}" for plugin in BEHAVIORAL_BLOCKLISTED_PLUGINS]
112-
results = execute_test_subprocess(
113-
pytest_cmd_list + common_pytest_args + blocklist_args + result_args + test_files,
114-
cwd=cwd,
115-
env=pytest_test_env,
116-
timeout=600, # TODO: Make this dynamic
117-
)
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+
)
118121
logger.debug(
119122
f"""Result return code: {results.returncode}, {"Result stderr:" + str(results.stderr) if results.stderr else ""}"""
120123
)
@@ -192,12 +195,13 @@ def run_line_profile_tests(
192195
pytest_test_env["PYTEST_PLUGINS"] = "codeflash.verification.pytest_plugin"
193196
blocklist_args = [f"-p no:{plugin}" for plugin in BENCHMARKING_BLOCKLISTED_PLUGINS]
194197
pytest_test_env["LINE_PROFILE"] = "1"
195-
results = execute_test_subprocess(
196-
pytest_cmd_list + pytest_args + blocklist_args + result_args + test_files,
197-
cwd=cwd,
198-
env=pytest_test_env,
199-
timeout=600, # TODO: Make this dynamic
200-
)
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+
)
201205
else:
202206
msg = f"Unsupported test framework: {test_framework}"
203207
raise ValueError(msg)
@@ -252,13 +256,13 @@ def run_benchmarking_tests(
252256
pytest_test_env = test_env.copy()
253257
pytest_test_env["PYTEST_PLUGINS"] = "codeflash.verification.pytest_plugin"
254258
blocklist_args = [f"-p no:{plugin}" for plugin in BENCHMARKING_BLOCKLISTED_PLUGINS]
255-
256-
results = execute_test_subprocess(
257-
pytest_cmd_list + pytest_args + blocklist_args + result_args + test_files,
258-
cwd=cwd,
259-
env=pytest_test_env,
260-
timeout=600, # TODO: Make this dynamic
261-
)
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+
)
262266
elif test_framework == "unittest":
263267
test_files = [file.benchmarking_file_path for file in test_paths.test_files]
264268
result_file_path, results = run_unittest_tests(
@@ -278,8 +282,8 @@ def run_unittest_tests(
278282
log_level = ["-v"] if verbose else []
279283
files = [str(file) for file in test_file_paths]
280284
output_file = ["--output-file", str(result_file_path)]
281-
282-
results = execute_test_subprocess(
283-
unittest_cmd_list + log_level + files + output_file, cwd=cwd, env=test_env, timeout=600
284-
)
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+
)
285289
return result_file_path, results

0 commit comments

Comments
 (0)