Skip to content

Commit 16126d0

Browse files
authored
test(coverage): add a test to check the sys.path under bzlmod (bazel-contrib#1223)
Whilst it is for documentation purposes for maintainers it is also a regression test for bazel-contrib#1045. Also add a test to ensure that the `html` module is not shadowed when running under `coverage`. Fixes bazel-contrib#1196.
1 parent 9268d91 commit 16126d0

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

examples/bzlmod/test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,57 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
16+
import pathlib
17+
import sys
1518
import unittest
1619

1720
from lib import main
1821

1922

2023
class ExampleTest(unittest.TestCase):
24+
def test_coverage_doesnt_shadow_stdlib(self):
25+
# When we try to import the html module
26+
import html as html_stdlib
27+
28+
try:
29+
import coverage.html as html_coverage
30+
except ImportError:
31+
self.skipTest("not running under coverage, skipping")
32+
33+
self.assertNotEqual(
34+
html_stdlib,
35+
html_coverage,
36+
"'html' import should not be shadowed by coverage",
37+
)
38+
39+
def test_coverage_sys_path(self):
40+
all_paths = ",\n ".join(sys.path)
41+
42+
for i, path in enumerate(sys.path[1:-2]):
43+
self.assertFalse(
44+
"/coverage" in path,
45+
f"Expected {i + 2}th '{path}' to not contain 'coverage.py' paths, "
46+
f"sys.path has {len(sys.path)} items:\n {all_paths}",
47+
)
48+
49+
first_item, last_item = sys.path[0], sys.path[-1]
50+
self.assertFalse(
51+
first_item.endswith("coverage"),
52+
f"Expected the first item in sys.path '{first_item}' to not be related to coverage",
53+
)
54+
if os.environ.get("COVERAGE_MANIFEST"):
55+
# we are running under the 'bazel coverage :test'
56+
self.assertTrue(
57+
"pypi__coverage_cp" in last_item,
58+
f"Expected {last_item} to be related to coverage",
59+
)
60+
self.assertEqual(pathlib.Path(last_item).name, "coverage")
61+
else:
62+
self.assertFalse(
63+
"coverage" in last_item, f"Expected coverage tooling to not be present"
64+
)
65+
2166
def test_main(self):
2267
self.assertEquals(
2368
"""\

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy