Skip to content

Commit

Permalink
fix: updating error handling for non tar files
Browse files Browse the repository at this point in the history
Signed-off-by: Adnan Gulegulzar <[email protected]>
  • Loading branch information
ADorigi committed Jul 21, 2024
1 parent c8c2048 commit 995e97f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions content/oci/readonlyoci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/memory"
"oras.land/oras-go/v2/errdef"
"oras.land/oras-go/v2/internal/docker"
"oras.land/oras-go/v2/internal/spec"
"oras.land/oras-go/v2/registry"
Expand Down Expand Up @@ -836,3 +837,15 @@ func Test_deleteAnnotationRefName(t *testing.T) {
})
}
}

func TestNewFromTar_NonTarFile(t *testing.T) {

emptyfile := "testdata/emptyfile"

_, err := NewFromTar(context.Background(), emptyfile)

if want := errdef.ErrNotTarFile; !errors.Is(err, want) {
t.Errorf("OCI.NewFromTar(%s) error = %v, wantErr %v", emptyfile, err, want)
}

}
Empty file added content/oci/testdata/dummyfile
Empty file.
1 change: 1 addition & 0 deletions errdef/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ var (
ErrSizeExceedsLimit = errors.New("size exceeds limit")
ErrUnsupported = errors.New("unsupported")
ErrUnsupportedVersion = errors.New("unsupported version")
ErrNotTarFile = errors.New("tar file type expected")
)
7 changes: 7 additions & 0 deletions internal/fs/tarfs/tarfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ func New(path string) (*TarFS, error) {
if err != nil {
return nil, fmt.Errorf("failed to resolve absolute path for %s: %w", path, err)
}

extension := filepath.Ext(pathAbs)

if extension != ".tar" {
return nil, errdef.ErrNotTarFile
}

tarfs := &TarFS{
path: pathAbs,
entries: make(map[string]*entry),
Expand Down
12 changes: 12 additions & 0 deletions internal/fs/tarfs/tarfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,15 @@ func TestTarFS_Stat_Unsupported(t *testing.T) {
}
}
}

func TestTarFS_New_NonTarFile(t *testing.T) {

emptyfile := "testdata/emptyfile"

_, err := New(emptyfile)

if want := errdef.ErrNotTarFile; !errors.Is(err, want) {
t.Errorf("TarFS.New(%s) error = %v, wantErr %v", emptyfile, err, want)
}

}
Empty file.

0 comments on commit 995e97f

Please sign in to comment.