Skip to content

Commit 3dd705b

Browse files
committed
rename conftest
1 parent ae63d4a commit 3dd705b

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import tomlkit
1414

1515
from codeflash.cli_cmds.console import logger
16-
from codeflash.code_utils.config_parser import find_pyproject_toml
16+
from codeflash.code_utils.config_parser import find_pyproject_toml, find_conftest
1717

1818

1919
@contextmanager
@@ -84,6 +84,19 @@ def add_addopts_to_pyproject() -> None:
8484
with Path.open(pyproject_file, "w", encoding="utf-8") as f:
8585
f.write(original_content)
8686

87+
@contextmanager
88+
def rename_conftest() -> None:
89+
conftest_file = find_conftest()
90+
tmp_conftest_file = Path(conftest_file + ".tmp")
91+
try:
92+
# Rename original file
93+
if conftest_file.exists():
94+
conftest_file.rename(tmp_conftest_file)
95+
yield
96+
finally:
97+
# Restore original file
98+
if conftest_file.exists():
99+
tmp_conftest_file.rename(conftest_file)
87100

88101
def encoded_tokens_len(s: str) -> int:
89102
"""Return the approximate length of the encoded tokens.

codeflash/code_utils/config_parser.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from pathlib import Path
4-
from typing import Any
4+
from typing import Any, Union
55

66
import tomlkit
77

@@ -30,6 +30,17 @@ def find_pyproject_toml(config_file: Path | None = None) -> Path:
3030

3131
raise ValueError(msg)
3232

33+
def find_conftest() -> Union[Path, None]:
34+
# Find the conftest file on the root of the project
35+
dir_path = Path.cwd()
36+
while dir_path != dir_path.parent:
37+
config_file = dir_path / "conftest.py"
38+
if config_file.exists():
39+
return config_file
40+
# Search for conftest.py in the parent directories
41+
dir_path = dir_path.parent
42+
return None
43+
3344

3445
def parse_config_file(
3546
config_file_path: Path | None = None,

codeflash/discovery/discover_unit_tests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
from pydantic.dataclasses import dataclass
1717

1818
from codeflash.cli_cmds.console import console, logger, test_files_progress_bar
19-
from codeflash.code_utils.code_utils import custom_addopts, get_run_tmp_file, module_name_from_file_path
19+
from codeflash.code_utils.code_utils import custom_addopts, get_run_tmp_file, module_name_from_file_path, \
20+
rename_conftest
2021
from codeflash.code_utils.compat import SAFE_SYS_EXECUTABLE, codeflash_cache_db
2122
from codeflash.models.models import CodePosition, FunctionCalledInTest, TestsInFile, TestType
2223

@@ -149,7 +150,7 @@ def discover_tests_pytest(
149150
project_root = cfg.project_root_path
150151

151152
tmp_pickle_path = get_run_tmp_file("collected_tests.pkl")
152-
with custom_addopts():
153+
with custom_addopts(), rename_conftest():
153154
result = subprocess.run(
154155
[
155156
SAFE_SYS_EXECUTABLE,

codeflash/verification/test_runner.py

Lines changed: 2 additions & 2 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 custom_addopts, get_run_tmp_file
9+
from codeflash.code_utils.code_utils import custom_addopts, get_run_tmp_file, rename_conftest
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
@@ -23,7 +23,7 @@ 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-
with custom_addopts():
26+
with custom_addopts(), rename_conftest():
2727
logger.debug(f"executing test run with command: {' '.join(cmd_list)}")
2828
return subprocess.run(cmd_list, capture_output=True, cwd=cwd, env=env, text=True, timeout=timeout, check=False)
2929

0 commit comments

Comments
 (0)