From 53786932fdc3a4099c30fbfbe15c578eee31c5b0 Mon Sep 17 00:00:00 2001 From: Itai Hay <3392524+itaihay@users.noreply.github.com> Date: Wed, 22 Oct 2025 16:09:03 +0300 Subject: [PATCH] Move pytest test IDs file deletion to finally block If the vscode-pytest execution is wrapped and re-triggered then the deletion of the file causes the second run the fail. Deleting the file on the finally block ensures that the pytest execution will work even if re-run. - Move the deletion of the test IDs temp file from before pytest execution to a `finally` block. - This ensures the temp file is always cleaned up, even if pytest execution fails or an exception occurs. - Move `ids_path` initialization outside the try block so it's accessible in the finally block for cleanup. --- python_files/vscode_pytest/run_pytest_script.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/python_files/vscode_pytest/run_pytest_script.py b/python_files/vscode_pytest/run_pytest_script.py index c0f5114b375c..8d30ba7e4399 100644 --- a/python_files/vscode_pytest/run_pytest_script.py +++ b/python_files/vscode_pytest/run_pytest_script.py @@ -51,20 +51,22 @@ def run_pytest(args): run_test_ids_pipe = os.environ.get("RUN_TEST_IDS_PIPE") if run_test_ids_pipe: + ids_path = pathlib.Path(run_test_ids_pipe) try: - # Read the test ids from the file, delete file, and run pytest. - ids_path = pathlib.Path(run_test_ids_pipe) + # Read the test ids from the file and run pytest. ids = ids_path.read_text(encoding="utf-8").splitlines() - try: - ids_path.unlink() - except Exception as e: - print("Error[vscode-pytest]: unable to delete temp file" + str(e)) arg_array = ["-p", "vscode_pytest", *args, *ids] print("Running pytest with args: " + str(arg_array)) pytest.main(arg_array) except Exception as e: print("Error[vscode-pytest]: unable to read testIds from temp file" + str(e)) run_pytest(args) + finally: + # Delete the test ids temp file. + try: + ids_path.unlink() + except Exception as e: + print("Error[vscode-pytest]: unable to delete temp file" + str(e)) else: print("Error[vscode-pytest]: RUN_TEST_IDS_PIPE env var is not set.") run_pytest(args)