Skip to content

Commit ec094ce

Browse files
committed
Hide afero behind fsext package
This is mostly so that no other part by `fsext` knows anything about afero. In the future we can replace the actual implementation. Updates #1079
1 parent e7b1c62 commit ec094ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+403
-361
lines changed

cmd/archive_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
"syscall"
1313
"testing"
1414

15-
"github.com/spf13/afero"
1615
"github.com/stretchr/testify/require"
1716
"go.k6.io/k6/cmd/tests"
1817
"go.k6.io/k6/errext/exitcodes"
18+
"go.k6.io/k6/lib/fsext"
1919
)
2020

2121
func TestArchiveThresholds(t *testing.T) {
@@ -81,7 +81,7 @@ func TestArchiveThresholds(t *testing.T) {
8181
require.NoError(t, err)
8282

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

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

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

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

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

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

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

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

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

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

162-
archiveFile, err := afero.ReadFile(fileSystem, fileName)
162+
archiveFile, err := fsext.ReadFile(fileSystem, fileName)
163163
if err != nil {
164164
return err
165165
}

cmd/config.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/mstoykov/envconfig"
1313
"github.com/sirupsen/logrus"
14-
"github.com/spf13/afero"
1514
"github.com/spf13/pflag"
1615
"gopkg.in/guregu/null.v3"
1716

@@ -20,6 +19,7 @@ import (
2019
"go.k6.io/k6/errext/exitcodes"
2120
"go.k6.io/k6/lib"
2221
"go.k6.io/k6/lib/executor"
22+
"go.k6.io/k6/lib/fsext"
2323
"go.k6.io/k6/lib/types"
2424
"go.k6.io/k6/metrics"
2525
)
@@ -116,7 +116,7 @@ func readDiskConfig(gs *state.GlobalState) (Config, error) {
116116
return Config{}, err
117117
}
118118

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

143-
return afero.WriteFile(gs.FS, gs.Flags.ConfigFilePath, data, 0o644)
143+
return fsext.WriteFile(gs.FS, gs.Flags.ConfigFilePath, data, 0o644)
144144
}
145145

146146
// Reads configuration variables from the environment.

cmd/config_consolidation_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/spf13/afero"
98
"github.com/stretchr/testify/assert"
109
"github.com/stretchr/testify/require"
1110
"gopkg.in/guregu/null.v3"
@@ -14,6 +13,7 @@ import (
1413
"go.k6.io/k6/cmd/tests"
1514
"go.k6.io/k6/lib"
1615
"go.k6.io/k6/lib/executor"
16+
"go.k6.io/k6/lib/fsext"
1717
"go.k6.io/k6/lib/types"
1818
"go.k6.io/k6/metrics"
1919
)
@@ -109,10 +109,10 @@ type file struct {
109109
filepath, contents string
110110
}
111111

112-
func getFS(files []file) afero.Fs {
113-
fs := afero.NewMemMapFs()
112+
func getFS(files []file) fsext.Fs {
113+
fs := fsext.NewMemMapFs()
114114
for _, f := range files {
115-
must(afero.WriteFile(fs, f.filepath, []byte(f.contents), 0o644)) // modes don't matter in the afero.MemMapFs
115+
must(fsext.WriteFile(fs, f.filepath, []byte(f.contents), 0o644)) // modes don't matter in the afero.MemMapFs
116116
}
117117
return fs
118118
}
@@ -121,7 +121,7 @@ type opts struct {
121121
cli []string
122122
env []string
123123
runner *lib.Options
124-
fs afero.Fs
124+
fs fsext.Fs
125125
cmds []string
126126
}
127127

@@ -145,7 +145,7 @@ type configConsolidationTestCase struct {
145145

146146
func getConfigConsolidationTestCases() []configConsolidationTestCase {
147147
defaultFlags := state.GetDefaultFlags(".config")
148-
defaultConfig := func(jsonConfig string) afero.Fs {
148+
defaultConfig := func(jsonConfig string) fsext.Fs {
149149
return getFS([]file{{defaultFlags.ConfigFilePath, jsonConfig}})
150150
}
151151
I := null.IntFrom // shortcut for "Valid" (i.e. user-specified) ints

cmd/convert.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"encoding/json"
55
"io"
66

7-
"github.com/spf13/afero"
87
"github.com/spf13/cobra"
98
"gopkg.in/guregu/null.v3"
109

1110
"go.k6.io/k6/cmd/state"
1211
"go.k6.io/k6/converter/har"
1312
"go.k6.io/k6/lib"
13+
"go.k6.io/k6/lib/fsext"
1414
)
1515

1616
// TODO: split apart like `k6 run` and `k6 archive`?
@@ -34,13 +34,13 @@ func getCmdConvert(gs *state.GlobalState) *cobra.Command {
3434
exampleText := getExampleText(gs, `
3535
# Convert a HAR file to a k6 script.
3636
{{.}} convert -O har-session.js session.har
37-
37+
3838
# Convert a HAR file to a k6 script creating requests only for the given domain/s.
3939
{{.}} convert -O har-session.js --only yourdomain.com,additionaldomain.com session.har
40-
40+
4141
# Convert a HAR file. Batching requests together as long as idle time between requests <800ms
4242
{{.}} convert --batch-threshold 800 session.har
43-
43+
4444
# Run the k6 script.
4545
{{.}} run har-session.js`[1:])
4646

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

7171
if optionsFilePath != "" {
72-
optionsFileContents, readErr := afero.ReadFile(gs.FS, optionsFilePath)
72+
optionsFileContents, readErr := fsext.ReadFile(gs.FS, optionsFilePath)
7373
if readErr != nil {
7474
return readErr
7575
}

cmd/convert_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"testing"
77

88
"github.com/pmezard/go-difflib/difflib"
9-
"github.com/spf13/afero"
109
"github.com/stretchr/testify/assert"
1110
"github.com/stretchr/testify/require"
1211
"go.k6.io/k6/cmd/tests"
12+
"go.k6.io/k6/lib/fsext"
1313
)
1414

1515
const testHAR = `
@@ -109,15 +109,15 @@ func TestConvertCmdCorrelate(t *testing.T) {
109109
require.NoError(t, err)
110110

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

118118
newRootCommand(ts.GlobalState).execute()
119119

120-
result, err := afero.ReadFile(ts.FS, "result.js")
120+
result, err := fsext.ReadFile(ts.FS, "result.js")
121121
require.NoError(t, err)
122122

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

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

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

161161
newRootCommand(ts.GlobalState).execute()
162162

163-
output, err := afero.ReadFile(ts.FS, "result.js")
163+
output, err := fsext.ReadFile(ts.FS, "result.js")
164164
assert.NoError(t, err)
165165
assert.Equal(t, testHARConvertResult, string(output))
166166
}

cmd/panic_integration_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"testing"
88

99
"github.com/sirupsen/logrus"
10-
"github.com/spf13/afero"
1110
"github.com/stretchr/testify/assert"
1211
"github.com/stretchr/testify/require"
1312
"go.k6.io/k6/cmd/tests"
1413
"go.k6.io/k6/errext/exitcodes"
1514
"go.k6.io/k6/js/modules"
15+
"go.k6.io/k6/lib/fsext"
1616
"go.k6.io/k6/lib/testutils"
1717
)
1818

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

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

9292
ts.ExpectedExitCode = int(exitcodes.ScriptAborted)

cmd/run.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"time"
1717

1818
"github.com/sirupsen/logrus"
19-
"github.com/spf13/afero"
2019
"github.com/spf13/cobra"
2120
"github.com/spf13/pflag"
2221

@@ -28,6 +27,7 @@ import (
2827
"go.k6.io/k6/js/common"
2928
"go.k6.io/k6/lib"
3029
"go.k6.io/k6/lib/consts"
30+
"go.k6.io/k6/lib/fsext"
3131
"go.k6.io/k6/metrics"
3232
"go.k6.io/k6/metrics/engine"
3333
"go.k6.io/k6/output"
@@ -422,7 +422,7 @@ func reportUsage(ctx context.Context, execScheduler *execution.Scheduler) error
422422
return err
423423
}
424424

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

428428
getWriter := func(path string) (io.Writer, error) {

cmd/run_test.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"testing"
1414

1515
"github.com/sirupsen/logrus"
16-
"github.com/spf13/afero"
1716
"github.com/stretchr/testify/assert"
1817
"github.com/stretchr/testify/require"
1918

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

3938
var _ io.Writer = mockWriter{}
4039

41-
func getFiles(t *testing.T, fileSystem afero.Fs) map[string]*bytes.Buffer {
40+
func getFiles(t *testing.T, fileSystem fsext.Fs) map[string]*bytes.Buffer {
4241
result := map[string]*bytes.Buffer{}
4342
walkFn := func(filePath string, _ fs.FileInfo, err error) error {
4443
if filePath == "/" || filePath == "\\" {
4544
return nil
4645
}
4746
require.NoError(t, err)
48-
contents, err := afero.ReadFile(fileSystem, filePath)
47+
contents, err := fsext.ReadFile(fileSystem, filePath)
4948
require.NoError(t, err)
5049
result[filePath] = bytes.NewBuffer(contents)
5150
return nil
5251
}
5352

54-
err := fsext.Walk(fileSystem, afero.FilePathSeparator, filepath.WalkFunc(walkFn))
53+
err := fsext.Walk(fileSystem, fsext.FilePathSeparator, filepath.WalkFunc(walkFn))
5554
require.NoError(t, err)
5655

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

6665
func initVars() (
67-
content map[string]io.Reader, stdout *bytes.Buffer, stderr *bytes.Buffer, fs afero.Fs,
66+
content map[string]io.Reader, stdout *bytes.Buffer, stderr *bytes.Buffer, fs fsext.Fs,
6867
) {
69-
return map[string]io.Reader{}, bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{}), afero.NewMemMapFs()
68+
return map[string]io.Reader{}, bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{}), fsext.NewMemMapFs()
7069
}
7170

7271
func TestHandleSummaryResultSimple(t *testing.T) {
@@ -201,7 +200,7 @@ func TestRunScriptErrorsAndAbort(t *testing.T) {
201200
require.NoError(t, err)
202201

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

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

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

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

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

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

0 commit comments

Comments
 (0)