Skip to content

Commit be1ae93

Browse files
committed
normalize paths and tmp_dir here too
1 parent 09e9d12 commit be1ae93

File tree

7 files changed

+106
-119
lines changed

7 files changed

+106
-119
lines changed

codeflash/code_utils/coverage_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ def generate_candidates(source_code_path: Path) -> set[str]:
4747

4848
last_added = source_code_path.name
4949
while current_path != current_path.parent:
50-
candidate_path = str(Path(current_path.name) / last_added)
50+
candidate_path = (Path(current_path.name) / last_added).as_posix()
5151
candidates.add(candidate_path)
5252
last_added = candidate_path
5353
current_path = current_path.parent
5454

55-
candidates.add(str(source_code_path))
55+
candidates.add(source_code_path.as_posix())
5656
return candidates
5757

5858

codeflash/result/create_pr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def existing_tests_source_for(
8585
):
8686
print_optimized_runtime = format_time(optimized_tests_to_runtimes[filename][qualified_name])
8787
print_original_runtime = format_time(original_tests_to_runtimes[filename][qualified_name])
88-
print_filename = filename.relative_to(tests_root)
88+
print_filename = filename.relative_to(tests_root).as_posix()
8989
greater = (
9090
optimized_tests_to_runtimes[filename][qualified_name]
9191
> original_tests_to_runtimes[filename][qualified_name]

tests/test_formatter.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,19 @@ def foo():
193193
assert actual == expected
194194

195195

196-
def test_formatter_error(temp_dir):
196+
def test_formatter_error(tmp_path: Path):
197197
original_code = """
198198
import os
199199
import sys
200200
def foo():
201201
return os.path.join(sys.path[0], 'bar')"""
202-
with tempfile.NamedTemporaryFile("w") as tmp:
203-
tmp.write(original_code)
204-
tmp.flush()
205-
tmp_path = tmp.name
206-
try:
207-
new_code = format_code(formatter_cmds=["exit 1"], path=Path(tmp_path), exit_on_failure=False)
208-
assert new_code == original_code
209-
except Exception as e:
210-
assert False, f"Shouldn't throw an exception even if the formatter is not found: {e}"
202+
temp_file = tmp_path / "test_formatter_error.py"
203+
temp_file.write_text(original_code, encoding="utf-8")
204+
try:
205+
new_code = format_code(formatter_cmds=["exit 1"], path=temp_file, exit_on_failure=False)
206+
assert new_code == original_code
207+
except Exception as e:
208+
assert False, f"Shouldn't throw an exception even if the formatter is not found: {e}"
211209

212210

213211
def _run_formatting_test(source_code: str, should_content_change: bool, expected = None, optimized_function: str = ""):

tests/test_instrument_tests.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def test_sort():
394394
assert new_test is not None
395395
assert new_test.replace('"', "'") == expected.format(
396396
module_path="code_to_optimize.tests.pytest.test_perfinjector_bubble_sort_results_temp",
397-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix(),
397+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
398398
).replace('"', "'")
399399

400400
success, new_perf_test = inject_profiling_into_existing_test(
@@ -409,7 +409,7 @@ def test_sort():
409409
assert new_perf_test is not None
410410
assert new_perf_test.replace('"', "'") == expected_perfonly.format(
411411
module_path="code_to_optimize.tests.pytest.test_perfinjector_bubble_sort_results_temp",
412-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
412+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
413413
).replace('"', "'")
414414

415415
with test_path.open("w") as f:
@@ -648,11 +648,11 @@ def test_sort_parametrized(input, expected_output):
648648
assert new_test is not None
649649
assert new_test.replace('"', "'") == expected.format(
650650
module_path="code_to_optimize.tests.pytest.test_perfinjector_bubble_sort_parametrized_results_temp",
651-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
651+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix(),
652652
).replace('"', "'")
653653
assert new_test_perf.replace('"', "'") == expected_perfonly.format(
654654
module_path="code_to_optimize.tests.pytest.test_perfinjector_bubble_sort_parametrized_results_temp",
655-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
655+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix(),
656656
).replace('"', "'")
657657
#
658658
# Overwrite old test with new instrumented test
@@ -921,7 +921,7 @@ def test_sort_parametrized_loop(input, expected_output):
921921
assert new_test is not None
922922
assert new_test.replace('"', "'") == expected.format(
923923
module_path="code_to_optimize.tests.pytest.test_perfinjector_bubble_sort_parametrized_loop_results_temp",
924-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
924+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
925925
).replace('"', "'")
926926

927927
# Overwrite old test with new instrumented test
@@ -930,7 +930,7 @@ def test_sort_parametrized_loop(input, expected_output):
930930

931931
assert new_test_perf.replace('"', "'") == expected_perf.format(
932932
module_path="code_to_optimize.tests.pytest.test_perfinjector_bubble_sort_parametrized_loop_results_temp",
933-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
933+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
934934
).replace('"', "'")
935935

936936
# Overwrite old test with new instrumented test
@@ -1276,12 +1276,12 @@ def test_sort():
12761276
assert new_test_behavior is not None
12771277
assert new_test_behavior.replace('"', "'") == expected.format(
12781278
module_path="code_to_optimize.tests.pytest.test_perfinjector_bubble_sort_loop_results_temp",
1279-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
1279+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
12801280
).replace('"', "'")
12811281

12821282
assert new_test_perf.replace('"', "'") == expected_perf.format(
12831283
module_path="code_to_optimize.tests.pytest.test_perfinjector_bubble_sort_loop_results_temp",
1284-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
1284+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
12851285
).replace('"', "'")
12861286

12871287
# Overwrite old test with new instrumented test
@@ -1585,11 +1585,11 @@ def test_sort(self):
15851585
assert new_test_behavior is not None
15861586
assert new_test_behavior.replace('"', "'") == expected.format(
15871587
module_path="code_to_optimize.tests.unittest.test_perfinjector_bubble_sort_unittest_results_temp",
1588-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
1588+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
15891589
).replace('"', "'")
15901590
assert new_test_perf.replace('"', "'") == expected_perf.format(
15911591
module_path="code_to_optimize.tests.unittest.test_perfinjector_bubble_sort_unittest_results_temp",
1592-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
1592+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
15931593
).replace('"', "'")
15941594
#
15951595
# Overwrite old test with new instrumented test
@@ -1842,13 +1842,13 @@ def test_sort(self, input, expected_output):
18421842
assert new_test_behavior is not None
18431843
assert new_test_behavior.replace('"', "'") == expected_behavior.format(
18441844
module_path="code_to_optimize.tests.unittest.test_perfinjector_bubble_sort_unittest_parametrized_results_temp",
1845-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
1845+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
18461846
).replace('"', "'")
18471847

18481848
assert new_test_perf is not None
18491849
assert new_test_perf.replace('"', "'") == expected_perf.format(
18501850
module_path="code_to_optimize.tests.unittest.test_perfinjector_bubble_sort_unittest_parametrized_results_temp",
1851-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
1851+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
18521852
).replace('"', "'")
18531853

18541854
#
@@ -2108,11 +2108,11 @@ def test_sort(self):
21082108
assert new_test_behavior is not None
21092109
assert new_test_behavior.replace('"', "'") == expected_behavior.format(
21102110
module_path="code_to_optimize.tests.unittest.test_perfinjector_bubble_sort_unittest_loop_results_temp",
2111-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
2111+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
21122112
).replace('"', "'")
21132113
assert new_test_perf.replace('"', "'") == expected_perf.format(
21142114
module_path="code_to_optimize.tests.unittest.test_perfinjector_bubble_sort_unittest_loop_results_temp",
2115-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
2115+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
21162116
).replace('"', "'")
21172117
#
21182118
# # Overwrite old test with new instrumented test
@@ -2367,11 +2367,11 @@ def test_sort(self, input, expected_output):
23672367
assert new_test_behavior is not None
23682368
assert new_test_behavior.replace('"', "'") == expected_behavior.format(
23692369
module_path="code_to_optimize.tests.unittest.test_perfinjector_bubble_sort_unittest_parametrized_loop_results_temp",
2370-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
2370+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
23712371
).replace('"', "'")
23722372
assert new_test_perf.replace('"', "'") == expected_perf.format(
23732373
module_path="code_to_optimize.tests.unittest.test_perfinjector_bubble_sort_unittest_parametrized_loop_results_temp",
2374-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
2374+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
23752375
).replace('"', "'")
23762376
#
23772377
# Overwrite old test with new instrumented test
@@ -2668,7 +2668,7 @@ def test_class_name_A_function_name():
26682668
assert success
26692669
assert new_test is not None
26702670
assert new_test.replace('"', "'") == expected.format(
2671-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
2671+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix(),
26722672
module_path="tests.pytest.test_class_function_instrumentation_temp",
26732673
).replace('"', "'")
26742674

@@ -2739,7 +2739,7 @@ def test_common_tags_1():
27392739
assert new_test is not None
27402740
assert new_test.replace('"', "'") == expected.format(
27412741
module_path="tests.pytest.test_wrong_function_instrumentation_temp",
2742-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
2742+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix(),
27432743
).replace('"', "'")
27442744
finally:
27452745
test_path.unlink(missing_ok=True)
@@ -2802,7 +2802,7 @@ def test_sort():
28022802
assert new_test is not None
28032803
assert new_test.replace('"', "'") == expected.format(
28042804
module_path="tests.pytest.test_conditional_instrumentation_temp",
2805-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
2805+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
28062806
).replace('"', "'")
28072807
finally:
28082808
test_path.unlink(missing_ok=True)
@@ -2879,15 +2879,15 @@ def test_sort():
28792879
assert success
28802880
formatted_expected = expected.format(
28812881
module_path="tests.pytest.test_perfinjector_bubble_sort_results_temp",
2882-
tmp_dir_path=str(get_run_tmp_file(Path("test_return_values"))),
2882+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
28832883
)
28842884
assert new_test is not None
28852885
assert new_test.replace('"', "'") == formatted_expected.replace('"', "'")
28862886
finally:
28872887
test_path.unlink(missing_ok=True)
28882888

28892889

2890-
def test_class_method_instrumentation() -> None:
2890+
def test_class_method_instrumentation(tmp_path: Path) -> None:
28912891
code = """from codeflash.optimization.optimizer import Optimizer
28922892
def test_code_replacement10() -> None:
28932893
get_code_output = '''random code'''
@@ -2957,24 +2957,24 @@ def test_code_replacement10() -> None:
29572957
"""
29582958
)
29592959

2960-
with tempfile.NamedTemporaryFile(mode="w") as f:
2961-
f.write(code)
2962-
f.flush()
2963-
func = FunctionToOptimize(
2964-
function_name="get_code_optimization_context",
2965-
parents=[FunctionParent("Optimizer", "ClassDef")],
2966-
file_path=Path(f.name),
2967-
)
2968-
original_cwd = Path.cwd()
2969-
run_cwd = Path(__file__).parent.parent.resolve()
2970-
os.chdir(run_cwd)
2971-
success, new_test = inject_profiling_into_existing_test(
2972-
Path(f.name), [CodePosition(22, 28), CodePosition(28, 28)], func, Path(f.name).parent, "pytest"
2973-
)
2974-
os.chdir(original_cwd)
2960+
test_file_path = tmp_path / "test_class_method_instrumentation.py"
2961+
test_file_path.write_text(code, encoding="utf-8")
2962+
2963+
func = FunctionToOptimize(
2964+
function_name="get_code_optimization_context",
2965+
parents=[FunctionParent("Optimizer", "ClassDef")],
2966+
file_path=test_file_path,
2967+
)
2968+
original_cwd = Path.cwd()
2969+
run_cwd = Path(__file__).parent.parent.resolve()
2970+
os.chdir(run_cwd)
2971+
success, new_test = inject_profiling_into_existing_test(
2972+
test_file_path, [CodePosition(22, 28), CodePosition(28, 28)], func, test_file_path.parent, "pytest"
2973+
)
2974+
os.chdir(original_cwd)
29752975
assert success
29762976
assert new_test.replace('"', "'") == expected.replace('"', "'").format(
2977-
module_path=Path(f.name).name, tmp_dir_path=get_run_tmp_file(Path("test_return_values"))
2977+
module_path=test_file_path.name, tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
29782978
)
29792979

29802980

@@ -3039,7 +3039,7 @@ def test_sleepfunc_sequence_short(n, expected_total_sleep_time):
30393039
assert new_test is not None
30403040
assert new_test.replace('"', "'") == expected.format(
30413041
module_path="code_to_optimize.tests.pytest.test_time_correction_instrumentation_temp",
3042-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
3042+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix(),
30433043
).replace('"', "'")
30443044
# Overwrite old test with new instrumented test
30453045
with test_path.open("w") as f:
@@ -3158,7 +3158,7 @@ def test_sleepfunc_sequence_short(self, n, expected_total_sleep_time):
31583158
assert new_test is not None
31593159
assert new_test.replace('"', "'") == expected.format(
31603160
module_path="code_to_optimize.tests.unittest.test_time_correction_instrumentation_unittest_temp",
3161-
tmp_dir_path=get_run_tmp_file(Path("test_return_values")),
3161+
tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix(),
31623162
).replace('"', "'")
31633163
# Overwrite old test with new instrumented test
31643164
with test_path.open("w") as f:

0 commit comments

Comments
 (0)