Skip to content

Commit b84e448

Browse files
committed
fix tests
Signed-off-by: Sinelnikov Michail <mikhail.sinelnikov@flant.com>
1 parent 15e2650 commit b84e448

File tree

1 file changed

+79
-76
lines changed

1 file changed

+79
-76
lines changed

pkg/linters/module/rules/oss_library_test.go

Lines changed: 79 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -28,49 +28,62 @@ import (
2828

2929
func TestOSSRule_OssModuleRule(t *testing.T) {
3030
tests := []struct {
31-
name string
32-
disable bool
33-
setupFiles map[string]string
34-
wantErrors []string
35-
wantWarns []string
31+
name string
32+
disable bool
33+
createImagesDir bool
34+
setupFiles map[string]string
35+
wantErrors []string
36+
wantWarns []string
3637
}{
3738
{
38-
name: "rule disabled, no oss.yaml",
39-
disable: true,
40-
setupFiles: map[string]string{},
41-
wantErrors: nil,
42-
wantWarns: nil,
39+
name: "rule disabled, no oss.yaml",
40+
disable: true,
41+
createImagesDir: true,
42+
setupFiles: map[string]string{},
43+
wantErrors: nil,
44+
wantWarns: nil,
4345
},
4446
{
45-
name: "rule disabled, invalid oss.yaml",
46-
disable: true,
47+
name: "NO images dir, ignore missing oss.yaml",
48+
createImagesDir: false,
49+
setupFiles: map[string]string{},
50+
wantErrors: nil,
51+
wantWarns: nil,
52+
},
53+
{
54+
name: "NO images dir, ignore invalid oss.yaml",
55+
createImagesDir: false,
4756
setupFiles: map[string]string{
48-
"oss.yaml": "invalid yaml: [",
57+
"oss.yaml": "invalid: yaml: [",
4958
},
5059
wantErrors: nil,
5160
wantWarns: nil,
5261
},
5362
{
54-
name: "oss.yaml missing",
55-
setupFiles: map[string]string{},
56-
wantErrors: []string{"module should have oss.yaml"},
63+
name: "images dir exists, oss.yaml missing (WARN)",
64+
createImagesDir: true,
65+
setupFiles: map[string]string{},
66+
wantWarns: []string{"module has images folder, so it likely should have oss.yaml"},
5767
},
5868
{
59-
name: "oss.yaml invalid yaml",
69+
name: "oss.yaml invalid yaml",
70+
createImagesDir: true,
6071
setupFiles: map[string]string{
6172
"oss.yaml": "invalid: yaml: content",
6273
},
6374
wantErrors: []string{"error converting YAML to JSON"},
6475
},
6576
{
66-
name: "oss.yaml empty projects",
77+
name: "oss.yaml empty projects",
78+
createImagesDir: true,
6779
setupFiles: map[string]string{
6880
"oss.yaml": "[]",
6981
},
7082
wantErrors: []string{"no projects described"},
7183
},
7284
{
73-
name: "valid single project",
85+
name: "valid single project",
86+
createImagesDir: true,
7487
setupFiles: map[string]string{
7588
"oss.yaml": `
7689
- id: "dexidp/dex"
@@ -84,7 +97,8 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
8497
wantErrors: nil,
8598
},
8699
{
87-
name: "valid project with logo",
100+
name: "valid project with logo",
101+
createImagesDir: true,
88102
setupFiles: map[string]string{
89103
"oss.yaml": `
90104
- id: "dexidp/dex"
@@ -99,7 +113,8 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
99113
wantErrors: nil,
100114
},
101115
{
102-
name: "project with empty id",
116+
name: "project with empty id",
117+
createImagesDir: true,
103118
setupFiles: map[string]string{
104119
"oss.yaml": `
105120
- id: ""
@@ -113,7 +128,8 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
113128
wantErrors: []string{"id must not be empty"},
114129
},
115130
{
116-
name: "project with empty version",
131+
name: "project with empty version",
132+
createImagesDir: true,
117133
setupFiles: map[string]string{
118134
"oss.yaml": `
119135
- id: "dexidp/dex"
@@ -127,7 +143,8 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
127143
wantErrors: []string{"version must not be empty. Please fill in the parameter and configure CI (werf files for module images) to use these setting."},
128144
},
129145
{
130-
name: "project with invalid semver version",
146+
name: "project with invalid semver version",
147+
createImagesDir: true,
131148
setupFiles: map[string]string{
132149
"oss.yaml": `
133150
- id: "dexidp/dex"
@@ -141,7 +158,8 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
141158
wantWarns: []string{"version must be valid semver"},
142159
},
143160
{
144-
name: "project with empty name",
161+
name: "project with empty name",
162+
createImagesDir: true,
145163
setupFiles: map[string]string{
146164
"oss.yaml": `
147165
- id: "dexidp/dex"
@@ -155,7 +173,8 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
155173
wantErrors: []string{"name must not be empty"},
156174
},
157175
{
158-
name: "project with empty description",
176+
name: "project with empty description",
177+
createImagesDir: true,
159178
setupFiles: map[string]string{
160179
"oss.yaml": `
161180
- id: "dexidp/dex"
@@ -169,7 +188,8 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
169188
wantErrors: []string{"description must not be empty"},
170189
},
171190
{
172-
name: "project with empty link",
191+
name: "project with empty link",
192+
createImagesDir: true,
173193
setupFiles: map[string]string{
174194
"oss.yaml": `
175195
- id: "dexidp/dex"
@@ -183,7 +203,8 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
183203
wantErrors: []string{"link must not be empty"},
184204
},
185205
{
186-
name: "project with invalid link URL",
206+
name: "project with invalid link URL",
207+
createImagesDir: true,
187208
setupFiles: map[string]string{
188209
"oss.yaml": `
189210
- id: "dexidp/dex"
@@ -197,7 +218,8 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
197218
wantErrors: []string{"link URL is malformed"},
198219
},
199220
{
200-
name: "project with empty license",
221+
name: "project with empty license",
222+
createImagesDir: true,
201223
setupFiles: map[string]string{
202224
"oss.yaml": `
203225
- id: "dexidp/dex"
@@ -211,7 +233,8 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
211233
wantErrors: []string{"License must not be empty"},
212234
},
213235
{
214-
name: "project with invalid logo URL",
236+
name: "project with invalid logo URL",
237+
createImagesDir: true,
215238
setupFiles: map[string]string{
216239
"oss.yaml": `
217240
- id: "dexidp/dex"
@@ -226,7 +249,8 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
226249
wantErrors: []string{"project logo URL is malformed"},
227250
},
228251
{
229-
name: "multiple projects, one invalid",
252+
name: "multiple projects, one invalid",
253+
createImagesDir: true,
230254
setupFiles: map[string]string{
231255
"oss.yaml": `
232256
- id: "dexidp/dex"
@@ -251,6 +275,12 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
251275
t.Run(tt.name, func(t *testing.T) {
252276
// Create temp dir
253277
tempDir := t.TempDir()
278+
// Create images dir if needed
279+
if tt.createImagesDir {
280+
if err := os.Mkdir(filepath.Join(tempDir, "images"), 0755); err != nil {
281+
t.Fatalf("failed to create images dir: %v", err)
282+
}
283+
}
254284

255285
// Setup files
256286
for filename, content := range tt.setupFiles {
@@ -280,58 +310,31 @@ func TestOSSRule_OssModuleRule(t *testing.T) {
280310
}
281311
}
282312

283-
// Check expected errors
284-
if len(tt.wantErrors) == 0 {
285-
for _, et := range errorTexts {
286-
if !containsSubstring(tt.wantErrors, et) {
287-
t.Errorf("unexpected error: %s", et)
288-
}
289-
}
290-
} else {
291-
for _, wantErr := range tt.wantErrors {
292-
found := false
293-
for _, et := range errorTexts {
294-
if containsSubstring([]string{wantErr}, et) {
295-
found = true
296-
break
297-
}
298-
}
299-
if !found {
300-
t.Errorf("expected error containing %q, but not found in %v", wantErr, errorTexts)
301-
}
302-
}
303-
}
304-
305-
// Check expected warnings
306-
if len(tt.wantWarns) == 0 {
307-
if len(warnTexts) > 0 {
308-
t.Errorf("unexpected warnings: %v", warnTexts)
309-
}
310-
} else {
311-
for _, wantWarn := range tt.wantWarns {
312-
found := false
313-
for _, wt := range warnTexts {
314-
if containsSubstring([]string{wantWarn}, wt) {
315-
found = true
316-
break
317-
}
318-
}
319-
if !found {
320-
t.Errorf("expected warning containing %q, but not found in %v", wantWarn, warnTexts)
321-
}
322-
}
323-
}
313+
checkMessages(t, "error", tt.wantErrors, errorTexts)
314+
checkMessages(t, "warning", tt.wantWarns, warnTexts)
324315
})
325316
}
326317
}
327318

328-
func containsSubstring(slice []string, s string) bool {
329-
for _, item := range slice {
330-
if strings.Contains(s, item) {
331-
return true
319+
func checkMessages(t *testing.T, msgType string, want []string, got []string) {
320+
if len(want) == 0 {
321+
if len(got) > 0 {
322+
t.Errorf("unexpected %ss: %v", msgType, got)
323+
}
324+
} else {
325+
for _, w := range want {
326+
found := false
327+
for _, g := range got {
328+
if strings.Contains(g, w) {
329+
found = true
330+
break
331+
}
332+
}
333+
if !found {
334+
t.Errorf("expected %s containing %q, but not found in %v", msgType, w, got)
335+
}
332336
}
333337
}
334-
return false
335338
}
336339

337340
func Test_parseProjectList(t *testing.T) {

0 commit comments

Comments
 (0)