2019-02-07 12:12:20 -02:00
|
|
|
package gzip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
2021-04-25 13:00:51 -03:00
|
|
|
"io"
|
2019-02-07 12:12:20 -02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2021-07-21 22:09:02 -03:00
|
|
|
"time"
|
2019-02-07 12:12:20 -02:00
|
|
|
|
2021-07-21 22:09:02 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
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) {
|
2021-04-25 14:20:49 -03:00
|
|
|
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)
|
2021-04-25 11:34:40 -03:00
|
|
|
defer archive.Close() // nolint: errcheck
|
2019-02-07 12:12:20 -02:00
|
|
|
|
2021-07-21 22:09:02 -03:00
|
|
|
require.NoError(t, archive.Add(config.File{
|
|
|
|
Destination: "sub1/sub2/subfoo.txt",
|
|
|
|
Source: "../testdata/sub1/sub2/subfoo.txt",
|
|
|
|
}))
|
|
|
|
require.EqualError(t, archive.Add(config.File{
|
|
|
|
Destination: "foo.txt",
|
|
|
|
Source: "../testdata/foo.txt",
|
|
|
|
}), "gzip: failed to add foo.txt, only one file can be archived in gz format")
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, archive.Close())
|
|
|
|
require.NoError(t, f.Close())
|
2019-02-07 12:12:20 -02:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-04-25 13:00:51 -03:00
|
|
|
bts, err := io.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
|
|
|
}
|
2021-07-21 22:09:02 -03:00
|
|
|
|
|
|
|
func TestGzFileCustomMtime(t *testing.T) {
|
|
|
|
f, err := os.Create(filepath.Join(t.TempDir(), "test.gz"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close() // nolint: errcheck
|
|
|
|
archive := New(f)
|
|
|
|
defer archive.Close() // nolint: errcheck
|
|
|
|
|
|
|
|
now := time.Now().Truncate(time.Second)
|
|
|
|
|
|
|
|
require.NoError(t, archive.Add(config.File{
|
|
|
|
Destination: "sub1/sub2/subfoo.txt",
|
|
|
|
Source: "../testdata/sub1/sub2/subfoo.txt",
|
|
|
|
Info: config.FileInfo{
|
|
|
|
MTime: now,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
require.NoError(t, archive.Close())
|
|
|
|
require.NoError(t, f.Close())
|
|
|
|
|
|
|
|
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)
|
|
|
|
require.Equal(t, now, gzf.Header.ModTime)
|
|
|
|
}
|