Skip to content

Commit

Permalink
Rename system.FS to system.IPFS.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Aug 28, 2024
1 parent ed7ddf6 commit ef24094
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
20 changes: 10 additions & 10 deletions system/fs.go → system/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import (
"github.com/pkg/errors"
)

var _ fs.FS = (*FS)(nil)
var _ fs.FS = (*IPFS)(nil)

// An FS provides access to a hierarchical file system.
// An IPFS provides access to a hierarchical file system.
//
// The FS interface is the minimum implementation required of the file system.
// The IPFS interface is the minimum implementation required of the file system.
// A file system may implement additional interfaces,
// such as [ReadFileFS], to provide additional or optimized functionality.
//
// [testing/fstest.TestFS] may be used to test implementations of an FS for
// [testing/fstest.TestFS] may be used to test implementations of an IPFS for
// correctness.
type FS struct {
type IPFS struct {
Ctx context.Context
Root path.Path
Unix iface.UnixfsAPI
Expand All @@ -39,7 +39,7 @@ type FS struct {
// Open should reject attempts to open names that do not satisfy
// fs.ValidPath(name), returning a *fs.PathError with Err set to
// fs.ErrInvalid or fs.ErrNotExist.
func (f FS) Open(name string) (fs.File, error) {
func (f IPFS) Open(name string) (fs.File, error) {
path, node, err := f.Resolve(f.Ctx, name)
if err != nil {
return nil, &fs.PathError{
Expand All @@ -55,7 +55,7 @@ func (f FS) Open(name string) (fs.File, error) {
}, nil
}

func (f FS) Resolve(ctx context.Context, name string) (path.Path, files.Node, error) {
func (f IPFS) Resolve(ctx context.Context, name string) (path.Path, files.Node, error) {
if pathInvalid(name) {
return nil, nil, fs.ErrInvalid
}
Expand All @@ -73,16 +73,16 @@ func pathInvalid(name string) bool {
return !fs.ValidPath(name)
}

func (f FS) Sub(dir string) (fs.FS, error) {
func (f IPFS) Sub(dir string) (fs.FS, error) {
var root path.Path
var err error
if (f == FS{}) {
if (f == IPFS{}) {
root, err = path.NewPath(dir)
} else {
root, err = path.Join(f.Root, dir)
}

return &FS{
return &IPFS{
Ctx: f.Ctx,
Root: root,
Unix: f.Unix,
Expand Down
45 changes: 25 additions & 20 deletions system/fs_test.go → system/ipfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import (

const IPFS_ROOT = "/ipfs/QmRecDLNaESeNY3oUFYZKK9ftdANBB8kuLaMdAXMD43yon" // go/system/testdata/fs

func TestFS(t *testing.T) {
// TestIPFS_Env verifies that an IPFS node is available in the
// host environment, and that it exports IPFS_ROOT. It then
// checks that IPFS_ROOT contains the expected directory structure.
//
// Other tests in this file will likely fail if TestIPFS_Env fails.
func TestIPFS_Env(t *testing.T) {
t.Parallel()

root, err := path.NewPath(IPFS_ROOT)
Expand All @@ -23,15 +28,20 @@ func TestFS(t *testing.T) {
ipfs, err := rpc.NewLocalApi()
require.NoError(t, err)

fs := system.FS{Ctx: context.Background(), Unix: ipfs.Unixfs(), Root: root}
err = fstest.TestFS(fs,
"main.go",
"main.wasm",
"testdata")
dir, err := ipfs.Unixfs().Ls(context.Background(), root)
require.NoError(t, err)

var names []string
for entry := range dir {
names = append(names, entry.Name)
}

expect := []string{"testdata", "main.go", "main.wasm"}
require.ElementsMatch(t, names, expect,
"unexpected file path")
}

func TestIPFSNode(t *testing.T) {
func TestIPFS_FS(t *testing.T) {
t.Parallel()

root, err := path.NewPath(IPFS_ROOT)
Expand All @@ -40,23 +50,18 @@ func TestIPFSNode(t *testing.T) {
ipfs, err := rpc.NewLocalApi()
require.NoError(t, err)

dir, err := ipfs.Unixfs().Ls(context.Background(), root)
fs := system.IPFS{Ctx: context.Background(), Unix: ipfs.Unixfs(), Root: root}
err = fstest.TestFS(fs,
"main.go",
"main.wasm",
"testdata")
require.NoError(t, err)

var names []string
for entry := range dir {
names = append(names, entry.Name)
}

expect := []string{"testdata", "main.go", "main.wasm"}
require.ElementsMatch(t, names, expect,
"unexpected file path")
}

// TestSubFS ensures that the filesystem retunred by fs.Sub correctly
// TestIPFS_SubFS ensures that the filesystem retunred by fs.Sub correctly
// handles the '.' path. The returned filesystem MUST ensure that '.'
// resolves to the root IPFS path.
func TestSubFS(t *testing.T) {
func TestIPFS_SubFS(t *testing.T) {
t.Parallel()

root, err := path.NewPath("/ipfs/QmSAyttKvYkSCBTghuMxAJaBZC3jD2XLRCQ5FB3CTrb9rE") // go/system/testdata
Expand All @@ -65,7 +70,7 @@ func TestSubFS(t *testing.T) {
ipfs, err := rpc.NewLocalApi()
require.NoError(t, err)

fs, err := fs.Sub(system.FS{Ctx: context.Background(), Unix: ipfs.Unixfs(), Root: root}, "fs")
fs, err := fs.Sub(system.IPFS{Ctx: context.Background(), Unix: ipfs.Unixfs(), Root: root}, "fs")
require.NoError(t, err)
require.NotNil(t, fs)

Expand Down

0 comments on commit ef24094

Please sign in to comment.