-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_test.go
160 lines (140 loc) · 2.92 KB
/
generator_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package flagforge
import (
"bytes"
"fmt"
"os"
"testing"
)
func Test_NewParser(t *testing.T) {
p := NewParser()
if p == nil {
t.Fatalf("expected non-nil parser")
}
}
func Test_Generator_SingleArgument(t *testing.T) {
toml := `
[[arguments]]
name = "DataDir"
type = "string"
required = true
short_help = "Path to data directory"
long_help = "Path to the directory where the node stores its data"
`
tomlFile := mustWriteToTempTOMLFile(toml)
defer os.Remove(tomlFile)
parser := NewParser()
cfg, err := parser.ParsePath(tomlFile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
gen, err := NewGenerator(cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tempFD := mustTempFD()
defer os.Remove(tempFD.Name())
defer tempFD.Close()
err = gen.Execute(Go, tempFD)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func Test_Generator_SingleFlag(t *testing.T) {
toml := `
[[flags]]
name = "NodeID"
cli = "-node-id"
type = "string"
default = ""
short_help = "Node ID"
long_help = "Unique node identifier"
`
tomlFile := mustWriteToTempTOMLFile(toml)
defer os.Remove(tomlFile)
parser := NewParser()
cfg, err := parser.ParsePath(tomlFile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
gen, err := NewGenerator(cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tempFD := mustTempFD()
defer os.Remove(tempFD.Name())
defer tempFD.Close()
err = gen.Execute(Go, tempFD)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func Test_Generator_GoldenFiles(t *testing.T) {
for _, f := range []struct {
in string
out string
}{
{
in: "single-flag/in.toml",
out: "single-flag/out.go",
},
{
in: "multi-flag/in.toml",
out: "multi-flag/out.go",
},
{
in: "multi-argument-flag/in.toml",
out: "multi-argument-flag/out.go",
},
{
in: "rqlite/in.toml",
out: "rqlite/out.go",
},
} {
in := "testdata/" + f.in
out := "testdata/" + f.out
parser := NewParser()
cfg, err := parser.ParsePath(in)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
gen, err := NewGenerator(cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
buf := new(bytes.Buffer)
err = gen.Execute(Go, buf)
if err != nil {
t.Fatalf("unexpected error testing %s: %v", in, err)
}
if !bytes.Equal(buf.Bytes(), mustReadFile(out)) {
t.Errorf("generated output does not match %s\n", out)
fmt.Println(buf.String())
t.Fatal()
}
}
}
func mustWriteToTempTOMLFile(contents string) string {
f, err := os.CreateTemp("", "generator_test-*.toml")
if err != nil {
panic(err)
}
defer f.Close()
if _, err := f.WriteString(contents); err != nil {
panic(err)
}
return f.Name()
}
func mustTempFD() *os.File {
f, err := os.CreateTemp("", "generator_test")
if err != nil {
panic(err)
}
return f
}
func mustReadFile(path string) []byte {
b, err := os.ReadFile(path)
if err != nil {
panic(err)
}
return b
}