Skip to content

Commit e17106f

Browse files
committed
putting file restore in the finally block and also handling failures if earlier tests fail
1 parent ee90a08 commit e17106f

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

tests/test_instrument_tests.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ def test_prepare_image_for_yolo():
284284

285285

286286
def test_perfinjector_bubble_sort_results() -> None:
287+
computed_fn_opt = False
287288
code = """from code_to_optimize.bubble_sort import sorter
288289
289290
@@ -504,6 +505,7 @@ def test_sort():
504505
with helper_function_path.open(encoding="utf8") as f:
505506
helper_code = f.read()
506507
original_helper_code[helper_function_path] = helper_code
508+
computed_fn_opt = True
507509
line_profiler_output_file = add_decorator_imports(
508510
func_optimizer.function_to_optimize, code_context)
509511
line_profile_results, _ = func_optimizer.run_and_parse_tests(
@@ -519,14 +521,16 @@ def test_sort():
519521
tmp_lpr = list(line_profile_results['timings'].keys())
520522
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==2
521523
finally:
522-
func_optimizer.write_code_and_helpers(
523-
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
524-
)
524+
if computed_fn_opt:
525+
func_optimizer.write_code_and_helpers(
526+
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
527+
)
525528
test_path.unlink(missing_ok=True)
526529
test_path_perf.unlink(missing_ok=True)
527530

528531

529532
def test_perfinjector_bubble_sort_parametrized_results() -> None:
533+
computed_fn_opt = False
530534
code = """from code_to_optimize.bubble_sort import sorter
531535
import pytest
532536
@@ -754,6 +758,7 @@ def test_sort_parametrized(input, expected_output):
754758
with helper_function_path.open(encoding="utf8") as f:
755759
helper_code = f.read()
756760
original_helper_code[helper_function_path] = helper_code
761+
computed_fn_opt = True
757762
line_profiler_output_file = add_decorator_imports(
758763
func_optimizer.function_to_optimize, code_context)
759764
line_profile_results, _ = func_optimizer.run_and_parse_tests(
@@ -769,14 +774,16 @@ def test_sort_parametrized(input, expected_output):
769774
tmp_lpr = list(line_profile_results['timings'].keys())
770775
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==3
771776
finally:
772-
func_optimizer.write_code_and_helpers(
773-
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
774-
)
777+
if computed_fn_opt:
778+
func_optimizer.write_code_and_helpers(
779+
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
780+
)
775781
test_path.unlink(missing_ok=True)
776782
test_path_perf.unlink(missing_ok=True)
777783

778784

779785
def test_perfinjector_bubble_sort_parametrized_loop_results() -> None:
786+
computed_fn_opt = False
780787
code = """from code_to_optimize.bubble_sort import sorter
781788
import pytest
782789
@@ -1090,6 +1097,7 @@ def test_sort_parametrized_loop(input, expected_output):
10901097
with helper_function_path.open(encoding="utf8") as f:
10911098
helper_code = f.read()
10921099
original_helper_code[helper_function_path] = helper_code
1100+
computed_fn_opt = True
10931101
line_profiler_output_file = add_decorator_imports(
10941102
func_optimizer.function_to_optimize, code_context)
10951103
line_profile_results, _ = func_optimizer.run_and_parse_tests(
@@ -1105,15 +1113,17 @@ def test_sort_parametrized_loop(input, expected_output):
11051113
tmp_lpr = list(line_profile_results['timings'].keys())
11061114
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==6
11071115
finally:
1108-
func_optimizer.write_code_and_helpers(
1109-
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
1110-
)
1116+
if computed_fn_opt:
1117+
func_optimizer.write_code_and_helpers(
1118+
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
1119+
)
11111120
test_path.unlink(missing_ok=True)
11121121
test_path_behavior.unlink(missing_ok=True)
11131122
test_path_perf.unlink(missing_ok=True)
11141123

11151124

11161125
def test_perfinjector_bubble_sort_loop_results() -> None:
1126+
computed_fn_opt = False
11171127
code = """from code_to_optimize.bubble_sort import sorter
11181128
11191129
@@ -1360,6 +1370,7 @@ def test_sort():
13601370
with helper_function_path.open(encoding="utf8") as f:
13611371
helper_code = f.read()
13621372
original_helper_code[helper_function_path] = helper_code
1373+
computed_fn_opt = True
13631374
line_profiler_output_file = add_decorator_imports(
13641375
func_optimizer.function_to_optimize, code_context)
13651376
line_profile_results, _ = func_optimizer.run_and_parse_tests(
@@ -1375,9 +1386,10 @@ def test_sort():
13751386
tmp_lpr = list(line_profile_results['timings'].keys())
13761387
assert len(tmp_lpr) == 1 and line_profile_results['timings'][tmp_lpr[0]][0][1]==3
13771388
finally:
1378-
func_optimizer.write_code_and_helpers(
1379-
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
1380-
)
1389+
if computed_fn_opt is True:
1390+
func_optimizer.write_code_and_helpers(
1391+
func_optimizer.function_to_optimize_source_code, original_helper_code, func_optimizer.function_to_optimize.file_path
1392+
)
13811393
test_path.unlink(missing_ok=True)
13821394
test_path_perf.unlink(missing_ok=True)
13831395
test_path_behavior.unlink(missing_ok=True)

0 commit comments

Comments
 (0)