Skip to content

Commit b065cbc

Browse files
committed
start testing now
1 parent c5c9533 commit b065cbc

File tree

4 files changed

+21
-32
lines changed

4 files changed

+21
-32
lines changed

code_to_optimize/tests/pytest/benchmarks/test_benchmark_bubble_sort.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,10 @@
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-
246
def test_sort(benchmark):
257
result = benchmark(sorter, list(reversed(range(500))))
268
assert result == list(range(500))
279

28-
2910
# This should not be picked up as a benchmark test
3011
def test_sort2():
3112
result = sorter(list(reversed(range(500))))

codeflash/code_utils/code_replacer.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,27 @@ def leave_FunctionDef(self, original_node: cst.FunctionDef, updated_node: cst.Fu
7676

7777

7878
@contextlib.contextmanager
79-
def disable_autouse(test_path: Path) -> None:
79+
def disable_autouse(test_path: Path) -> str:
8080
file_content = test_path.read_text(encoding="utf-8")
81-
try:
82-
module = cst.parse_module(file_content)
83-
disable_autouse_fixture = AutouseFixtureModifier()
84-
modified_module = module.visit(disable_autouse_fixture)
85-
test_path.write_text(modified_module.code, encoding="utf-8")
86-
finally:
87-
test_path.write_text(file_content, encoding="utf-8")
81+
module = cst.parse_module(file_content)
82+
disable_autouse_fixture = AutouseFixtureModifier()
83+
modified_module = module.visit(disable_autouse_fixture)
84+
test_path.write_text(modified_module.code, encoding="utf-8")
85+
return file_content
8886

8987

90-
def modify_autouse_fixture(test_paths: list[Path]) -> None:
88+
def modify_autouse_fixture(test_paths: list[Path]) -> dict[Path, list[str]]:
9189
# find fixutre definition in conftetst.py (the one closest to the test)
9290
# get fixtures present in override-fixtures in pyproject.toml
9391
# add if marker closest return
92+
file_content_map = {}
9493
conftest_files = find_conftest_files(test_paths)
9594
for cf_file in conftest_files:
9695
# iterate over all functions in the file
9796
# if function has autouse fixture, modify function to bypass with custom marker
98-
disable_autouse(cf_file)
97+
original_content = disable_autouse(cf_file)
98+
file_content_map[cf_file] = original_content
99+
return file_content_map
99100

100101

101102
# # reuse line profiler utils to add decorator and import to test fns

codeflash/code_utils/code_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,8 @@ def cleanup_paths(paths: list[Path]) -> None:
208208
shutil.rmtree(path, ignore_errors=True)
209209
else:
210210
path.unlink(missing_ok=True)
211+
212+
213+
def restore_conftest(path_to_content_map: dict[Path, str]) -> None:
214+
for path, file_content in path_to_content_map.items():
215+
path.write_text(file_content, encoding="utf8")

codeflash/optimization/function_optimizer.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
get_run_tmp_file,
3434
has_any_async_functions,
3535
module_name_from_file_path,
36+
restore_conftest,
3637
)
3738
from codeflash.code_utils.config_consts import (
3839
INDIVIDUAL_TESTCASE_TIMEOUT,
@@ -212,7 +213,8 @@ def optimize_function(self) -> Result[BestOptimization, str]: # noqa: PLR0911
212213
for key in set(self.function_to_tests) | set(function_to_concolic_tests)
213214
}
214215
instrumented_unittests_created_for_function = self.instrument_existing_tests(function_to_all_tests)
215-
216+
# logger.debug("disabling all autouse fixtures associated with the test files")
217+
original_conftest_content = modify_autouse_fixture(list(instrumented_unittests_created_for_function))
216218
# Get a dict of file_path_to_classes of fto and helpers_of_fto
217219
file_path_to_helper_classes = defaultdict(set)
218220
for function_source in code_context.helper_functions:
@@ -234,13 +236,15 @@ def optimize_function(self) -> Result[BestOptimization, str]: # noqa: PLR0911
234236
)
235237

236238
if not is_successful(baseline_result):
239+
restore_conftest(original_conftest_content)
237240
cleanup_paths(paths_to_cleanup)
238241
return Failure(baseline_result.failure())
239242

240243
original_code_baseline, test_functions_to_remove = baseline_result.unwrap()
241244
if isinstance(original_code_baseline, OriginalCodeBaseline) and not coverage_critic(
242245
original_code_baseline.coverage_results, self.args.test_framework
243246
):
247+
restore_conftest(original_conftest_content)
244248
cleanup_paths(paths_to_cleanup)
245249
return Failure("The threshold for test coverage was not met.")
246250
# request for new optimizations but don't block execution, check for completion later
@@ -746,8 +750,6 @@ def instrument_existing_tests(self, function_to_all_tests: dict[str, list[Functi
746750
f"{concolic_coverage_test_files_count} concolic coverage test file"
747751
f"{'s' if concolic_coverage_test_files_count != 1 else ''} for {func_qualname}"
748752
)
749-
logger.debug("disabling all autouse fixtures associated with the test files")
750-
modify_autouse_fixture(list(unique_instrumented_test_files))
751753
logger.debug("add custom marker to all tests")
752754
add_custom_marker_to_all_tests(list(unique_instrumented_test_files))
753755
return unique_instrumented_test_files

0 commit comments

Comments
 (0)