Skip to content

Commit 0cecebc

Browse files
committed
Simplify tgz.Extract
Signed-off-by: Paulo Gomes <[email protected]>
1 parent 43baec7 commit 0cecebc

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

fixtures.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,12 @@ func (f *Fixture) DotGit(opts ...Option) billy.Filesystem {
272272
panic(err)
273273
}
274274

275-
fs, err := tgz.Extract(file, o.fsFactory)
275+
fs, err := o.fsFactory()
276+
if err != nil {
277+
panic(err)
278+
}
279+
280+
err = tgz.Extract(file, fs)
276281
if err != nil {
277282
panic(err)
278283
}
@@ -317,7 +322,12 @@ func (f *Fixture) Worktree(opts ...Option) billy.Filesystem {
317322
panic(err)
318323
}
319324

320-
fs, err := tgz.Extract(file, o.fsFactory)
325+
fs, err := o.fsFactory()
326+
if err != nil {
327+
panic(err)
328+
}
329+
330+
err = tgz.Extract(file, fs)
321331
if err != nil {
322332
panic(err)
323333
}

internal/tgz/tgz.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,10 @@ var MemFactory = func() (billy.Filesystem, error) {
1515
return memfs.New(), nil
1616
}
1717

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

40-
fs, err = newFS()
41-
if err != nil {
42-
return
43-
}
44-
4534
err = unTar(fs, tar)
4635
if err != nil {
4736
return

internal/tgz/tgz_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ func TestExtractError(t *testing.T) {
4343
if tc.notFound {
4444
require.ErrorIs(t, err, os.ErrNotExist)
4545
} else {
46-
_, err = Extract(f, MemFactory)
46+
fs, err := MemFactory()
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
err = Extract(f, fs)
4752
require.ErrorContains(t, err, tc.wantErr)
4853
}
4954
})
@@ -103,7 +108,12 @@ func TestExtract(t *testing.T) {
103108
f, err := source.Open(tc.tgz)
104109
require.NoError(t, err)
105110

106-
fs, err := Extract(f, ff.factory)
111+
fs, err := ff.factory()
112+
if err != nil {
113+
panic(err)
114+
}
115+
116+
err = Extract(f, fs)
107117
require.NoError(t, err, "%s: unexpected error extracting: %s", tc.tgz, err)
108118

109119
for _, path := range tc.tree {

0 commit comments

Comments
 (0)