Coverage for tests / test_python.py: 100.000%
43 statements
« prev ^ index » next coverage.py v7.12.1a0.dev1, created at 2025-11-30 17:57 +0000
« prev ^ index » next coverage.py v7.12.1a0.dev1, created at 2025-11-30 17:57 +0000
1# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2# For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt
4"""Tests of coverage/python.py"""
6from __future__ import annotations 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
8import pathlib 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
9import sys 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
11import pytest 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
13from coverage import env 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
14from coverage.python import get_zip_bytes, source_for_file 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
16from tests.coveragetest import CoverageTest 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
17from tests.helpers import os_sep 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
20@pytest.mark.xdist_group(name="get_zip_bytes_test") 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
21class GetZipBytesTest(CoverageTest): 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
22 """Tests of `get_zip_bytes`."""
24 run_in_temp_dir = False 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
26 @pytest.mark.parametrize( 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
27 "encoding",
28 ["utf-8", "gb2312", "hebrew", "shift_jis", "cp1252"],
29 )
30 def test_get_encoded_zip_files(self, encoding: str) -> None: 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
31 # See igor.py, do_zipmods, for the text of these files.
32 zip_file = "tests/zipmods.zip" 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
33 sys.path.append(zip_file) # So we can import the files. 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
34 filename = zip_file + "/encoded_" + encoding + ".py" 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
35 filename = os_sep(filename) 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
36 zip_data = get_zip_bytes(filename) 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
37 assert zip_data is not None 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
38 zip_text = zip_data.decode(encoding) 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
39 assert "All OK" in zip_text 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
40 # Run the code to see that we really got it encoded properly.
41 mod = __import__("encoded_" + encoding) 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
42 assert mod.encoding == encoding 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
45def test_source_for_file(tmp_path: pathlib.Path) -> None: 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
46 src = str(tmp_path / "a.py") 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
47 assert source_for_file(src) == src 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
48 assert source_for_file(src + "c") == src 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
49 assert source_for_file(src + "o") == src 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
50 unknown = src + "FOO" 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
51 assert source_for_file(unknown) == unknown 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
54@pytest.mark.skipif(not env.WINDOWS, reason="not windows") 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
55def test_source_for_file_windows(tmp_path: pathlib.Path) -> None: 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
56 a_py = tmp_path / "a.py" 1abcdefghijklmnopqrstuvw
57 src = str(a_py) 1abcdefghijklmnopqrstuvw
59 # On windows if a pyw exists, it is an acceptable source
60 path_windows = tmp_path / "a.pyw" 1abcdefghijklmnopqrstuvw
61 path_windows.write_text("", encoding="utf-8") 1abcdefghijklmnopqrstuvw
62 assert str(path_windows) == source_for_file(src + "c") 1abcdefghijklmnopqrstuvw
64 # If both pyw and py exist, py is preferred
65 a_py.write_text("", encoding="utf-8") 1abcdefghijklmnopqrstuvw
66 assert source_for_file(src + "c") == src 1abcdefghijklmnopqrstuvw
69class RunpyTest(CoverageTest): 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
70 """Tests using runpy."""
72 @pytest.mark.parametrize("convert_to", ["str", "Path"]) 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
73 def test_runpy_path(self, convert_to: str) -> None: 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
74 # Ensure runpy.run_path(path) works when path is pathlib.Path or str.
75 #
76 # runpy.run_path(pathlib.Path(...)) causes __file__ to be a Path,
77 # which may make source_for_file() stumble (#1819) with:
78 #
79 # AttributeError: 'PosixPath' object has no attribute 'endswith'
81 self.check_coverage(f"""\ 1xyzAabBCDEcdFGHIefJKLMghNOPQijRSTUVWklmXYZ012nop345678qrs9!#$%'tuv()w
82 import runpy
83 from pathlib import Path
84 pyfile = Path('script.py')
85 pyfile.write_text('', encoding='utf-8')
86 runpy.run_path({convert_to}(pyfile))
87 """)