1
1
package cmd
2
2
3
3
import (
4
- "os "
4
+ "path/filepath "
5
5
"testing"
6
6
7
7
"github.com/stretchr/testify/assert"
@@ -130,19 +130,14 @@ func TestNewScriptCmd_LocalTemplate(t *testing.T) {
130
130
131
131
ts := tests .NewGlobalTestState (t )
132
132
133
- // Create temp file with random name in OS temp dir
134
- tmpFile , err := os .CreateTemp ("" , "k6-template-*.js" )
135
- require .NoError (t , err )
136
- defer os .Remove (tmpFile .Name ()) //nolint:errcheck
137
-
133
+ // Create template file in test temp directory
134
+ templatePath := filepath .Join (t .TempDir (), "template.js" )
138
135
templateContent := `export default function() {
139
136
console.log("Hello, world!");
140
137
}`
141
- _ , err = tmpFile .Write ([]byte (templateContent ))
142
- require .NoError (t , err )
143
- require .NoError (t , tmpFile .Close ())
138
+ require .NoError (t , fsext .WriteFile (ts .FS , templatePath , []byte (templateContent ), 0o600 ))
144
139
145
- ts .CmdArgs = []string {"k6" , "new" , "--template" , tmpFile . Name () }
140
+ ts .CmdArgs = []string {"k6" , "new" , "--template" , templatePath }
146
141
147
142
newRootCommand (ts .GlobalState ).execute ()
148
143
@@ -157,20 +152,15 @@ func TestNewScriptCmd_LocalTemplateWith_ProjectID(t *testing.T) {
157
152
158
153
ts := tests .NewGlobalTestState (t )
159
154
160
- // Create temp file with random name in OS temp dir
161
- tmpFile , err := os .CreateTemp ("" , "k6-template-*.js" )
162
- require .NoError (t , err )
163
- defer os .Remove (tmpFile .Name ()) //nolint:errcheck
164
-
155
+ // Create template file in test temp directory
156
+ templatePath := filepath .Join (t .TempDir (), "template.js" )
165
157
templateContent := `export default function() {
166
158
// Template with {{ .ProjectID }} project ID
167
159
console.log("Hello from project {{ .ProjectID }}");
168
160
}`
169
- _ , err = tmpFile .Write ([]byte (templateContent ))
170
- require .NoError (t , err )
171
- require .NoError (t , tmpFile .Close ())
161
+ require .NoError (t , fsext .WriteFile (ts .FS , templatePath , []byte (templateContent ), 0o600 ))
172
162
173
- ts .CmdArgs = []string {"k6" , "new" , "--template" , tmpFile . Name () , "--project-id" , "9876" }
163
+ ts .CmdArgs = []string {"k6" , "new" , "--template" , templatePath , "--project-id" , "9876" }
174
164
175
165
newRootCommand (ts .GlobalState ).execute ()
176
166
@@ -190,12 +180,8 @@ func TestNewScriptCmd_LocalTemplate_NonExistentFile(t *testing.T) {
190
180
ts := tests .NewGlobalTestState (t )
191
181
ts .ExpectedExitCode = - 1
192
182
193
- // Create a temporary file path that we ensure doesn't exist
194
- tmpFile , err := os .CreateTemp ("" , "k6-nonexistent-*.js" )
195
- require .NoError (t , err )
196
- nonExistentPath := tmpFile .Name ()
197
- require .NoError (t , tmpFile .Close ())
198
- require .NoError (t , os .Remove (nonExistentPath ))
183
+ // Use a path that we know doesn't exist in the temp directory
184
+ nonExistentPath := filepath .Join (t .TempDir (), "nonexistent.js" )
199
185
200
186
ts .CmdArgs = []string {"k6" , "new" , "--template" , nonExistentPath }
201
187
ts .ExpectedExitCode = - 1
@@ -216,20 +202,15 @@ func TestNewScriptCmd_LocalTemplate_SyntaxError(t *testing.T) {
216
202
ts := tests .NewGlobalTestState (t )
217
203
ts .ExpectedExitCode = - 1
218
204
219
- // Create a temporary file with invalid Go template content
220
- tmpFile , err := os .CreateTemp ("" , "k6-invalid-template-*.js" )
221
- require .NoError (t , err )
222
- defer os .Remove (tmpFile .Name ()) //nolint:errcheck
223
-
205
+ // Create template file with invalid content in test temp directory
206
+ templatePath := filepath .Join (t .TempDir (), "template.js" )
224
207
invalidTemplateContent := `export default function() {
225
208
// Invalid template with {{ .InvalidField }} field
226
209
console.log("This will cause an error");
227
210
}`
228
- _ , err = tmpFile .Write ([]byte (invalidTemplateContent ))
229
- require .NoError (t , err )
230
- require .NoError (t , tmpFile .Close ())
211
+ require .NoError (t , fsext .WriteFile (ts .FS , templatePath , []byte (invalidTemplateContent ), 0o600 ))
231
212
232
- ts .CmdArgs = []string {"k6" , "new" , "--template" , tmpFile . Name () , "--project-id" , "9876" }
213
+ ts .CmdArgs = []string {"k6" , "new" , "--template" , templatePath , "--project-id" , "9876" }
233
214
ts .ExpectedExitCode = - 1
234
215
235
216
newRootCommand (ts .GlobalState ).execute ()
0 commit comments