Skip to content

Commit a3af1da

Browse files
committed
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.
1 parent 900ae9c commit a3af1da

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

python_files/vscode_pytest/run_pytest_script.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,22 @@ def run_pytest(args):
5151

5252
run_test_ids_pipe = os.environ.get("RUN_TEST_IDS_PIPE")
5353
if run_test_ids_pipe:
54+
ids_path = pathlib.Path(run_test_ids_pipe)
5455
try:
55-
# Read the test ids from the file, delete file, and run pytest.
56-
ids_path = pathlib.Path(run_test_ids_pipe)
56+
# Read the test ids from the file and run pytest.
5757
ids = ids_path.read_text(encoding="utf-8").splitlines()
58-
try:
59-
ids_path.unlink()
60-
except Exception as e:
61-
print("Error[vscode-pytest]: unable to delete temp file" + str(e))
6258
arg_array = ["-p", "vscode_pytest", *args, *ids]
6359
print("Running pytest with args: " + str(arg_array))
6460
pytest.main(arg_array)
6561
except Exception as e:
6662
print("Error[vscode-pytest]: unable to read testIds from temp file" + str(e))
6763
run_pytest(args)
64+
finally:
65+
# Delete the test ids temp file.
66+
try:
67+
ids_path.unlink()
68+
except Exception as e:
69+
print("Error[vscode-pytest]: unable to delete temp file" + str(e))
6870
else:
6971
print("Error[vscode-pytest]: RUN_TEST_IDS_PIPE env var is not set.")
7072
run_pytest(args)

0 commit comments

Comments
 (0)