read gzip archives

This commit is contained in:
2021-05-24 00:11:58 +03:00
parent a45e512697
commit fd244bfc23
4 changed files with 84 additions and 5 deletions

View File

@@ -3,6 +3,8 @@ package tartest
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io"
"testing"
@@ -53,6 +55,20 @@ func (tb Tarball) Buffer() *bytes.Buffer {
return &buf
}
// Gzip returns a gzipped buffer
func (tb Tarball) Gzip() *bytes.Buffer {
var buf bytes.Buffer
w := gzip.NewWriter(&buf)
_, err := io.Copy(w, tb.Buffer())
if err != nil {
panic(fmt.Errorf("Gzip(): %w", err))
}
if err := w.Close(); err != nil {
panic(fmt.Errorf("gzip.Close(): %w", err))
}
return &buf
}
// Tar tars the Dir
func (d Dir) Tar(tw *tar.Writer) error {
hdr := &tar.Header{

View File

@@ -2,9 +2,12 @@ package tartest
import (
"bytes"
"compress/gzip"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTarball(t *testing.T) {
@@ -23,6 +26,18 @@ func TestTarball(t *testing.T) {
Dir{Name: "bin"},
Hardlink{Name: "entrypoint2"},
}
assert.Equal(t, want, got)
}
func TestGzip(t *testing.T) {
tb := Tarball{File{Name: "entrypoint.sh", Contents: bytes.NewBufferString("hello")}}
tbuf := tb.Buffer()
tgz, err := gzip.NewReader(tb.Gzip())
require.NoError(t, err)
var uncompressed bytes.Buffer
_, err = io.Copy(&uncompressed, tgz)
require.NoError(t, err)
assert.Equal(t, tbuf.Bytes(), uncompressed.Bytes())
}