Skip to content

Commit

Permalink
fix: condensed the tests and added friendly message
Browse files Browse the repository at this point in the history
Signed-off-by: Adnan Gulegulzar <gulegulzaradnan@gmail.com>
  • Loading branch information
ADorigi committed Jul 24, 2024
1 parent b5c7ebd commit 275a44a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
20 changes: 9 additions & 11 deletions content/oci/readonlyoci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,17 +840,15 @@ func Test_deleteAnnotationRefName(t *testing.T) {

func TestNewFromTar_NonTarFile(t *testing.T) {

emptyfile := "testdata/emptyfile"
nontar := "testdata/nontar.tar"

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

_, err = NewFromTar(context.Background(), nontar)
if want := errdef.ErrInvalidTarFile; !errors.Is(err, want) {
t.Errorf("OCI.NewFromTar(%s) error = %v, wantErr %v", nontar, err, want)
testFiles := []string{
"testdata/emptyfile",
"testdata/nontar.tar",
}
for _, name := range testFiles {
_, err := NewFromTar(context.Background(), name)
if want := errdef.ErrInvalidTarFile; !errors.Is(err, want) {
t.Errorf("OCI.NewFromTar(%s) error = %v, wantErr %v", name, err, want)
}
}

}
4 changes: 2 additions & 2 deletions internal/fs/tarfs/tarfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ func (tfs *TarFS) indexEntries() error {
if errors.Is(err, io.EOF) {
if len(tfs.entries) == 0 {
// indicates the given file is empty
return fmt.Errorf("{%s}: %w", tfs.path, errdef.ErrInvalidTarFile)
return fmt.Errorf("%s: file is empty: %w", tfs.path, errdef.ErrInvalidTarFile)
}
break
}
if errors.Is(err, io.ErrUnexpectedEOF) {
// indicates that its either not a tarfile or it is a corrupted one
return fmt.Errorf("{%s}: %w", tfs.path, errdef.ErrInvalidTarFile)
return fmt.Errorf("%s: %w", tfs.path, errdef.ErrInvalidTarFile)
}
return err
}
Expand Down
23 changes: 10 additions & 13 deletions internal/fs/tarfs/tarfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,19 @@ func TestTarFS_Stat_Unsupported(t *testing.T) {
}

func TestTarFS_New_NonTarFile(t *testing.T) {

emptyfile := "testdata/emptyfile.tar"
nontar := "testdata/nontar.tar"
tarwithoutextension := "testdata/tarwithoutextension"

_, err := New(emptyfile)
if want := errdef.ErrInvalidTarFile; !errors.Is(err, want) {
t.Errorf("TarFS.New(%s) error = %v, wantErr %v", emptyfile, err, want)
testFiles := []string{
"testdata/emptyfile.tar",
"testdata/nontar.tar",
}

_, err = New(nontar)
if want := errdef.ErrInvalidTarFile; !errors.Is(err, want) {
t.Errorf("TarFS.New(%s) error = %v, wantErr %v", nontar, err, want)
for _, name := range testFiles {
_, err := New(name)
if want := errdef.ErrInvalidTarFile; !errors.Is(err, want) {
t.Errorf("TarFS.New(%s) error = %v, wantErr %v", name, err, want)
}
}

_, err = New(tarwithoutextension)
tarwithoutextension := "testdata/tarwithoutextension"
_, err := New(tarwithoutextension)
if err != nil {
t.Errorf("TarFS.New(%s) error = %v, wantErr %v", tarwithoutextension, err, nil)
}
Expand Down

0 comments on commit 275a44a

Please sign in to comment.