Skip to content

Commit 81ce1fe

Browse files
committed
wip
1 parent fee9f11 commit 81ce1fe

File tree

6 files changed

+96
-14
lines changed

6 files changed

+96
-14
lines changed

code_to_optimize/tests/pytest/benchmarks/test_benchmark_bubble_sort.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,29 @@
33
from code_to_optimize.bubble_sort import sorter
44

55

6+
class DummyBenchmark:
7+
def __call__(self, func, *args, **kwargs):
8+
# Mimic calling benchmark(func, *args, **kwargs)
9+
return func(*args, **kwargs)
10+
11+
def __getattr__(self, name):
12+
# Mimic benchmark attributes like .pedantic, .extra_info etc.
13+
def dummy(*args, **kwargs):
14+
return None
15+
16+
return dummy
17+
18+
19+
@pytest.fixture
20+
def benchmark():
21+
return DummyBenchmark()
22+
23+
624
def test_sort(benchmark):
725
result = benchmark(sorter, list(reversed(range(500))))
826
assert result == list(range(500))
927

28+
1029
# This should not be picked up as a benchmark test
1130
def test_sort2():
1231
result = sorter(list(reversed(range(500))))
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
import time
3+
4+
# @pytest.fixture(autouse=True)
5+
# def fixture(request):
6+
# if request.node.get_closest_marker("no_autouse"):
7+
# # Skip the fixture logic
8+
# yield
9+
# else:
10+
# start_time = time.time()
11+
# time.sleep(0.1)
12+
# yield
13+
# print(f"Took {time.time() - start_time} seconds")
14+
15+
16+
@pytest.fixture(autouse=True)
17+
def fixture1(request): # We don't need this fixture during testing
18+
start_time = time.time()
19+
time.sleep(0.1)
20+
yield
21+
print(f"Took {time.time() - start_time} seconds")
22+
23+
24+
@pytest.fixture(autouse=True)
25+
def fixture2(request): # We need it
26+
yield

code_to_optimize/tests/pytest/conftest.py.tmp

Lines changed: 0 additions & 14 deletions
This file was deleted.

codeflash/code_utils/code_replacer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ def normalize_code(code: str) -> str:
3333
return ast.unparse(normalize_node(ast.parse(code)))
3434

3535

36+
def modify_autouse_fixture():
37+
# find fixutre definition in conftetst.py (the one closest to the test)
38+
# get fixtures present in override-fixtures in pyproject.toml
39+
# add if marker closest return
40+
autousetransformer
41+
42+
3643
class OptimFunctionCollector(cst.CSTVisitor):
3744
METADATA_DEPENDENCIES = (cst.metadata.ParentNodeProvider,)
3845

codeflash/code_utils/code_utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,48 @@ 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+
90132
@contextmanager
91133
def rename_conftest(tests_path: Path) -> None:
92134
conftest_file = find_conftest(tests_path)

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ 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+
241243
[tool.pytest.ini_options]
242244
markers = [
243245
"no_autouse"

0 commit comments

Comments
 (0)