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

Add testutils.MakeMemMapFs test helper #3933

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Changes from 1 commit
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
Next Next commit
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.
  • Loading branch information
oleiade committed Sep 5, 2024
commit 674631b88cf889e4f80d7466b4e109e4a89bcd9b
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
}