1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00
goreleaser/pkg/archive/gzip/gzip_test.go
Carlos Alexandro Becker 92f52ac406
refactor(test): use testing.TB Cleanup and Tempdir (#1945)
* refactor: use t.Cleanup instead of defer

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* refactor: use t.Cleanup instead of defer

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* refactor: use t.Cleanup instead of defer

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* refactor: use t.Cleanup instead of defer

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: filepath

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
2020-12-12 13:27:35 -03:00

45 lines
1.1 KiB
Go

package gzip
import (
"compress/gzip"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestGzFile(t *testing.T) {
var tmp = t.TempDir()
f, err := os.Create(filepath.Join(tmp, "test.gz"))
require.NoError(t, err)
defer f.Close() // nolint: errcheck
archive := New(f)
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())
require.NoError(t, f.Close())
t.Log(f.Name())
f, err = os.Open(f.Name())
require.NoError(t, err)
defer f.Close() // nolint: errcheck
info, err := f.Stat()
require.NoError(t, err)
require.Truef(t, info.Size() < 500, "archived file should be smaller than %d", info.Size())
gzf, err := gzip.NewReader(f)
require.NoError(t, err)
defer gzf.Close() // nolint: errcheck
require.Equal(t, "sub1/sub2/subfoo.txt", gzf.Name)
bts, err := ioutil.ReadAll(gzf)
require.NoError(t, err)
require.Equal(t, "sub\n", string(bts))
}