Skip to content

Commit ed952d7

Browse files
committed
wip
1 parent 81ce1fe commit ed952d7

File tree

7 files changed

+22
-75
lines changed

7 files changed

+22
-75
lines changed

codeflash/cli_cmds/cmd_init.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from argparse import Namespace
3535

3636
CODEFLASH_LOGO: str = (
37-
f"{LF}" # noqa: ISC003
37+
f"{LF}"
3838
r" _ ___ _ _ " + f"{LF}"
3939
r" | | / __)| | | | " + f"{LF}"
4040
r" ____ ___ _ | | ____ | |__ | | ____ ___ | | _ " + f"{LF}"
@@ -723,11 +723,16 @@ def configure_pyproject_toml(setup_info: SetupInfo) -> None:
723723
formatter_cmds.append("disabled")
724724
check_formatter_installed(formatter_cmds, exit_on_failure=False)
725725
codeflash_section["formatter-cmds"] = formatter_cmds
726+
codeflash_section["override-fixtures"] = False # don't override fixtures by default, let the user decide
726727
# Add the 'codeflash' section, ensuring 'tool' section exists
727728
tool_section = pyproject_data.get("tool", tomlkit.table())
728729
tool_section["codeflash"] = codeflash_section
729730
pyproject_data["tool"] = tool_section
730-
731+
if "tool.pytest.ini_options" not in pyproject_data:
732+
pyproject_data["tool.pytest.ini_options"] = {}
733+
if "markers" not in pyproject_data["tool.pytest.ini_options"]:
734+
pyproject_data["tool.pytest.ini_options"]["markers"] = []
735+
pyproject_data["tool.pytest.ini_options"]["markers"].append("codeflash_no_autouse")
731736
with toml_path.open("w", encoding="utf8") as pyproject_file:
732737
pyproject_file.write(tomlkit.dumps(pyproject_data))
733738
click.echo(f"✅ Added Codeflash configuration to {toml_path}")

codeflash/code_utils/code_replacer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from codeflash.cli_cmds.console import logger
1111
from codeflash.code_utils.code_extractor import add_global_assignments, add_needed_imports_from_module
12+
from codeflash.code_utils.config_parser import find_conftest_files
1213
from codeflash.models.models import FunctionParent
1314

1415
if TYPE_CHECKING:
@@ -37,7 +38,13 @@ def modify_autouse_fixture():
3738
# find fixutre definition in conftetst.py (the one closest to the test)
3839
# get fixtures present in override-fixtures in pyproject.toml
3940
# add if marker closest return
40-
autousetransformer
41+
conftest_files = find_conftest_files()
42+
for cf_file in conftest_files:
43+
#iterate over all functions in the file
44+
# if function has autouse fixture, modify function to bypass with custom marker
45+
pass
46+
47+
#reuse line profiler utils to add decorator and import to test fns
4148

4249

4350
class OptimFunctionCollector(cst.CSTVisitor):

codeflash/code_utils/code_utils.py

Lines changed: 1 addition & 59 deletions
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_conftest, find_pyproject_toml
16+
from codeflash.code_utils.config_parser import find_pyproject_toml
1717

1818
ImportErrorPattern = re.compile(r"ModuleNotFoundError.*$", re.MULTILINE)
1919

@@ -87,64 +87,6 @@ def add_addopts_to_pyproject() -> None:
8787
f.write(original_content)
8888

8989

90-
@contextmanager
91-
def add_override_fixtures_to_pyproject() -> None:
92-
pyproject_file = find_pyproject_toml()
93-
try:
94-
# Read original file
95-
if pyproject_file.exists():
96-
with Path.open(pyproject_file, encoding="utf-8") as f:
97-
original_content = f.read()
98-
data = tomlkit.parse(original_content)
99-
# Backup original markers
100-
original_fixtures = data.get("tool", {}).get("codeflash", {}).get("override-fixtures", [])
101-
original_fixtures.append("please_put_your_fixtures_here")
102-
data["tool"]["pytest"]["override-fixtures"]["markers"] = list(original_fixtures)
103-
with Path.open(pyproject_file, "w", encoding="utf-8") as f:
104-
f.write(tomlkit.dumps(data))
105-
yield
106-
finally:
107-
with Path.open(pyproject_file, "w", encoding="utf-8") as f:
108-
f.write(original_content)
109-
110-
111-
@contextmanager
112-
def add_custom_markers_to_pyproject() -> None:
113-
pyproject_file = find_pyproject_toml()
114-
try:
115-
# Read original file
116-
if pyproject_file.exists():
117-
with Path.open(pyproject_file, encoding="utf-8") as f:
118-
original_content = f.read()
119-
data = tomlkit.parse(original_content)
120-
# Backup original markers
121-
original_markers = data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("markers", [])
122-
original_markers.append("codeflash_no_autouse")
123-
data["tool"]["pytest"]["ini_options"]["markers"] = list(original_markers)
124-
with Path.open(pyproject_file, "w", encoding="utf-8") as f:
125-
f.write(tomlkit.dumps(data))
126-
yield
127-
finally:
128-
with Path.open(pyproject_file, "w", encoding="utf-8") as f:
129-
f.write(original_content)
130-
131-
132-
@contextmanager
133-
def rename_conftest(tests_path: Path) -> None:
134-
conftest_file = find_conftest(tests_path)
135-
tmp_conftest_file = None
136-
try:
137-
# Rename original file
138-
if conftest_file:
139-
tmp_conftest_file = Path(str(conftest_file) + ".tmp")
140-
conftest_file.rename(tmp_conftest_file)
141-
yield
142-
finally:
143-
# Restore original file
144-
if conftest_file:
145-
tmp_conftest_file.rename(conftest_file)
146-
147-
14890
def encoded_tokens_len(s: str) -> int:
14991
"""Return the approximate length of the encoded tokens.
15092

codeflash/code_utils/config_parser.py

Lines changed: 5 additions & 4 deletions
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, Union
4+
from typing import Any
55

66
import tomlkit
77

@@ -31,17 +31,18 @@ def find_pyproject_toml(config_file: Path | None = None) -> Path:
3131
raise ValueError(msg)
3232

3333

34-
def find_conftest(tests_path: Path) -> Union[Path, None]:
34+
def find_conftest_files(tests_path: Path) -> list[Path]:
3535
# Find the conftest file on the root of the project
3636
dir_path = Path.cwd()
3737
cur_path = tests_path
38+
list_of_conftest_files = []
3839
while cur_path != dir_path:
3940
config_file = cur_path / "conftest.py"
4041
if config_file.exists():
41-
return config_file
42+
list_of_conftest_files.append(config_file)
4243
# Search for conftest.py in the parent directories
4344
cur_path = cur_path.parent
44-
return None
45+
return list_of_conftest_files
4546

4647

4748
def parse_config_file(

codeflash/discovery/discover_unit_tests.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
custom_addopts,
2424
get_run_tmp_file,
2525
module_name_from_file_path,
26-
rename_conftest,
2726
)
2827
from codeflash.code_utils.compat import SAFE_SYS_EXECUTABLE, codeflash_cache_db
2928
from codeflash.models.models import CodePosition, FunctionCalledInTest, TestsInFile, TestType
@@ -157,7 +156,7 @@ def discover_tests_pytest(
157156
project_root = cfg.project_root_path
158157

159158
tmp_pickle_path = get_run_tmp_file("collected_tests.pkl")
160-
with custom_addopts(), rename_conftest(tests_root):
159+
with custom_addopts():
161160
result = subprocess.run(
162161
[
163162
SAFE_SYS_EXECUTABLE,

codeflash/verification/test_runner.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def run_behavioral_tests(
5353
)
5454
else:
5555
test_files.append(str(file.instrumented_behavior_file_path))
56-
test_files = ['/Users/codeflash/Downloads/codeflash-dev/codeflash/code_to_optimize/tests/pytest/test_bubble_sort__perfinstrumented.py']
5756
pytest_cmd_list = (
5857
shlex.split(f"{SAFE_SYS_EXECUTABLE} -m pytest", posix=IS_POSIX)
5958
if pytest_cmd == "pytest"

pyproject.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,6 @@ formatter-cmds = [
238238
"uvx ruff check --exit-zero --fix $file",
239239
"uvx ruff format $file",
240240
]
241-
override-fixtures = ["fixture1"] #autouse fixtures present in conftest.py which have to be disabled during test execution
242-
243-
[tool.pytest.ini_options]
244-
markers = [
245-
"no_autouse"
246-
]
247241

248242

249243
[build-system]

0 commit comments

Comments
 (0)