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

Hide afero behind fsext package #3025

Merged
merged 1 commit into from
Apr 24, 2023
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
16 changes: 8 additions & 8 deletions cmd/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"syscall"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib/fsext"
)

func TestArchiveThresholds(t *testing.T) {
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestArchiveThresholds(t *testing.T) {
require.NoError(t, err)

ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, testCase.testFilename), testScript, 0o644))
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, testCase.testFilename), testScript, 0o644))
ts.CmdArgs = []string{"k6", "archive", testCase.testFilename}
if testCase.noThresholds {
ts.CmdArgs = append(ts.CmdArgs, "--no-thresholds")
Expand All @@ -102,15 +102,15 @@ func TestArchiveContainsEnv(t *testing.T) {
fileName := "script.js"
testScript := []byte(`export default function () {}`)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, fileName), testScript, 0o644))
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, fileName), testScript, 0o644))

// when we do archiving and passing the `--env` flags
ts.CmdArgs = []string{"k6", "--env", "ENV1=lorem", "--env", "ENV2=ipsum", "archive", fileName}

newRootCommand(ts.GlobalState).execute()
require.NoError(t, untar(t, ts.FS, "archive.tar", "tmp/"))

data, err := afero.ReadFile(ts.FS, "tmp/metadata.json")
data, err := fsext.ReadFile(ts.FS, "tmp/metadata.json")
require.NoError(t, err)

metadata := struct {
Expand All @@ -135,15 +135,15 @@ func TestArchiveNotContainsEnv(t *testing.T) {
fileName := "script.js"
testScript := []byte(`export default function () {}`)
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, fileName), testScript, 0o644))
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, fileName), testScript, 0o644))

// when we do archiving and passing the `--env` flags altogether with `--exclude-env-vars` flag
ts.CmdArgs = []string{"k6", "--env", "ENV1=lorem", "--env", "ENV2=ipsum", "archive", "--exclude-env-vars", fileName}

newRootCommand(ts.GlobalState).execute()
require.NoError(t, untar(t, ts.FS, "archive.tar", "tmp/"))

data, err := afero.ReadFile(ts.FS, "tmp/metadata.json")
data, err := fsext.ReadFile(ts.FS, "tmp/metadata.json")
require.NoError(t, err)

