From 8e7d62600d3634e2b188a342abe8b9f2ed49f84f Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Sun, 25 Jan 2026 15:10:25 +0000 Subject: [PATCH] Make `EmitCannotFindPackages` testable and add tests --- go/extractor/diagnostics/BUILD.bazel | 9 ++- go/extractor/diagnostics/diagnostics.go | 5 +- go/extractor/diagnostics/diagnostics_test.go | 85 ++++++++++++++++++++ go/extractor/extractor.go | 2 +- 4 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 go/extractor/diagnostics/diagnostics_test.go diff --git a/go/extractor/diagnostics/BUILD.bazel b/go/extractor/diagnostics/BUILD.bazel index 436cf15e854..698c27e02b6 100644 --- a/go/extractor/diagnostics/BUILD.bazel +++ b/go/extractor/diagnostics/BUILD.bazel @@ -1,6 +1,6 @@ # generated running `bazel run //go/gazelle`, do not edit -load("@rules_go//go:def.bzl", "go_library") +load("@rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "diagnostics", @@ -9,3 +9,10 @@ go_library( visibility = ["//visibility:public"], deps = ["//go/extractor/util"], ) + +go_test( + name = "diagnostics_test", + srcs = ["diagnostics_test.go"], + embed = [":diagnostics"], + deps = ["@com_github_stretchr_testify//assert"], +) diff --git a/go/extractor/diagnostics/diagnostics.go b/go/extractor/diagnostics/diagnostics.go index a825bf86288..aea46a9827c 100644 --- a/go/extractor/diagnostics/diagnostics.go +++ b/go/extractor/diagnostics/diagnostics.go @@ -169,7 +169,7 @@ func plural(n int, singular, plural string) string { const maxNumPkgPaths = 5 -func EmitCannotFindPackages(pkgPaths []string) { +func EmitCannotFindPackages(writer DiagnosticsWriter, pkgPaths []string) { numPkgPaths := len(pkgPaths) numPrinted := numPkgPaths @@ -216,7 +216,8 @@ func EmitCannotFindPackages(pkgPaths []string) { "If any of the packages are already present in the repository, but were not found, then you may need a [custom build command](https://docs.github.com/en/code-security/how-tos/scan-code-for-vulnerabilities/manage-your-configuration/codeql-code-scanning-for-compiled-languages)." } - emitDiagnostic( + emitDiagnosticTo( + writer, "go/autobuilder/package-not-found", "Some packages could not be found", message, diff --git a/go/extractor/diagnostics/diagnostics_test.go b/go/extractor/diagnostics/diagnostics_test.go new file mode 100644 index 00000000000..f2b560004ba --- /dev/null +++ b/go/extractor/diagnostics/diagnostics_test.go @@ -0,0 +1,85 @@ +package diagnostics + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type memoryDiagnosticsWriter struct { + diagnostics []diagnostic +} + +func newMemoryDiagnosticsWriter() *memoryDiagnosticsWriter { + return &memoryDiagnosticsWriter{[]diagnostic{}} +} + +func (writer *memoryDiagnosticsWriter) WriteDiagnostic(d diagnostic) { + writer.diagnostics = append(writer.diagnostics, d) +} + +func Test_EmitCannotFindPackages_Default(t *testing.T) { + writer := newMemoryDiagnosticsWriter() + + // Clear environment variables that affect the diagnostic message. + t.Setenv("GITHUB_EVENT_NAME", "") + t.Setenv("GITHUB_ACTIONS", "") + + EmitCannotFindPackages(writer, []string{"github.com/github/foo"}) + + assert.Len(t, writer.diagnostics, 1, "Expected one diagnostic to be emitted") + + d := writer.diagnostics[0] + assert.Equal(t, d.Source.Id, "go/autobuilder/package-not-found") + assert.Equal(t, d.Severity, string(severityWarning)) + assert.True(t, d.Visibility.CliSummaryTable) + assert.True(t, d.Visibility.StatusPage) + assert.True(t, d.Visibility.Telemetry) + // Non-Actions suggestion for private registries + assert.Contains(t, d.MarkdownMessage, "ensure that the necessary credentials and environment variables are set up") + // Custom build command suggestion + assert.Contains(t, d.MarkdownMessage, "If any of the packages are already present in the repository") +} + +func Test_EmitCannotFindPackages_Dynamic(t *testing.T) { + writer := newMemoryDiagnosticsWriter() + + // Set environment variables that affect the diagnostic message. + t.Setenv("GITHUB_EVENT_NAME", "dynamic") + t.Setenv("GITHUB_ACTIONS", "true") + + EmitCannotFindPackages(writer, []string{"github.com/github/foo"}) + + assert.Len(t, writer.diagnostics, 1, "Expected one diagnostic to be emitted") + + d := writer.diagnostics[0] + assert.Equal(t, d.Source.Id, "go/autobuilder/package-not-found") + assert.Equal(t, d.Severity, string(severityWarning)) + // Dynamic workflow suggestion for private registries + assert.Contains(t, d.MarkdownMessage, "can grant access to private registries for GitHub security products") + // No default suggestions for private registries and custom build command + assert.NotContains(t, d.MarkdownMessage, "ensure that the necessary credentials and environment variables are set up") + assert.NotContains(t, d.MarkdownMessage, "If any of the packages are already present in the repository") +} + +func Test_EmitCannotFindPackages_Actions(t *testing.T) { + writer := newMemoryDiagnosticsWriter() + + // Set environment variables that affect the diagnostic message. + t.Setenv("GITHUB_EVENT_NAME", "push") + t.Setenv("GITHUB_ACTIONS", "true") + + EmitCannotFindPackages(writer, []string{"github.com/github/foo"}) + + assert.Len(t, writer.diagnostics, 1, "Expected one diagnostic to be emitted") + + d := writer.diagnostics[0] + assert.Equal(t, d.Source.Id, "go/autobuilder/package-not-found") + assert.Equal(t, d.Severity, string(severityWarning)) + // Advanced workflow suggestion for private registries + assert.Contains(t, d.MarkdownMessage, "add a step to your workflow which sets up") + // No default suggestion for private registries + assert.NotContains(t, d.MarkdownMessage, "ensure that the necessary credentials and environment variables are set up") + // Custom build command suggestion + assert.Contains(t, d.MarkdownMessage, "If any of the packages are already present in the repository") +} diff --git a/go/extractor/extractor.go b/go/extractor/extractor.go index 314fb8a56c1..bbcd32c10d2 100644 --- a/go/extractor/extractor.go +++ b/go/extractor/extractor.go @@ -223,7 +223,7 @@ func ExtractWithFlags(buildFlags []string, patterns []string, extractTests bool, }) if len(pkgsNotFound) > 0 { - diagnostics.EmitCannotFindPackages(pkgsNotFound) + diagnostics.EmitCannotFindPackages(diagnostics.DefaultWriter, pkgsNotFound) } for _, pkg := range pkgs {