-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathnew_test.go
224 lines (166 loc) · 6.31 KB
/
new_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
package cmd
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/internal/cmd/tests"
"go.k6.io/k6/lib/fsext"
)
func TestNewScriptCmd(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
scriptNameArg string
expectedCloudName string
expectedFilePath string
}{
{
name: "default script name",
expectedCloudName: "script.js",
expectedFilePath: defaultNewScriptName,
},
{
name: "user-specified script name",
scriptNameArg: "mytest.js",
expectedCloudName: "mytest.js",
expectedFilePath: "mytest.js",
},
{
name: "script outside of current working directory",
scriptNameArg: "../mytest.js",
expectedCloudName: "mytest.js",
expectedFilePath: "../mytest.js",
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
ts := tests.NewGlobalTestState(t)
ts.CmdArgs = []string{"k6", "new"}
if testCase.scriptNameArg != "" {
ts.CmdArgs = append(ts.CmdArgs, testCase.scriptNameArg)
}
newRootCommand(ts.GlobalState).execute()
data, err := fsext.ReadFile(ts.FS, testCase.expectedFilePath)
require.NoError(t, err)
jsData := string(data)
assert.Contains(t, jsData, "export const options = {")
assert.Contains(t, jsData, "export default function() {")
})
}
}
func TestNewScriptCmd_FileExists_NoOverwrite(t *testing.T) {
t.Parallel()
ts := tests.NewGlobalTestState(t)
require.NoError(t, fsext.WriteFile(ts.FS, defaultNewScriptName, []byte("untouched"), 0o644))
ts.CmdArgs = []string{"k6", "new"}
ts.ExpectedExitCode = -1
newRootCommand(ts.GlobalState).execute()
data, err := fsext.ReadFile(ts.FS, defaultNewScriptName)
require.NoError(t, err)
assert.Contains(t, string(data), "untouched")
}
func TestNewScriptCmd_FileExists_Overwrite(t *testing.T) {
t.Parallel()
ts := tests.NewGlobalTestState(t)
require.NoError(t, fsext.WriteFile(ts.FS, defaultNewScriptName, []byte("untouched"), 0o644))
ts.CmdArgs = []string{"k6", "new", "-f"}
newRootCommand(ts.GlobalState).execute()
data, err := fsext.ReadFile(ts.FS, defaultNewScriptName)
require.NoError(t, err)
assert.Contains(t, string(data), "export const options = {")
assert.Contains(t, string(data), "export default function() {")
}
func TestNewScriptCmd_InvalidTemplateType(t *testing.T) {
t.Parallel()
ts := tests.NewGlobalTestState(t)
ts.CmdArgs = []string{"k6", "new", "--template", "invalid-template"}
ts.ExpectedExitCode = -1
newRootCommand(ts.GlobalState).execute()
assert.Contains(t, ts.Stderr.String(), "invalid template type")
// Verify that no script file was created
exists, err := fsext.Exists(ts.FS, defaultNewScriptName)
require.NoError(t, err)
assert.False(t, exists, "script file should not exist")
}
func TestNewScriptCmd_ProjectID(t *testing.T) {
t.Parallel()
ts := tests.NewGlobalTestState(t)
ts.CmdArgs = []string{"k6", "new", "--project-id", "1422"}
newRootCommand(ts.GlobalState).execute()
data, err := fsext.ReadFile(ts.FS, defaultNewScriptName)
require.NoError(t, err)
assert.Contains(t, string(data), "projectID: 1422")
}
func TestNewScriptCmd_LocalTemplate(t *testing.T) {
t.Parallel()
ts := tests.NewGlobalTestState(t)
// Create template file in test temp directory
templatePath := filepath.Join(t.TempDir(), "template.js")
templateContent := `export default function() {
console.log("Hello, world!");
}`
require.NoError(t, fsext.WriteFile(ts.FS, templatePath, []byte(templateContent), 0o600))
ts.CmdArgs = []string{"k6", "new", "--template", templatePath}
newRootCommand(ts.GlobalState).execute()
data, err := fsext.ReadFile(ts.FS, defaultNewScriptName)
require.NoError(t, err)
assert.Equal(t, templateContent, string(data), "generated file should match the template content")
}
func TestNewScriptCmd_LocalTemplateWith_ProjectID(t *testing.T) {
t.Parallel()
ts := tests.NewGlobalTestState(t)
// Create template file in test temp directory
templatePath := filepath.Join(t.TempDir(), "template.js")
templateContent := `export default function() {
// Template with {{ .ProjectID }} project ID
console.log("Hello from project {{ .ProjectID }}");
}`
require.NoError(t, fsext.WriteFile(ts.FS, templatePath, []byte(templateContent), 0o600))
ts.CmdArgs = []string{"k6", "new", "--template", templatePath, "--project-id", "9876"}
newRootCommand(ts.GlobalState).execute()
data, err := fsext.ReadFile(ts.FS, defaultNewScriptName)
require.NoError(t, err)
expectedContent := `export default function() {
// Template with 9876 project ID
console.log("Hello from project 9876");
}`
assert.Equal(t, expectedContent, string(data), "generated file should have project ID interpolated")
}
func TestNewScriptCmd_LocalTemplate_NonExistentFile(t *testing.T) {
t.Parallel()
ts := tests.NewGlobalTestState(t)
ts.ExpectedExitCode = -1
// Use a path that we know doesn't exist in the temp directory
nonExistentPath := filepath.Join(t.TempDir(), "nonexistent.js")
ts.CmdArgs = []string{"k6", "new", "--template", nonExistentPath}
ts.ExpectedExitCode = -1
newRootCommand(ts.GlobalState).execute()
assert.Contains(t, ts.Stderr.String(), "failed to read template file")
// Verify that no script file was created
exists, err := fsext.Exists(ts.FS, defaultNewScriptName)
require.NoError(t, err)
assert.False(t, exists, "script file should not exist")
}
func TestNewScriptCmd_LocalTemplate_SyntaxError(t *testing.T) {
t.Parallel()
ts := tests.NewGlobalTestState(t)
ts.ExpectedExitCode = -1
// Create template file with invalid content in test temp directory
templatePath := filepath.Join(t.TempDir(), "template.js")
invalidTemplateContent := `export default function() {
// Invalid template with {{ .InvalidField }} field
console.log("This will cause an error");
}`
require.NoError(t, fsext.WriteFile(ts.FS, templatePath, []byte(invalidTemplateContent), 0o600))
ts.CmdArgs = []string{"k6", "new", "--template", templatePath, "--project-id", "9876"}
ts.ExpectedExitCode = -1
newRootCommand(ts.GlobalState).execute()
assert.Contains(t, ts.Stderr.String(), "failed to execute template")
// Verify that no script file was created
exists, err := fsext.Exists(ts.FS, defaultNewScriptName)
require.NoError(t, err)
assert.False(t, exists, "script file should not exist")
}