From ab908fee43e6a44bd8a88d1520e592246c6d4ba2 Mon Sep 17 00:00:00 2001
From: Wolf-Bastian Poettner <bastian@poettner.de>
Date: Fri, 10 Apr 2020 13:12:17 +0000
Subject: [PATCH] Remove temporary directories after failed tests

---
 src/documents/tests/test_file_handling.py | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/documents/tests/test_file_handling.py b/src/documents/tests/test_file_handling.py
index 0da2c27e7..1dbe1ca18 100644
--- a/src/documents/tests/test_file_handling.py
+++ b/src/documents/tests/test_file_handling.py
@@ -4,6 +4,7 @@ import shutil
 from unittest import mock
 from uuid import uuid4
 from pathlib import Path
+from shutil import rmtree
 
 from dateutil import tz
 from django.test import TestCase, override_settings
@@ -14,6 +15,19 @@ from django.conf import settings
 
 
 class TestDate(TestCase):
+    def tearDown(self):
+        # Delete all temporary directories from failed tests
+        root = os.path.normpath("/tmp")
+
+        for filename in os.listdir(root):
+            fullname = os.path.join(root, filename)
+
+            if not os.path.isdir(fullname):
+                continue
+
+            if filename.startswith("paperless-tests-"):
+                shutil.rmtree(fullname, ignore_errors=True)
+
     @override_settings(PAPERLESS_FILENAME_FORMAT="")
     def test_source_filename(self):
         document = Document()