Tests pin: - every compressor's arguments for every profile; - the zip and 7z levels; - the full tar, zip and 7z command lines, including names that start with "-"; - the exclude pattern translation and type-resolution errors.
130 lines
4.8 KiB
Go
130 lines
4.8 KiB
Go
package create
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"src.panahifar.ir/ahp/archive-utils/internal/cli"
|
|
)
|
|
|
|
func TestTypeFromName(t *testing.T) {
|
|
for name, want := range map[string]Type{
|
|
"a.tar": Tar, "a.tar.gz": TarGz, "a.tgz": TarGz, "A.TAR.GZ": TarGz,
|
|
"a.tar.bz2": TarBz2, "a.tbz2": TarBz2, "a.tbz": TarBz2,
|
|
"a.tar.xz": TarXz, "a.txz": TarXz, "a.tar.zst": TarZst, "a.tzst": TarZst,
|
|
"a.zip": Zip, "a.7z": SevenZ,
|
|
} {
|
|
if got, ok := TypeFromName(name); !ok || got != want {
|
|
t.Errorf("TypeFromName(%q) = %q, %v; want %q", name, got, ok, want)
|
|
}
|
|
}
|
|
for _, name := range []string{"a.gz", "a.rar", "a.tar.lzma", ".tar", "tar"} {
|
|
if got, ok := TypeFromName(name); ok {
|
|
t.Errorf("TypeFromName(%q) = %q, want none", name, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestResolveType(t *testing.T) {
|
|
tests := []struct {
|
|
typ, output string
|
|
want Type
|
|
err string
|
|
}{
|
|
{"", "x.tar.zst", TarZst, ""},
|
|
{"zip", "release.zip", Zip, ""},
|
|
{"zip", "release.pkg", Zip, ""}, // unknown extension: name kept as given
|
|
{"", "x.rar", "", "cannot infer the archive type from 'x.rar'; use a known extension or --type"},
|
|
{"rar", "x.tar", "", "unknown type 'rar' (choose from: tar, tar.gz, tar.bz2, tar.xz, tar.zst, zip, 7z)"},
|
|
{"tgz", "x", "", "unknown type 'tgz' (choose from: tar, tar.gz, tar.bz2, tar.xz, tar.zst, zip, 7z)"},
|
|
{"zip", "x.tar.gz", "", "output name 'x.tar.gz' implies type 'tar.gz', but --type 'zip' was given"},
|
|
}
|
|
for _, tt := range tests {
|
|
got, err := resolveType(tt.typ, tt.output)
|
|
if tt.err == "" {
|
|
if err != nil || got != tt.want {
|
|
t.Errorf("resolveType(%q, %q) = %q, %v", tt.typ, tt.output, got, err)
|
|
}
|
|
continue
|
|
}
|
|
var e *cli.Error
|
|
if !errors.As(err, &e) || e.Code != cli.ExitUsage || e.Msg != tt.err {
|
|
t.Errorf("resolveType(%q, %q) error = %v, want %q", tt.typ, tt.output, err, tt.err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestProfiles pins the exact compressor arguments of every profile.
|
|
func TestProfiles(t *testing.T) {
|
|
tests := []struct {
|
|
typ Type
|
|
profile Profile
|
|
want []string
|
|
}{
|
|
{TarGz, Default, []string{"gzip", "-c", "-n", "-6"}},
|
|
{TarGz, Fast, []string{"gzip", "-c", "-n", "-1"}},
|
|
{TarGz, Best, []string{"gzip", "-c", "-n", "-9"}},
|
|
{TarBz2, Default, []string{"bzip2", "-c", "-9"}},
|
|
{TarBz2, Fast, []string{"bzip2", "-c", "-1"}},
|
|
{TarBz2, Best, []string{"bzip2", "-c", "-9"}},
|
|
{TarXz, Default, []string{"xz", "-c", "-T0", "-6"}},
|
|
{TarXz, Fast, []string{"xz", "-c", "-T0", "-1"}},
|
|
{TarXz, Best, []string{"xz", "-c", "-T0", "-9"}},
|
|
{TarZst, Default, []string{"zstd", "-c", "-q", "-T0", "-3"}},
|
|
{TarZst, Fast, []string{"zstd", "-c", "-q", "-T0", "-1"}},
|
|
{TarZst, Best, []string{"zstd", "-c", "-q", "-T0", "-19"}},
|
|
{Tar, Best, nil},
|
|
{Zip, Best, nil},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := compressorArgs(tt.typ, tt.profile); !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("compressorArgs(%s, %s) = %q, want %q", tt.typ, tt.profile, got, tt.want)
|
|
}
|
|
}
|
|
for p, want := range map[Profile][2]string{Default: {"-6", "-mx=5"}, Fast: {"-1", "-mx=1"}, Best: {"-9", "-mx=9"}} {
|
|
if got := [2]string{level(Zip, p), level(SevenZ, p)}; got != want {
|
|
t.Errorf("zip/7z levels for %s = %q, want %q", p, got, want)
|
|
}
|
|
}
|
|
// The shared program slice must not be modified by appending levels.
|
|
compressorArgs(TarGz, Fast)
|
|
if got := compressorArgs(TarGz, Best); got[len(got)-1] != "-9" {
|
|
t.Errorf("compressorArgs aliasing: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestBackendArgs(t *testing.T) {
|
|
inputs := []input{{parent: "/src", name: "proj"}, {parent: "/etc", name: "-dash"}}
|
|
tests := []struct{ got, want []string }{
|
|
{tarArgs(inputs, []string{"*.o"}, false),
|
|
[]string{"tar", "--create", "--file=-", "--exclude=*.o", "-C", "/src", "--add-file=proj", "-C", "/etc", "--add-file=-dash"}},
|
|
{tarArgs(inputs[:1], nil, true),
|
|
[]string{"tar", "--create", "--file=-", "--verbose", "-C", "/src", "--add-file=proj"}},
|
|
{zipArgs("-9", false, "/t/a.zip", "-dash", "/t/x.lst"),
|
|
[]string{"zip", "-r", "-y", "-9", "-q", "/t/a.zip", "./-dash", "-x@/t/x.lst"}},
|
|
{zipArgs("-6", true, "/t/a.zip", "p", "/t/x.lst"),
|
|
[]string{"zip", "-r", "-y", "-6", "/t/a.zip", "./p", "-x@/t/x.lst"}},
|
|
{sevenZipArgs("7zz", "-mx=5", false, []string{"node_modules"}, "/t/a.7z", inputs),
|
|
[]string{"7zz", "a", "-t7z", "-mx=5", "-snl", "-bsp0", "-bso0", "-xr!node_modules", "--", "/t/a.7z", "/src/proj", "/etc/-dash"}},
|
|
{zipPatterns([]string{"*.o", "-tmp"}),
|
|
[]string{"*.o", "*/*.o", "*.o/*", "*/*.o/*", "-tmp", "*/-tmp", "-tmp/*", "*/-tmp/*"}},
|
|
}
|
|
for _, tt := range tests {
|
|
if !reflect.DeepEqual(tt.got, tt.want) {
|
|
t.Errorf("got %q\nwant %q", tt.got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCheckExcludes(t *testing.T) {
|
|
if err := checkExcludes([]string{"node_modules", "*.o", "-tmp", "[ab]?"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, bad := range []string{"", "a/b", "a\nb"} {
|
|
if err := checkExcludes([]string{bad}); err == nil {
|
|
t.Errorf("checkExcludes(%q) accepted", bad)
|
|
}
|
|
}
|
|
}
|