summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS8
-rw-r--r--mocker.py53
-rwxr-xr-xtest.py96
3 files changed, 156 insertions, 1 deletions
diff --git a/NEWS b/NEWS
index 9322105..59de88f 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,11 @@
+0.9 (2007-XX-XX)
+================
+
+- New MockerTestCase.makeFile() and .makeDir() helpers. They offer
+ easy creation of temporary files/directories with certainty of
+ removal after tests finish running.
+
+
0.8 (2007-11-11)
================
diff --git a/mocker.py b/mocker.py
index 8dd943c..c7acd26 100644
--- a/mocker.py
+++ b/mocker.py
@@ -4,8 +4,10 @@ Copyright (c) 2007 Gustavo Niemeyer <gustavo@niemeyer.net>
Graceful platform for test doubles in Python (mocks, stubs, fakes, and dummies).
"""
import __builtin__
+import tempfile
import unittest
import inspect
+import shutil
import types
import sys
import os
@@ -94,9 +96,11 @@ class MockerTestCase(unittest.TestCase):
try:
test_method()
except:
+ self.__cleanup()
self.mocker.restore()
raise
else:
+ self.__cleanup()
self.mocker.restore()
self.mocker.verify()
test_method_wrapper.__doc__ = test_method.__doc__
@@ -104,8 +108,57 @@ class MockerTestCase(unittest.TestCase):
self.mocker = Mocker()
+ self.__cleanup_paths = []
+
super(MockerTestCase, self).__init__(methodName)
+ def __cleanup(self):
+ for path in self.__cleanup_paths:
+ if os.path.isfile(path):
+ os.unlink(path)
+ elif os.path.isdir(path):
+ shutil.rmtree(path)
+
+ def makeFile(self, content=None, suffix="", prefix="tmp", basename=None,
+ dirname=None):
+ """Create a temporary file and return the path to it.
+
+ @param content: Initial content for the file.
+ @param suffix: Suffix to be given to the file's basename.
+ @param prefix: Prefix to be given to the file's basename.
+ @param basename: Full basename for the file.
+ @param dirname: Put file inside this directory.
+
+ The file is removed after the test runs.
+ """
+ if basename is not None:
+ if dirname is None:
+ dirname = tempfile.mkdtemp()
+ self.__cleanup_paths.append(dirname)
+ filename = os.path.join(dirname, basename)
+ else:
+ fd, filename = tempfile.mkstemp(suffix, prefix, dirname)
+ self.__cleanup_paths.append(filename)
+ os.close(fd)
+ if content is not None:
+ file = open(filename, "w")
+ file.write(content)
+ file.close()
+ return filename
+
+ def makeDir(self, suffix="", prefix="tmp", dirname=None):
+ """Create a temporary directory and return the path to it.
+
+ @param suffix: Suffix to be given to the file's basename.
+ @param prefix: Prefix to be given to the file's basename.
+ @param dirname: Put directory inside this parent directory.
+
+ The directory is removed after the test runs.
+ """
+ dirname = tempfile.mkdtemp(suffix, prefix, dirname)
+ self.__cleanup_paths.append(dirname)
+ return dirname
+
def failUnlessIs(self, first, second, msg=None):
"""Assert that C{first} is the same object as C{second}."""
if first is not second:
diff --git a/test.py b/test.py
index 315c507..db6527d 100755
--- a/test.py
+++ b/test.py
@@ -1,6 +1,8 @@
#!/usr/bin/python
import unittest
+import tempfile
import inspect
+import shutil
import sys
import os
import gc
@@ -245,7 +247,12 @@ class ExpectTest(unittest.TestCase):
class MockerTestCaseTest(unittest.TestCase):
def setUp(self):
- self.test = MockerTestCase("__init__")
+ self.test = MockerTestCase("shortDescription")
+
+ def tearDown(self):
+ self.test.mocker.restore()
+ # Run it so that any cleanups are performed.
+ self.test.run()
def test_has_mocker(self):
self.assertEquals(type(self.test.mocker), Mocker)
@@ -510,6 +517,93 @@ class MockerTestCaseTest(unittest.TestCase):
self.assertEquals(get_method("failIfIdentical"),
get_method("failIfIs"))
+ def test_make_file_returns_filename(self):
+ filename = self.test.makeFile()
+ self.assertEquals(os.path.getsize(filename), 0)
+
+ def test_make_file_cleansup_on_success(self):
+ filename = self.test.makeFile()
+ self.test.run()
+ self.assertEquals(os.path.isfile(filename), False)
+
+ def test_make_file_cleansup_on_failure(self):
+ class MyTest(MockerTestCase):
+ def test_method(self):
+ raise AssertionError("BOOM!")
+ test = MyTest("test_method")
+ filename = test.makeFile()
+ test.run()
+ self.assertEquals(os.path.isfile(filename), False)
+
+ def test_make_file_with_content(self):
+ filename = self.test.makeFile("content")
+ self.assertEquals(open(filename).read(), "content")
+
+ def test_make_file_with_prefix(self):
+ filename = self.test.makeFile(prefix="prefix-")
+ self.assertTrue(os.path.basename(filename).startswith("prefix-"))
+
+ def test_make_file_with_suffix(self):
+ filename = self.test.makeFile(suffix="-suffix")
+ self.assertTrue(os.path.basename(filename).endswith("-suffix"))
+
+ def test_make_file_with_dirname(self):
+ dirname = tempfile.mkdtemp()
+ try:
+ filename = self.test.makeFile(dirname=dirname)
+ self.assertEquals(os.path.dirname(filename), dirname)
+ finally:
+ shutil.rmtree(dirname)
+
+ def test_make_file_with_basename(self):
+ filename = self.test.makeFile(basename="basename")
+ self.assertEquals(os.path.basename(filename), "basename")
+ self.test.run()
+ self.assertFalse(os.path.exists(filename))
+
+ def test_make_file_with_basename_and_dirname(self):
+ dirname = tempfile.mkdtemp()
+ try:
+ filename = self.test.makeFile(dirname=dirname, basename="basename")
+ self.assertEquals(os.path.dirname(filename), dirname)
+ self.assertEquals(os.path.basename(filename), "basename")
+ finally:
+ shutil.rmtree(dirname)
+
+ def test_make_dir_returns_dirname(self):
+ dirname = self.test.makeDir()
+ self.assertEquals(os.path.isdir(dirname), True)
+
+ def test_make_dir_cleansup_on_success(self):
+ dirname = self.test.makeDir()
+ self.test.run()
+ self.assertEquals(os.path.isdir(dirname), False)
+
+ def test_make_dir_cleansup_on_failure(self):
+ class MyTest(MockerTestCase):
+ def test_method(self):
+ raise AssertionError("BOOM!")
+ test = MyTest("test_method")
+ dirname = test.makeDir()
+ test.run()
+ self.assertEquals(os.path.isdir(dirname), False)
+
+ def test_make_dir_with_prefix(self):
+ dirname = self.test.makeDir(prefix="prefix-")
+ self.assertTrue(os.path.basename(dirname).startswith("prefix-"))
+
+ def test_make_dir_with_suffix(self):
+ dirname = self.test.makeDir(suffix="-suffix")
+ self.assertTrue(os.path.basename(dirname).endswith("-suffix"))
+
+ def test_make_dir_with_dirname(self):
+ parent_dirname = tempfile.mkdtemp()
+ try:
+ dirname = self.test.makeDir(dirname=parent_dirname)
+ self.assertEquals(os.path.dirname(dirname), parent_dirname)
+ finally:
+ shutil.rmtree(parent_dirname)
+
class MockerTest(unittest.TestCase):