package testci_test import ( "encoding/json" "strings" "testing" "amadan.net/oficina/equipo/internal/testci" ) // The lanes split packages by test name, and every test must run in // exactly one of them: scripts/ci-test.py check (make lanes-check, in the // static checks shard) reads the real test files on CI. This holds its // rules on made-up packages: each broken layout names what is wrong, and // the good one passes. func TestLanesRunEveryTestOnce(t *testing.T) { testci.Files(t, "../../scripts/ci-test.py") inventory := map[string]any{ "packages": []string{"./a", "./b", "./c"}, "tests": map[string][]string{ "./a": {"TestAlpha", "TestBeta", "TestScenarios"}, "./b": {"TestOne", "TestTwo"}, "./c": {"TestC"}, }, "sources": map[string]string{"./a": `{"first", run}, {"second", run}`}, } good := [][]any{ {"a-scenarios", []string{"./a"}, []string{"TestScenarios/first"}}, {"a-more", []string{"./a"}, []string{"^TestA", "TestScenarios/second"}}, {"a-rest", []string{"./a"}, nil}, {"b", []string{"./b"}, nil}, {"rest", nil, nil}, } with := func(name string, lane []any) [][]any { var out [][]any for _, l := range good { if l[0] != name { out = append(out, l) } } if lane != nil { out = append(out, lane) } return out } for _, c := range []struct { name string lanes [][]any want string // "" is no problem }{ {"good", good, ""}, {"the same subtest in two lanes", with("a-more", []any{"a-more", []string{"./a"}, []string{"^TestA", "TestScenarios/first"}}), "./a TestScenarios/first runs in more than one lane: a-more, a-scenarios"}, {"a whole test beside its subtest", with("a-more", []any{"a-more", []string{"./a"}, []string{"TestScenarios"}}), "./a TestScenarios runs in more than one lane: a-more whole; a-scenarios some of it"}, {"two patterns take one test", with("a-more", []any{"a-more", []string{"./a"}, []string{"^Test[A-Z]"}}), "./a TestScenarios runs in more than one lane"}, {"a renamed test", with("a-more", []any{"a-more", []string{"./a"}, []string{"TestGamma"}}), "lane a-more: TestGamma names no test in ./a"}, {"a renamed subtest", with("a-scenarios", []any{"a-scenarios", []string{"./a"}, []string{"TestScenarios/third"}}), "lane a-scenarios: ./a has no subtest 'third' of TestScenarios"}, {"a pattern that takes nothing", with("a-more", []any{"a-more", []string{"./a"}, []string{"^TestZ"}}), "lane a-more: ^TestZ names no test in ./a"}, {"a split package with nowhere for the rest", with("a-rest", nil), "./a: want one lane that names no tests and runs what the others do not; have []"}, {"a package in two whole lanes", with("", []any{"b-again", []string{"./b"}, nil}), "./b: want one lane that names no tests and runs what the others do not; have ['b', 'b-again']"}, {"a deleted package", with("", []any{"gone", []string{"./gone"}, nil}), "lane gone: no package ./gone (renamed or deleted?)"}, {"tests across packages", with("b", []any{"b", []string{"./b", "./c"}, []string{"TestOne"}}), "lane b names tests, so it must name one package"}, {"no lane for the other packages", with("rest", nil), "want one lane that names no package"}, } { out := python(t, nil, "-c", loadCITest+` args = json.loads(sys.argv[1]) lanes = [tuple(l) for l in json.loads(sys.argv[2])] print(json.dumps(m.lane_problems(lanes, set(args["packages"]), args["tests"], args["sources"]))) `, mustJSON(t, inventory), mustJSON(t, c.lanes)) var problems []string if err := json.Unmarshal([]byte(out), &problems); err != nil { t.Fatalf("%s: %v: %s", c.name, err, out) } if c.want == "" { if len(problems) != 0 { t.Errorf("%s: want no problems, got %q", c.name, problems) } continue } found := false for _, p := range problems { found = found || strings.Contains(p, c.want) } if !found { t.Errorf("%s: want a problem containing %q, got %q", c.name, c.want, problems) } } }