Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b3bf79c

Browse files
committedMar 10, 2025
Avoid using os package in k6 new tests except for the os.Remove()
1 parent 5510ea1 commit b3bf79c

File tree

1 file changed

+15
-34
lines changed

1 file changed

+15
-34
lines changed
 

‎internal/cmd/new_test.go

+15-34
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cmd
22

33
import (
4-
"os"
4+
"path/filepath"
55
"testing"
66

77
"github.com/stretchr/testify/assert"
@@ -130,19 +130,14 @@ func TestNewScriptCmd_LocalTemplate(t *testing.T) {
130130

131131
ts := tests.NewGlobalTestState(t)
132132

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")
138135
templateContent := `export default function() {
139136
console.log("Hello, world!");
140137
}`
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))
144139

145-
ts.CmdArgs = []string{"k6", "new", "--template", tmpFile.Name()}
140+
ts.CmdArgs = []string{"k6", "new", "--template", templatePath}
146141

147142
newRootCommand(ts.GlobalState).execute()
148143

@@ -157,20 +152,15 @@ func TestNewScriptCmd_LocalTemplateWith_ProjectID(t *testing.T) {
157152

158153
ts := tests.NewGlobalTestState(t)
159154

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")
165157
templateContent := `export default function() {
166158
// Template with {{ .ProjectID }} project ID
167159
console.log("Hello from project {{ .ProjectID }}");
168160
}`
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))
172162

173-
ts.CmdArgs = []string{"k6", "new", "--template", tmpFile.Name(), "--project-id", "9876"}
163+
ts.CmdArgs = []string{"k6", "new", "--template", templatePath, "--project-id", "9876"}
174164

175165
newRootCommand(ts.GlobalState).execute()
176166

@@ -190,12 +180,8 @@ func TestNewScriptCmd_LocalTemplate_NonExistentFile(t *testing.T) {
190180
ts := tests.NewGlobalTestState(t)
191181
ts.ExpectedExitCode = -1
192182

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")
199185

200186
ts.CmdArgs = []string{"k6", "new", "--template", nonExistentPath}
201187
ts.ExpectedExitCode = -1
@@ -216,20 +202,15 @@ func TestNewScriptCmd_LocalTemplate_SyntaxError(t *testing.T) {
216202
ts := tests.NewGlobalTestState(t)
217203
ts.ExpectedExitCode = -1
218204

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")
224207
invalidTemplateContent := `export default function() {
225208
// Invalid template with {{ .InvalidField }} field
226209
console.log("This will cause an error");
227210
}`
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))
231212

232-
ts.CmdArgs = []string{"k6", "new", "--template", tmpFile.Name(), "--project-id", "9876"}
213+
ts.CmdArgs = []string{"k6", "new", "--template", templatePath, "--project-id", "9876"}
233214
ts.ExpectedExitCode = -1
234215

235216
newRootCommand(ts.GlobalState).execute()

0 commit comments

Comments
 (0)
Please sign in to comment.