-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
103 lines (96 loc) · 2.06 KB
/
main_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"github.com/crumbhole/kubecog-plugin/pkg/engine"
"github.com/crumbhole/kubecog-plugin/pkg/kubecogconfig"
"github.com/otiai10/copy"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
)
const (
testsPath = "test/"
testsPathCopy = "test_copy/"
)
type checker struct {
t *testing.T
}
func (c *checker) checkExpected(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
kubecogRegexp := regexp.MustCompile(`\.kubecog\.yaml$`)
if kubecogRegexp.MatchString(path) {
return nil
}
fileRegexp := regexp.MustCompile(`\.yaml$`)
if fileRegexp.MatchString(path) {
expectedPath := strings.Replace(path, `.yaml`, `.expectyaml`, 1)
got, err := os.ReadFile(path)
if err != nil {
return err
}
expected, err := os.ReadFile(expectedPath)
if err != nil {
return err
}
if string(got) != string(expected) {
c.t.Errorf("Got \n%s\n and expected \n%s\n for %s\n",
got,
expected,
path)
}
}
return nil
}
func (c *checker) checkDir(path string) error {
cwd, _ := os.Getwd()
os.Chdir(path)
os.Setenv(`ARGOCD_ENV_KUBECOG_URL_PREFIX`, `file:///`+cwd+`/`+path+`/`)
defer os.Chdir(cwd)
config, err := kubecogconfig.Values()
if err != nil {
return err
}
s := scanner{engine: engine.Engine{Config: config}}
err = s.scanDir(`.`)
if err != nil {
return err
}
return filepath.Walk(`.`, c.checkExpected)
}
// Finds directories under ./test and evaluates all the .yaml/.ymls
func TestDirectories(t *testing.T) {
opt := copy.Options{
OnDirExists: func(_ string, _ string) copy.DirExistsAction {
return copy.Replace
},
}
err := os.RemoveAll(testsPathCopy)
if err != nil {
t.Error(err)
}
err = copy.Copy(testsPath, testsPathCopy, opt)
if err != nil {
t.Error(err)
}
dirs, err := os.ReadDir(testsPathCopy)
if err != nil {
t.Error(err)
}
checker := checker{t: t}
for _, d := range dirs {
if d.IsDir() {
t.Run(d.Name(), func(t *testing.T) {
err := checker.checkDir(testsPathCopy + d.Name())
if err != nil {
t.Error(err)
}
})
}
}
}