-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_test.go
188 lines (167 loc) · 4.33 KB
/
generate_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package cogs
import (
"errors"
"fmt"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/pelletier/go-toml"
)
var (
// AllowUnexported whitelists private fields for cmp.Diff comparison
AllowUnexported cmp.Option = cmp.Exporter(func(reflect.Type) bool { return true })
)
type generateTestOut struct {
name string
env string
toml string
config CfgMap
err error
}
func TestGenerate(t *testing.T) {
testCases := []generateTestOut{
{
name: "BasicConfig",
env: "local",
toml: basicCogToml,
config: map[string]interface{}{
"var": "var_value",
"other_var": "other_var_value",
},
err: nil,
},
{
name: "ConfigWithPath",
env: "qa",
toml: basicCogToml,
config: map[string]interface{}{
"enc_var": "|path.enc|./path.enc|subpath|.subpath",
"var": "|path|./path|subpath|.subpath",
},
err: nil,
},
{
name: "ConfigWithInheritedPath",
env: "path_env",
toml: basicCogToml,
config: map[string]interface{}{
"var1": "|path|./path|subpath|.subpath",
"var2": "|path|./path|subpath|.other_subpath",
"var3": "|path|./other_path|subpath|.subpath",
"enc_var1": "|path.enc|./path.enc|subpath|.subpath",
"enc_var2": "|path.enc|./path.enc|subpath|.other_subpath",
"enc_var3": "|path.enc|./other_path.enc|subpath|.subpath",
},
err: nil,
},
{
name: "DuplicateKeyInEnc/Error",
env: "local",
toml: errCogToml,
config: nil,
err: errors.New("local: var: duplicate key present in ctx and ctx.enc"),
},
{
name: "InvalidPathArray/Error",
env: "qa",
toml: errCogToml,
config: nil,
err: errors.New("qa: var: var.path: path array must have a length of two, providing path and subpath respectively"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tree, err := toml.Load(tc.toml)
if err != nil {
t.Errorf("toml.Load: %s", err)
}
cogName := tree.Get("name").(string)
config, err := generate(tc.env, tree, &testGear{Name: tc.env})
// TODO implement (err cogError) Unwrap() error { return err.err } so that "%w" directive is used
if diff := cmp.Diff(fmt.Errorf("%s", tc.err), fmt.Errorf("%s", err), AllowUnexported); diff != "" {
t.Errorf("toml[%s], env[%s]: (-expected err +actual err)\n-%s", cogName, tc.env, diff)
}
if diff := cmp.Diff(tc.config, config, AllowUnexported); diff != "" {
t.Errorf("toml[%s], env[%s]: (-expected config +actual config):\n%s", cogName, tc.env, diff)
}
})
}
}
var (
basicCogToml = `
name = "basicCogToml"
[local.vars]
var = "var_value"
other_var = "other_var_value"
[qa.vars]
var.path = ["./path", ".subpath"]
[qa.enc.vars]
enc_var.path = ["./path.enc", ".subpath"]
[path_env]
path = ["./path", ".subpath"]
[path_env.vars]
var1.path = []
var2.path = [[], ".other_subpath"]
var3.path = ["./other_path", []]
[path_env.enc]
path = ["./path.enc", ".subpath"]
[path_env.enc.vars]
enc_var1.path = []
enc_var2.path = [[], ".other_subpath"]
enc_var3.path = ["./other_path.enc", []]
`
errCogToml = `
name = "errCogToml"
[local.vars]
var = "var_value"
[local.enc.vars]
var = "other_var_value"
[qa.vars]
var.path = ["./path", ".subpath", "err_index"]
[qa.enc.vars]
enc_var.path = ["./path.enc", ".subpath"]
`
)
type testGear struct {
Name string
linkMap LinkMap
}
// SetName sets the gear name to the provided string
func (g *testGear) SetName(name string) {
g.Name = name
}
// ResolveMap is used to satisfy the Generator interface
func (g *testGear) ResolveMap(ctx baseContext) (CfgMap, error) {
var err error
g.linkMap, err = parseCtx(ctx)
if err != nil {
return nil, err
}
// final output
linkOut := make(map[string]interface{})
for k, link := range g.linkMap {
linkOut[k] = ResolveValue(link)
}
return linkOut, nil
}
// ResolveValue returns the value corresponding to a Link struct
// if Path resolves to a valid file the file byte value
// is passed to a file reader object, attempting to serialize the contents of
// the file if type is supported
func ResolveValue(c *Link) string {
// if Path is empty or Value is non empty
if c.Path == "" {
if val, ok := c.Value.(string); ok {
return val
}
}
pathStr := "|path|"
if c.encrypted {
// decrypt.File(c.Path, c.SubPath)
pathStr = "|path.enc|"
}
if c.SubPath != "" {
return pathStr + c.Path + "|subpath|" + c.SubPath
}
return "|path|" + c.Path
}