2019-02-07 16:12:20 +02:00
|
|
|
package targz
|
2018-08-05 15:23:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"compress/gzip"
|
|
|
|
"io"
|
2021-07-22 03:09:02 +02:00
|
|
|
"io/fs"
|
2018-08-05 15:23:39 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2021-07-22 03:09:02 +02:00
|
|
|
"time"
|
2018-08-05 15:23:39 +02:00
|
|
|
|
2021-07-22 03:09:02 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
2020-10-06 14:48:04 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-05 15:23:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTarGzFile(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
tmp := t.TempDir()
|
2018-08-05 15:23:39 +02:00
|
|
|
f, err := os.Create(filepath.Join(tmp, "test.tar.gz"))
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
2018-08-05 15:23:39 +02:00
|
|
|
defer f.Close() // nolint: errcheck
|
|
|
|
archive := New(f)
|
2021-04-25 16:34:40 +02:00
|
|
|
defer archive.Close() // nolint: errcheck
|
2018-08-05 15:23:39 +02:00
|
|
|
|
2021-07-22 03:09:02 +02:00
|
|
|
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",
|
|
|
|
}))
|
2021-09-01 04:08:07 +02:00
|
|
|
require.NoError(t, archive.Add(config.File{
|
|
|
|
Source: "../testdata/regular.txt",
|
|
|
|
Destination: "regular.txt",
|
|
|
|
}))
|
|
|
|
require.NoError(t, archive.Add(config.File{
|
|
|
|
Source: "../testdata/link.txt",
|
|
|
|
Destination: "link.txt",
|
|
|
|
}))
|
2018-08-05 15:23:39 +02:00
|
|
|
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, archive.Close())
|
2021-07-22 03:09:02 +02:00
|
|
|
require.Error(t, archive.Add(config.File{
|
|
|
|
Source: "tar.go",
|
|
|
|
Destination: "tar.go",
|
|
|
|
}))
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, f.Close())
|
2018-08-05 15:23:39 +02:00
|
|
|
|
|
|
|
f, err = os.Open(f.Name())
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
2019-02-07 16:12:20 +02:00
|
|
|
defer f.Close() // nolint: errcheck
|
|
|
|
|
2018-08-05 15:23:39 +02:00
|
|
|
info, err := f.Stat()
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Truef(t, info.Size() < 500, "archived file should be smaller than %d", info.Size())
|
2019-02-07 16:12:20 +02:00
|
|
|
|
2018-08-05 15:23:39 +02:00
|
|
|
gzf, err := gzip.NewReader(f)
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
2019-02-07 16:12:20 +02:00
|
|
|
defer gzf.Close() // nolint: errcheck
|
|
|
|
|
2018-08-05 15:23:39 +02:00
|
|
|
var paths []string
|
|
|
|
r := tar.NewReader(gzf)
|
|
|
|
for {
|
|
|
|
next, err := r.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
2018-08-05 15:23:39 +02:00
|
|
|
paths = append(paths, next.Name)
|
|
|
|
if next.Name == "sub1/executable" {
|
2023-06-18 16:58:17 +02:00
|
|
|
ex := next.FileInfo().Mode()&0o111 != 0
|
|
|
|
require.True(t, ex, "expected executable permissions, got %s", next.FileInfo().Mode())
|
2018-08-05 15:23:39 +02:00
|
|
|
}
|
2021-09-01 04:08:07 +02:00
|
|
|
if next.Name == "link.txt" {
|
|
|
|
require.Equal(t, next.Linkname, "regular.txt")
|
|
|
|
}
|
2018-08-05 15:23:39 +02:00
|
|
|
}
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Equal(t, []string{
|
2018-08-05 15:23:39 +02:00
|
|
|
"foo.txt",
|
|
|
|
"sub1",
|
|
|
|
"sub1/bar.txt",
|
|
|
|
"sub1/executable",
|
|
|
|
"sub1/sub2",
|
|
|
|
"sub1/sub2/subfoo.txt",
|
2021-09-01 04:08:07 +02:00
|
|
|
"regular.txt",
|
|
|
|
"link.txt",
|
2018-08-05 15:23:39 +02:00
|
|
|
}, paths)
|
|
|
|
}
|
2021-07-22 03:09:02 +02:00
|
|
|
|
|
|
|
func TestTarGzFileInfo(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{
|
2022-12-14 17:16:43 +02:00
|
|
|
Mode: 0o755,
|
|
|
|
Owner: "carlos",
|
|
|
|
Group: "root",
|
|
|
|
ParsedMTime: now,
|
2021-07-22 03:09:02 +02:00
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
gzf, err := gzip.NewReader(f)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer gzf.Close() // nolint: errcheck
|
|
|
|
|
|
|
|
var found int
|
|
|
|
r := tar.NewReader(gzf)
|
|
|
|
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)
|
2021-07-24 04:54:13 +02:00
|
|
|
require.Equal(t, fs.FileMode(0o755), next.FileInfo().Mode())
|
2021-07-22 03:09:02 +02:00
|
|
|
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)
|
|
|
|
}
|