Skip to content

Commit 1061183

Browse files
committed
move fs bridge to launcher
Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
1 parent 312ea8d commit 1061183

File tree

4 files changed

+46
-59
lines changed

4 files changed

+46
-59
lines changed

internal/cmd/launcher.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"io/fs"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -20,6 +21,27 @@ import (
2021
"go.k6.io/k6/lib/fsext"
2122
)
2223

24+
// OFSBridge allows an afero.Fs to implement the Go standard library io/fs.FS.
25+
type iofSBridge struct {
26+
fsext fsext.Fs
27+
}
28+
29+
// newIofsBridge returns an IOFSBridge from a Fs
30+
func newIofsBridge(fs fsext.Fs) fs.FS {
31+
return &iofSBridge{
32+
fsext: fs,
33+
}
34+
}
35+
36+
// Open implements fs.Fs Open
37+
func (b *iofSBridge) Open(name string) (fs.File, error) {
38+
f, err := b.fsext.Open(name)
39+
if err != nil {
40+
return nil, fmt.Errorf("opening file: %w", err)
41+
}
42+
return f, nil
43+
}
44+
2345
// commandExecutor executes the requested k6 command line command.
2446
// It abstract the execution path from the concrete binary.
2547
type commandExecutor interface {
@@ -308,7 +330,7 @@ func analyze(gs *state.GlobalState, args []string) (k6deps.Dependencies, error)
308330
}
309331
dopts.Script.Name = sourceRootPath
310332
dopts.Script.Contents = src.Data
311-
dopts.Fs = fsext.NewIOFSBridge(gs.FS)
333+
dopts.Fs = newIofsBridge(gs.FS)
312334
}
313335

314336
return k6deps.Analyze(dopts)

internal/cmd/launcher_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"io"
8+
"io/fs"
79
"os"
810
"path/filepath"
911
"testing"
1012

1113
"github.com/grafana/k6deps"
1214
"github.com/spf13/cobra"
1315
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
1417
"go.k6.io/k6/cmd/state"
1518

1619
"go.k6.io/k6/errext"
@@ -394,3 +397,23 @@ func TestIsAnalysisRequired(t *testing.T) {
394397
})
395398
}
396399
}
400+
401+
func TestBridgeOpen(t *testing.T) {
402+
t.Parallel()
403+
404+
testfs := afero.NewMemMapFs()
405+
require.NoError(t, fsext.WriteFile(testfs, "abasicpath/onetwo.txt", []byte(`test123`), 0o644))
406+
407+
bridge := &iofSBridge{fsext: testfs}
408+
409+
// it asserts that bridge implements io/fs.FS
410+
goiofs := fs.FS(bridge)
411+
f, err := goiofs.Open("abasicpath/onetwo.txt")
412+
require.NoError(t, err)
413+
require.NotNil(t, f)
414+
415+
content, err := io.ReadAll(f)
416+
require.NoError(t, err)
417+
418+
assert.Equal(t, "test123", string(content))
419+
}

lib/fsext/bridge.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/fsext/bridge_test.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)