|
| 1 | +package diagnostics |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +type memoryDiagnosticsWriter struct { |
| 10 | + diagnostics []diagnostic |
| 11 | +} |
| 12 | + |
| 13 | +func newMemoryDiagnosticsWriter() *memoryDiagnosticsWriter { |
| 14 | + return &memoryDiagnosticsWriter{[]diagnostic{}} |
| 15 | +} |
| 16 | + |
| 17 | +func (writer *memoryDiagnosticsWriter) WriteDiagnostic(d diagnostic) { |
| 18 | + writer.diagnostics = append(writer.diagnostics, d) |
| 19 | +} |
| 20 | + |
| 21 | +func Test_EmitCannotFindPackages_Default(t *testing.T) { |
| 22 | + writer := newMemoryDiagnosticsWriter() |
| 23 | + |
| 24 | + // Clear environment variables that affect the diagnostic message. |
| 25 | + t.Setenv("GITHUB_EVENT_NAME", "") |
| 26 | + t.Setenv("GITHUB_ACTIONS", "") |
| 27 | + |
| 28 | + EmitCannotFindPackages(writer, []string{"github.com/github/foo"}) |
| 29 | + |
| 30 | + assert.Len(t, writer.diagnostics, 1, "Expected one diagnostic to be emitted") |
| 31 | + |
| 32 | + d := writer.diagnostics[0] |
| 33 | + assert.Equal(t, d.Source.Id, "go/autobuilder/package-not-found") |
| 34 | + assert.Equal(t, d.Severity, string(severityWarning)) |
| 35 | + assert.True(t, d.Visibility.CliSummaryTable) |
| 36 | + assert.True(t, d.Visibility.StatusPage) |
| 37 | + assert.True(t, d.Visibility.Telemetry) |
| 38 | + // Non-Actions suggestion for private registries |
| 39 | + assert.Contains(t, d.MarkdownMessage, "ensure that the necessary credentials and environment variables are set up") |
| 40 | + // Custom build command suggestion |
| 41 | + assert.Contains(t, d.MarkdownMessage, "If any of the packages are already present in the repository") |
| 42 | +} |
| 43 | + |
| 44 | +func Test_EmitCannotFindPackages_Dynamic(t *testing.T) { |
| 45 | + writer := newMemoryDiagnosticsWriter() |
| 46 | + |
| 47 | + // Set environment variables that affect the diagnostic message. |
| 48 | + t.Setenv("GITHUB_EVENT_NAME", "dynamic") |
| 49 | + t.Setenv("GITHUB_ACTIONS", "true") |
| 50 | + |
| 51 | + EmitCannotFindPackages(writer, []string{"github.com/github/foo"}) |
| 52 | + |
| 53 | + assert.Len(t, writer.diagnostics, 1, "Expected one diagnostic to be emitted") |
| 54 | + |
| 55 | + d := writer.diagnostics[0] |
| 56 | + assert.Equal(t, d.Source.Id, "go/autobuilder/package-not-found") |
| 57 | + assert.Equal(t, d.Severity, string(severityWarning)) |
| 58 | + // Dynamic workflow suggestion for private registries |
| 59 | + assert.Contains(t, d.MarkdownMessage, "can grant access to private registries for GitHub security products") |
| 60 | + // No default suggestions for private registries and custom build command |
| 61 | + assert.NotContains(t, d.MarkdownMessage, "ensure that the necessary credentials and environment variables are set up") |
| 62 | + assert.NotContains(t, d.MarkdownMessage, "If any of the packages are already present in the repository") |
| 63 | +} |
| 64 | + |
| 65 | +func Test_EmitCannotFindPackages_Actions(t *testing.T) { |
| 66 | + writer := newMemoryDiagnosticsWriter() |
| 67 | + |
| 68 | + // Set environment variables that affect the diagnostic message. |
| 69 | + t.Setenv("GITHUB_EVENT_NAME", "push") |
| 70 | + t.Setenv("GITHUB_ACTIONS", "true") |
| 71 | + |
| 72 | + EmitCannotFindPackages(writer, []string{"github.com/github/foo"}) |
| 73 | + |
| 74 | + assert.Len(t, writer.diagnostics, 1, "Expected one diagnostic to be emitted") |
| 75 | + |
| 76 | + d := writer.diagnostics[0] |
| 77 | + assert.Equal(t, d.Source.Id, "go/autobuilder/package-not-found") |
| 78 | + assert.Equal(t, d.Severity, string(severityWarning)) |
| 79 | + // Advanced workflow suggestion for private registries |
| 80 | + assert.Contains(t, d.MarkdownMessage, "add a step to your workflow which sets up") |
| 81 | + // No default suggestion for private registries |
| 82 | + assert.NotContains(t, d.MarkdownMessage, "ensure that the necessary credentials and environment variables are set up") |
| 83 | + // Custom build command suggestion |
| 84 | + assert.Contains(t, d.MarkdownMessage, "If any of the packages are already present in the repository") |
| 85 | +} |
0 commit comments