diff --git a/system/fs.go b/system/ipfs.go similarity index 93% rename from system/fs.go rename to system/ipfs.go index 4b90513..fbfbe3b 100644 --- a/system/fs.go +++ b/system/ipfs.go @@ -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 @@ -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{ @@ -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 } @@ -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, diff --git a/system/fs_test.go b/system/ipfs_test.go similarity index 69% rename from system/fs_test.go rename to system/ipfs_test.go index f06e0a9..1aaa189 100644 --- a/system/fs_test.go +++ b/system/ipfs_test.go @@ -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) @@ -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) @@ -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 @@ -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)