summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2007-12-05 21:02:10 -0200
committerGustavo Niemeyer <gustavo@niemeyer.net>2007-12-05 21:02:10 -0200
commitbdfd5ccb1e2e22408371b26b806886e20b971285 (patch)
treef8099d4e888275537c9d9f8b5300ca08328c134f
parentb04e870828e81f0f519ad31eae43d1eb0091d692 (diff)
downloadmocker-bdfd5ccb1e2e22408371b26b806886e20b971285.tar.gz
New 'path' option to MockerTestCase.makeFile() and makeDir(),
which allows setting the full target path with a single option.
-rw-r--r--NEWS3
-rw-r--r--mocker.py27
-rwxr-xr-xtest.py32
3 files changed, 47 insertions, 15 deletions
diff --git a/NEWS b/NEWS
index 10cb855..7bff9ac 100644
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,9 @@
- New MATCH() argument matcher, which allows using a function
to match an argument generically. E.g. MATCH(lambda x: x > 10)
+- New 'path' option to MockerTestCase.makeFile() and makeDir(),
+ which allows setting the full target path with a single option.
+
- Now when a spec is provided (or with proxy/replace/patch) the
existence of the real method is checked even if the mocked
method doesn't have to execute (e.g. due to count(0)).
diff --git a/mocker.py b/mocker.py
index ee19523..99450d7 100644
--- a/mocker.py
+++ b/mocker.py
@@ -159,7 +159,7 @@ class MockerTestCase(unittest.TestCase):
self.__cleanup_funcs.append((func, args, kwargs))
def makeFile(self, content=None, suffix="", prefix="tmp", basename=None,
- dirname=None):
+ dirname=None, path=None):
"""Create a temporary file and return the path to it.
@param content: Initial content for the file.
@@ -170,22 +170,24 @@ class MockerTestCase(unittest.TestCase):
The file is removed after the test runs.
"""
- if basename is not None:
+ if path is not None:
+ self.__cleanup_paths.append(path)
+ elif basename is not None:
if dirname is None:
dirname = tempfile.mkdtemp()
self.__cleanup_paths.append(dirname)
- filename = os.path.join(dirname, basename)
+ path = os.path.join(dirname, basename)
else:
- fd, filename = tempfile.mkstemp(suffix, prefix, dirname)
- self.__cleanup_paths.append(filename)
+ fd, path = tempfile.mkstemp(suffix, prefix, dirname)
+ self.__cleanup_paths.append(path)
os.close(fd)
if content is not None:
- file = open(filename, "w")
+ file = open(path, "w")
file.write(content)
file.close()
- return filename
+ return path
- def makeDir(self, suffix="", prefix="tmp", dirname=None):
+ def makeDir(self, suffix="", prefix="tmp", dirname=None, path=None):
"""Create a temporary directory and return the path to it.
@param suffix: Suffix to be given to the file's basename.
@@ -194,9 +196,12 @@ class MockerTestCase(unittest.TestCase):
The directory is removed after the test runs.
"""
- dirname = tempfile.mkdtemp(suffix, prefix, dirname)
- self.__cleanup_paths.append(dirname)
- return dirname
+ if path is not None:
+ os.makedirs(path)
+ else:
+ path = tempfile.mkdtemp(suffix, prefix, dirname)
+ self.__cleanup_paths.append(path)
+ return path
def failUnlessIs(self, first, second, msg=None):
"""Assert that C{first} is the same object as C{second}."""
diff --git a/test.py b/test.py
index a37977b..df463a5 100755
--- a/test.py
+++ b/test.py
@@ -863,6 +863,18 @@ class MockerTestCaseTest(TestCase):
finally:
shutil.rmtree(dirname)
+ def test_make_file_with_path(self):
+ path = tempfile.mktemp()
+ try:
+ filename = self.test.makeFile("", path=path)
+ self.assertEquals(filename, path)
+ self.assertEquals(os.path.getsize(filename), 0)
+ self.test.run()
+ self.assertFalse(os.path.exists(filename))
+ finally:
+ if os.path.isfile(path):
+ os.unlink(path)
+
def test_make_dir_returns_dirname(self):
dirname = self.test.makeDir()
self.assertEquals(os.path.isdir(dirname), True)
@@ -890,12 +902,24 @@ class MockerTestCaseTest(TestCase):
self.assertTrue(os.path.basename(dirname).endswith("-suffix"))
def test_make_dir_with_dirname(self):
- parent_dirname = tempfile.mkdtemp()
+ dirname = tempfile.mkdtemp()
+ try:
+ path = self.test.makeDir(dirname=dirname)
+ self.assertEquals(os.path.dirname(path), dirname)
+ finally:
+ if os.path.exists(dirname):
+ shutil.rmtree(dirname)
+
+ def test_make_dir_with_path(self):
+ path = tempfile.mktemp()
try:
- dirname = self.test.makeDir(dirname=parent_dirname)
- self.assertEquals(os.path.dirname(dirname), parent_dirname)
+ self.assertEquals(self.test.makeDir(path=path), path)
+ self.assertEquals(os.path.isdir(path), True)
+ self.test.run()
+ self.assertEquals(os.path.isdir(path), False)
finally:
- shutil.rmtree(parent_dirname)
+ if os.path.exists(path):
+ shutil.rmtree(path)
class MockerTest(TestCase):