From 0448c6dfcbabd47dc669263b1d1763f8afb4093b Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sun, 9 Jan 2022 14:18:10 -0700 Subject: [PATCH] fs: Return ErrNotExist in ArchiveFS Other minor fixes --- archiver_test.go | 27 --------------------------- fs.go | 5 ++++- fs_test.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 fs_test.go diff --git a/archiver_test.go b/archiver_test.go index f0df1f30..27f10dfe 100644 --- a/archiver_test.go +++ b/archiver_test.go @@ -113,30 +113,3 @@ func TestSkipList(t *testing.T) { } } } - -func TestPathWithoutTopDir(t *testing.T) { - for i, tc := range []struct { - input, expect string - }{ - { - input: "a/b/c", - expect: "b/c", - }, - { - input: "b/c", - expect: "c", - }, - { - input: "c", - expect: "", - }, - { - input: "", - expect: "", - }, - } { - if actual := pathWithoutTopDir(tc.input); actual != tc.expect { - t.Errorf("Test %d (input=%s): Expected '%s' but got '%s'", i, tc.input, tc.expect, actual) - } - } -} diff --git a/fs.go b/fs.go index 3bf6b9aa..d4d9f4aa 100644 --- a/fs.go +++ b/fs.go @@ -308,6 +308,9 @@ func (f ArchiveFS) Open(name string) (fs.File, error) { if err != nil { return nil, err } + if fsFile == nil { + return nil, fs.ErrNotExist + } return fsFile, nil } @@ -472,7 +475,7 @@ func TopDirReadDir(fsys fs.ReadDirFS, name string) ([]fs.DirEntry, error) { func pathWithoutTopDir(fpath string) string { slashIdx := strings.Index(fpath, "/") if slashIdx < 0 { - return "" + return fpath } return fpath[slashIdx+1:] } diff --git a/fs_test.go b/fs_test.go new file mode 100644 index 00000000..8319249d --- /dev/null +++ b/fs_test.go @@ -0,0 +1,30 @@ +package archiver + +import "testing" + +func TestPathWithoutTopDir(t *testing.T) { + for i, tc := range []struct { + input, expect string + }{ + { + input: "a/b/c", + expect: "b/c", + }, + { + input: "b/c", + expect: "c", + }, + { + input: "c", + expect: "c", + }, + { + input: "", + expect: "", + }, + } { + if actual := pathWithoutTopDir(tc.input); actual != tc.expect { + t.Errorf("Test %d (input=%s): Expected '%s' but got '%s'", i, tc.input, tc.expect, actual) + } + } +}