Skip to content

Commit

Permalink
Add tests for k6 init command
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewslotin committed Oct 13, 2023
1 parent 2206cfa commit edc2f73
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 deletions.
13 changes: 9 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"path"
"text/template"
Expand All @@ -13,7 +14,10 @@ import (

const defaultNewScriptName = "script.js"

var defaultNewScriptTemplate = template.Must(template.New("init").Parse(`import http from 'k6/http';
//nolint:gochecknoglobals
var (
errFileExists = errors.New("file already exists")
defaultNewScriptTemplate = template.Must(template.New("init").Parse(`import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
Expand Down Expand Up @@ -69,6 +73,7 @@ export default function() {
sleep(1);
}
`))
)

type initScriptTemplateArgs struct {
ScriptName string
Expand All @@ -88,7 +93,7 @@ func (c *initCmd) flagSet() *pflag.FlagSet {
return flags
}

func (c *initCmd) run(cmd *cobra.Command, args []string) error {
func (c *initCmd) run(cmd *cobra.Command, args []string) error { //nolint:revive
target := defaultNewScriptName
if len(args) > 0 {
target = args[0]
Expand All @@ -101,14 +106,14 @@ func (c *initCmd) run(cmd *cobra.Command, args []string) error {

if fileExists && !c.overwriteFiles {
c.gs.Logger.Errorf("%s already exists", target)
return err
return errFileExists
}

fd, err := c.gs.FS.Create(target)
if err != nil {
return err
}
defer fd.Close()
defer fd.Close() //nolint:errcheck

if err := defaultNewScriptTemplate.Execute(fd, initScriptTemplateArgs{
ScriptName: path.Base(target),
Expand Down
97 changes: 97 additions & 0 deletions cmd/init_test.go
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() {")
}

0 comments on commit edc2f73

Please sign in to comment.