-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2206cfa
commit edc2f73
Showing
2 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.k6.io/k6/cmd/tests" | ||
"go.k6.io/k6/lib/fsext" | ||
) | ||
|
||
func TestInitScript(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", "init"} | ||
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, fmt.Sprintf(`name: "%s"`, testCase.expectedCloudName)) | ||
assert.Contains(t, jsData, "export default function() {") | ||
}) | ||
} | ||
} | ||
|
||
func TestInitScript_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", "init"} | ||
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 TestInitScript_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", "init", "-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() {") | ||
} |