-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstep_command_cache_test.go
More file actions
230 lines (215 loc) · 5.66 KB
/
step_command_cache_test.go
File metadata and controls
230 lines (215 loc) · 5.66 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package pipeline
import (
"errors"
"testing"
"github.com/buildkite/go-pipeline/internal/env"
"github.com/buildkite/go-pipeline/ordered"
"github.com/google/go-cmp/cmp"
)
func TestCacheMarshalJSON(t *testing.T) {
t.Parallel()
cases := []struct {
name string
c Cache
want string
}{
{
name: "single path",
c: Cache{Paths: []string{"path/to/cache"}},
want: `{"paths":["path/to/cache"]}`,
},
{
name: "multiple paths",
c: Cache{Paths: []string{"path/to/cache", "another/path"}},
want: `{"paths":["path/to/cache","another/path"]}`,
},
{
name: "empty cache settings block",
c: Cache{},
want: `{}`,
},
{
name: "full cache settings block",
c: Cache{
Paths: []string{"path/to/cache", "another/path"},
Name: "cache-name",
Size: "25g",
},
want: `{"name":"cache-name","paths":["path/to/cache","another/path"],"size":"25g"}`,
},
{
name: "full cache settings block with extra fields",
c: Cache{
Paths: []string{"path/to/cache", "another/path"},
Name: "cache-name",
Size: "25g",
RemainingFields: map[string]any{"extra": "field"},
},
want: `{"extra":"field","name":"cache-name","paths":["path/to/cache","another/path"],"size":"25g"}`,
},
{
name: "disabled cache",
c: Cache{Disabled: true},
want: `false`,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
b, err := tc.c.MarshalJSON()
if err != nil {
t.Fatalf("Cache.MarshalJSON() error: %v", err)
}
if diff := cmp.Diff(string(b), tc.want); diff != "" {
t.Errorf("Cache.MarshalJSON() diff (-got +want):\n%s", diff)
}
})
}
}
func TestCacheUnmarshalOrdered(t *testing.T) {
t.Parallel()
cases := []struct {
name string
input any
want Cache
wantErr error
}{
{
name: "single path",
input: "path/to/cache",
want: Cache{Paths: []string{"path/to/cache"}},
},
{
name: "array of paths",
input: []any{"path/to/cache", "another/path"},
want: Cache{Paths: []string{"path/to/cache", "another/path"}},
},
{
name: "full cache settings block",
input: ordered.MapFromItems(
ordered.TupleSA{Key: "paths", Value: []any{"path/to/cache", "another/path"}},
ordered.TupleSA{Key: "name", Value: "cache-name"},
ordered.TupleSA{Key: "size", Value: "25g"},
),
want: Cache{Paths: []string{"path/to/cache", "another/path"}, Name: "cache-name", Size: "25g"},
},
{
name: "full cache settings block with extra fields",
input: ordered.MapFromItems(
ordered.TupleSA{Key: "paths", Value: []any{"path/to/cache", "another/path"}},
ordered.TupleSA{Key: "name", Value: "cache-name"},
ordered.TupleSA{Key: "size", Value: "25g"},
ordered.TupleSA{Key: "extra", Value: "field"},
),
want: Cache{
Paths: []string{"path/to/cache", "another/path"},
Name: "cache-name",
Size: "25g",
RemainingFields: map[string]any{"extra": "field"},
},
},
{
name: "multi-type list of scalar paths get normalised to strings",
input: []any{"path/to/cache", 42, true}, // 42 and true are valid directory paths, so we should keep them as strings
want: Cache{Paths: []string{"path/to/cache", "42", "true"}},
},
{
name: "non-scalar elements in an array",
input: []any{"path/to/cache", []int{1, 2, 3}, map[string]any{"hi": "there"}},
wantErr: ordered.ErrUnsupportedSrc,
},
{
name: "invalid typed scalar",
input: 42,
wantErr: errUnsupportedCacheType,
},
{
name: "invalid map",
input: ordered.MapFromItems(
ordered.TupleSA{
Key: "paths",
Value: ordered.MapFromItems( // nested map, not allowed
ordered.TupleSA{Key: "path", Value: "path/to/cache"},
),
},
),
wantErr: ordered.ErrIncompatibleTypes,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var c Cache
if err := c.UnmarshalOrdered(tc.input); !errors.Is(err, tc.wantErr) {
t.Fatalf("Cache.UnmarshalOrdered(%v) = %v, want: %v", tc.input, err, tc.wantErr)
}
if diff := cmp.Diff(c, tc.want); diff != "" {
t.Errorf("Cache diff after UnmarshalOrdered (-got +want):\n%s", diff)
}
})
}
}
func TestCacheInterpolate(t *testing.T) {
t.Parallel()
cases := []struct {
name string
env map[string]string
input Cache
want Cache
}{
{
name: "interpolate cache name",
env: map[string]string{"BUILDKITE_CACHE_NAME": "random-named"},
input: Cache{
Name: "${BUILDKITE_CACHE_NAME}-cache",
Paths: []string{"path/to/cache"},
Size: "25g",
},
want: Cache{
Name: "random-named-cache",
Paths: []string{"path/to/cache"},
Size: "25g",
},
},
{
name: "interpolate cache path",
env: map[string]string{"BUILDKITE_CACHE_PATH": "path/to/cache"},
input: Cache{
Name: "name",
Paths: []string{"${BUILDKITE_CACHE_PATH}"},
Size: "25g",
},
want: Cache{
Name: "name",
Paths: []string{"path/to/cache"},
Size: "25g",
},
},
{
name: "interpolate cache size",
env: map[string]string{"BUILDKITE_CACHE_SIZE": "25g"},
input: Cache{
Name: "name",
Paths: []string{"path/to/cache"},
Size: "${BUILDKITE_CACHE_SIZE}",
},
want: Cache{
Name: "name",
Paths: []string{"path/to/cache"},
Size: "25g",
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tf := envInterpolator{env: env.New(env.FromMap(tc.env))}
if err := tc.input.interpolate(tf); err != nil {
t.Fatalf("Cache.interpolate() error: %v", err)
}
if diff := cmp.Diff(tc.want, tc.input); diff != "" {
t.Errorf("Cache.interpolate got and want do not match:\n%s", diff)
}
})
}
}