diff --git a/internal/fs/tarfs/tarfs.go b/internal/fs/tarfs/tarfs.go index 1a3de0c1..c059e261 100644 --- a/internal/fs/tarfs/tarfs.go +++ b/internal/fs/tarfs/tarfs.go @@ -154,8 +154,8 @@ func (tfs *TarFS) indexEntries() error { return nil } +// getEntryKey cleans the name and returns a key for the entry. func getEntryKey(name string) string { - // TODO: documentation return filepath.Clean(name) } diff --git a/internal/fs/tarfs/tarfs_test.go b/internal/fs/tarfs/tarfs_test.go index ab4325ee..9523e4f9 100644 --- a/internal/fs/tarfs/tarfs_test.go +++ b/internal/fs/tarfs/tarfs_test.go @@ -30,11 +30,9 @@ import ( === Contents of testdata/cleaned_path.tar === dir/ - dir/hello dir/subdir/ dir/subdir/world - foobar foobar_link foobar_symlink @@ -43,11 +41,9 @@ foobar_symlink ./ ./dir/ - ./dir/hello ./dir/subdir/ ./dir/subdir/world - ./foobar ./foobar_link ./foobar_symlink @@ -65,7 +61,7 @@ func TestTarFS_Open_Success(t *testing.T) { } for _, tarPath := range tarPaths { - t.Run("Should open successfully", func(t *testing.T) { + t.Run(tarPath, func(t *testing.T) { tfs, err := New(tarPath) if err != nil { t.Fatalf("New() error = %v, wantErr %v", err, nil) @@ -79,22 +75,23 @@ func TestTarFS_Open_Success(t *testing.T) { } for name, data := range testFiles { - f, err := tfs.Open(name) - if err != nil { - t.Fatalf("TarFS.Open(%s) error = %v, wantErr %v", name, err, nil) - continue - } - - got, err := io.ReadAll(f) - if err != nil { - t.Fatalf("failed to read %s: %v", name, err) - } - if err = f.Close(); err != nil { - t.Errorf("TarFS.Open(%s).Close() error = %v", name, err) - } - if want := data; !bytes.Equal(got, want) { - t.Errorf("TarFS.Open(%s) = %v, want %v", name, string(got), string(want)) - } + t.Run(name, func(t *testing.T) { + f, err := tfs.Open(name) + if err != nil { + t.Fatalf("TarFS.Open(%s) error = %v, wantErr %v", name, err, nil) + } + + got, err := io.ReadAll(f) + if err != nil { + t.Fatalf("failed to read %s: %v", name, err) + } + if err = f.Close(); err != nil { + t.Errorf("TarFS.Open(%s).Close() error = %v", name, err) + } + if want := data; !bytes.Equal(got, want) { + t.Errorf("TarFS.Open(%s) = %v, want %v", name, string(got), string(want)) + } + }) } }) } @@ -107,49 +104,50 @@ func TestTarFS_Open_MoreThanOnce(t *testing.T) { } for _, tarPath := range tarPaths { - tfs, err := New(tarPath) - if err != nil { - t.Fatalf("New() error = %v, wantErr %v", err, nil) - } - - name := "foobar" - data := []byte("foobar") - // open once - f1, err := tfs.Open(name) - if err != nil { - t.Fatalf("1st: TarFS.Open(%s) error = %v, wantErr %v", name, err, nil) - } - - got, err := io.ReadAll(f1) - if err != nil { - t.Fatalf("1st: failed to read %s: %v", name, err) - } - if want := data; !bytes.Equal(got, want) { - t.Errorf("1st: TarFS.Open(%s) = %v, want %v", name, string(got), string(want)) - } - - // open twice - f2, err := tfs.Open(name) - if err != nil { - t.Fatalf("2nd: TarFS.Open(%s) error = %v, wantErr %v", name, err, nil) - } - got, err = io.ReadAll(f2) - if err != nil { - t.Fatalf("2nd: failed to read %s: %v", name, err) - } - if want := data; !bytes.Equal(got, want) { - t.Errorf("2nd: TarFS.Open(%s) = %v, want %v", name, string(got), string(want)) - } - - // close - if err = f1.Close(); err != nil { - t.Errorf("1st TarFS.Open(%s).Close() error = %v", name, err) - } - if err = f2.Close(); err != nil { - t.Errorf("2nd TarFS.Open(%s).Close() error = %v", name, err) - } - } + t.Run(tarPath, func(t *testing.T) { + tfs, err := New(tarPath) + if err != nil { + t.Fatalf("New() error = %v, wantErr %v", err, nil) + } + + name := "foobar" + data := []byte("foobar") + // open once + f1, err := tfs.Open(name) + if err != nil { + t.Fatalf("1st: TarFS.Open(%s) error = %v, wantErr %v", name, err, nil) + } + + got, err := io.ReadAll(f1) + if err != nil { + t.Fatalf("1st: failed to read %s: %v", name, err) + } + if want := data; !bytes.Equal(got, want) { + t.Errorf("1st: TarFS.Open(%s) = %v, want %v", name, string(got), string(want)) + } + // open twice + f2, err := tfs.Open(name) + if err != nil { + t.Fatalf("2nd: TarFS.Open(%s) error = %v, wantErr %v", name, err, nil) + } + got, err = io.ReadAll(f2) + if err != nil { + t.Fatalf("2nd: failed to read %s: %v", name, err) + } + if want := data; !bytes.Equal(got, want) { + t.Errorf("2nd: TarFS.Open(%s) = %v, want %v", name, string(got), string(want)) + } + + // close + if err = f1.Close(); err != nil { + t.Errorf("1st TarFS.Open(%s).Close() error = %v", name, err) + } + if err = f2.Close(); err != nil { + t.Errorf("2nd TarFS.Open(%s).Close() error = %v", name, err) + } + }) + } } func TestTarFS_Open_NotExist(t *testing.T) { @@ -164,16 +162,20 @@ func TestTarFS_Open_NotExist(t *testing.T) { } for _, tarPath := range tarPaths { - tfs, err := New(tarPath) - if err != nil { - t.Fatalf("New() error = %v, wantErr %v", err, nil) - } - for _, name := range testFiles { - _, err := tfs.Open(name) - if want := fs.ErrNotExist; !errors.Is(err, want) { - t.Errorf("TarFS.Open(%s) error = %v, wantErr %v", name, err, want) + t.Run(tarPath, func(t *testing.T) { + tfs, err := New(tarPath) + if err != nil { + t.Fatalf("New() error = %v, wantErr %v", err, nil) + } + for _, name := range testFiles { + t.Run(name, func(t *testing.T) { + _, err := tfs.Open(name) + if want := fs.ErrNotExist; !errors.Is(err, want) { + t.Errorf("TarFS.Open(%s) error = %v, wantErr %v", name, err, want) + } + }) } - } + }) } } @@ -197,16 +199,20 @@ func TestTarFS_Open_InvalidPath(t *testing.T) { } for _, tarPath := range tarPaths { - tfs, err := New(tarPath) - if err != nil { - t.Fatalf("New() error = %v, wantErr %v", err, nil) - } - for _, name := range testFiles { - _, err := tfs.Open(name) - if want := fs.ErrInvalid; !errors.Is(err, want) { - t.Errorf("TarFS.Open(%s) error = %v, wantErr %v", name, err, want) + t.Run(tarPath, func(t *testing.T) { + tfs, err := New(tarPath) + if err != nil { + t.Fatalf("New() error = %v, wantErr %v", err, nil) + } + for _, name := range testFiles { + t.Run(name, func(t *testing.T) { + _, err := tfs.Open(name) + if want := fs.ErrInvalid; !errors.Is(err, want) { + t.Errorf("TarFS.Open(%s) error = %v, wantErr %v", name, err, want) + } + }) } - } + }) } } @@ -221,16 +227,20 @@ func TestTarFS_Open_Unsupported(t *testing.T) { } for _, tarPath := range tarPaths { - tfs, err := New(tarPath) - if err != nil { - t.Fatalf("New() error = %v, wantErr %v", err, nil) - } - for _, name := range testFiles { - _, err := tfs.Open(name) - if want := errdef.ErrUnsupported; !errors.Is(err, want) { - t.Errorf("TarFS.Open(%s) error = %v, wantErr %v", name, err, want) + t.Run(tarPath, func(t *testing.T) { + tfs, err := New(tarPath) + if err != nil { + t.Fatalf("New() error = %v, wantErr %v", err, nil) + } + for _, name := range testFiles { + t.Run(name, func(t *testing.T) { + _, err := tfs.Open(name) + if want := errdef.ErrUnsupported; !errors.Is(err, want) { + t.Errorf("TarFS.Open(%s) error = %v, wantErr %v", name, err, want) + } + }) } - } + }) } } @@ -241,46 +251,54 @@ func TestTarFS_Stat(t *testing.T) { } for _, tarPath := range tarPaths { - tfs, err := New(tarPath) - if err != nil { - t.Fatalf("New() error = %v, wantErr %v", err, nil) - } - - name := "foobar" - fi, err := tfs.Stat(name) - if err != nil { - t.Fatal("Stat() error =", err) - } - if got, want := fi.Name(), "foobar"; got != want { - t.Errorf("Stat().want() = %v, want %v", got, want) - } - if got, want := fi.Size(), int64(6); got != want { - t.Errorf("Stat().Size() = %v, want %v", got, want) - } - - name = "dir/hello" - fi, err = tfs.Stat(name) - if err != nil { - t.Fatal("Stat() error =", err) - } - if got, want := fi.Name(), "hello"; got != want { - t.Errorf("Stat().want() = %v, want %v", got, want) - } - if got, want := fi.Size(), int64(5); got != want { - t.Errorf("Stat().Size() = %v, want %v", got, want) - } - - name = "dir/subdir/world" - fi, err = tfs.Stat(name) - if err != nil { - t.Fatal("Stat() error =", err) - } - if got, want := fi.Name(), "world"; got != want { - t.Errorf("Stat().want() = %v, want %v", got, want) - } - if got, want := fi.Size(), int64(5); got != want { - t.Errorf("Stat().Size() = %v, want %v", got, want) - } + t.Run(tarPath, func(t *testing.T) { + tfs, err := New(tarPath) + if err != nil { + t.Fatalf("New() error = %v, wantErr %v", err, nil) + } + + name := "foobar" + t.Run(name, func(t *testing.T) { + fi, err := tfs.Stat(name) + if err != nil { + t.Fatal("Stat() error =", err) + } + if got, want := fi.Name(), "foobar"; got != want { + t.Errorf("Stat().want() = %v, want %v", got, want) + } + if got, want := fi.Size(), int64(6); got != want { + t.Errorf("Stat().Size() = %v, want %v", got, want) + } + }) + + name = "dir/hello" + t.Run(name, func(t *testing.T) { + fi, err := tfs.Stat(name) + if err != nil { + t.Fatal("Stat() error =", err) + } + if got, want := fi.Name(), "hello"; got != want { + t.Errorf("Stat().want() = %v, want %v", got, want) + } + if got, want := fi.Size(), int64(5); got != want { + t.Errorf("Stat().Size() = %v, want %v", got, want) + } + }) + + name = "dir/subdir/world" + t.Run(name, func(t *testing.T) { + fi, err := tfs.Stat(name) + if err != nil { + t.Fatal("Stat() error =", err) + } + if got, want := fi.Name(), "world"; got != want { + t.Errorf("Stat().want() = %v, want %v", got, want) + } + if got, want := fi.Size(), int64(5); got != want { + t.Errorf("Stat().Size() = %v, want %v", got, want) + } + }) + }) } } @@ -296,16 +314,20 @@ func TestTarFS_Stat_NotExist(t *testing.T) { } for _, tarPath := range tarPaths { - tfs, err := New(tarPath) - if err != nil { - t.Fatalf("New() error = %v, wantErr %v", err, nil) - } - for _, name := range testFiles { - _, err := tfs.Stat(name) - if want := fs.ErrNotExist; !errors.Is(err, want) { - t.Errorf("TarFS.Stat(%s) error = %v, wantErr %v", name, err, want) + t.Run(tarPath, func(t *testing.T) { + tfs, err := New(tarPath) + if err != nil { + t.Fatalf("New() error = %v, wantErr %v", err, nil) } - } + for _, name := range testFiles { + t.Run(name, func(t *testing.T) { + _, err := tfs.Stat(name) + if want := fs.ErrNotExist; !errors.Is(err, want) { + t.Errorf("TarFS.Stat(%s) error = %v, wantErr %v", name, err, want) + } + }) + } + }) } } @@ -328,21 +350,27 @@ func TestTarFS_Stat_InvalidPath(t *testing.T) { } for _, tarPath := range tarPaths { - tfs, err := New(tarPath) - if err != nil { - t.Fatalf("New() error = %v, wantErr %v", err, nil) - } - for _, name := range testFiles { - _, err := tfs.Stat(name) - if want := fs.ErrInvalid; !errors.Is(err, want) { - t.Errorf("TarFS.Stat(%s) error = %v, wantErr %v", name, err, want) + t.Run(tarPath, func(t *testing.T) { + tfs, err := New(tarPath) + if err != nil { + t.Fatalf("New() error = %v, wantErr %v", err, nil) } - } + for _, name := range testFiles { + t.Run(name, func(t *testing.T) { + _, err := tfs.Stat(name) + if want := fs.ErrInvalid; !errors.Is(err, want) { + t.Errorf("TarFS.Stat(%s) error = %v, wantErr %v", name, err, want) + } + }) + } + }) } } func TestTarFS_Stat_Unsupported(t *testing.T) { testFiles := []string{ + "dir", + "dir/subdir", "foobar_link", "foobar_symlink", } @@ -352,15 +380,19 @@ func TestTarFS_Stat_Unsupported(t *testing.T) { } for _, tarPath := range tarPaths { - tfs, err := New(tarPath) - if err != nil { - t.Fatalf("New() error = %v, wantErr %v", err, nil) - } - for _, name := range testFiles { - _, err := tfs.Stat(name) - if want := errdef.ErrUnsupported; !errors.Is(err, want) { - t.Errorf("TarFS.Stat(%s) error = %v, wantErr %v", name, err, want) + t.Run(tarPath, func(t *testing.T) { + tfs, err := New(tarPath) + if err != nil { + t.Fatalf("New() error = %v, wantErr %v", err, nil) } - } + for _, name := range testFiles { + t.Run(name, func(t *testing.T) { + _, err := tfs.Stat(name) + if want := errdef.ErrUnsupported; !errors.Is(err, want) { + t.Errorf("TarFS.Stat(%s) error = %v, wantErr %v", name, err, want) + } + }) + } + }) } }