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 re 18import sys 19import unittest 20 21from lib import main 22 23 24class ExampleTest(unittest.TestCase): 25 def test_coverage_doesnt_shadow_stdlib(self): 26 # When we try to import the html module 27 import html as html_stdlib 28 29 try: 30 import coverage.html as html_coverage 31 except ImportError: 32 self.skipTest("not running under coverage, skipping") 33 34 self.assertEqual( 35 "html", 36 f"{html_stdlib.__name__}", 37 "'html' from stdlib was not loaded correctly", 38 ) 39 40 self.assertEqual( 41 "coverage.html", 42 f"{html_coverage.__name__}", 43 "'coverage.html' was not loaded correctly", 44 ) 45 46 self.assertNotEqual( 47 html_stdlib, 48 html_coverage, 49 "'html' import should not be shadowed by coverage", 50 ) 51 52 def test_coverage_sys_path(self): 53 all_paths = ",\n ".join(sys.path) 54 55 for i, path in enumerate(sys.path[1:-2]): 56 self.assertFalse( 57 "/coverage" in path, 58 f"Expected {i + 2}th '{path}' to not contain 'coverage.py' paths, " 59 f"sys.path has {len(sys.path)} items:\n {all_paths}", 60 ) 61 62 first_item, last_item = sys.path[0], sys.path[-1] 63 self.assertFalse( 64 first_item.endswith("coverage"), 65 f"Expected the first item in sys.path '{first_item}' to not be related to coverage", 66 ) 67 68 # We're trying to make sure that the coverage library added by the 69 # toolchain is _after_ any user-provided dependencies. This lets users 70 # override what coverage version they're using. 71 first_coverage_index = None 72 last_user_dep_index = None 73 for i, path in enumerate(sys.path): 74 if re.search("rules_python.*[~+]pip[~+]", path): 75 last_user_dep_index = i 76 if first_coverage_index is None and re.search( 77 ".*rules_python.*[~+]python[~+].*coverage.*", path 78 ): 79 first_coverage_index = i 80 81 if os.environ.get("COVERAGE_MANIFEST"): 82 self.assertIsNotNone( 83 first_coverage_index, 84 "Expected to find toolchain coverage, but " 85 + f"it was not found.\nsys.path:\n{all_paths}", 86 ) 87 self.assertIsNotNone( 88 last_user_dep_index, 89 "Expected to find at least one user dep, " 90 + "but none were found.\nsys.path:\n{all_paths}", 91 ) 92 # we are running under the 'bazel coverage :test' 93 self.assertGreater( 94 first_coverage_index, 95 last_user_dep_index, 96 "Expected coverage provided by the toolchain to be after " 97 + "user provided dependencies.\n" 98 + f"Found coverage at index: {first_coverage_index}\n" 99 + f"Last user dep at index: {last_user_dep_index}\n" 100 + f"Full sys.path:\n{all_paths}", 101 ) 102 else: 103 self.assertIsNone( 104 first_coverage_index, 105 "Expected toolchain coverage to not be present\n" 106 + f"Found coverage at index: {first_coverage_index}\n" 107 + f"Full sys.path:\n{all_paths}", 108 ) 109 110 def test_main(self): 111 self.assertEqual( 112 """\ 113- - 114A 1 115B 2 116- -""", 117 main([["A", 1], ["B", 2]]), 118 ) 119 120 121if __name__ == "__main__": 122 unittest.main() 123