-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathdiagnostics_test.go
More file actions
128 lines (96 loc) · 4.55 KB
/
diagnostics_test.go
File metadata and controls
128 lines (96 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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")
}
func Test_EmitPrivateRegistryUsed_Single(t *testing.T) {
writer := newMemoryDiagnosticsWriter()
testItems := []string{
"https://github.com/github/example (Git Source)",
}
EmitPrivateRegistryUsed(writer, testItems)
assert.Len(t, writer.diagnostics, 1, "Expected one diagnostic to be emitted")
d := writer.diagnostics[0]
assert.Equal(t, d.Source.Id, "go/autobuilder/analysis-using-private-registries")
assert.Equal(t, d.Severity, string(severityNote))
assert.Contains(t, d.MarkdownMessage, "following private package registry")
for i := range testItems {
assert.Contains(t, d.MarkdownMessage, testItems[i])
}
}
func Test_EmitPrivateRegistryUsed_Multiple(t *testing.T) {
writer := newMemoryDiagnosticsWriter()
testItems := []string{
"https://github.com/github/example (Git Source)",
"https://example.com/goproxy (GOPROXY Server)",
}
EmitPrivateRegistryUsed(writer, testItems)
assert.Len(t, writer.diagnostics, 1, "Expected one diagnostic to be emitted")
d := writer.diagnostics[0]
assert.Equal(t, d.Source.Id, "go/autobuilder/analysis-using-private-registries")
assert.Equal(t, d.Severity, string(severityNote))
assert.Contains(t, d.MarkdownMessage, "following private package registries")
for i := range testItems {
assert.Contains(t, d.MarkdownMessage, testItems[i])
}
}