-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
gen_test.go
284 lines (253 loc) 路 7.04 KB
/
gen_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// nolint: lll
package nodejs
import (
"bytes"
"encoding/json"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/test"
)
// For better CI test to job distribution, we split the test cases into three tests.
var genPkgBatchSize = len(test.PulumiPulumiSDKTests) / 3
func TestGeneratePackageOne(t *testing.T) {
t.Parallel()
testGeneratePackageBatch(t, test.PulumiPulumiSDKTests[0:genPkgBatchSize])
}
func TestGeneratePackageTwo(t *testing.T) {
t.Parallel()
testGeneratePackageBatch(t, test.PulumiPulumiSDKTests[genPkgBatchSize:2*genPkgBatchSize])
}
func TestGeneratePackageThree(t *testing.T) {
t.Parallel()
testGeneratePackageBatch(t, test.PulumiPulumiSDKTests[2*genPkgBatchSize:])
}
func testGeneratePackageBatch(t *testing.T, testCases []*test.SDKTest) {
test.TestSDKCodegen(t, &test.SDKCodegenOptions{
Language: "nodejs",
GenPackage: GeneratePackage,
Checks: map[string]test.CodegenCheck{
"nodejs/compile": func(t *testing.T, pwd string) {
typeCheckGeneratedPackage(t, pwd, true)
},
"nodejs/test": testGeneratedPackage,
},
TestCases: testCases,
})
}
// Runs unit tests against the generated code.
func testGeneratedPackage(t *testing.T, pwd string) {
// Some tests have do not have mocha as a dependency.
hasMocha := false
for _, c := range getYarnCommands(t, pwd) {
if c == "mocha" {
hasMocha = true
break
}
}
// We are attempting to ensure that we don't write tests that are not run. The `nodejs-extras`
// folder exists to mixin tests of the form `*.spec.ts`. We assume that if this folder is
// present and contains `*.spec.ts` files, we want to run those tests.
foundTests := false
findTests := func(path string, _ os.DirEntry, _ error) error {
if strings.HasSuffix(path, ".spec.ts") {
foundTests = true
}
return nil
}
mixinFolder := filepath.Join(filepath.Dir(pwd), "nodejs-extras")
if err := filepath.WalkDir(mixinFolder, findTests); !hasMocha && !os.IsNotExist(err) && foundTests {
t.Errorf("%s has at least one nodejs-extras/**/*.spec.ts file , but does not have mocha as a dependency."+
" Tests were not run. Please add mocha as a dependency in the schema or remove the *.spec.ts files.",
pwd)
}
if hasMocha {
// If mocha is a dev dependency but no test files exist, this will fail.
test.RunCommand(t, "mocha", pwd,
"yarn", "run", "mocha",
"--require", "ts-node/register",
"tests/**/*.spec.ts")
} else {
t.Logf("No mocha tests found for %s", pwd)
}
}
// Get the commands runnable with yarn run
func getYarnCommands(t *testing.T, pwd string) []string {
cmd := exec.Command("yarn", "run", "--json")
cmd.Dir = pwd
out, err := cmd.Output()
if err != nil {
t.Errorf("Got error determining valid commands: %s", err)
}
dec := json.NewDecoder(bytes.NewReader(out))
parsed := []map[string]interface{}{}
for {
var m map[string]interface{}
if err := dec.Decode(&m); err != nil {
if err == io.EOF {
break
}
t.FailNow()
}
parsed = append(parsed, m)
}
var cmds []string
addProvidedCmds := func(c map[string]interface{}) {
// If this fails, we want the test to fail. We don't want to accidentally skip tests.
data := c["data"].(map[string]interface{})
if data["type"] == "possibleCommands" {
return
}
for _, cmd := range data["items"].([]interface{}) {
cmds = append(cmds, cmd.(string))
}
}
addBinaryCmds := func(c map[string]interface{}) {
data := c["data"].(string)
if !strings.HasPrefix(data, "Commands available from binary scripts:") {
return
}
cmdList := data[strings.Index(data, ":")+1:]
for _, cmd := range strings.Split(cmdList, ",") {
cmds = append(cmds, strings.TrimSpace(cmd))
}
}
for _, c := range parsed {
switch c["type"] {
case "list":
addProvidedCmds(c)
case "info":
addBinaryCmds(c)
}
}
t.Logf("Found yarn commands in %s: %v", pwd, cmds)
return cmds
}
func TestGenerateTypeNames(t *testing.T) {
t.Parallel()
test.TestTypeNameCodegen(t, "nodejs", func(pkg *schema.Package) test.TypeNameGeneratorFunc {
modules, info, err := generateModuleContextMap("test", pkg, nil)
require.NoError(t, err)
pkg.Language["nodejs"] = info
root, ok := modules[""]
require.True(t, ok)
// Parallel tests will use the TypeNameGeneratorFunc
// from multiple goroutines, but root.typeString is
// not safe. Mutex is needed to avoid panics on
// concurrent map write.
//
// Note this problem is test-only since prod code
// works on a single goroutine.
var mutex sync.Mutex
return func(t schema.Type) string {
mutex.Lock()
defer mutex.Unlock()
return root.typeString(t, false, nil)
}
})
}
func TestPascalCases(t *testing.T) {
t.Parallel()
tests := []struct {
input string
expected string
}{
{
input: "hi",
expected: "Hi",
},
{
input: "NothingChanges",
expected: "NothingChanges",
},
{
input: "everything-changed",
expected: "EverythingChanged",
},
}
for _, tt := range tests {
result := pascal(tt.input)
require.Equal(t, tt.expected, result)
}
}
func Test_isStringType(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input schema.Type
expected bool
}{
{"string", schema.StringType, true},
{"int", schema.IntType, false},
{"Input[string]", &schema.InputType{ElementType: schema.StringType}, true},
{"Input[int]", &schema.InputType{ElementType: schema.IntType}, false},
{"StrictStringEnum", &schema.EnumType{ElementType: schema.StringType}, true},
{"StrictIntEnum", &schema.EnumType{ElementType: schema.IntType}, false},
{"RelaxedStringEnum", &schema.UnionType{
ElementTypes: []schema.Type{&schema.EnumType{ElementType: schema.StringType}, schema.StringType},
}, true},
{"RelaxedIntEnum", &schema.UnionType{
ElementTypes: []schema.Type{&schema.EnumType{ElementType: schema.IntType}, schema.IntType},
}, false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := isStringType(tt.input); got != tt.expected {
t.Errorf("isStringType() = %v, want %v", got, tt.expected)
}
})
}
}
// This test asserts that modContext.getRelativePath()
// returns the right relative path, regardless of whether
// the file is a resource definition or an Input/Output declaration
// from /types/
func TestGetRelativePath(t *testing.T) {
t.Parallel()
type TestCase struct {
mod string
dirRoot string
expected string
}
var cases = []TestCase{
{
mod: "foo",
dirRoot: "",
expected: "..",
}, {
mod: "foo/bar",
dirRoot: "",
expected: "../..",
}, {
mod: "types/accessanalyzer/input",
dirRoot: "",
expected: "../../..",
}, {
mod: "input",
dirRoot: "types/accessanalyzer",
expected: "../..",
},
}
for _, tc := range cases {
var ctx = &modContext{mod: tc.mod}
var observed = ctx.getRelativePath(tc.dirRoot)
require.Equal(
t,
tc.expected,
observed,
"Case (%s, %s): Expected %s, Observed %s",
tc.mod,
tc.dirRoot,
tc.expected,
observed,
)
}
}