-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathspec_test.go
More file actions
180 lines (161 loc) 路 3.83 KB
/
Copy pathspec_test.go
File metadata and controls
180 lines (161 loc) 路 3.83 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
package dalec
import (
"encoding/json"
"testing"
"time"
"github.com/goccy/go-yaml"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)
func TestBuildStepListAliasResolution(t *testing.T) {
t.Parallel()
const header = `name: t
version: "1.0"
revision: "1"
packager: t
vendor: t
license: MIT
description: t
`
t.Run("an aliased build step resolves to the anchor's step", func(t *testing.T) {
t.Parallel()
spec, err := LoadSpec([]byte(header + `
x-step: &s
command: echo hi
build:
steps:
- *s
`))
assert.NilError(t, err)
assert.Assert(t, cmp.Len(spec.Build.Steps, 1))
assert.Equal(t, spec.Build.Steps[0].Command, "echo hi")
})
t.Run("a merge key in a build step resolves to the anchor's step", func(t *testing.T) {
t.Parallel()
spec, err := LoadSpec([]byte(header + `
x-step: &s
command: echo hi
build:
steps:
- <<: *s
`))
assert.NilError(t, err)
assert.Assert(t, cmp.Len(spec.Build.Steps, 1))
assert.Equal(t, spec.Build.Steps[0].Command, "echo hi")
})
}
func TestDate(t *testing.T) {
expect := "2023-10-01"
expectTime, err := time.Parse(time.DateOnly, expect)
assert.NilError(t, err)
d := Date{Time: expectTime}
assert.Check(t, cmp.Equal(d.Format(time.DateOnly), expect))
dtJSON, err := json.Marshal(d)
assert.NilError(t, err)
dtYAML, err := yaml.Marshal(d)
assert.NilError(t, err)
var d2 Date
err = json.Unmarshal(dtJSON, &d2)
assert.NilError(t, err)
assert.Check(t, cmp.Equal(d2.Format(time.DateOnly), expect))
d3 := Date{}
err = yaml.Unmarshal(dtYAML, &d3)
assert.NilError(t, err)
assert.Check(t, cmp.Equal(d3.Format(time.DateOnly), expect))
}
func TestSourceFilterConfig(t *testing.T) {
t.Parallel()
var cfg SourceFilterConfig
err := yaml.Unmarshal([]byte(`
global_excludes:
- github.com/klauspost/compress@*/zip/corpus/14.zip
- cache/download/github.com/klauspost/compress/@v/*.zip
`), &cfg)
assert.NilError(t, err)
assert.Check(t, cmp.DeepEqual(cfg.GlobalExcludes, []string{
"github.com/klauspost/compress@*/zip/corpus/14.zip",
"cache/download/github.com/klauspost/compress/@v/*.zip",
}))
assert.Check(t, !cfg.IsEmpty())
var empty SourceFilterConfig
assert.Check(t, empty.IsEmpty())
}
func TestSourceGeneratorValidateGomodEdits(t *testing.T) {
t.Parallel()
tests := []struct {
name string
gen *SourceGenerator
expectErr bool
expectedErrSubstr string
}{
{
name: "valid gomod edits",
gen: &SourceGenerator{
Gomod: &GeneratorGomod{
Edits: &GomodEdits{
Replace: []GomodReplace{
{Original: "github.com/old/module", Update: "github.com/new/module@v1.0.0"},
},
},
},
},
expectErr: false,
},
{
name: "invalid replace - empty old",
gen: &SourceGenerator{
Gomod: &GeneratorGomod{
Edits: &GomodEdits{
Replace: []GomodReplace{
{Original: "", Update: "github.com/new/module@v1.0.0"},
},
},
},
},
expectErr: true,
expectedErrSubstr: "must be non-empty",
},
{
name: "invalid replace - empty new",
gen: &SourceGenerator{
Gomod: &GeneratorGomod{
Edits: &GomodEdits{
Replace: []GomodReplace{
{Original: "github.com/old/module", Update: ""},
},
},
},
},
expectErr: true,
expectedErrSubstr: "must be non-empty",
},
{
name: "multiple errors",
gen: &SourceGenerator{
Gomod: &GeneratorGomod{
Edits: &GomodEdits{
Replace: []GomodReplace{
{Original: "", Update: ""}, // Both invalid
},
},
},
},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.gen.Validate()
if tt.expectErr {
if err == nil {
t.Fatal("expected error, got nil")
}
if tt.expectedErrSubstr != "" {
assert.Check(t, cmp.Contains(err.Error(), tt.expectedErrSubstr))
}
return
}
assert.NilError(t, err)
})
}
}