diff options
-rw-r--r-- | tests/coveragetest.py | 20 | ||||
-rw-r--r-- | tests/helpers.py | 31 | ||||
-rw-r--r-- | tox.ini | 4 |
3 files changed, 36 insertions, 19 deletions
diff --git a/tests/coveragetest.py b/tests/coveragetest.py index cb5d6711..4df947d5 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -5,13 +5,10 @@ import contextlib import datetime -import glob -import itertools import os import random import re import shlex -import shutil import sys import types @@ -24,12 +21,11 @@ import coverage from coverage import env from coverage.backunittest import TestCase, unittest from coverage.backward import StringIO, import_local_file, string_class, shlex_quote -from coverage.backward import invalidate_import_caches from coverage.cmdline import CoverageScript from coverage.debug import _TEST_NAME_FILE, DebugControl from coverage.misc import StopEverything -from tests.helpers import run_command +from tests.helpers import run_command, SuperModuleCleaner # Status returns for the command line. @@ -79,6 +75,8 @@ class CoverageTest( def setUp(self): super(CoverageTest, self).setUp() + self.module_cleaner = SuperModuleCleaner() + # Attributes for getting info about what happened. self.last_command_status = None self.last_command_output = None @@ -97,17 +95,7 @@ class CoverageTest( one test. """ - # So that we can re-import files, clean them out first. - self.cleanup_modules() - # Also have to clean out the .pyc file, since the timestamp - # resolution is only one second, a changed file might not be - # picked up. - for pyc in itertools.chain(glob.glob('*.pyc'), glob.glob('*$py.class')): - os.remove(pyc) - if os.path.exists("__pycache__"): - shutil.rmtree("__pycache__") - - invalidate_import_caches() + self.module_cleaner.clean_local_file_imports() def start_import_stop(self, cov, modname, modfile=None): """Start coverage, import a file, then stop coverage. diff --git a/tests/helpers.py b/tests/helpers.py index cbb6e01c..f10169a9 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -3,13 +3,18 @@ """Helpers for coverage.py tests.""" +import glob +import itertools import os import re +import shutil import subprocess import sys +from unittest_mixins import ModuleCleaner + from coverage import env -from coverage.backward import unicode_class +from coverage.backward import invalidate_import_caches, unicode_class from coverage.misc import output_encoding @@ -99,3 +104,27 @@ def re_line(text, pat): lines = re_lines(text, pat).splitlines() assert len(lines) == 1 return lines[0] + + +class SuperModuleCleaner(ModuleCleaner): + """Remember the state of sys.modules and restore it later.""" + + def clean_local_file_imports(self): + """Clean up the results of calls to `import_local_file`. + + Use this if you need to `import_local_file` the same file twice in + one test. + + """ + # So that we can re-import files, clean them out first. + self.cleanup_modules() + + # Also have to clean out the .pyc file, since the timestamp + # resolution is only one second, a changed file might not be + # picked up. + for pyc in itertools.chain(glob.glob('*.pyc'), glob.glob('*$py.class')): + os.remove(pyc) + if os.path.exists("__pycache__"): + shutil.rmtree("__pycache__") + + invalidate_import_caches() @@ -14,8 +14,8 @@ deps = pip==9.0.1 mock==2.0.0 PyContracts==1.7.15 - unittest-mixins==1.1.1 - #-egit+/Users/ned/unittest_mixins#egg=unittest-mixins==0.0 + #unittest-mixins==1.1.1 + -egit+/Users/ned/unittest_mixins#egg=unittest-mixins==0.0 py26: unittest2==1.1.0 py{27,33,34,35,36}: gevent==1.2.1 py{26,27,33,34,35,36}: eventlet==0.20.1 |