-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Go: Make diagnostics unit-testable and add test for EmitCannotFindPackages
#21214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mbg
wants to merge
4
commits into
mbg/go/improve-package-not-found
Choose a base branch
from
mbg/go/diagnostics-unit-tests
base: mbg/go/improve-package-not-found
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−40
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fafc2dd
Add `DiagnosticsWriter` interface to abstract over diagnostics output…
mbg f1f4ddb
Add dependency on `testify/assert`
mbg 8e7d626
Make `EmitCannotFindPackages` testable and add tests
mbg 45e0a92
Move `nil` check into `FileDiagnosticsWriter` implementation of `Writ…
mbg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,17 @@ | ||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
| github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= | ||
| github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= | ||
| golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c= | ||
| golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU= | ||
| golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= | ||
| golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= | ||
| golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc= | ||
| golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
| gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.