1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/pkg/archive/tarxz/tarxz_test.go
Carlos Alexandro Becker 73641c71ac
feat: file mappings in archives (#2347)
* 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>
2021-07-21 22:09:02 -03:00

146 lines
3.4 KiB
Go

package tarxz
import (
"archive/tar"
"io"
"io/fs"
"os"
"path/filepath"
"testing"
"time"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
"github.com/ulikunitz/xz"
)
func TestTarXzFile(t *testing.T) {
tmp := t.TempDir()
f, err := os.Create(filepath.Join(tmp, "test.tar.xz"))
require.NoError(t, err)
defer f.Close() // nolint: errcheck
archive := New(f)
defer archive.Close() // nolint: errcheck
require.Error(t, archive.Add(config.File{
Source: "../testdata/nope.txt",
Destination: "nope.txt",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/foo.txt",
Destination: "foo.txt",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/sub1",
Destination: "sub1",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/sub1/bar.txt",
Destination: "sub1/bar.txt",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/sub1/executable",
Destination: "sub1/executable",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/sub1/sub2",
Destination: "sub1/sub2",
}))
require.NoError(t, archive.Add(config.File{
Source: "../testdata/sub1/sub2/subfoo.txt",
Destination: "sub1/sub2/subfoo.txt",
}))
require.NoError(t, archive.Close())
require.Error(t, archive.Add(config.File{
Source: "tar.go",
Destination: "tar.go",
}))
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())
xzf, err := xz.NewReader(f)
require.NoError(t, err)
var paths []string
r := tar.NewReader(xzf)
for {
next, err := r.Next()
if err == io.EOF {
break
}
require.NoError(t, err)
paths = append(paths, next.Name)
t.Logf("%s: %v", next.Name, next.FileInfo().Mode())
if next.Name == "sub1/executable" {
ex := next.FileInfo().Mode() | 0o111
require.Equal(t, next.FileInfo().Mode().String(), ex.String())
}
}
require.Equal(t, []string{
"foo.txt",
"sub1",
"sub1/bar.txt",
"sub1/executable",
"sub1/sub2",
"sub1/sub2/subfoo.txt",
}, paths)
}
func TestTarXzFileInfo(t *testing.T) {
now := time.Now().Truncate(time.Second)
f, err := os.Create(filepath.Join(t.TempDir(), "test.tar.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{
Source: "../testdata/foo.txt",
Destination: "nope.txt",
Info: config.FileInfo{
Mode: 0755,
Owner: "carlos",
Group: "root",
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
xzf, err := xz.NewReader(f)
require.NoError(t, err)
var found int
r := tar.NewReader(xzf)
for {
next, err := r.Next()
if err == io.EOF {
break
}
require.NoError(t, err)
found++
require.Equal(t, "nope.txt", next.Name)
require.Equal(t, now, next.ModTime)
require.Equal(t, fs.FileMode(0755), next.FileInfo().Mode())
require.Equal(t, "carlos", next.Uname)
require.Equal(t, 0, next.Uid)
require.Equal(t, "root", next.Gname)
require.Equal(t, 0, next.Gid)
}
require.Equal(t, 1, found)
}