summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coverage/backward.py34
-rw-r--r--coverage/execfile.py8
-rw-r--r--tests/backunittest.py22
-rw-r--r--tests/coveragetest.py21
-rw-r--r--tests/modules/pkg1/p1a.py2
-rw-r--r--tests/test_api.py2
-rw-r--r--tests/test_backward.py2
-rw-r--r--tests/test_cmdline.py2
-rw-r--r--tests/test_coverage.py14
-rw-r--r--tests/test_data.py14
-rw-r--r--tests/test_execfile.py4
-rw-r--r--tests/test_files.py6
-rw-r--r--tests/test_html.py6
-rw-r--r--tests/test_process.py6
-rw-r--r--tests/test_templite.py2
-rw-r--r--tests/test_testing.py10
16 files changed, 93 insertions, 62 deletions
diff --git a/coverage/backward.py b/coverage/backward.py
index 98407e38..a7888a24 100644
--- a/coverage/backward.py
+++ b/coverage/backward.py
@@ -135,7 +135,7 @@ except KeyError:
BUILTINS = sys.modules['builtins']
-# imp was deprecated in Python 3.4
+# imp was deprecated in Python 3.3
try:
import importlib, importlib.util
imp = None
@@ -144,12 +144,40 @@ except ImportError:
# we only want to use importlib if it has everything we need.
try:
- importlib.util.find_spec
+ importlib_util_find_spec = importlib.util.find_spec
except Exception:
import imp
- importlib = None
+ importlib_util_find_spec = None
try:
PYC_MAGIC_NUMBER = importlib.util.MAGIC_NUMBER
except AttributeError:
PYC_MAGIC_NUMBER = imp.get_magic()
+
+
+def import_local_file(modname):
+ """Import a local file as a module.
+
+ Opens a file in the current directory named `modname`.py, imports it
+ as `modname`, and returns the module object.
+
+ """
+ try:
+ from importlib.machinery import SourceFileLoader
+ except ImportError:
+ SourceFileLoader = None
+
+ modfile = modname + '.py'
+ if SourceFileLoader:
+ mod = SourceFileLoader(modname, modfile).load_module()
+ else:
+ for suff in imp.get_suffixes():
+ if suff[0] == '.py':
+ break
+
+ with open(modfile, 'r') as f:
+ # pylint: disable=W0631
+ # (Using possibly undefined loop variable 'suff')
+ mod = imp.load_module(modname, f, modfile, suff)
+
+ return mod
diff --git a/coverage/execfile.py b/coverage/execfile.py
index bc8fdaa2..b7877b6a 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -3,11 +3,11 @@
import marshal, os, sys, types
from coverage.backward import open_python_source, BUILTINS
-from coverage.backward import PYC_MAGIC_NUMBER, imp, importlib
+from coverage.backward import PYC_MAGIC_NUMBER, imp, importlib_util_find_spec
from coverage.misc import ExceptionDuringRun, NoCode, NoSource
-if importlib:
+if importlib_util_find_spec:
def find_module(modulename):
"""Find the module named `modulename`.
@@ -16,7 +16,7 @@ if importlib:
"""
# pylint: disable=no-member
try:
- spec = importlib.util.find_spec(modulename)
+ spec = importlib_util_find_spec(modulename)
except ImportError as err:
raise NoSource(str(err))
if not spec:
@@ -25,7 +25,7 @@ if importlib:
packagename = spec.name
if pathname.endswith("__init__.py"):
mod_main = modulename + ".__main__"
- spec = importlib.util.find_spec(mod_main)
+ spec = importlib_util_find_spec(mod_main)
if not spec:
raise NoSource(
"No module named %s; "
diff --git a/tests/backunittest.py b/tests/backunittest.py
index ca741d37..a4377b0b 100644
--- a/tests/backunittest.py
+++ b/tests/backunittest.py
@@ -20,7 +20,21 @@ class TestCase(unittest.TestCase):
`unittest` doesn't have them.
"""
- if _need('assertSameElements'):
- def assertSameElements(self, s1, s2):
- """Assert that the two arguments are equal as sets."""
- self.assertEqual(set(s1), set(s2))
+ if _need('assertCountEqual'):
+ try:
+ assertCountEqual = assertSameElements
+ except NameError:
+ def assertCountEqual(self, s1, s2):
+ """Assert these have the same elements, regardless of order."""
+ self.assertEqual(set(s1), set(s2))
+ else:
+ def assertCountEqual(self, *args, **kwargs):
+ return self.assertSameElements(*args, **kwargs)
+
+ if _need('assertRaisesRegex'):
+ def assertRaisesRegex(self, *args, **kwargs):
+ return self.assertRaisesRegexp(*args, **kwargs)
+
+ if _need('assertRegex'):
+ def assertRegex(self, *args, **kwargs):
+ return self.assertRegexpMatches(*args, **kwargs)
diff --git a/tests/coveragetest.py b/tests/coveragetest.py
index a309f17d..e6376a7b 100644
--- a/tests/coveragetest.py
+++ b/tests/coveragetest.py
@@ -4,7 +4,7 @@ import glob, imp, os, random, shlex, shutil, sys, tempfile, textwrap
import atexit, collections
import coverage
-from coverage.backward import StringIO, to_bytes
+from coverage.backward import StringIO, to_bytes, imp, importlib, import_local_file
from coverage.control import _TEST_NAME_FILE
from tests.backtest import run_command
from tests.backunittest import TestCase
@@ -221,18 +221,7 @@ class CoverageTest(TestCase):
as `modname`, and returns the module object.
"""
- modfile = modname + '.py'
-
- for suff in imp.get_suffixes():
- if suff[0] == '.py':
- break
-
- with open(modfile, 'r') as f:
- # pylint: disable=W0631
- # (Using possibly undefined loop variable 'suff')
- mod = imp.load_module(modname, f, modfile, suff)
-
- return mod
+ return import_local_file(modname)
def start_import_stop(self, cov, modname):
"""Start coverage, import a file, then stop coverage.
@@ -412,17 +401,17 @@ class CoverageTest(TestCase):
"""Assert that `flist1` and `flist2` are the same set of file names."""
flist1_nice = [self.nice_file(f) for f in flist1]
flist2_nice = [self.nice_file(f) for f in flist2]
- self.assertSameElements(flist1_nice, flist2_nice)
+ self.assertCountEqual(flist1_nice, flist2_nice)
def assert_exists(self, fname):
"""Assert that `fname` is a file that exists."""
msg = "File %r should exist" % fname
- self.assert_(os.path.exists(fname), msg)
+ self.assertTrue(os.path.exists(fname), msg)
def assert_doesnt_exist(self, fname):
"""Assert that `fname` is a file that doesn't exist."""
msg = "File %r shouldn't exist" % fname
- self.assert_(not os.path.exists(fname), msg)
+ self.assertTrue(not os.path.exists(fname), msg)
def assert_starts_with(self, s, prefix, msg=None):
"""Assert that `s` starts with `prefix`."""
diff --git a/tests/modules/pkg1/p1a.py b/tests/modules/pkg1/p1a.py
index be5fcdd3..337add49 100644
--- a/tests/modules/pkg1/p1a.py
+++ b/tests/modules/pkg1/p1a.py
@@ -1,5 +1,5 @@
import os, sys
# Invoke functions in os and sys so we can see if we measure code there.
-x = sys.getcheckinterval()
+x = sys.getfilesystemencoding()
y = os.getcwd()
diff --git a/tests/test_api.py b/tests/test_api.py
index 097947d2..c1e9294f 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -100,7 +100,7 @@ class ApiTest(CoverageTest):
"""Assert that the files here are `files`, ignoring the usual junk."""
here = os.listdir(".")
here = self.clean_files(here, ["*.pyc", "__pycache__"])
- self.assertSameElements(here, files)
+ self.assertCountEqual(here, files)
def test_unexecuted_file(self):
cov = coverage.coverage()
diff --git a/tests/test_backward.py b/tests/test_backward.py
index e98017ae..2c688edd 100644
--- a/tests/test_backward.py
+++ b/tests/test_backward.py
@@ -12,7 +12,7 @@ class BackwardTest(TestCase):
def test_iitems(self):
d = {'a': 1, 'b': 2, 'c': 3}
items = [('a', 1), ('b', 2), ('c', 3)]
- self.assertSameElements(list(iitems(d)), items)
+ self.assertCountEqual(list(iitems(d)), items)
def test_binary_bytes(self):
byte_values = [0, 255, 17, 23, 42, 57]
diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py
index 99bae516..038e9214 100644
--- a/tests/test_cmdline.py
+++ b/tests/test_cmdline.py
@@ -754,7 +754,7 @@ class CmdMainTest(CoverageTest):
self.assertEqual(err[-2], 'Exception: oh noes!')
def test_internalraise(self):
- with self.assertRaisesRegexp(ValueError, "coverage is broken"):
+ with self.assertRaisesRegex(ValueError, "coverage is broken"):
coverage.cmdline.main(['internalraise'])
def test_exit(self):
diff --git a/tests/test_coverage.py b/tests/test_coverage.py
index 33f644fa..565fa4e1 100644
--- a/tests/test_coverage.py
+++ b/tests/test_coverage.py
@@ -46,7 +46,7 @@ class TestCoverageTest(CoverageTest):
def test_failed_coverage(self):
# If the lines are wrong, the message shows right and wrong.
- with self.assertRaisesRegexp(AssertionError, r"\[1, 2] != \[1]"):
+ with self.assertRaisesRegex(AssertionError, r"\[1, 2] != \[1]"):
self.check_coverage("""\
a = 1
b = 2
@@ -55,7 +55,7 @@ class TestCoverageTest(CoverageTest):
)
# If the list of lines possibilities is wrong, the msg shows right.
msg = r"None of the lines choices matched \[1, 2]"
- with self.assertRaisesRegexp(AssertionError, msg):
+ with self.assertRaisesRegex(AssertionError, msg):
self.check_coverage("""\
a = 1
b = 2
@@ -63,7 +63,7 @@ class TestCoverageTest(CoverageTest):
([1], [2])
)
# If the missing lines are wrong, the message shows right and wrong.
- with self.assertRaisesRegexp(AssertionError, r"'3' != '37'"):
+ with self.assertRaisesRegex(AssertionError, r"'3' != '37'"):
self.check_coverage("""\
a = 1
if a == 2:
@@ -74,7 +74,7 @@ class TestCoverageTest(CoverageTest):
)
# If the missing lines possibilities are wrong, the msg shows right.
msg = r"None of the missing choices matched '3'"
- with self.assertRaisesRegexp(AssertionError, msg):
+ with self.assertRaisesRegex(AssertionError, msg):
self.check_coverage("""\
a = 1
if a == 2:
@@ -1671,7 +1671,7 @@ class ReportingTest(CoverageTest):
def test_no_data_to_report_on_annotate(self):
# Reporting with no data produces a nice message and no output dir.
- with self.assertRaisesRegexp(CoverageException, "No data to report."):
+ with self.assertRaisesRegex(CoverageException, "No data to report."):
self.command_line("annotate -d ann")
self.assert_doesnt_exist("ann")
@@ -1681,12 +1681,12 @@ class ReportingTest(CoverageTest):
def test_no_data_to_report_on_html(self):
# Reporting with no data produces a nice message and no output dir.
- with self.assertRaisesRegexp(CoverageException, "No data to report."):
+ with self.assertRaisesRegex(CoverageException, "No data to report."):
self.command_line("html -d htmlcov")
self.assert_doesnt_exist("htmlcov")
def test_no_data_to_report_on_xml(self):
# Reporting with no data produces a nice message.
- with self.assertRaisesRegexp(CoverageException, "No data to report."):
+ with self.assertRaisesRegex(CoverageException, "No data to report."):
self.command_line("xml")
self.assert_doesnt_exist("coverage.xml")
diff --git a/tests/test_data.py b/tests/test_data.py
index 31578f26..b048fd18 100644
--- a/tests/test_data.py
+++ b/tests/test_data.py
@@ -33,7 +33,7 @@ class DataTest(CoverageTest):
def assert_measured_files(self, covdata, measured):
"""Check that `covdata`'s measured files are `measured`."""
- self.assertSameElements(covdata.measured_files(), measured)
+ self.assertCountEqual(covdata.measured_files(), measured)
def test_reading_empty(self):
covdata = CoverageData()
@@ -96,9 +96,9 @@ class DataTest(CoverageTest):
data = pickle.load(fdata)
lines = data['lines']
- self.assertSameElements(lines.keys(), MEASURED_FILES_1)
- self.assertSameElements(lines['a.py'], A_PY_LINES_1)
- self.assertSameElements(lines['b.py'], B_PY_LINES_1)
+ self.assertCountEqual(lines.keys(), MEASURED_FILES_1)
+ self.assertCountEqual(lines['a.py'], A_PY_LINES_1)
+ self.assertCountEqual(lines['b.py'], B_PY_LINES_1)
# If not measuring branches, there's no arcs entry.
self.assertEqual(data.get('arcs', 'not there'), 'not there')
@@ -111,10 +111,10 @@ class DataTest(CoverageTest):
with open(".coverage", 'rb') as fdata:
data = pickle.load(fdata)
- self.assertSameElements(data['lines'].keys(), [])
+ self.assertCountEqual(data['lines'].keys(), [])
arcs = data['arcs']
- self.assertSameElements(arcs['x.py'], X_PY_ARCS_3)
- self.assertSameElements(arcs['y.py'], Y_PY_ARCS_3)
+ self.assertCountEqual(arcs['x.py'], X_PY_ARCS_3)
+ self.assertCountEqual(arcs['y.py'], Y_PY_ARCS_3)
def test_combining_with_aliases(self):
covdata1 = CoverageData()
diff --git a/tests/test_execfile.py b/tests/test_execfile.py
index 7f672495..2427847e 100644
--- a/tests/test_execfile.py
+++ b/tests/test_execfile.py
@@ -118,11 +118,11 @@ class RunPycFileTest(CoverageTest):
fpyc.write(binary_bytes([0x2a, 0xeb, 0x0d, 0x0a]))
fpyc.close()
- with self.assertRaisesRegexp(NoCode, "Bad magic number in .pyc file"):
+ with self.assertRaisesRegex(NoCode, "Bad magic number in .pyc file"):
run_python_file(pycfile, [pycfile])
def test_no_such_pyc_file(self):
- with self.assertRaisesRegexp(NoCode, "No file to run: 'xyzzy.pyc'"):
+ with self.assertRaisesRegex(NoCode, "No file to run: 'xyzzy.pyc'"):
run_python_file("xyzzy.pyc", [])
diff --git a/tests/test_files.py b/tests/test_files.py
index f93feba7..991c2b15 100644
--- a/tests/test_files.py
+++ b/tests/test_files.py
@@ -131,11 +131,11 @@ class PathAliasesTest(CoverageTest):
def test_cant_have_wildcard_at_end(self):
aliases = PathAliases()
msg = "Pattern must not end with wildcards."
- with self.assertRaisesRegexp(CoverageException, msg):
+ with self.assertRaisesRegex(CoverageException, msg):
aliases.add("/ned/home/*", "fooey")
- with self.assertRaisesRegexp(CoverageException, msg):
+ with self.assertRaisesRegex(CoverageException, msg):
aliases.add("/ned/home/*/", "fooey")
- with self.assertRaisesRegexp(CoverageException, msg):
+ with self.assertRaisesRegex(CoverageException, msg):
aliases.add("/ned/home/*/*/", "fooey")
def test_no_accidental_munging(self):
diff --git a/tests/test_html.py b/tests/test_html.py
index 41859382..b728847f 100644
--- a/tests/test_html.py
+++ b/tests/test_html.py
@@ -217,7 +217,7 @@ class HtmlWithUnparsableFilesTest(CoverageTest):
self.start_import_stop(cov, "innocuous")
self.make_file("innocuous.py", "<h1>This isn't python!</h1>")
msg = "Couldn't parse '.*innocuous.py' as Python source: .* at line 1"
- with self.assertRaisesRegexp(NotPython, msg):
+ with self.assertRaisesRegex(NotPython, msg):
cov.html_report()
def test_dotpy_not_python_ignored(self):
@@ -283,7 +283,7 @@ class HtmlTest(CoverageTest):
missing_file = os.path.join(self.temp_dir, "sub", "another.py")
missing_file = os.path.realpath(missing_file)
msg = "(?i)No source for code: '%s'" % re.escape(missing_file)
- with self.assertRaisesRegexp(NoSource, msg):
+ with self.assertRaisesRegex(NoSource, msg):
cov.html_report()
class HtmlStaticFileTest(CoverageTest):
@@ -340,5 +340,5 @@ class HtmlStaticFileTest(CoverageTest):
cov = coverage.coverage()
self.start_import_stop(cov, "main")
msg = "Couldn't find static file '.*'"
- with self.assertRaisesRegexp(CoverageException, msg):
+ with self.assertRaisesRegex(CoverageException, msg):
cov.html_report()
diff --git a/tests/test_process.py b/tests/test_process.py
index bb110499..e5836567 100644
--- a/tests/test_process.py
+++ b/tests/test_process.py
@@ -229,7 +229,7 @@ class ProcessTest(CoverageTest):
self.run_command("coverage run fleeting.py")
os.remove("fleeting.py")
out = self.run_command("coverage html -d htmlcov")
- self.assertRegexpMatches(out, "No source for code: '.*fleeting.py'")
+ self.assertRegex(out, "No source for code: '.*fleeting.py'")
self.assertNotIn("Traceback", out)
# It happens that the code paths are different for *.py and other
@@ -241,13 +241,13 @@ class ProcessTest(CoverageTest):
self.run_command("coverage run fleeting")
os.remove("fleeting")
status, out = self.run_command_status("coverage html -d htmlcov", 1)
- self.assertRegexpMatches(out, "No source for code: '.*fleeting'")
+ self.assertRegex(out, "No source for code: '.*fleeting'")
self.assertNotIn("Traceback", out)
self.assertEqual(status, 1)
def test_running_missing_file(self):
status, out = self.run_command_status("coverage run xyzzy.py", 1)
- self.assertRegexpMatches(out, "No file to run: .*xyzzy.py")
+ self.assertRegex(out, "No file to run: .*xyzzy.py")
self.assertNotIn("raceback", out)
self.assertNotIn("rror", out)
self.assertEqual(status, 1)
diff --git a/tests/test_templite.py b/tests/test_templite.py
index b49cc42f..a4667a62 100644
--- a/tests/test_templite.py
+++ b/tests/test_templite.py
@@ -40,7 +40,7 @@ class TempliteTest(CoverageTest):
A context manager, and the message should be `msg`.
"""
pat = "^" + re.escape(msg) + "$"
- return self.assertRaisesRegexp(TempliteSyntaxError, pat)
+ return self.assertRaisesRegex(TempliteSyntaxError, pat)
def test_passthrough(self):
# Strings without variables are passed through unchanged.
diff --git a/tests/test_testing.py b/tests/test_testing.py
index a89a59a9..049a1982 100644
--- a/tests/test_testing.py
+++ b/tests/test_testing.py
@@ -12,13 +12,13 @@ class TestingTest(TestCase):
run_in_temp_dir = False
- def test_assert_same_elements(self):
- self.assertSameElements(set(), set())
- self.assertSameElements(set([1,2,3]), set([3,1,2]))
+ def test_assert_count_equal(self):
+ self.assertCountEqual(set(), set())
+ self.assertCountEqual(set([1,2,3]), set([3,1,2]))
with self.assertRaises(AssertionError):
- self.assertSameElements(set([1,2,3]), set())
+ self.assertCountEqual(set([1,2,3]), set())
with self.assertRaises(AssertionError):
- self.assertSameElements(set([1,2,3]), set([4,5,6]))
+ self.assertCountEqual(set([1,2,3]), set([4,5,6]))
class CoverageTestTest(CoverageTest):