mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-08 03:31:59 +02:00
73641c71ac
* refactor: archive files Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * feat: better archives Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * feat: better archives Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: test todos Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: go mod tidy et al Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: improve docs and remove typoe 'licence' from defaults Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * test: fixes Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: error string Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * test: remove some logs Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * test: use utc Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
package gzip
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGzFile(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
f, err := os.Create(filepath.Join(tmp, "test.gz"))
|
|
require.NoError(t, err)
|
|
defer f.Close() // nolint: errcheck
|
|
archive := New(f)
|
|
defer archive.Close() // nolint: errcheck
|
|
|
|
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")
|
|
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)
|
|
|
|
bts, err := io.ReadAll(gzf)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "sub\n", string(bts))
|
|
}
|
|
|
|
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)
|
|
}
|