-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin_test.go
202 lines (185 loc) · 4.28 KB
/
plugin_test.go
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package pipeline
import (
"encoding/json"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestPluginFullSource(t *testing.T) {
t.Parallel()
tests := []struct {
source, want string
}{
{
source: "thing",
want: "github.com/buildkite-plugins/thing-buildkite-plugin",
},
{
source: "thing#main",
want: "github.com/buildkite-plugins/thing-buildkite-plugin#main",
},
{
source: "my-org/thing",
want: "github.com/my-org/thing-buildkite-plugin",
},
{
source: "./.buildkite/plugins/llamas/rock",
want: "./.buildkite/plugins/llamas/rock",
},
{
source: `.\.buildkite\plugins\llamas\rock`,
want: `.\.buildkite\plugins\llamas\rock`,
},
{
source: `C:\llamas\rock`,
want: `C:\llamas\rock`,
},
{
source: `\\\\?\C:\user\docs`,
want: `\\\\?\C:\user\docs`,
},
{
source: "/a-plugin",
want: "/a-plugin",
},
{
source: "/my-org/a-plugin",
want: "/my-org/a-plugin",
},
{
source: "https://my-plugin.git",
want: "https://my-plugin.git",
},
{
source: "file:///Users/user/Desktop/my-plugin.git",
want: "file:///Users/user/Desktop/my-plugin.git",
},
{
source: "[email protected]:buildkite/private-buildkite-plugin.git",
want: "[email protected]:buildkite/private-buildkite-plugin.git",
},
{
source: "ssh://[email protected]:buildkite/private-buildkite-plugin.git",
want: "ssh://[email protected]:buildkite/private-buildkite-plugin.git",
},
{
source: "my:plugin",
want: "my:plugin",
},
}
for _, test := range tests {
p := Plugin{
Source: test.source,
}
if got, want := p.FullSource(), test.want; got != want {
t.Errorf("%#v.FullSource() = %q, want %q", p, got, want)
}
// Test idempotency - the backend should be applying the same transform,
// so it's important for multiple normalisations to be idempotent.
p.Source = test.want
if got, want := p.FullSource(), test.want; got != want {
t.Errorf("%#v.FullSource() = %q, want %q", p, got, want)
}
}
}
func TestPluginMarshalJSON_Canonicalisation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
p *Plugin
}{
{
name: "nil interface",
p: &Plugin{Source: "docker#v1.2.3", Config: nil},
},
{
name: "nil map",
p: &Plugin{Source: "docker#v1.2.3", Config: map[string]any(nil)},
},
{
name: "empty map",
p: &Plugin{Source: "docker#v1.2.3", Config: map[string]any{}},
},
{
name: "nil slice??",
p: &Plugin{Source: "docker#v1.2.3", Config: []any(nil)},
},
{
name: "empty slice??",
p: &Plugin{Source: "docker#v1.2.3", Config: []any{}},
},
}
const want = `{"github.com/buildkite-plugins/docker-buildkite-plugin#v1.2.3":null}`
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got, err := json.Marshal(test.p)
if err != nil {
t.Errorf("json.Marshal(%+v) error = %v", test.p, err)
}
if diff := cmp.Diff(string(got), want); diff != "" {
t.Errorf("JSON marshalled plugin diff (-got +want):\n%s", diff)
}
})
}
}
func TestPluginMatrixInterpolate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
ms MatrixPermutation
p, want *Plugin
}{
{
name: "no matrix",
p: &Plugin{
Source: "docker#v1.2.3",
Config: map[string]any{
"something": "foo",
"other": map[string]any{
"thing": "bar",
},
},
},
want: &Plugin{
Source: "docker#v1.2.3",
Config: map[string]any{
"something": "foo",
"other": map[string]any{
"thing": "bar",
},
},
},
},
{
name: "matrix",
ms: MatrixPermutation{
"docker_version": "4.5.6",
"image": "alpine",
},
p: &Plugin{
Source: "docker#{{matrix.docker_version}}",
Config: map[string]any{
"image": "{{matrix.image}}",
},
},
want: &Plugin{
Source: "docker#4.5.6",
Config: map[string]any{
"image": "alpine",
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
tf := newMatrixInterpolator(test.ms)
if err := test.p.interpolate(tf); err != nil {
t.Errorf("plugin.interpolate(matrixInterpolator) error = %v", err)
}
if diff := cmp.Diff(test.p, test.want); diff != "" {
t.Errorf("plugin.interpolate(matrixInterpolator) mismatch (-want +got):\n%s", diff)
}
})
}
}