1# Copyright 2023 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import os 16import pathlib 17import sys 18import unittest 19 20from lib import main 21 22 23class 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.assertEqual( 34 "html", 35 f"{html_stdlib.__name__}", 36 "'html' from stdlib was not loaded correctly", 37 ) 38 39 self.assertEqual( 40 "coverage.html", 41 f"{html_coverage.__name__}", 42 "'coverage.html' was not loaded correctly", 43 ) 44 45 self.assertNotEqual( 46 html_stdlib, 47 html_coverage, 48 "'html' import should not be shadowed by coverage", 49 ) 50 51 def test_coverage_sys_path(self): 52 all_paths = ",\n ".join(sys.path) 53 54 for i, path in enumerate(sys.path[1:-2]): 55 self.assertFalse( 56 "/coverage" in path, 57 f"Expected {i + 2}th '{path}' to not contain 'coverage.py' paths, " 58 f"sys.path has {len(sys.path)} items:\n {all_paths}", 59 ) 60 61 first_item, last_item = sys.path[0], sys.path[-1] 62 self.assertFalse( 63 first_item.endswith("coverage"), 64 f"Expected the first item in sys.path '{first_item}' to not be related to coverage", 65 ) 66 if os.environ.get("COVERAGE_MANIFEST"): 67 # we are running under the 'bazel coverage :test' 68 self.assertTrue( 69 "_coverage" in last_item, 70 f"Expected {last_item} to be related to coverage", 71 ) 72 self.assertEqual(pathlib.Path(last_item).name, "coverage") 73 else: 74 self.assertFalse( 75 "coverage" in last_item, f"Expected coverage tooling to not be present" 76 ) 77 78 def test_main(self): 79 self.assertEquals( 80 """\ 81- - 82A 1 83B 2 84- -""", 85 main([["A", 1], ["B", 2]]), 86 ) 87 88 89if __name__ == "__main__": 90 unittest.main() 91