|
| 1 | +import subprocess |
| 2 | +import sys |
| 3 | +import tempfile |
| 4 | +import unittest |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from select_windows_bazel_targets import DURATIONS_PATH |
| 8 | +from select_windows_bazel_targets import assign_targets |
| 9 | +from select_windows_bazel_targets import read_durations |
| 10 | + |
| 11 | + |
| 12 | +class SelectWindowsBazelTargetsTest(unittest.TestCase): |
| 13 | + def test_balances_targets_with_complete_deterministic_coverage(self) -> None: |
| 14 | + durations = { |
| 15 | + f"//test:{name}": duration |
| 16 | + for name, duration in zip("abcdefgh", range(8, 0, -1)) |
| 17 | + } |
| 18 | + targets = list(durations) |
| 19 | + |
| 20 | + assignments, loads = assign_targets(targets, durations, 4) |
| 21 | + |
| 22 | + self.assertEqual(loads, [9, 9, 9, 9]) |
| 23 | + self.assertEqual( |
| 24 | + sorted(target for shard in assignments for target in shard), sorted(targets) |
| 25 | + ) |
| 26 | + self.assertEqual( |
| 27 | + assign_targets(list(reversed(targets)), durations, 4), (assignments, loads) |
| 28 | + ) |
| 29 | + self.assertTrue(all(shard == sorted(shard) for shard in assignments)) |
| 30 | + |
| 31 | + def test_missing_weights_use_one_and_unused_weights_do_not_create_targets( |
| 32 | + self, |
| 33 | + ) -> None: |
| 34 | + targets = ["//test:c", "//test:a", "//test:b"] |
| 35 | + |
| 36 | + assignments, loads = assign_targets(targets, {"//test:stale": 100}, 2) |
| 37 | + |
| 38 | + self.assertEqual(assignments, [["//test:a", "//test:c"], ["//test:b"]]) |
| 39 | + self.assertEqual(loads, [2, 1]) |
| 40 | + |
| 41 | + def test_rejects_empty_or_duplicate_target_input(self) -> None: |
| 42 | + for targets in ([], [""], ["//test:a", "//test:a"]): |
| 43 | + with self.subTest(targets=targets), self.assertRaises(ValueError): |
| 44 | + assign_targets(targets, {}, 4) |
| 45 | + |
| 46 | + def test_reads_duration_file_and_rejects_invalid_weights(self) -> None: |
| 47 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 48 | + path = Path(temp_dir) / "durations.tsv" |
| 49 | + path.write_text( |
| 50 | + "# comment\n\n//test:a\t12\n//test:b\t1\n", encoding="utf-8" |
| 51 | + ) |
| 52 | + self.assertEqual(read_durations(path), {"//test:a": 12, "//test:b": 1}) |
| 53 | + |
| 54 | + for contents in ( |
| 55 | + "//test:a 12\n", |
| 56 | + "//test:a\t0\n", |
| 57 | + "//test:a\t-1\n", |
| 58 | + "//test:a\t1.5\n", |
| 59 | + "//test:a\t1\n//test:a\t2\n", |
| 60 | + ): |
| 61 | + with self.subTest(contents=contents), self.assertRaises(ValueError): |
| 62 | + path.write_text(contents, encoding="utf-8") |
| 63 | + read_durations(path) |
| 64 | + |
| 65 | + def test_cli_uses_checked_in_weights_and_writes_only_lf_delimited_targets( |
| 66 | + self, |
| 67 | + ) -> None: |
| 68 | + durations = read_durations(DURATIONS_PATH) |
| 69 | + known = sorted(durations, key=lambda target: (-durations[target], target))[:4] |
| 70 | + self.assertEqual(len(known), 4) |
| 71 | + targets = [*known, "//test:new-target"] |
| 72 | + expected, _ = assign_targets(targets, durations, 4) |
| 73 | + script = DURATIONS_PATH.with_name("select_windows_bazel_targets.py") |
| 74 | + |
| 75 | + for shard, assignment in enumerate(expected, 1): |
| 76 | + command = [ |
| 77 | + sys.executable, |
| 78 | + str(script), |
| 79 | + "--shard", |
| 80 | + str(shard), |
| 81 | + "--shard-count", |
| 82 | + "4", |
| 83 | + ] |
| 84 | + for ordered_targets in (targets, list(reversed(targets))): |
| 85 | + with self.subTest(shard=shard, ordered_targets=ordered_targets): |
| 86 | + result = subprocess.run( |
| 87 | + command, |
| 88 | + input=("\r\n".join(ordered_targets) + "\r\n").encode(), |
| 89 | + capture_output=True, |
| 90 | + check=True, |
| 91 | + ) |
| 92 | + self.assertEqual( |
| 93 | + result.stdout, ("\n".join(assignment) + "\n").encode() |
| 94 | + ) |
| 95 | + self.assertIn(b"default weights=1", result.stderr) |
| 96 | + |
| 97 | + def test_cli_uses_only_the_requested_duration_file(self) -> None: |
| 98 | + script = DURATIONS_PATH.with_name("select_windows_bazel_targets.py").resolve() |
| 99 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 100 | + path = Path(temp_dir) / "durations.tsv" |
| 101 | + path.write_text("//test:a\t1\n//test:b\t9\n", encoding="utf-8") |
| 102 | + command = [sys.executable, str(script), "--shard-count", "2"] |
| 103 | + for shard, expected in ((1, b"//test:b\n"), (2, b"//test:a\n//test:c\n")): |
| 104 | + with self.subTest(shard=shard): |
| 105 | + result = subprocess.run( |
| 106 | + [*command, "--shard", str(shard), "--durations", path.name], |
| 107 | + input=b"//test:a\n//test:b\n//test:c\n", |
| 108 | + cwd=temp_dir, |
| 109 | + capture_output=True, |
| 110 | + check=True, |
| 111 | + ) |
| 112 | + self.assertEqual(result.stdout, expected) |
| 113 | + self.assertIn(b"estimated test-seconds=[9, 2]", result.stderr) |
| 114 | + self.assertIn(b"unused weights=0", result.stderr) |
| 115 | + |
| 116 | + result = subprocess.run( |
| 117 | + [*command, "--shard", "1", "--durations", "missing.tsv"], |
| 118 | + input=b"//test:a\n", |
| 119 | + cwd=temp_dir, |
| 120 | + capture_output=True, |
| 121 | + check=False, |
| 122 | + ) |
| 123 | + self.assertNotEqual(result.returncode, 0) |
| 124 | + self.assertEqual(result.stdout, b"") |
| 125 | + self.assertIn(b"missing.tsv", result.stderr) |
| 126 | + |
| 127 | + def test_cli_rejects_bad_or_empty_selection_without_partial_output(self) -> None: |
| 128 | + script = DURATIONS_PATH.with_name("select_windows_bazel_targets.py") |
| 129 | + for targets, error in ( |
| 130 | + (b"", b"no Bazel test targets were provided"), |
| 131 | + (b"//test:a\n//test:a\n", b"duplicate Bazel test target labels"), |
| 132 | + (b"//test:a\n", b"no Bazel test targets selected for shard 2/2"), |
| 133 | + ): |
| 134 | + with self.subTest(targets=targets): |
| 135 | + result = subprocess.run( |
| 136 | + [sys.executable, str(script), "--shard", "2", "--shard-count", "2"], |
| 137 | + input=targets, |
| 138 | + capture_output=True, |
| 139 | + check=False, |
| 140 | + ) |
| 141 | + self.assertNotEqual(result.returncode, 0) |
| 142 | + self.assertEqual(result.stdout, b"") |
| 143 | + self.assertIn(error, result.stderr) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + unittest.main() |
0 commit comments