Skip to content

Commit

Permalink
Add seek tests covering happy case and fix SeekModeEnd starting offset
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Nov 10, 2023
1 parent ac8a5a7 commit d48fb67
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion js/modules/k6/experimental/fs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (f *file) Seek(offset int64, whence SeekMode) (int64, error) {
return 0, newFsError(TypeError, "offset cannot be positive when using SeekModeEnd")
}

newOffset = f.size() + offset
newOffset = (f.size() - 1) + offset
default:
return 0, newFsError(TypeError, "invalid seek mode")
}
Expand Down
3 changes: 1 addition & 2 deletions js/modules/k6/experimental/fs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,13 @@ func isUint8Array(rt *goja.Runtime, o *goja.Object) bool {
return true
}

//nolint:unparam
func exportInt(rt *goja.Runtime, v goja.Value) (int64, error) {
if common.IsNullish(v) {
return 0, errors.New("cannot be null or undefined")
}

var i int64
if err := rt.ExportTo(v, i); err != nil {
if err := rt.ExportTo(v, &i); err != nil {
return 0, errors.New("cannot be interpreted as integer")
}

Expand Down
35 changes: 35 additions & 0 deletions js/modules/k6/experimental/fs/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,41 @@ func TestFile(t *testing.T) {
assert.NoError(t, err)
})

t.Run("seek", func(t *testing.T) {
t.Parallel()

runtime, err := newConfiguredRuntime(t)
require.NoError(t, err)

testFilePath := fsext.FilePathSeparator + testFileName
fs := newTestFs(t, func(fs fsext.Fs) error {
return fsext.WriteFile(fs, testFilePath, []byte("012"), 0o644)
})
runtime.VU.InitEnvField.FileSystems["file"] = fs

_, err = runtime.RunOnEventLoop(wrapInAsyncLambda(fmt.Sprintf(`
const file = await fs.open(%q)
let newOffset = await file.seek(1, fs.SeekMode.Start)
if (newOffset != 1) {
throw "file.seek(1, fs.SeekMode.Start) returned unexpected offset: " + newOffset;
}
newOffset = await file.seek(-1, fs.SeekMode.Current)
if (newOffset != 0) {
throw "file.seek(-1, fs.SeekMode.Current) returned unexpected offset: " + newOffset;
}
newOffset = await file.seek(0, fs.SeekMode.End)
if (newOffset != 2) {
throw "file.seek(0, fs.SeekMode.End) returned unexpected offset: " + newOffset;
}
`, testFilePath)))

assert.NoError(t, err)
})

t.Run("seek with invalid arguments should fail", func(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit d48fb67

Please sign in to comment.