diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2016-11-17 16:06:16 +0000 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2016-11-17 16:06:16 +0000 |
commit | 3243add2eee93ee3c23afa1f04bc15d72dab479e (patch) | |
tree | 6958a4437e5ed808a6a31f3c9457f91a4115cb33 | |
parent | 724dba8e31ca636a120a71c3cc74449ef470a5e1 (diff) | |
download | python-coveragepy-3243add2eee93ee3c23afa1f04bc15d72dab479e.tar.gz |
Don't collapse in an ascii-only file-world. #533
-rw-r--r-- | CHANGES.rst | 3 | ||||
-rw-r--r-- | coverage/files.py | 12 | ||||
-rw-r--r-- | tests/test_api.py | 10 | ||||
-rw-r--r-- | tests/test_process.py | 17 |
4 files changed, 30 insertions, 12 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 865d105..1ff0dbb 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -43,6 +43,8 @@ Unreleased - Fixed an unusual bug involving multiple coding declarations affecting code containing code in multi-line strings: `issue 529`_. +- Deal properly with non-ASCII file names in an ASCII-only world, `issue 533`_. + - Prevented deprecation warnings from configparser that happened in some circumstances, closing `issue 530`_. @@ -59,6 +61,7 @@ Unreleased .. _issue 525: https://bitbucket.org/ned/coveragepy/issues/525/coverage-combine-when-not-in-parallel-mode .. _issue 529: https://bitbucket.org/ned/coveragepy/issues/529/encoding-marker-may-only-appear-on-the .. _issue 530: https://bitbucket.org/ned/coveragepy/issues/530/deprecationwarning-you-passed-a-bytestring +.. _issue 533: https://bitbucket.org/ned/coveragepy/issues/533/exception-on-unencodable-file-name Version 4.2 --- 2016-07-26 diff --git a/coverage/files.py b/coverage/files.py index 9de4849..af2fe52 100644 --- a/coverage/files.py +++ b/coverage/files.py @@ -63,7 +63,11 @@ def canonical_filename(filename): if path is None: continue f = os.path.join(path, filename) - if os.path.exists(f): + try: + exists = os.path.exists(f) + except UnicodeError: + exists = False + if exists: filename = f break cf = abs_file(filename) @@ -147,7 +151,11 @@ else: def abs_file(filename): """Return the absolute normalized form of `filename`.""" path = os.path.expandvars(os.path.expanduser(filename)) - path = os.path.abspath(os.path.realpath(path)) + try: + path = os.path.realpath(path) + except UnicodeError: + pass + path = os.path.abspath(path) path = actual_path(path) path = unicode_filename(path) return path diff --git a/tests/test_api.py b/tests/test_api.py index 1018ff0..6f14210 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -100,16 +100,6 @@ class ApiTest(CoverageTest): filename, _, _, _ = cov.analysis(sys.modules["mymod"]) self.assertEqual(os.path.basename(filename), "mymod.py") - def test_unencodable_filename(self): - code = """a = 1\nb = 2\n""" - cov = coverage.Coverage() - cov.start() - globs = {} - exec(compile(code, "wut\xe9\xea\xeb\xec\x01\x02.py", 'exec'), globs) - cov.stop() - self.assertEqual(globs['a'], 1) - self.assertEqual(globs['b'], 2) - def test_ignore_stdlib(self): self.make_file("mymain.py", """\ import colorsys diff --git a/tests/test_process.py b/tests/test_process.py index aa2045f..62b6dc8 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -721,6 +721,23 @@ class ProcessTest(CoverageTest): # about 5. self.assertGreater(data.line_counts()['os.py'], 50) + def test_lang_c(self): + # LANG=C forces getfilesystemencoding on Linux to 'ascii', which causes + # failures with non-ascii file names. We don't want to make a real file + # with strange characters, though, because that gets the test runners + # tangled up. This will isolate the concerns to the coverage.py code. + # https://bitbucket.org/ned/coveragepy/issues/533/exception-on-unencodable-file-name + self.make_file("weird_file.py", r""" + globs = {} + code = "a = 1\nb = 2\n" + exec(compile(code, "wut\xe9\xea\xeb\xec\x01\x02.py", 'exec'), globs) + print(globs['a']) + print(globs['b']) + """) + self.set_environ("LANG", "C") + out = self.run_command("python -m coverage run weird_file.py") + self.assertEqual(out, "1\n2\n") + def test_deprecation_warnings(self): # Test that coverage doesn't trigger deprecation warnings. # https://bitbucket.org/ned/coveragepy/issue/305/pendingdeprecationwarning-the-imp-module |