Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/convert refactor to not use global variables #2336

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ import (
"go.k6.io/k6/loader"
)

// Use these when interacting with fs and writing to terminal, makes a command testable
//nolint:gochecknoglobals
var (
defaultFs = afero.NewOsFs()
defaultWriter io.Writer = os.Stdout
)

// Panic if the given error is not nil.
func must(err error) {
if err != nil {
Expand Down
32 changes: 15 additions & 17 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,29 @@ import (
"io/ioutil"
"path/filepath"

"github.com/spf13/afero"
"github.com/spf13/cobra"
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/converter/har"
"go.k6.io/k6/lib"
)

// TODO: fix this... or remove k6 convert
//nolint: gochecknoglobals
var (
convertOutput string
optionsFilePath string
minSleep uint
maxSleep uint
enableChecks bool
returnOnFailedCheck bool
correlate bool
threshold uint
nobatch bool
only []string
skip []string
)

//nolint:funlen,gocognit
func getConvertCmd() *cobra.Command {
func getConvertCmd(defaultFs afero.Fs, defaultWriter io.Writer) *cobra.Command {
var (
convertOutput string
optionsFilePath string
minSleep uint
maxSleep uint
enableChecks bool
returnOnFailedCheck bool
correlate bool
threshold uint
nobatch bool
only []string
skip []string
)
convertCmd := &cobra.Command{
Use: "convert",
Short: "Convert a HAR file to a k6 script",
Expand Down
19 changes: 10 additions & 9 deletions cmd/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ export default function() {
}
`

//nolint:paralleltest // a lot of global variables, hopefully convert will just be dropped
func TestIntegrationConvertCmd(t *testing.T) {
t.Parallel()
t.Run("Correlate", func(t *testing.T) {
t.Parallel()
harFile, err := filepath.Abs("correlate.har")
require.NoError(t, err)
har, err := ioutil.ReadFile("testdata/example.har")
Expand All @@ -132,15 +133,14 @@ func TestIntegrationConvertCmd(t *testing.T) {
expectedTestPlan, err := ioutil.ReadFile("testdata/example.js")
require.NoError(t, err)

defaultFs = afero.NewMemMapFs()
defaultFs := afero.NewMemMapFs()

err = afero.WriteFile(defaultFs, harFile, har, 0o644)
require.NoError(t, err)

buf := &bytes.Buffer{}
defaultWriter = buf

convertCmd := getConvertCmd()
convertCmd := getConvertCmd(defaultFs, buf)
assert.NoError(t, convertCmd.Flags().Set("correlate", "true"))
assert.NoError(t, convertCmd.Flags().Set("no-batch", "true"))
assert.NoError(t, convertCmd.Flags().Set("enable-status-code-checks", "true"))
Expand Down Expand Up @@ -175,28 +175,29 @@ func TestIntegrationConvertCmd(t *testing.T) {
}
})
t.Run("Stdout", func(t *testing.T) {
t.Parallel()
harFile, err := filepath.Abs("stdout.har")
require.NoError(t, err)
defaultFs = afero.NewMemMapFs()
defaultFs := afero.NewMemMapFs()
err = afero.WriteFile(defaultFs, harFile, []byte(testHAR), 0o644)
assert.NoError(t, err)

buf := &bytes.Buffer{}
defaultWriter = buf

convertCmd := getConvertCmd()
convertCmd := getConvertCmd(defaultFs, buf)
err = convertCmd.RunE(convertCmd, []string{harFile})
assert.NoError(t, err)
assert.Equal(t, testHARConvertResult, buf.String())
})
t.Run("Output file", func(t *testing.T) {
t.Parallel()
harFile, err := filepath.Abs("output.har")
require.NoError(t, err)
defaultFs = afero.NewMemMapFs()
defaultFs := afero.NewMemMapFs()
err = afero.WriteFile(defaultFs, harFile, []byte(testHAR), 0o644)
assert.NoError(t, err)

convertCmd := getConvertCmd()
convertCmd := getConvertCmd(defaultFs, nil)
err = convertCmd.Flags().Set("output", "/output.js")
defer func() {
err = convertCmd.Flags().Set("output", "")
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand Down Expand Up @@ -167,7 +168,7 @@ func Execute() {
c.cmd.AddCommand(
getArchiveCmd(logger),
getCloudCmd(ctx, logger),
getConvertCmd(),
getConvertCmd(afero.NewOsFs(), stdout),
getInspectCmd(logger),
loginCmd,
getPauseCmd(ctx),
Expand Down