Skip to content

Commit c6d5f6f

Browse files
committed
wip
1 parent 4b1a2f3 commit c6d5f6f

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
ImportErrorPattern = re.compile(r"ModuleNotFoundError.*$", re.MULTILINE)
2121

22+
BLACKLIST_ADDOPTS = ("benchmark", "sugar", "codespeed", "cov", "profile", "junitxml")
23+
2224

2325
def unified_diff_strings(code1: str, code2: str, fromfile: str = "original", tofile: str = "modified") -> str:
2426
"""Return the unified diff between two code strings as a single string.
@@ -84,6 +86,7 @@ def create_rank_dictionary_compact(int_array: list[int]) -> dict[int, int]:
8486
@contextmanager
8587
def custom_addopts() -> None:
8688
pyproject_file = find_pyproject_toml()
89+
# closest_config_files = get_all_closest_config_files()
8790
original_content = None
8891
non_blacklist_plugin_args = ""
8992

@@ -97,9 +100,16 @@ def custom_addopts() -> None:
97100
original_addopts = data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("addopts", "")
98101
# nothing to do if no addopts present
99102
if original_addopts != "" and isinstance(original_addopts, list):
100-
original_addopts = [x.strip() for x in original_addopts]
101-
non_blacklist_plugin_args = re.sub(r"-n(?: +|=)\S+", "", " ".join(original_addopts)).split(" ")
102-
non_blacklist_plugin_args = [x for x in non_blacklist_plugin_args if x != ""]
103+
non_blacklist_plugin_args = []
104+
for opt in original_addopts:
105+
opt_stripped = opt.strip().lstrip("-")
106+
# Filter out -n/--numprocesses and blacklisted options
107+
if opt_stripped.startswith(("n=", "numprocesses=")) or any(
108+
opt_stripped.startswith(b) for b in BLACKLIST_ADDOPTS
109+
):
110+
continue
111+
non_blacklist_plugin_args.append(opt)
112+
103113
if non_blacklist_plugin_args != original_addopts:
104114
data["tool"]["pytest"]["ini_options"]["addopts"] = non_blacklist_plugin_args
105115
# Write modified file

codeflash/code_utils/config_parser.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import tomlkit
77

8+
ALL_CONFIG_FILES = {} # map path to closest config file
9+
810

911
def find_pyproject_toml(config_file: Path | None = None) -> Path:
1012
# Find the pyproject.toml file on the root of the project
@@ -31,6 +33,35 @@ def find_pyproject_toml(config_file: Path | None = None) -> Path:
3133
raise ValueError(msg)
3234

3335

36+
def get_all_closest_config_files() -> list[Path]:
37+
all_closest_config_files = []
38+
for file_type in ["pyproject.toml", "pytest.ini", ".pytest.ini", "tox.ini", "setup.cfg"]:
39+
closest_config_file = find_closest_config_file(file_type)
40+
if closest_config_file:
41+
all_closest_config_files.append(closest_config_file)
42+
return all_closest_config_files
43+
44+
45+
def find_closest_config_file(file_type: str) -> Path:
46+
# Find the closest pyproject.toml, pytest.ini, tox.ini, or setup.cfg file on the root of the project
47+
dir_path = Path.cwd()
48+
cur_path = dir_path
49+
if cur_path in ALL_CONFIG_FILES and file_type in ALL_CONFIG_FILES[cur_path]:
50+
return ALL_CONFIG_FILES[cur_path][file_type]
51+
while dir_path != dir_path.parent:
52+
config_file = dir_path / file_type
53+
if config_file.exists():
54+
if cur_path not in ALL_CONFIG_FILES:
55+
ALL_CONFIG_FILES[cur_path] = {}
56+
ALL_CONFIG_FILES[cur_path][file_type] = config_file
57+
return config_file
58+
# Search for pyproject.toml in the parent directories
59+
dir_path = dir_path.parent
60+
msg = f"Could not find pyproject.toml in the current directory {Path.cwd()} or any of the parent directories. Please create it by running `codeflash init`, or pass the path to pyproject.toml with the --config-file argument."
61+
62+
raise ValueError(msg)
63+
64+
3465
def find_conftest_files(test_paths: list[Path]) -> list[Path]:
3566
list_of_conftest_files = set()
3667
for test_path in test_paths:

0 commit comments

Comments
 (0)