From 610c0e1bd49a3a37eee47383fe781250b9c6a5d0 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 12 Nov 2026 14:55:08 +0100 Subject: [PATCH 1/2] Harden the test-only labeler guard test Fail when the test-only-fix config grows a second rule, match type or glob, since the test reads only one and the labeler would OR the rest in. Read the release targets from .goreleaser.yaml instead of hardcoding them. Co-authored-by: Isaac --- Taskfile.yml | 1 + internal/build/labeler_test.go | 88 ++++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 25 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index d47a72d61ca..04775da31e0 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -448,6 +448,7 @@ tasks: - NOTICE - .codegen/_openapi_sha - .github/labeler-test-only.yml + - .goreleaser.yaml # bundle/internal/schema TestRequiredAnnotationsForNewFields reads these. - bundle/internal/schema/annotations*.yml generates: diff --git a/internal/build/labeler_test.go b/internal/build/labeler_test.go index 44ef2c98d60..64afea6dfe3 100644 --- a/internal/build/labeler_test.go +++ b/internal/build/labeler_test.go @@ -15,32 +15,9 @@ import ( // under a directory that .github/labeler-test-only.yml treats as test-only, since // a PR changing that directory would then be labeled test-only-fix. func TestLabelerTestOnlyDirsNotInBinary(t *testing.T) { - raw, err := os.ReadFile("../../.github/labeler-test-only.yml") - require.NoError(t, err) + dirs := labelerTestOnlyDirs(t) - var config struct { - TestOnlyFix []struct { - ChangedFiles []struct { - AnyGlobToAllFiles []string `yaml:"any-glob-to-all-files"` - } `yaml:"changed-files"` - } `yaml:"test-only-fix"` - } - require.NoError(t, yaml.Unmarshal(raw, &config)) - glob := config.TestOnlyFix[0].ChangedFiles[0].AnyGlobToAllFiles[0] - - // Directory entries look like "libs/testserver/**". Entries such as - // "**/*_test.go" match by file name and are skipped. - var dirs []string - for alt := range strings.SplitSeq(strings.Trim(glob, "{}"), ",") { - dir, ok := strings.CutSuffix(alt, "/**") - if ok && !strings.Contains(dir, "*") { - dirs = append(dirs, dir) - } - } - require.NotEmpty(t, dirs) - - // Release targets from .goreleaser.yaml; some files build only on some of them. - for _, target := range []string{"linux/amd64", "linux/arm64", "darwin/amd64", "darwin/arm64", "windows/amd64", "windows/arm64"} { + for _, target := range releaseTargets(t) { t.Run(target, func(t *testing.T) { t.Parallel() goos, goarch, _ := strings.Cut(target, "/") @@ -65,3 +42,64 @@ func TestLabelerTestOnlyDirsNotInBinary(t *testing.T) { }) } } + +// labelerTestOnlyDirs returns the directory entries of the test-only-fix glob. +func labelerTestOnlyDirs(t *testing.T) []string { + raw, err := os.ReadFile("../../.github/labeler-test-only.yml") + require.NoError(t, err) + + var config struct { + TestOnlyFix []struct { + ChangedFiles []map[string][]string `yaml:"changed-files"` + } `yaml:"test-only-fix"` + } + require.NoError(t, yaml.Unmarshal(raw, &config)) + + // The labeler ORs extra rules and globs into the label, and this test reads only + // one glob, so a config that grows another one must fail here until it is covered. + require.Len(t, config.TestOnlyFix, 1) + require.Len(t, config.TestOnlyFix[0].ChangedFiles, 1) + match := config.TestOnlyFix[0].ChangedFiles[0] + require.Len(t, match, 1) + globs := match["any-glob-to-all-files"] + require.Len(t, globs, 1) + + // Directory entries look like "libs/testserver/**". Entries such as + // "**/*_test.go" match by file name and are skipped. + var dirs []string + for alt := range strings.SplitSeq(strings.Trim(globs[0], "{}"), ",") { + dir, ok := strings.CutSuffix(alt, "/**") + if ok && !strings.Contains(dir, "*") { + dirs = append(dirs, dir) + } + } + require.NotEmpty(t, dirs) + return dirs +} + +// releaseTargets returns the goos/goarch pairs the release build in .goreleaser.yaml +// produces. Some files build only on some of them. +func releaseTargets(t *testing.T) []string { + raw, err := os.ReadFile("../../.goreleaser.yaml") + require.NoError(t, err) + + var config struct { + Builds []struct { + Goos []string `yaml:"goos"` + Goarch []string `yaml:"goarch"` + } `yaml:"builds"` + } + require.NoError(t, yaml.Unmarshal(raw, &config)) + require.Len(t, config.Builds, 1) + build := config.Builds[0] + require.NotEmpty(t, build.Goos) + require.NotEmpty(t, build.Goarch) + + var targets []string + for _, goos := range build.Goos { + for _, goarch := range build.Goarch { + targets = append(targets, goos+"/"+goarch) + } + } + return targets +} From 1e79697c7edb8afaafea6910542a0d092b606ea3 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Fri, 13 Nov 2026 10:12:16 +0100 Subject: [PATCH 2/2] Run go list once in the labeler guard test All release targets give the same CLI import graph, and the only platform-specific CLI files import nothing from the CLI. CI runs the test on Linux, macOS and Windows, which covers the OS-specific files, so drop the per-target loop and the .goreleaser.yaml parsing. Co-authored-by: Isaac --- Taskfile.yml | 1 - internal/build/labeler_test.go | 69 ++++++++-------------------------- 2 files changed, 16 insertions(+), 54 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index 04775da31e0..d47a72d61ca 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -448,7 +448,6 @@ tasks: - NOTICE - .codegen/_openapi_sha - .github/labeler-test-only.yml - - .goreleaser.yaml # bundle/internal/schema TestRequiredAnnotationsForNewFields reads these. - bundle/internal/schema/annotations*.yml generates: diff --git a/internal/build/labeler_test.go b/internal/build/labeler_test.go index 64afea6dfe3..a240303213f 100644 --- a/internal/build/labeler_test.go +++ b/internal/build/labeler_test.go @@ -15,36 +15,6 @@ import ( // under a directory that .github/labeler-test-only.yml treats as test-only, since // a PR changing that directory would then be labeled test-only-fix. func TestLabelerTestOnlyDirsNotInBinary(t *testing.T) { - dirs := labelerTestOnlyDirs(t) - - for _, target := range releaseTargets(t) { - t.Run(target, func(t *testing.T) { - t.Parallel() - goos, goarch, _ := strings.Cut(target, "/") - // go test does not see the sources go list reads, so a cached local pass can be - // stale. CI reruns it because a fresh checkout changes the config file's mtime. - cmd := exec.CommandContext(t.Context(), "go", "list", "-deps", "-f", "{{.ImportPath}}", ".") - cmd.Dir = "../.." - cmd.Env = append(os.Environ(), "GOOS="+goos, "GOARCH="+goarch, "CGO_ENABLED=0") - out, err := cmd.Output() - require.NoError(t, err) - - for pkg := range strings.FieldsSeq(string(out)) { - rel, ok := strings.CutPrefix(pkg, "github.com/databricks/cli/") - if !ok { - continue - } - for _, dir := range dirs { - assert.False(t, rel == dir || strings.HasPrefix(rel, dir+"/"), - "CLI binary imports %s, but .github/labeler-test-only.yml lists %s/** as test-only", pkg, dir) - } - } - }) - } -} - -// labelerTestOnlyDirs returns the directory entries of the test-only-fix glob. -func labelerTestOnlyDirs(t *testing.T) []string { raw, err := os.ReadFile("../../.github/labeler-test-only.yml") require.NoError(t, err) @@ -74,32 +44,25 @@ func labelerTestOnlyDirs(t *testing.T) []string { } } require.NotEmpty(t, dirs) - return dirs -} -// releaseTargets returns the goos/goarch pairs the release build in .goreleaser.yaml -// produces. Some files build only on some of them. -func releaseTargets(t *testing.T) []string { - raw, err := os.ReadFile("../../.goreleaser.yaml") + // go list resolves build constraints for the host platform only. CI runs this test + // on Linux, macOS and Windows, which covers the OS-specific files. + // go test does not see the sources go list reads, so a cached local pass can be + // stale. CI reruns it because a fresh checkout changes the config file's mtime. + cmd := exec.CommandContext(t.Context(), "go", "list", "-deps", "-f", "{{.ImportPath}}", ".") + cmd.Dir = "../.." + cmd.Env = append(os.Environ(), "CGO_ENABLED=0") + out, err := cmd.Output() require.NoError(t, err) - var config struct { - Builds []struct { - Goos []string `yaml:"goos"` - Goarch []string `yaml:"goarch"` - } `yaml:"builds"` - } - require.NoError(t, yaml.Unmarshal(raw, &config)) - require.Len(t, config.Builds, 1) - build := config.Builds[0] - require.NotEmpty(t, build.Goos) - require.NotEmpty(t, build.Goarch) - - var targets []string - for _, goos := range build.Goos { - for _, goarch := range build.Goarch { - targets = append(targets, goos+"/"+goarch) + for pkg := range strings.FieldsSeq(string(out)) { + rel, ok := strings.CutPrefix(pkg, "github.com/databricks/cli/") + if !ok { + continue + } + for _, dir := range dirs { + assert.False(t, rel == dir || strings.HasPrefix(rel, dir+"/"), + "CLI binary imports %s, but .github/labeler-test-only.yml lists %s/** as test-only", pkg, dir) } } - return targets }