Skip to content

Commit cc21249

Browse files
committed
Update test_instrument_tests.py
1 parent 3066298 commit cc21249

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

tests/test_instrument_tests.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,24 +2773,29 @@ def test_code_replacement10() -> None:
27732773
codeflash_con.close()
27742774
"""
27752775

2776-
with tempfile.NamedTemporaryFile(mode="w") as f:
2777-
f.write(code)
2778-
f.flush()
2776+
with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_file:
2777+
temp_file.write(code)
2778+
temp_file_path = Path(temp_file.name)
2779+
2780+
try:
27792781
func = FunctionToOptimize(
27802782
function_name="get_code_optimization_context",
27812783
parents=[FunctionParent("Optimizer", "ClassDef")],
2782-
file_path=Path(f.name),
2784+
file_path=temp_file_path,
27832785
)
27842786
original_cwd = Path.cwd()
27852787
run_cwd = Path(__file__).parent.parent.resolve()
27862788
os.chdir(run_cwd)
27872789
success, new_test = inject_profiling_into_existing_test(
2788-
Path(f.name), [CodePosition(22, 28), CodePosition(28, 28)], func, Path(f.name).parent, "pytest"
2790+
temp_file_path, [CodePosition(22, 28), CodePosition(28, 28)], func, temp_file_path.parent, "pytest"
27892791
)
27902792
os.chdir(original_cwd)
2793+
finally:
2794+
temp_file_path.unlink(missing_ok=True)
2795+
27912796
assert success
27922797
assert new_test == expected.format(
2793-
module_path=Path(f.name).name, tmp_dir_path=get_run_tmp_file(Path("test_return_values"))
2798+
module_path=temp_file_path.name, tmp_dir_path=get_run_tmp_file(Path("test_return_values")).as_posix()
27942799
)
27952800

27962801

0 commit comments

Comments
 (0)