test_fixtures: Add logic for removing stale test directories.

Similarly to how stale database removal is handled, we add a check for
stale test run directories at the end of the `test-backend` script.
This commit is contained in:
Wyatt Hoodes
2019-07-05 12:29:17 -10:00
committed by Tim Abbott
parent f7f32df3ef
commit 0e3fddbc6e
2 changed files with 19 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ from importlib import import_module
from io import StringIO
import glob
import time
import shutil
from django.db import connections, DEFAULT_DB_ALIAS, ProgrammingError, \
connection
@@ -296,3 +297,15 @@ def destroy_leaked_test_databases(expiry_time: int = 60 * 60) -> int:
if p.returncode != 0:
raise RuntimeError("Error cleaning up test databases!")
return len(databases_to_drop)
def remove_test_run_directories(expiry_time: int = 60 * 60) -> int:
removed = 0
directories = glob.glob(os.path.join(UUID_VAR_DIR, "test-backend", "run_*"))
for test_run in directories:
if round(time.time()) - os.path.getmtime(test_run) > expiry_time:
try:
shutil.rmtree(test_run)
removed += 1
except FileNotFoundError:
pass
return removed