-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add testutils.MakeMemMapFs test helper (#3933)
* 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
Showing
4 changed files
with
48 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |