Skip to content

Commit

Permalink
Simplify tgz.Extract
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Gomes <[email protected]>
  • Loading branch information
pjbgf committed Apr 26, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
marcoag Marco A. Gutierrez
1 parent 9a63f9d commit 7dfa5f9
Showing 3 changed files with 27 additions and 18 deletions.
14 changes: 12 additions & 2 deletions fixtures.go
Original file line number Diff line number Diff line change
@@ -272,7 +272,12 @@ func (f *Fixture) DotGit(opts ...Option) billy.Filesystem {
panic(err)
}

fs, err := tgz.Extract(file, o.fsFactory)
fs, err := o.fsFactory()
if err != nil {
panic(err)
}

err = tgz.Extract(file, fs)
if err != nil {
panic(err)
}
@@ -317,7 +322,12 @@ func (f *Fixture) Worktree(opts ...Option) billy.Filesystem {
panic(err)
}

fs, err := tgz.Extract(file, o.fsFactory)
fs, err := o.fsFactory()
if err != nil {
panic(err)
}

err = tgz.Extract(file, fs)
if err != nil {
panic(err)
}
17 changes: 3 additions & 14 deletions internal/tgz/tgz.go
Original file line number Diff line number Diff line change
@@ -15,16 +15,10 @@ var MemFactory = func() (billy.Filesystem, error) {
return memfs.New(), nil
}

// Extract decompress a gziped tarball into a billy Filesystem.
// Extract decompress a gziped tarball into the fs billy.Filesystem.
//
// On success, the path of the newly created directory and a nil error
// is returned.
//
// A non-nil error is returned if the method fails to complete. The
// returned path will be an empty string if no information was extracted.
// Otherwise, a non-empty string with the temporal directory holding
// whatever information was extracted before the error is returned.
func Extract(tgz billy.File, newFS func() (billy.Filesystem, error)) (fs billy.Filesystem, err error) {
// A non-nil error is returned if the method fails to complete.
func Extract(tgz billy.File, fs billy.Filesystem) (err error) {
defer func() {
errClose := tgz.Close()
if err == nil {
@@ -37,11 +31,6 @@ func Extract(tgz billy.File, newFS func() (billy.Filesystem, error)) (fs billy.F
return
}

fs, err = newFS()
if err != nil {
return
}

err = unTar(fs, tar)
if err != nil {
return
14 changes: 12 additions & 2 deletions internal/tgz/tgz_test.go
Original file line number Diff line number Diff line change
@@ -43,7 +43,12 @@ func TestExtractError(t *testing.T) {
if tc.notFound {
require.ErrorIs(t, err, os.ErrNotExist)
} else {
_, err = Extract(f, MemFactory)
fs, err := MemFactory()
if err != nil {
panic(err)
}

err = Extract(f, fs)
require.ErrorContains(t, err, tc.wantErr)
}
})
@@ -103,7 +108,12 @@ func TestExtract(t *testing.T) {
f, err := source.Open(tc.tgz)
require.NoError(t, err)

fs, err := Extract(f, ff.factory)
fs, err := ff.factory()
if err != nil {
panic(err)
}

err = Extract(f, fs)
require.NoError(t, err, "%s: unexpected error extracting: %s", tc.tgz, err)

for _, path := range tc.tree {

0 comments on commit 7dfa5f9

Please sign in to comment.