-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhelpers_test.go
More file actions
93 lines (77 loc) · 2.08 KB
/
helpers_test.go
File metadata and controls
93 lines (77 loc) · 2.08 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
package publiccode
import (
"log"
"os"
"path/filepath"
"reflect"
"testing"
)
type testType struct {
file string
err error
}
var cwd string
func init() {
var err error
cwd, err = os.Getwd()
if err != nil {
log.Fatalf("failed to get cwd: %v", err)
}
}
// Parse the YAML file passed as argument, using the current directory
// as base path.
//
// Return nil if the parsing succeeded or an error if it failed.
func parse(file string) error {
var p *Parser
var err error
if p, err = NewDefaultParser(); err != nil {
return err
}
_, err = p.Parse(file)
return err
}
func parseNoNetwork(file string) error {
var p *Parser
var err error
if p, err = NewParser(ParserConfig{DisableNetwork: true}); err != nil {
return err
}
_, err = p.Parse(file)
return err
}
// Check all the YAML files matching the glob pattern and fail for each file
// with parsing or validation errors.
func checkValidFiles(pattern string, t *testing.T) {
testFiles, _ := filepath.Glob(pattern)
for _, file := range testFiles {
t.Run(file, func(t *testing.T) {
if err := parse(file); err != nil {
t.Errorf("[%s] validation failed for valid file: %T - %s\n", file, err, err)
}
})
}
}
// Check all the YAML files matching the glob pattern and fail for each file
// with parsing or validation errors, with the network disabled.
func checkValidFilesNoNetwork(pattern string, t *testing.T) {
testFiles, _ := filepath.Glob(pattern)
for _, file := range testFiles {
t.Run(file, func(t *testing.T) {
if err := parseNoNetwork(file); err != nil {
t.Errorf("[%s] validation failed for valid file: %T - %s\n", file, err, err)
}
})
}
}
func checkParseErrors(t *testing.T, err error, test testType) {
if test.err == nil && err != nil {
t.Errorf("[%s] unexpected error: %v\n", test.file, err)
} else if test.err != nil && err == nil {
t.Errorf("[%s] no error generated\n", test.file)
} else if test.err != nil && err != nil {
if !reflect.DeepEqual(test.err, err) {
t.Errorf("[%s] wrong error generated:\n%T - %s\n- instead of:\n%T - %s", test.file, err, err, test.err, test.err)
}
}
}