-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_test.go
83 lines (71 loc) · 1.69 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
package main
import (
"flag"
"io"
"os"
"testing"
"github.com/google/go-cmp/cmp"
)
var (
update = flag.Bool("update", false, "update the golden files of this test")
)
func TestMain(m *testing.M) {
flag.Parse()
os.Exit(m.Run())
}
func Test_generateCrdDocs(t *testing.T) {
type args struct {
configFilePath string
}
tests := []struct {
name string
args args
golden string
outputFile string
wantErr bool
}{
{
name: "case1",
args: args{
configFilePath: "testdata/case1/config.yaml",
},
golden: "testdata/case1/output.golden",
outputFile: "testdata/case1/output/examples.example.giantswarm.io.md",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := generateCrdDocs(tt.args.configFilePath)
if (err != nil) != tt.wantErr {
t.Errorf("generateCrdDocs() error = %v, wantErr %v", err, tt.wantErr)
}
content, err := os.ReadFile(tt.outputFile)
if err != nil {
t.Fatalf("Error loading output file %s: %s", tt.outputFile, err)
}
got := string(content)
want := goldenValue(t, tt.golden, got, *update)
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("generateCrdDocs() mismatch (-want +got):\n%s", diff)
}
})
}
}
func goldenValue(t *testing.T, goldenPath string, actual string, update bool) string {
t.Helper()
f, _ := os.OpenFile(goldenPath, os.O_RDWR, 0644)
defer f.Close()
if update {
_, err := f.WriteString(actual)
if err != nil {
t.Fatalf("Error writing to file %s: %s", goldenPath, err)
}
return actual
}
content, err := io.ReadAll(f)
if err != nil {
t.Fatalf("Error opening file %s: %s", goldenPath, err)
}
return string(content)
}