| 1 | package testci_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/testci" |
| 9 | ) |
| 10 | |
| 11 | // The lanes split packages by test name, and every test must run in |
| 12 | // exactly one of them: scripts/ci-test.py check (make lanes-check, in the |
| 13 | // static checks shard) reads the real test files on CI. This holds its |
| 14 | // rules on made-up packages: each broken layout names what is wrong, and |
| 15 | // the good one passes. |
| 16 | func TestLanesRunEveryTestOnce(t *testing.T) { |
| 17 | testci.Files(t, "../../scripts/ci-test.py") |
| 18 | inventory := map[string]any{ |
| 19 | "packages": []string{"./a", "./b", "./c"}, |
| 20 | "tests": map[string][]string{ |
| 21 | "./a": {"TestAlpha", "TestBeta", "TestScenarios"}, |
| 22 | "./b": {"TestOne", "TestTwo"}, |
| 23 | "./c": {"TestC"}, |
| 24 | }, |
| 25 | "sources": map[string]string{"./a": `{"first", run}, {"second", run}`}, |
| 26 | } |
| 27 | good := [][]any{ |
| 28 | {"a-scenarios", []string{"./a"}, []string{"TestScenarios/first"}}, |
| 29 | {"a-more", []string{"./a"}, []string{"^TestA", "TestScenarios/second"}}, |
| 30 | {"a-rest", []string{"./a"}, nil}, |
| 31 | {"b", []string{"./b"}, nil}, |
| 32 | {"rest", nil, nil}, |
| 33 | } |
| 34 | with := func(name string, lane []any) [][]any { |
| 35 | var out [][]any |
| 36 | for _, l := range good { |
| 37 | if l[0] != name { |
| 38 | out = append(out, l) |
| 39 | } |
| 40 | } |
| 41 | if lane != nil { |
| 42 | out = append(out, lane) |
| 43 | } |
| 44 | return out |
| 45 | } |
| 46 | for _, c := range []struct { |
| 47 | name string |
| 48 | lanes [][]any |
| 49 | want string // "" is no problem |
| 50 | }{ |
| 51 | {"good", good, ""}, |
| 52 | {"the same subtest in two lanes", with("a-more", []any{"a-more", []string{"./a"}, []string{"^TestA", "TestScenarios/first"}}), |
| 53 | "./a TestScenarios/first runs in more than one lane: a-more, a-scenarios"}, |
| 54 | {"a whole test beside its subtest", with("a-more", []any{"a-more", []string{"./a"}, []string{"TestScenarios"}}), |
| 55 | "./a TestScenarios runs in more than one lane: a-more whole; a-scenarios some of it"}, |
| 56 | {"two patterns take one test", with("a-more", []any{"a-more", []string{"./a"}, []string{"^Test[A-Z]"}}), |
| 57 | "./a TestScenarios runs in more than one lane"}, |
| 58 | {"a renamed test", with("a-more", []any{"a-more", []string{"./a"}, []string{"TestGamma"}}), |
| 59 | "lane a-more: TestGamma names no test in ./a"}, |
| 60 | {"a renamed subtest", with("a-scenarios", []any{"a-scenarios", []string{"./a"}, []string{"TestScenarios/third"}}), |
| 61 | "lane a-scenarios: ./a has no subtest 'third' of TestScenarios"}, |
| 62 | {"a pattern that takes nothing", with("a-more", []any{"a-more", []string{"./a"}, []string{"^TestZ"}}), |
| 63 | "lane a-more: ^TestZ names no test in ./a"}, |
| 64 | {"a split package with nowhere for the rest", with("a-rest", nil), |
| 65 | "./a: want one lane that names no tests and runs what the others do not; have []"}, |
| 66 | {"a package in two whole lanes", with("", []any{"b-again", []string{"./b"}, nil}), |
| 67 | "./b: want one lane that names no tests and runs what the others do not; have ['b', 'b-again']"}, |
| 68 | {"a deleted package", with("", []any{"gone", []string{"./gone"}, nil}), |
| 69 | "lane gone: no package ./gone (renamed or deleted?)"}, |
| 70 | {"tests across packages", with("b", []any{"b", []string{"./b", "./c"}, []string{"TestOne"}}), |
| 71 | "lane b names tests, so it must name one package"}, |
| 72 | {"no lane for the other packages", with("rest", nil), |
| 73 | "want one lane that names no package"}, |
| 74 | } { |
| 75 | out := python(t, nil, "-c", loadCITest+` |
| 76 | args = json.loads(sys.argv[1]) |
| 77 | lanes = [tuple(l) for l in json.loads(sys.argv[2])] |
| 78 | print(json.dumps(m.lane_problems(lanes, set(args["packages"]), args["tests"], args["sources"]))) |
| 79 | `, mustJSON(t, inventory), mustJSON(t, c.lanes)) |
| 80 | var problems []string |
| 81 | if err := json.Unmarshal([]byte(out), &problems); err != nil { |
| 82 | t.Fatalf("%s: %v: %s", c.name, err, out) |
| 83 | } |
| 84 | if c.want == "" { |
| 85 | if len(problems) != 0 { |
| 86 | t.Errorf("%s: want no problems, got %q", c.name, problems) |
| 87 | } |
| 88 | continue |
| 89 | } |
| 90 | found := false |
| 91 | for _, p := range problems { |
| 92 | found = found || strings.Contains(p, c.want) |
| 93 | } |
| 94 | if !found { |
| 95 | t.Errorf("%s: want a problem containing %q, got %q", c.name, c.want, problems) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |