-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
lint_test.go
74 lines (61 loc) · 1.21 KB
/
lint_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
package main
import (
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"github.com/go-playground/validator"
"github.com/pkic/ltl/ltl"
yaml "gopkg.in/yaml.v3"
)
func TestDecode(t *testing.T) {
validate := validator.New()
err := filepath.WalkDir("./data", func(s string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
if d.IsDir() {
return nil
}
// Skip hidden files and directories such as .git
if s[0:1] == "." || strings.HasPrefix(s, "ltl.") {
return nil
}
switch filepath.Ext(d.Name()) {
case ".yaml":
t.Run(s, func(t *testing.T) {
file, err := os.Open(s)
if err != nil {
t.Errorf("failed to open file: %v", err)
return
}
var pki ltl.PKI
d := yaml.NewDecoder(file)
d.KnownFields(true)
err = d.Decode(&pki)
if err != nil {
t.Error(err)
return
}
err = validate.Struct(pki)
if err != nil {
t.Error(err)
return
}
if pki.ID != "" {
t.Error("ID is automatically generated and must not be specified")
return
}
})
case ".go", ".mod", ".sum", ".md", "":
return nil
default:
t.Errorf("unexpected file type: %s", s)
}
return nil
})
if err != nil {
t.Fatal(err)
}
}