1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
"""Tests of coverage/python.py"""
import sys
import pytest
from coverage import env
from coverage.python import get_zip_bytes, source_for_file
from tests.coveragetest import CoverageTest
from tests.helpers import os_sep
class GetZipBytesTest(CoverageTest):
"""Tests of `get_zip_bytes`."""
run_in_temp_dir = False
@pytest.mark.parametrize(
"encoding",
["utf-8", "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.
filename = zip_file + "/encoded_" + encoding + ".py"
filename = os_sep(filename)
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):
path = tmpdir.join("a.py")
src = str(path)
assert source_for_file(src) == src
assert source_for_file(src + 'c') == src
assert source_for_file(src + 'o') == src
unknown = src + 'FOO'
assert source_for_file(unknown) == unknown
@pytest.mark.skipif(not env.WINDOWS, reason="not windows")
def test_source_for_file_windows(tmpdir):
path = tmpdir.join("a.py")
src = str(path)
# On windows if a pyw exists, it is an acceptable source
path_windows = tmpdir.ensure("a.pyw")
assert str(path_windows) == source_for_file(src + 'c')
# If both pyw and py exist, py is preferred
path.ensure(file=True)
assert source_for_file(src + 'c') == src
def test_source_for_file_jython():
assert source_for_file("a$py.class") == "a.py"
|