[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#754143: Fix the tests to not pollute the data directory



> I'm not sure what's the best approach but I see two clean solutions:
> 
> - something global implemented as a derivative class of TestCase that
>   does the required directory creation and settings change in self.setUp
>   and drops the directory with a function recorded with self.addCleanup

See attached patch. Feel free to adapt, I am unsure where to put the
subclass bits.

Cheers,
Christophe
>From 59dfcc53ab2e3c294b09dbddb9334b63132092e0 Mon Sep 17 00:00:00 2001
From: Christophe Siraut <d@tobald.eu.org>
Date: Wed, 9 Jul 2014 17:35:23 +0200
Subject: [PATCH] Subclass TestCase for using temporary folders

---
 distro_tracker/core/tests/cleantestcase.py |   12 ++++++++++++
 distro_tracker/vendor/debian/tests.py      |    3 ++-
 2 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 distro_tracker/core/tests/cleantestcase.py

diff --git a/distro_tracker/core/tests/cleantestcase.py b/distro_tracker/core/tests/cleantestcase.py
new file mode 100644
index 0000000..cff53a7
--- /dev/null
+++ b/distro_tracker/core/tests/cleantestcase.py
@@ -0,0 +1,12 @@
+import shutil
+import tempfile
+from django.test import TestCase as DjangoTestCase
+from django.conf import settings
+
+class TestCase(DjangoTestCase):
+    def setUp(self):
+        settings.DISTRO_TRACKER_CACHE_DIRECTORY = tempfile.mkdtemp()
+        settings.DISTRO_TRACKER_KEYRING_DIRECTORY = tempfile.mkdtemp()
+        self.addCleanup(shutil.rmtree, settings.DISTRO_TRACKER_CACHE_DIRECTORY)
+        self.addCleanup(shutil.rmtree, settings.DISTRO_TRACKER_KEYRING_DIRECTORY)
+        super(TestCase, self).setUp()
diff --git a/distro_tracker/vendor/debian/tests.py b/distro_tracker/vendor/debian/tests.py
index 0848044..3ed2fbf 100644
--- a/distro_tracker/vendor/debian/tests.py
+++ b/distro_tracker/vendor/debian/tests.py
@@ -15,7 +15,8 @@ Tests for Debian-specific modules/functionality of Distro Tracker.
 """
 
 from __future__ import unicode_literals
-from django.test import TestCase, SimpleTestCase
+from django.test import SimpleTestCase
+from distro_tracker.core.tests.cleantestcase import TestCase
 from django.test.utils import override_settings
 from django.core import mail
 from django.core.urlresolvers import reverse
-- 
1.7.10.4


Reply to: