diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-10-20 18:37:46 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-10-20 18:37:46 -0400 |
commit | 17c94a9f94916ba892f7ef0518881776d6b55d66 (patch) | |
tree | 51a7eda6cf8d9e61adcb3ca791f9917065125085 /coverage/test_helpers.py | |
parent | ad4c7f3a5194f6966454d534f02e6b02633fa370 (diff) | |
parent | cd015c45c278aca757263746ed2e64c46d578ddd (diff) | |
download | python-coveragepy-git-17c94a9f94916ba892f7ef0518881776d6b55d66.tar.gz |
Merged pull request #18 manually
Diffstat (limited to 'coverage/test_helpers.py')
-rw-r--r-- | coverage/test_helpers.py | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/coverage/test_helpers.py b/coverage/test_helpers.py index efe68dcd..665593a2 100644 --- a/coverage/test_helpers.py +++ b/coverage/test_helpers.py @@ -132,8 +132,13 @@ class StdStreamCapturingMixin(TestCase): return self.captured_stderr.getvalue() -class TempDirMixin(TestCase): - """A test case mixin that creates a temp directory and files in it.""" +class TempDirMixin(SysPathAwareMixin, ModuleAwareMixin, TestCase): + """A test case mixin that creates a temp directory and files in it. + + Includes SysPathAwareMixin and ModuleAwareMixin, because making and using + temp dirs like this will also need that kind of isolation. + + """ # Our own setting: most of these tests run in their own temp directory. run_in_temp_dir = True @@ -143,12 +148,8 @@ class TempDirMixin(TestCase): if self.run_in_temp_dir: # Create a temporary directory. - noise = str(random.random())[2:] - self.temp_root = os.path.join(tempfile.gettempdir(), 'test_cover') - self.temp_dir = os.path.join(self.temp_root, noise) - os.makedirs(self.temp_dir) - self.old_dir = os.getcwd() - os.chdir(self.temp_dir) + self.temp_dir = self.make_temp_dir("test_cover") + self.chdir(self.temp_dir) # Modules should be importable from this temp directory. We don't # use '' because we make lots of different temp directories and @@ -161,15 +162,24 @@ class TempDirMixin(TestCase): class_behavior.test_method_made_any_files = False class_behavior.temp_dir = self.run_in_temp_dir - self.addCleanup(self.cleanup_temp_dir) + self.addCleanup(self.check_behavior) - def cleanup_temp_dir(self): - """Clean up the temp directories we made.""" + def make_temp_dir(self, slug="test_cover"): + """Make a temp directory that is cleaned up when the test is done.""" + name = "%s_%08d" % (slug, random.randint(0, 99999999)) + temp_dir = os.path.join(tempfile.gettempdir(), name) + os.makedirs(temp_dir) + self.addCleanup(shutil.rmtree, temp_dir) + return temp_dir - if self.run_in_temp_dir: - # Get rid of the temporary directory. - os.chdir(self.old_dir) - shutil.rmtree(self.temp_root) + def chdir(self, new_dir): + """Change directory, and change back when the test is done.""" + old_dir = os.getcwd() + os.chdir(new_dir) + self.addCleanup(os.chdir, old_dir) + + def check_behavior(self): + """Check that we did the right things.""" class_behavior = self.class_behavior() if class_behavior.test_method_made_any_files: |