2019-02-07 12:12:20 -02:00
|
|
|
package gzip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-06 09:48:04 -03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-07 12:12:20 -02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGzFile(t *testing.T) {
|
2020-12-12 13:27:35 -03:00
|
|
|
var tmp = t.TempDir()
|
2019-02-07 12:12:20 -02:00
|
|
|
f, err := os.Create(filepath.Join(tmp, "test.gz"))
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2019-02-07 12:12:20 -02:00
|
|
|
defer f.Close() // nolint: errcheck
|
|
|
|
archive := New(f)
|
|
|
|
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, archive.Add("sub1/sub2/subfoo.txt", "../testdata/sub1/sub2/subfoo.txt"))
|
|
|
|
require.EqualError(t, archive.Add("foo.txt", "../testdata/foo.txt"), "gzip: failed to add foo.txt, only one file can be archived in gz format")
|
|
|
|
require.NoError(t, archive.Close())
|
2019-02-07 12:12:20 -02:00
|
|
|
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, f.Close())
|
2019-02-07 12:12:20 -02:00
|
|
|
|
|
|
|
t.Log(f.Name())
|
|
|
|
f, err = os.Open(f.Name())
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2019-02-07 12:12:20 -02:00
|
|
|
defer f.Close() // nolint: errcheck
|
|
|
|
|
|
|
|
info, err := f.Stat()
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Truef(t, info.Size() < 500, "archived file should be smaller than %d", info.Size())
|
2019-02-07 12:12:20 -02:00
|
|
|
|
|
|
|
gzf, err := gzip.NewReader(f)
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
2019-02-07 12:12:20 -02:00
|
|
|
defer gzf.Close() // nolint: errcheck
|
|
|
|
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Equal(t, "sub1/sub2/subfoo.txt", gzf.Name)
|
2019-02-07 12:12:20 -02:00
|
|
|
|
|
|
|
bts, err := ioutil.ReadAll(gzf)
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "sub\n", string(bts))
|
2019-02-07 12:12:20 -02:00
|
|
|
}
|