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

Remove most direct access to the os package, support NO_COLOR and K6_NO_COLOR #2410

Merged
merged 3 commits into from
Mar 16, 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
30 changes: 14 additions & 16 deletions cmd/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
package cmd

import (
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand All @@ -33,7 +29,8 @@ import (
"go.k6.io/k6/lib/metrics"
)

func getArchiveCmd(logger *logrus.Logger, globalFlags *commandFlags) *cobra.Command {
func getArchiveCmd(globalState *globalState) *cobra.Command { // nolint: funlen
archiveOut := "archive.tar"
// archiveCmd represents the archive command
archiveCmd := &cobra.Command{
Use: "archive",
Expand All @@ -49,19 +46,22 @@ An archive is a fully self-contained test run, and can be executed identically e
k6 run myarchive.tar`[1:],
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
src, filesystems, err := readSource(args[0], logger)
src, filesystems, err := readSource(globalState, args[0])
if err != nil {
return err
}

runtimeOptions, err := getRuntimeOptions(cmd.Flags(), buildEnvMap(os.Environ()))
runtimeOptions, err := getRuntimeOptions(cmd.Flags(), globalState.envVars)
if err != nil {
return err
}

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r, err := newRunner(logger, src, globalFlags.runType, filesystems, runtimeOptions, builtinMetrics, registry)
r, err := newRunner(
globalState.logger, src, globalState.flags.runType,
filesystems, runtimeOptions, builtinMetrics, registry,
)
if err != nil {
return err
}
Expand All @@ -70,9 +70,7 @@ An archive is a fully self-contained test run, and can be executed identically e
if err != nil {
return err
}
conf, err := getConsolidatedConfig(
afero.NewOsFs(), Config{Options: cliOpts}, r.GetOptions(), buildEnvMap(os.Environ()), globalFlags,
)
conf, err := getConsolidatedConfig(globalState, Config{Options: cliOpts}, r.GetOptions())
if err != nil {
return err
}
Expand All @@ -89,7 +87,7 @@ An archive is a fully self-contained test run, and can be executed identically e
}
}

_, err = deriveAndValidateConfig(conf, r.IsExecutable, logger)
_, err = deriveAndValidateConfig(conf, r.IsExecutable, globalState.logger)
if err != nil {
return err
}
Expand All @@ -101,7 +99,7 @@ An archive is a fully self-contained test run, and can be executed identically e

// Archive.
arc := r.MakeArchive()
f, err := os.Create(globalFlags.archiveOut)
f, err := globalState.fs.Create(archiveOut)
if err != nil {
return err
}
Expand All @@ -115,16 +113,16 @@ An archive is a fully self-contained test run, and can be executed identically e
}

archiveCmd.Flags().SortFlags = false
archiveCmd.Flags().AddFlagSet(archiveCmdFlagSet(globalFlags))
archiveCmd.Flags().AddFlagSet(archiveCmdFlagSet(&archiveOut))

return archiveCmd
}

func archiveCmdFlagSet(globalFlags *commandFlags) *pflag.FlagSet {
func archiveCmdFlagSet(archiveOut *string) *pflag.FlagSet {
olegbespalov marked this conversation as resolved.
Show resolved Hide resolved
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
flags.SortFlags = false
flags.AddFlagSet(optionFlagSet())
flags.AddFlagSet(runtimeOptionFlagSet(false))
flags.StringVarP(&globalFlags.archiveOut, "archive-out", "O", globalFlags.archiveOut, "archive output filename")
flags.StringVarP(archiveOut, "archive-out", "O", *archiveOut, "archive output filename")
return flags
}
25 changes: 12 additions & 13 deletions cmd/archive_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package cmd

import (
"io/ioutil"
"path/filepath"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib/testutils"
)

func TestArchiveThresholds(t *testing.T) {
Expand Down Expand Up @@ -40,20 +41,17 @@ func TestArchiveThresholds(t *testing.T) {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

tmpPath := filepath.Join(t.TempDir(), "archive.tar")

cmd := getArchiveCmd(testutils.NewLogger(t), newCommandFlags())
filename, err := filepath.Abs(testCase.testFilename)
testScript, err := ioutil.ReadFile(testCase.testFilename)
require.NoError(t, err)
args := []string{filename, "--archive-out", tmpPath}

testState := newGlobalTestState(t)
require.NoError(t, afero.WriteFile(testState.fs, filepath.Join(testState.cwd, testCase.testFilename), testScript, 0o644))
testState.args = []string{"k6", "archive", testCase.testFilename}
if testCase.noThresholds {
args = append(args, "--no-thresholds")
testState.args = append(testState.args, "--no-thresholds")
}
cmd.SetArgs(args)
wantExitCode := exitcodes.InvalidConfig

var gotErrExt errext.HasExitCode
gotErr := cmd.Execute()
gotErr := newRootCommand(testState.globalState).cmd.Execute()

assert.Equal(t,
testCase.wantErr,
Expand All @@ -62,9 +60,10 @@ func TestArchiveThresholds(t *testing.T) {
)

if testCase.wantErr {
var gotErrExt errext.HasExitCode
require.ErrorAs(t, gotErr, &gotErrExt)
assert.Equalf(t, wantExitCode, gotErrExt.ExitCode(),
"status code must be %d", wantExitCode,
assert.Equalf(t, exitcodes.InvalidConfig, gotErrExt.ExitCode(),
"status code must be %d", exitcodes.InvalidConfig,
)
}
})
Expand Down
Loading