metadata := struct {
Expand All @@ -156,10 +156,10 @@ func TestArchiveNotContainsEnv(t *testing.T) {
}

// untar untars a `fileName` file to a `destination` path
func untar(t *testing.T, fileSystem afero.Fs, fileName string, destination string) error {
func untar(t *testing.T, fileSystem fsext.Fs, fileName string, destination string) error {
t.Helper()

archiveFile, err := afero.ReadFile(fileSystem, fileName)
archiveFile, err := fsext.ReadFile(fileSystem, fileName)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/mstoykov/envconfig"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/pflag"
"gopkg.in/guregu/null.v3"

Expand All @@ -20,6 +19,7 @@ import (
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/executor"
"go.k6.io/k6/lib/fsext"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/metrics"
)
Expand Down Expand Up @@ -116,7 +116,7 @@ func readDiskConfig(gs *state.GlobalState) (Config, error) {
return Config{}, err
}

data, err := afero.ReadFile(gs.FS, gs.Flags.ConfigFilePath)
data, err := fsext.ReadFile(gs.FS, gs.Flags.ConfigFilePath)
if err != nil {
return Config{}, fmt.Errorf("couldn't load the configuration from %q: %w", gs.Flags.ConfigFilePath, err)
}
Expand All @@ -140,7 +140,7 @@ func writeDiskConfig(gs *state.GlobalState, conf Config) error {
return err
}

return afero.WriteFile(gs.FS, gs.Flags.ConfigFilePath, data, 0o644)
return fsext.WriteFile(gs.FS, gs.Flags.ConfigFilePath, data, 0o644)
}

// Reads configuration variables from the environment.
Expand Down
12 changes: 6 additions & 6 deletions cmd/config_consolidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"
"time"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"
Expand All @@ -14,6 +13,7 @@ import (
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/executor"
"go.k6.io/k6/lib/fsext"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/metrics"
)
Expand Down Expand Up @@ -109,10 +109,10 @@ type file struct {
filepath, contents string
}

func getFS(files []file) afero.Fs {
fs := afero.NewMemMapFs()
func getFS(files []file) fsext.Fs {
fs := fsext.NewMemMapFs()
for _, f := range files {
must(afero.WriteFile(fs, f.filepath, []byte(f.contents), 0o644)) // modes don't matter in the afero.MemMapFs
must(fsext.WriteFile(fs, f.filepath, []byte(f.contents), 0o644)) // modes don't matter in the afero.MemMapFs
}
return fs
}
Expand All @@ -121,7 +121,7 @@ type opts struct {
cli []string
env []string
runner *lib.Options
fs afero.Fs
fs fsext.Fs
cmds []string
}

Expand All @@ -145,7 +145,7 @@ type configConsolidationTestCase struct {

func getConfigConsolidationTestCases() []configConsolidationTestCase {
defaultFlags := state.GetDefaultFlags(".config")
defaultConfig := func(jsonConfig string) afero.Fs {
defaultConfig := func(jsonConfig string) fsext.Fs {
return getFS([]file{{defaultFlags.ConfigFilePath, jsonConfig}})
}
I := null.IntFrom // shortcut for "Valid" (i.e. user-specified) ints
Expand Down
10 changes: 5 additions & 5 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"encoding/json"
"io"

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

"go.k6.io/k6/cmd/state"
"go.k6.io/k6/converter/har"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/fsext"
)

// TODO: split apart like `k6 run` and `k6 archive`?
Expand All @@ -34,13 +34,13 @@ func getCmdConvert(gs *state.GlobalState) *cobra.Command {
exampleText := getExampleText(gs, `
# Convert a HAR file to a k6 script.
{{.}} convert -O har-session.js session.har

# Convert a HAR file to a k6 script creating requests only for the given domain/s.
{{.}} convert -O har-session.js --only yourdomain.com,additionaldomain.com session.har

# Convert a HAR file. Batching requests together as long as idle time between requests <800ms
{{.}} convert --batch-threshold 800 session.har

# Run the k6 script.
{{.}} run har-session.js`[1:])

Expand Down Expand Up @@ -69,7 +69,7 @@ func getCmdConvert(gs *state.GlobalState) *cobra.Command {
options := lib.Options{MaxRedirects: null.IntFrom(0)}

if optionsFilePath != "" {
optionsFileContents, readErr := afero.ReadFile(gs.FS, optionsFilePath)
optionsFileContents, readErr := fsext.ReadFile(gs.FS, optionsFilePath)
if readErr != nil {
return readErr
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"testing"

"github.com/pmezard/go-difflib/difflib"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/lib/fsext"
)

const testHAR = `
Expand Down Expand Up @@ -109,15 +109,15 @@ func TestConvertCmdCorrelate(t *testing.T) {
require.NoError(t, err)

ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, "correlate.har", har, 0o644))
require.NoError(t, fsext.WriteFile(ts.FS, "correlate.har", har, 0o644))
ts.CmdArgs = []string{
"k6", "convert", "--output=result.js", "--correlate=true", "--no-batch=true",
"--enable-status-code-checks=true", "--return-on-failed-check=true", "correlate.har",
}

newRootCommand(ts.GlobalState).execute()

result, err := afero.ReadFile(ts.FS, "result.js")
result, err := fsext.ReadFile(ts.FS, "result.js")
require.NoError(t, err)

// Sanitizing to avoid windows problems with carriage returns
Expand All @@ -144,7 +144,7 @@ func TestConvertCmdCorrelate(t *testing.T) {
func TestConvertCmdStdout(t *testing.T) {
t.Parallel()
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, "stdout.har", []byte(testHAR), 0o644))
require.NoError(t, fsext.WriteFile(ts.FS, "stdout.har", []byte(testHAR), 0o644))
ts.CmdArgs = []string{"k6", "convert", "stdout.har"}

newRootCommand(ts.GlobalState).execute()
Expand All @@ -155,12 +155,12 @@ func TestConvertCmdOutputFile(t *testing.T) {
t.Parallel()

ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, "output.har", []byte(testHAR), 0o644))
require.NoError(t, fsext.WriteFile(ts.FS, "output.har", []byte(testHAR), 0o644))
ts.CmdArgs = []string{"k6", "convert", "--output", "result.js", "output.har"}

newRootCommand(ts.GlobalState).execute()

output, err := afero.ReadFile(ts.FS, "result.js")
output, err := fsext.ReadFile(ts.FS, "result.js")
assert.NoError(t, err)
assert.Equal(t, testHARConvertResult, string(output))
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/panic_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"testing"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib/fsext"
"go.k6.io/k6/lib/testutils"
)

Expand Down Expand Up @@ -86,7 +86,7 @@ func TestRunScriptPanicsErrorsAndAbort(t *testing.T) {

testFilename := "script.js"
ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, testFilename), []byte(tc.testScript), 0o644))
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, testFilename), []byte(tc.testScript), 0o644))
ts.CmdArgs = []string{"k6", "run", testFilename}

ts.ExpectedExitCode = int(exitcodes.ScriptAborted)
Expand Down
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"time"

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

Expand All @@ -28,6 +27,7 @@ import (
"go.k6.io/k6/js/common"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/lib/fsext"
"go.k6.io/k6/metrics"
"go.k6.io/k6/metrics/engine"
"go.k6.io/k6/output"
Expand Down Expand Up @@ -422,7 +422,7 @@ func reportUsage(ctx context.Context, execScheduler *execution.Scheduler) error
return err
}

func handleSummaryResult(fs afero.Fs, stdOut, stdErr io.Writer, result map[string]io.Reader) error {
func handleSummaryResult(fs fsext.Fs, stdOut, stdErr io.Writer, result map[string]io.Reader) error {
var errs []error

getWriter := func(path string) (io.Writer, error) {
Expand Down
17 changes: 8 additions & 9 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"testing"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -38,20 +37,20 @@ func (fw mockWriter) Write(p []byte) (n int, err error) {

var _ io.Writer = mockWriter{}

func getFiles(t *testing.T, fileSystem afero.Fs) map[string]*bytes.Buffer {
func getFiles(t *testing.T, fileSystem fsext.Fs) map[string]*bytes.Buffer {
result := map[string]*bytes.Buffer{}
walkFn := func(filePath string, _ fs.FileInfo, err error) error {
if filePath == "/" || filePath == "\\" {
return nil
}
require.NoError(t, err)
contents, err := afero.ReadFile(fileSystem, filePath)
contents, err := fsext.ReadFile(fileSystem, filePath)
require.NoError(t, err)
result[filePath] = bytes.NewBuffer(contents)
return nil
}

err := fsext.Walk(fileSystem, afero.FilePathSeparator, filepath.WalkFunc(walkFn))
err := fsext.Walk(fileSystem, fsext.FilePathSeparator, filepath.WalkFunc(walkFn))
require.NoError(t, err)

return result
Expand All @@ -64,9 +63,9 @@ func assertEqual(t *testing.T, exp string, actual io.Reader) {
}

func initVars() (
content map[string]io.Reader, stdout *bytes.Buffer, stderr *bytes.Buffer, fs afero.Fs,
content map[string]io.Reader, stdout *bytes.Buffer, stderr *bytes.Buffer, fs fsext.Fs,
) {
return map[string]io.Reader{}, bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{}), afero.NewMemMapFs()
return map[string]io.Reader{}, bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{}), fsext.NewMemMapFs()
}

func TestHandleSummaryResultSimple(t *testing.T) {
Expand Down Expand Up @@ -201,7 +200,7 @@ func TestRunScriptErrorsAndAbort(t *testing.T) {
require.NoError(t, err)

ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))
ts.CmdArgs = append([]string{"k6", "run", tc.testFilename}, tc.extraArgs...)

ts.ExpectedExitCode = int(tc.expExitCode)
Expand Down Expand Up @@ -256,7 +255,7 @@ func TestInvalidOptionsThresholdErrExitCode(t *testing.T) {
require.NoError(t, err)

ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))
ts.CmdArgs = append([]string{"k6", "run", tc.testFilename}, tc.extraArgs...)

ts.ExpectedExitCode = int(tc.expExitCode)
Expand Down Expand Up @@ -306,7 +305,7 @@ func TestThresholdsRuntimeBehavior(t *testing.T) {
require.NoError(t, err)

ts := tests.NewGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))

ts.CmdArgs = []string{"k6", "run", tc.testFilename}
ts.ExpectedExitCode = int(tc.expExitCode)
Expand Down
Loading