-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(go): remove experimental FS API usage in Wasm (#3299)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
- Loading branch information
Showing
3 changed files
with
90 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package module | ||
|
||
import ( | ||
"io" | ||
"io/fs" | ||
"path/filepath" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
dio "github.com/aquasecurity/go-dep-parser/pkg/io" | ||
"github.com/aquasecurity/memoryfs" | ||
) | ||
|
||
// memFS is a wrapper of memoryfs.FS and can change its underlying file system | ||
// at runtime. This implements fs.FS. | ||
type memFS struct { | ||
current *memoryfs.FS | ||
} | ||
|
||
// Open implements fs.FS. | ||
func (m *memFS) Open(name string) (fs.File, error) { | ||
return m.current.Open(name) | ||
} | ||
|
||
// initialize changes the underlying memory file system with the given file path and contents. | ||
// | ||
// Note: it is always to safe swap the underlying FS with this API since this is called only at the beginning of | ||
// Analyze interface call, which is not concurrently called per module instance. | ||
func (m *memFS) initialize(filePath string, content dio.ReadSeekerAt) (err error) { | ||
memfs := memoryfs.New() | ||
if err = memfs.MkdirAll(filepath.Dir(filePath), fs.ModePerm); err != nil { | ||
return xerrors.Errorf("memory fs mkdir error: %w", err) | ||
} | ||
err = memfs.WriteLazyFile(filePath, func() (io.Reader, error) { | ||
return content, nil | ||
}, fs.ModePerm) | ||
if err != nil { | ||
return xerrors.Errorf("memory fs write error: %w", err) | ||
} | ||
|
||
m.current = memfs | ||
return | ||
} |
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,33 @@ | ||
package module | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMemFS(t *testing.T) { | ||
m := &memFS{} | ||
require.Nil(t, m.current) | ||
|
||
const path, content = "/usr/foo/bar.txt", "my-content" | ||
err := m.initialize(path, strings.NewReader(content)) | ||
require.NoError(t, err) | ||
require.NotNil(t, m.current) | ||
|
||
t.Run("happy", func(t *testing.T) { | ||
f, err := m.Open(path) | ||
require.NoError(t, err) | ||
actual, err := io.ReadAll(f) | ||
require.NoError(t, err) | ||
require.Equal(t, content, string(actual)) | ||
}) | ||
|
||
t.Run("not found", func(t *testing.T) { | ||
_, err = m.Open(path + "tmp") | ||
require.ErrorIs(t, err, os.ErrNotExist) | ||
}) | ||
} |
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