forked from kcl-lang/krm-kcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
89 lines (81 loc) · 1.78 KB
/
example_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"kcl-lang.io/krm-kcl/pkg/options"
pkg "kcl-lang.io/kpm/pkg/package"
)
type fields struct {
InputPath string
OutputPath string
}
type suite struct {
name string
fields fields
wantErr bool
}
func TestRunExamples(t *testing.T) {
var tests []suite
err := filepath.Walk("./examples", func(path string, info fs.FileInfo, err error) error {
if !strings.HasSuffix(path, "kcl.mod") {
return nil
}
dir := filepath.Dir(path)
kPkg, err := pkg.LoadKclPkg(dir)
if err != nil {
return err
}
suiteDir := filepath.Join(dir, "suite")
goodSuite := filepath.Join(suiteDir, "good.yaml")
badSuite := filepath.Join(suiteDir, "bad.yaml")
pkgName := kPkg.GetPkgName()
if filepath.Base(dir) != pkgName {
return fmt.Errorf("Mismatch package name %s and base dir %s", pkgName, dir)
}
tests = append(tests, suite{
kPkg.GetPkgName() + "-good-suite",
fields{
InputPath: goodSuite,
},
false,
})
// Bad test suite is optional
if FileExists(badSuite) {
tests = append(tests, suite{
dir + "-bad-suite",
fields{
InputPath: badSuite,
},
true,
})
}
return nil
})
if err != nil {
t.Errorf("TestRunExamples() error = %v", err)
}
fmt.Printf("%d total suites checked\n", len(tests))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &options.RunOptions{
InputPath: tt.fields.InputPath,
OutputPath: tt.fields.OutputPath,
}
if err := o.Run(); (err != nil) != tt.wantErr {
t.Errorf("TestRunExamples() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
// FileExists mark whether the path exists.
func FileExists(path string) bool {
fi, err := os.Lstat(path)
if err != nil || fi.IsDir() {
return false
}
return true
}