@@ -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
28922892def 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