Skip to content

Commit

Permalink
Add testutils.MakeMemMapFs test helper (#3933)
Browse files Browse the repository at this point in the history
* Add MakeMemMapFs to testutils package

While working on another PR I was looking for a way to quickly
initialize a test Fs. I found in the codebase occurences of a
makeMemMapFs function, which was already duplicated twice in the
codebase.

Instead of adding a third copy, I thought I would instead make testutil
out of it. This is exactly what this commit does.

* Replace existing makeMemMapFs occurences and use with testutils version
  • Loading branch information
oleiade authored Sep 6, 2024
1 parent f495046 commit 007660a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 31 deletions.
24 changes: 9 additions & 15 deletions lib/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"runtime"
"testing"

"go.k6.io/k6/lib/testutils"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"
Expand Down Expand Up @@ -52,14 +54,6 @@ func TestNormalizeAndAnonymizePath(t *testing.T) {
}
}

func makeMemMapFs(t *testing.T, input map[string][]byte) fsext.Fs {
fs := fsext.NewMemMapFs()
for path, data := range input {
require.NoError(t, fsext.WriteFile(fs, path, data, 0o644))
}
return fs
}

func getMapKeys(m map[string]fsext.Fs) []string {
keys := make([]string, 0, len(m))
for key := range m {
Expand Down Expand Up @@ -129,13 +123,13 @@ func TestArchiveReadWrite(t *testing.T) {
Data: []byte(`// a contents`),
PwdURL: &url.URL{Scheme: "file", Path: "/path/to"},
Filesystems: map[string]fsext.Fs{
"file": makeMemMapFs(t, map[string][]byte{
"file": testutils.MakeMemMapFs(t, map[string][]byte{
"/path/to/a.js": []byte(`// a contents`),
"/path/to/b.js": []byte(`// b contents`),
"/path/to/file1.txt": []byte(`hi!`),
"/path/to/file2.txt": []byte(`bye!`),
}),
"https": makeMemMapFs(t, map[string][]byte{
"https": testutils.MakeMemMapFs(t, map[string][]byte{
"/cdnjs.com/libraries/Faker": []byte(`// faker contents`),
"/github.com/loadimpact/k6/README.md": []byte(`README`),
}),
Expand Down Expand Up @@ -181,13 +175,13 @@ func TestArchiveReadWrite(t *testing.T) {
Data: []byte(`// a contents`),
PwdURL: &url.URL{Scheme: "file", Path: entry.Pwd},
Filesystems: map[string]fsext.Fs{
"file": makeMemMapFs(t, map[string][]byte{
"file": testutils.MakeMemMapFs(t, map[string][]byte{
fmt.Sprintf("%s/a.js", entry.Pwd): []byte(`// a contents`),
fmt.Sprintf("%s/b.js", entry.Pwd): []byte(`// b contents`),
fmt.Sprintf("%s/file1.txt", entry.Pwd): []byte(`hi!`),
fmt.Sprintf("%s/file2.txt", entry.Pwd): []byte(`bye!`),
}),
"https": makeMemMapFs(t, map[string][]byte{
"https": testutils.MakeMemMapFs(t, map[string][]byte{
"/cdnjs.com/libraries/Faker": []byte(`// faker contents`),
"/github.com/loadimpact/k6/README.md": []byte(`README`),
}),
Expand All @@ -205,13 +199,13 @@ func TestArchiveReadWrite(t *testing.T) {
PwdURL: &url.URL{Scheme: "file", Path: entry.PwdNormAnon},

Filesystems: map[string]fsext.Fs{
"file": makeMemMapFs(t, map[string][]byte{
"file": testutils.MakeMemMapFs(t, map[string][]byte{
fmt.Sprintf("%s/a.js", entry.PwdNormAnon): []byte(`// a contents`),
fmt.Sprintf("%s/b.js", entry.PwdNormAnon): []byte(`// b contents`),
fmt.Sprintf("%s/file1.txt", entry.PwdNormAnon): []byte(`hi!`),
fmt.Sprintf("%s/file2.txt", entry.PwdNormAnon): []byte(`bye!`),
}),
"https": makeMemMapFs(t, map[string][]byte{
"https": testutils.MakeMemMapFs(t, map[string][]byte{
"/cdnjs.com/libraries/Faker": []byte(`// faker contents`),
"/github.com/loadimpact/k6/README.md": []byte(`README`),
}),
Expand Down Expand Up @@ -335,7 +329,7 @@ func TestStrangePaths(t *testing.T) {
Data: []byte(`// ` + pathToChange + ` contents`),
PwdURL: &url.URL{Scheme: "file", Path: path.Dir(pathToChange)},
Filesystems: map[string]fsext.Fs{
"file": makeMemMapFs(t, otherMap),
"file": testutils.MakeMemMapFs(t, otherMap),
},
}

Expand Down
13 changes: 3 additions & 10 deletions lib/executor/executors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"time"

"go.k6.io/k6/lib/testutils"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"
Expand Down Expand Up @@ -492,7 +494,7 @@ func TestArchiveRoundTripExecutorConfig(t *testing.T) {
Data: []byte(`// a contents`),
PwdURL: &url.URL{Scheme: "file", Path: "/path/to"},
Filesystems: map[string]fsext.Fs{
"file": makeMemMapFs(t, map[string][]byte{
"file": testutils.MakeMemMapFs(t, map[string][]byte{
"/path/to/a.js": []byte(`// a contents`),
}),
},
Expand All @@ -511,12 +513,3 @@ func TestArchiveRoundTripExecutorConfig(t *testing.T) {

assert.EqualValues(t, execCfg, execCfg2)
}

// copied from lib/archive_test.go
func makeMemMapFs(t *testing.T, input map[string][]byte) fsext.Fs {
fs := fsext.NewMemMapFs()
for path, data := range input {
require.NoError(t, fsext.WriteFile(fs, path, data, 0o644))
}
return fs
}
14 changes: 8 additions & 6 deletions lib/old_archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"path/filepath"
"testing"

"go.k6.io/k6/lib/testutils"

"github.com/stretchr/testify/require"

"go.k6.io/k6/lib/fsext"
Expand Down Expand Up @@ -70,7 +72,7 @@ func TestOldArchive(t *testing.T) {
t.Run(filename, func(t *testing.T) {
t.Parallel()
metadata := `{"filename": "` + filename + `", "options": {}}`
fs := makeMemMapFs(t, map[string][]byte{
fs := testutils.MakeMemMapFs(t, map[string][]byte{
// files
"/files/example.com/path/to.js": []byte(`example.com file`),
"/files/_/C/something/path": []byte(`windows file`),
Expand All @@ -88,13 +90,13 @@ func TestOldArchive(t *testing.T) {
require.NoError(t, err)

expectedFilesystems := map[string]fsext.Fs{
"file": makeMemMapFs(t, map[string][]byte{
"file": testutils.MakeMemMapFs(t, map[string][]byte{
"/C:/something/path": []byte(`windows file`),
"/absolulte/path": []byte(`unix file`),
"/C:/something/path2": []byte(`windows script`),
"/absolulte/path2": []byte(`unix script`),
}),
"https": makeMemMapFs(t, map[string][]byte{
"https": testutils.MakeMemMapFs(t, map[string][]byte{
"/example.com/path/to.js": []byte(`example.com file`),
"/example.com/path/too.js": []byte(`example.com script`),
}),
Expand All @@ -110,7 +112,7 @@ func TestOldArchive(t *testing.T) {

func TestUnknownPrefix(t *testing.T) {
t.Parallel()
fs := makeMemMapFs(t, map[string][]byte{
fs := testutils.MakeMemMapFs(t, map[string][]byte{
"/strange/something": []byte(`github file`),
})
buf, err := dumpMemMapFsToBuf(fs)
Expand Down Expand Up @@ -174,7 +176,7 @@ func TestFilenamePwdResolve(t *testing.T) {
"options": {}
}`

buf, err := dumpMemMapFsToBuf(makeMemMapFs(t, map[string][]byte{
buf, err := dumpMemMapFsToBuf(testutils.MakeMemMapFs(t, map[string][]byte{
"/metadata.json": []byte(metadata),
}))
require.NoError(t, err)
Expand Down Expand Up @@ -241,7 +243,7 @@ func TestDerivedExecutionDiscarding(t *testing.T) {
}

for _, test := range tests {
buf, err := dumpMemMapFsToBuf(makeMemMapFs(t, map[string][]byte{
buf, err := dumpMemMapFsToBuf(testutils.MakeMemMapFs(t, map[string][]byte{
"/metadata.json": []byte(test.metadata),
}))
require.NoError(t, err)
Expand Down
28 changes: 28 additions & 0 deletions lib/testutils/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package testutils

import (
"testing"

"github.com/stretchr/testify/require"
"go.k6.io/k6/lib/fsext"
)

// MakeMemMapFs creates a new in-memory filesystem with the given files.
//
// It is particularly useful for testing code that interacts with the
// filesystem, as it allows to create a filesystem with a known state
// without having to create temporary directories and files on disk.
//
// The keys of the withFiles map are the paths of the files to create, and the
// values are the contents of the files. The files are created with 644 mode.
//
// The filesystem is returned as a [fsext.Fs].
func MakeMemMapFs(t *testing.T, withFiles map[string][]byte) fsext.Fs {
fs := fsext.NewMemMapFs()

for path, data := range withFiles {
require.NoError(t, fsext.WriteFile(fs, path, data, 0o644))
}

return fs
}

0 comments on commit 007660a

Please sign in to comment.