read gzip archives
This commit is contained in:
@@ -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{
|
||||
|
||||
@@ -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())
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user