summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-04-14 11:50:15 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-04-14 12:52:39 -0400
commit50cc68846f9b78a4d0984c5a9475203b3c87c1e0 (patch)
treecad38e5b57c4eebab80ad99c645396200a3b824c
parent52e5088a39b4d47e124a79c357dd04233658c583 (diff)
downloadpython-coveragepy-git-50cc68846f9b78a4d0984c5a9475203b3c87c1e0.tar.gz
test: improve zipfile test
Before this commit, the GetZipBytesTest.test_get_encoded_zip_files test was flaky on Python 3.10.0a7. Since I had just added new files to the common zip file, I tried splitting the newly added stuff into its own file, and that seemed to fix the problem.
-rw-r--r--.gitignore1
-rw-r--r--igor.py70
-rw-r--r--tests/test_filereporter.py2
-rw-r--r--tests/test_python.py22
4 files changed, 50 insertions, 45 deletions
diff --git a/.gitignore b/.gitignore
index 5205b9c2..94314500 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,6 +33,7 @@ setuptools-*.egg
# Stuff in the test directory.
covmain.zip
zipmods.zip
+zip1.zip
# Stuff in the doc directory.
doc/_build
diff --git a/igor.py b/igor.py
index c99c8c3a..8d162798 100644
--- a/igor.py
+++ b/igor.py
@@ -217,41 +217,41 @@ def do_test_with_tracer(tracer, *runner_args):
def do_zip_mods():
- """Build the zipmods.zip file."""
- zf = zipfile.ZipFile("tests/zipmods.zip", "w")
-
- # Take some files from disk.
- zf.write("tests/covmodzip1.py", "covmodzip1.py")
- zf.write("tests/zipsrc/zip1/__init__.py", "zip1/__init__.py")
- zf.write("tests/zipsrc/zip1/zip1.py", "zip1/zip1.py")
-
- # The others will be various encodings.
- source = textwrap.dedent(u"""\
- # coding: {encoding}
- text = u"{text}"
- ords = {ords}
- assert [ord(c) for c in text] == ords
- print(u"All OK with {encoding}")
- """)
- # These encodings should match the list in tests/test_python.py
- details = [
- (u'utf8', u'ⓗⓔⓛⓛⓞ, ⓦⓞⓡⓛⓓ'),
- (u'gb2312', u'你好,世界'),
- (u'hebrew', u'שלום, עולם'),
- (u'shift_jis', u'こんにちは世界'),
- (u'cp1252', u'“hi”'),
- ]
- for encoding, text in details:
- filename = 'encoded_{}.py'.format(encoding)
- ords = [ord(c) for c in text]
- source_text = source.format(encoding=encoding, text=text, ords=ords)
- zf.writestr(filename, source_text.encode(encoding))
-
- zf.close()
-
- zf = zipfile.ZipFile("tests/covmain.zip", "w")
- zf.write("coverage/__main__.py", "__main__.py")
- zf.close()
+ """Build the zip files needed for tests."""
+ with zipfile.ZipFile("tests/zipmods.zip", "w") as zf:
+
+ # Take some files from disk.
+ zf.write("tests/covmodzip1.py", "covmodzip1.py")
+
+ # The others will be various encodings.
+ source = textwrap.dedent(u"""\
+ # coding: {encoding}
+ text = u"{text}"
+ ords = {ords}
+ assert [ord(c) for c in text] == ords
+ print(u"All OK with {encoding}")
+ encoding = "{encoding}"
+ """)
+ # These encodings should match the list in tests/test_python.py
+ details = [
+ (u'utf8', u'ⓗⓔⓛⓛⓞ, ⓦⓞⓡⓛⓓ'),
+ (u'gb2312', u'你好,世界'),
+ (u'hebrew', u'שלום, עולם'),
+ (u'shift_jis', u'こんにちは世界'),
+ (u'cp1252', u'“hi”'),
+ ]
+ for encoding, text in details:
+ filename = 'encoded_{}.py'.format(encoding)
+ ords = [ord(c) for c in text]
+ source_text = source.format(encoding=encoding, text=text, ords=ords)
+ zf.writestr(filename, source_text.encode(encoding))
+
+ with zipfile.ZipFile("tests/zip1.zip", "w") as zf:
+ zf.write("tests/zipsrc/zip1/__init__.py", "zip1/__init__.py")
+ zf.write("tests/zipsrc/zip1/zip1.py", "zip1/zip1.py")
+
+ with zipfile.ZipFile("tests/covmain.zip", "w") as zf:
+ zf.write("coverage/__main__.py", "__main__.py")
def do_check_eol():
diff --git a/tests/test_filereporter.py b/tests/test_filereporter.py
index 8ce2201d..1e8513f8 100644
--- a/tests/test_filereporter.py
+++ b/tests/test_filereporter.py
@@ -89,7 +89,7 @@ class FileReporterTest(UsingModulesMixin, CoverageTest):
assert bcu > acu and bcu >= acu and bcu != acu
def test_zipfile(self):
- sys.path.append("tests/zipmods.zip")
+ sys.path.append("tests/zip1.zip")
# Test that we can get files out of zipfiles, and read their source files.
# The zip1 module is installed by an action in igor.py.
diff --git a/tests/test_python.py b/tests/test_python.py
index 0175f5af..dc9609c9 100644
--- a/tests/test_python.py
+++ b/tests/test_python.py
@@ -19,18 +19,22 @@ class GetZipBytesTest(CoverageTest):
run_in_temp_dir = False
- def test_get_encoded_zip_files(self):
+ @pytest.mark.parametrize(
+ "encoding",
+ ["utf8", "gb2312", "hebrew", "shift_jis", "cp1252"],
+ )
+ def test_get_encoded_zip_files(self, encoding):
# See igor.py, do_zipmods, for the text of these files.
zip_file = "tests/zipmods.zip"
sys.path.append(zip_file) # So we can import the files.
- for encoding in ["utf8", "gb2312", "hebrew", "shift_jis", "cp1252"]:
- filename = zip_file + "/encoded_" + encoding + ".py"
- filename = filename.replace("/", os.sep)
- zip_data = get_zip_bytes(filename)
- zip_text = zip_data.decode(encoding)
- assert 'All OK' in zip_text
- # Run the code to see that we really got it encoded properly.
- __import__("encoded_"+encoding)
+ filename = zip_file + "/encoded_" + encoding + ".py"
+ filename = filename.replace("/", os.sep)
+ zip_data = get_zip_bytes(filename)
+ zip_text = zip_data.decode(encoding)
+ assert 'All OK' in zip_text
+ # Run the code to see that we really got it encoded properly.
+ mod = __import__("encoded_"+encoding)
+ assert mod.encoding == encoding
def test_source_for_file(tmpdir):