|
14 | 14 |
|
15 | 15 | import os
|
16 | 16 | import pathlib
|
| 17 | +import re |
17 | 18 | import sys
|
18 | 19 | import unittest
|
19 | 20 |
|
@@ -63,16 +64,47 @@ def test_coverage_sys_path(self):
|
63 | 64 | first_item.endswith("coverage"),
|
64 | 65 | f"Expected the first item in sys.path '{first_item}' to not be related to coverage",
|
65 | 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 | + |
66 | 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 | + first_coverage_index, |
| 89 | + "Expected to find at least one uiser dep, " |
| 90 | + + "but none were found.\nsys.path:\n{all_paths}", |
| 91 | + ) |
67 | 92 | # 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", |
| 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}", |
71 | 101 | )
|
72 |
| - self.assertEqual(pathlib.Path(last_item).name, "coverage") |
73 | 102 | else:
|
74 |
| - self.assertFalse( |
75 |
| - "coverage" in last_item, f"Expected coverage tooling to not be present" |
| 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}", |
76 | 108 | )
|
77 | 109 |
|
78 | 110 | def test_main(self):
|
|
0 commit comments