tests: cover c end to end
Every type and profile is created and then checked with the format's own tools, independently of x, and round-tripped through x. Tests also cover naming, symlink inputs, refusals, backend failures, missing dependencies, and exclusion behaving the same across tar, zip and 7z.
This commit is contained in:
1 file changed
+389
+389
@@ -0,0 +1,389 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var allTypes = []string{"tar", "tar.gz", "tar.bz2", "tar.xz", "tar.zst", "zip", "7z"}
|
||||
|
||||
// toolsFor lists the programs needed to create and independently check typ.
|
||||
func toolsFor(typ string) []string {
|
||||
return map[string][]string{
|
||||
"tar": {"tar"}, "tar.gz": {"tar", "gzip"}, "tar.bz2": {"tar", "bzip2"},
|
||||
"tar.xz": {"tar", "xz"}, "tar.zst": {"tar", "zstd"}, "zip": {"zip", "unzip"}, "7z": {"7z"},
|
||||
}[typ]
|
||||
}
|
||||
|
||||
// forEachType runs fn as a subtest for every archive type whose tools exist.
|
||||
func forEachType(t *testing.T, fn func(t *testing.T, typ string)) {
|
||||
for _, typ := range allTypes {
|
||||
t.Run(typ, func(t *testing.T) {
|
||||
require(t, toolsFor(typ)...)
|
||||
fn(t, typ)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// verifyAndExtract checks archive with the format's own tools and extracts
|
||||
// it into dir with them, so that c is verified independently of x.
|
||||
func (w *work) verifyAndExtract(typ, archive, dir string) {
|
||||
w.t.Helper()
|
||||
w.mkdir(dir)
|
||||
switch typ {
|
||||
case "tar":
|
||||
w.sh("tar", "-tf", archive)
|
||||
case "tar.gz":
|
||||
w.sh("gzip", "-t", archive)
|
||||
case "tar.bz2":
|
||||
w.sh("bzip2", "-t", archive)
|
||||
case "tar.xz":
|
||||
w.sh("xz", "-t", archive)
|
||||
case "tar.zst":
|
||||
w.sh("zstd", "-tq", archive)
|
||||
case "zip":
|
||||
w.sh("unzip", "-tq", archive)
|
||||
case "7z":
|
||||
w.sh("7z", "t", "-bso0", "-bsp0", archive)
|
||||
}
|
||||
switch typ {
|
||||
case "zip":
|
||||
w.sh("unzip", "-q", archive, "-d", dir)
|
||||
case "7z":
|
||||
w.sh("7z", "x", "-bso0", "-bsp0", "-o"+dir, archive)
|
||||
default:
|
||||
w.sh("tar", "-xf", archive, "-C", dir)
|
||||
}
|
||||
}
|
||||
|
||||
// tarList returns the sorted member names of a tar archive.
|
||||
func (w *work) tarList(archive string) []string {
|
||||
w.t.Helper()
|
||||
cmd := exec.Command("tar", "-tf", archive)
|
||||
cmd.Dir = w.dir
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
w.t.Fatal(err)
|
||||
}
|
||||
names := strings.Fields(strings.ReplaceAll(string(out), " ", "\x00"))
|
||||
for i := range names {
|
||||
names[i] = strings.ReplaceAll(names[i], "\x00", " ")
|
||||
}
|
||||
slices.Sort(names)
|
||||
return names
|
||||
}
|
||||
|
||||
func (w *work) wantTarList(archive string, want ...string) {
|
||||
w.t.Helper()
|
||||
if got := w.tarList(archive); !slices.Equal(got, want) {
|
||||
w.t.Fatalf("%s contains %q, want %q", archive, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// --- argument parsing ---------------------------------------------------------------
|
||||
|
||||
func TestCMissingOperands(t *testing.T) {
|
||||
w := newWork(t)
|
||||
w.c().wantStatus(2).wantErr("missing output operand")
|
||||
w.c("out.tar").wantStatus(2).wantErr("missing input operand")
|
||||
}
|
||||
|
||||
func TestCInvalidOptions(t *testing.T) {
|
||||
w := newWork(t)
|
||||
w.c("-z", "out.tar", "x").wantStatus(2).wantErr("invalid option '-z'")
|
||||
w.c("--bogus", "out.tar", "x").wantStatus(2).wantErr("unrecognized option '--bogus'")
|
||||
}
|
||||
|
||||
func TestCMissingOptionValues(t *testing.T) {
|
||||
w := newWork(t)
|
||||
w.c("out.tar", "x", "-t").wantStatus(2).wantErr("option '-t' requires an argument")
|
||||
w.c("out.tar", "x", "--exclude").wantStatus(2).wantErr("option '--exclude' requires an argument")
|
||||
}
|
||||
|
||||
func TestCHelpAndVersion(t *testing.T) {
|
||||
w := newWork(t)
|
||||
if r := w.c("-h").wantStatus(0); !strings.HasPrefix(r.stdout, "Usage: c") {
|
||||
t.Fatalf("help: %q", r.stdout)
|
||||
}
|
||||
if r := w.c("--version").wantStatus(0); r.stdout != "c 1.0.0\n" {
|
||||
t.Fatalf("version: %q", r.stdout)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCTypeResolutionErrors(t *testing.T) {
|
||||
w := newWork(t)
|
||||
w.write("in", "")
|
||||
w.c("out.rar", "in").wantStatus(2).wantErr("cannot infer the archive type")
|
||||
w.c("-t", "rar", "out.tar", "in").wantStatus(2).wantErr("unknown type 'rar'")
|
||||
w.c("-t", "zip", "out.tar.gz", "in").wantStatus(2).wantErr("implies type 'tar.gz'")
|
||||
w.wantListing(".", "in")
|
||||
}
|
||||
|
||||
func TestCExplicitTypeWithUnknownExtension(t *testing.T) {
|
||||
require(t, "zip", "unzip")
|
||||
w := newWork(t)
|
||||
w.write("in", "x\n")
|
||||
w.c("-t", "zip", "release.pkg", "in").wantStatus(0)
|
||||
w.sh("unzip", "-tq", "release.pkg")
|
||||
w.wantAbsent("release.pkg.zip") // never renamed behind the user's back
|
||||
}
|
||||
|
||||
func TestCDoubleDashAndPermutation(t *testing.T) {
|
||||
require(t, "tar")
|
||||
w := newWork(t)
|
||||
w.write("-input", "dash\n")
|
||||
w.c("out.tar", "-q", "--", "-input").wantStatus(0).wantNoStderr()
|
||||
w.wantTarList("out.tar", "-input")
|
||||
}
|
||||
|
||||
// --- formats ------------------------------------------------------------------------
|
||||
|
||||
func TestCEveryTypeRoundTrips(t *testing.T) {
|
||||
forEachType(t, func(t *testing.T, typ string) {
|
||||
w := newWork(t)
|
||||
w.makeTree("src/proj")
|
||||
w.c("out."+typ, "src/proj").wantStatus(0).wantNoStdout().wantErr("created 'out." + typ + "' (")
|
||||
w.verifyAndExtract(typ, "out."+typ, "x")
|
||||
w.wantListing("x", "proj")
|
||||
w.wantSameTree("src/proj", "x/proj")
|
||||
})
|
||||
}
|
||||
|
||||
func TestCRoundTripThroughX(t *testing.T) {
|
||||
forEachType(t, func(t *testing.T, typ string) {
|
||||
require(t, "bsdtar")
|
||||
w := newWork(t)
|
||||
w.makeTree("src/proj")
|
||||
w.c("out."+typ, "src/proj").wantStatus(0)
|
||||
w.x("-d", "back", "out."+typ).wantStatus(0)
|
||||
w.wantSameTree("src/proj", "back/proj")
|
||||
})
|
||||
}
|
||||
|
||||
func TestCEveryProfileProducesValidArchives(t *testing.T) {
|
||||
forEachType(t, func(t *testing.T, typ string) {
|
||||
w := newWork(t)
|
||||
w.makeTree("src/proj")
|
||||
for _, flag := range []string{"--fast", "--best"} {
|
||||
out := "out" + flag + "." + typ
|
||||
w.c("-q", flag, out, "src/proj").wantStatus(0)
|
||||
w.verifyAndExtract(typ, out, "x"+flag)
|
||||
w.wantSameTree("src/proj", "x"+flag+"/proj")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCProfileOnPlainTarWarns(t *testing.T) {
|
||||
require(t, "tar")
|
||||
w := newWork(t)
|
||||
w.write("in", "")
|
||||
w.c("-b", "out.tar", "in").wantStatus(0).wantErr("no effect on an uncompressed tar")
|
||||
}
|
||||
|
||||
func TestCExtensionAliases(t *testing.T) {
|
||||
require(t, "tar", "gzip", "bzip2", "xz", "zstd")
|
||||
w := newWork(t)
|
||||
w.write("in", "x\n")
|
||||
for _, ext := range []string{"tgz", "tbz2", "tbz", "txz", "tzst", "TAR.GZ"} {
|
||||
w.c("out."+ext, "in").wantStatus(0)
|
||||
w.wantTarList("out."+ext, "in")
|
||||
}
|
||||
if magic := w.read("out.tgz")[:2]; magic != "\x1f\x8b" {
|
||||
t.Fatalf("out.tgz is not gzip: %q", magic)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCMultipleInputsFromAnywhere(t *testing.T) {
|
||||
require(t, "tar", "gzip")
|
||||
w := newWork(t)
|
||||
w.makeTree("src/proj")
|
||||
w.write("elsewhere/deep dir/notes.txt", "notes\n")
|
||||
w.c("out.tar.gz", "src/proj", w.path("elsewhere/deep dir/notes.txt"), "../"+lastElem(w.dir)+"/src").wantStatus(0)
|
||||
w.mkdir("x")
|
||||
w.sh("tar", "-xzf", "out.tar.gz", "-C", "x")
|
||||
w.wantListing("x", "proj", "notes.txt", "src") // each input under its own name
|
||||
w.wantFile("x/notes.txt", "notes\n")
|
||||
}
|
||||
|
||||
func TestCDotInputUsesDirectoryName(t *testing.T) {
|
||||
require(t, "tar")
|
||||
w := newWork(t)
|
||||
w.write("my project/a", "a\n")
|
||||
w.sh("sh", "-c", `cd "my project" && "$1" -q ../out.tar .`, "sh", cBin)
|
||||
w.wantTarList("out.tar", "my project/", "my project/a")
|
||||
}
|
||||
|
||||
func TestCSymlinkInputIsFollowed(t *testing.T) {
|
||||
require(t, "tar")
|
||||
w := newWork(t)
|
||||
w.write("releases/v1/a", "a\n")
|
||||
w.symlink("releases/v1", "current")
|
||||
w.c("out.tar", "current").wantStatus(0)
|
||||
w.wantTarList("out.tar", "v1/", "v1/a")
|
||||
}
|
||||
|
||||
func TestCEmptyDirectoryInput(t *testing.T) {
|
||||
forEachType(t, func(t *testing.T, typ string) {
|
||||
w := newWork(t)
|
||||
w.mkdir("empty")
|
||||
w.c("e."+typ, "empty").wantStatus(0)
|
||||
w.verifyAndExtract(typ, "e."+typ, "x")
|
||||
w.wantDir("x/empty")
|
||||
})
|
||||
}
|
||||
|
||||
// --- output safety ------------------------------------------------------------------
|
||||
|
||||
func TestCExistingOutputIsNotReplaced(t *testing.T) {
|
||||
require(t, "tar")
|
||||
w := newWork(t)
|
||||
w.write("out.tar", "precious\n")
|
||||
w.write("in", "x\n")
|
||||
w.c("out.tar", "in").wantStatus(1).wantErr("already exists (use --force to replace it)")
|
||||
w.wantFile("out.tar", "precious\n")
|
||||
w.c("-f", "out.tar", "in").wantStatus(1) // -f is --fast in c, never force
|
||||
w.wantFile("out.tar", "precious\n")
|
||||
w.c("--force", "out.tar", "in").wantStatus(0)
|
||||
w.wantTarList("out.tar", "in")
|
||||
w.wantNoTemporaries(".")
|
||||
}
|
||||
|
||||
func TestCOutputIsADirectory(t *testing.T) {
|
||||
w := newWork(t)
|
||||
w.mkdir("out.tar")
|
||||
w.write("in", "")
|
||||
w.c("--force", "out.tar", "in").wantStatus(3).wantErr("is a directory")
|
||||
}
|
||||
|
||||
func TestCOutputDirectoryMustExist(t *testing.T) {
|
||||
w := newWork(t)
|
||||
w.write("in", "")
|
||||
w.c("missing/out.tar", "in").wantStatus(3).wantErr("directory 'missing' does not exist")
|
||||
}
|
||||
|
||||
func TestCOutputInsideInputIsRefused(t *testing.T) {
|
||||
w := newWork(t)
|
||||
w.mkdir("proj/sub")
|
||||
w.c("proj/sub/out.tar", "proj").wantStatus(3).wantErr("would include itself")
|
||||
w.c("proj/out.tar", "proj").wantStatus(3)
|
||||
w.wantAbsent("proj/out.tar")
|
||||
w.wantNoTemporaries("proj")
|
||||
}
|
||||
|
||||
func TestCOutputAsInputIsRefused(t *testing.T) {
|
||||
require(t, "tar")
|
||||
w := newWork(t)
|
||||
w.write("in", "x\n")
|
||||
w.c("-q", "out.tar", "in").wantStatus(0)
|
||||
w.c("--force", "out.tar", "out.tar").wantStatus(3).wantErr("is the output file itself")
|
||||
}
|
||||
|
||||
func TestCInvalidInputsCreateNothing(t *testing.T) {
|
||||
w := newWork(t)
|
||||
w.write("ok", "x\n")
|
||||
w.c("out.tar.gz", "ok", "missing", "also missing").wantStatus(3).
|
||||
wantErr("'missing': no such file or directory").
|
||||
wantErr("'also missing': no such file or directory")
|
||||
w.wantListing(".", "ok")
|
||||
}
|
||||
|
||||
func TestCDuplicateNamesAreRefused(t *testing.T) {
|
||||
w := newWork(t)
|
||||
w.mkdir("a/src", "b/src")
|
||||
w.c("out.tar", "a/src", "b/src").wantStatus(3).wantErr("both would be stored as 'src'")
|
||||
}
|
||||
|
||||
func TestCRootIsRefused(t *testing.T) {
|
||||
newWork(t).c("out.tar", "/").wantStatus(3).wantErr("refusing to archive the root directory")
|
||||
}
|
||||
|
||||
func TestCBackendFailureLeavesNothing(t *testing.T) {
|
||||
require(t, "tar", "gzip")
|
||||
if os.Geteuid() == 0 {
|
||||
t.Skip("root can read unreadable files")
|
||||
}
|
||||
w := newWork(t)
|
||||
w.write("src/secret", "x\n")
|
||||
os.Chmod(w.path("src/secret"), 0)
|
||||
defer os.Chmod(w.path("src/secret"), 0o600)
|
||||
w.c("out.tar.gz", "src").wantStatus(1).wantErr("tar exited with status 2").wantErr("nothing was written")
|
||||
w.wantListing(".", "src")
|
||||
}
|
||||
|
||||
func TestCMissingDependency(t *testing.T) {
|
||||
require(t, "zstd", "7z")
|
||||
w := newWork(t)
|
||||
w.write("in", "")
|
||||
w.withPathWithout("zstd", "7z", "7zz")
|
||||
w.c("out.tar.zst", "in").wantStatus(4).wantErr("requires 'zstd', which is not installed")
|
||||
w.c("out.7z", "in").wantStatus(4).wantErr("requires '7z'")
|
||||
w.wantListing(".", "in")
|
||||
}
|
||||
|
||||
// --- exclusion ----------------------------------------------------------------------
|
||||
|
||||
func TestCExcludeIsConsistentAcrossTypes(t *testing.T) {
|
||||
forEachType(t, func(t *testing.T, typ string) {
|
||||
w := newWork(t)
|
||||
w.makeTree("src/proj")
|
||||
for _, f := range []string{"node_modules/pkg/i.js", "sub/node_modules/x", "a.o", "sub/b.o", "-tmp/t"} {
|
||||
w.write("src/proj/"+f, "")
|
||||
}
|
||||
w.c("--exclude", "node_modules", "--exclude", "*.o", "--exclude=-tmp", "out."+typ, "src/proj").wantStatus(0)
|
||||
w.verifyAndExtract(typ, "out."+typ, "x")
|
||||
for name := range w.snapshot("x") {
|
||||
if strings.Contains(name, "node_modules") || strings.HasSuffix(name, ".o") || strings.Contains(name, "-tmp") {
|
||||
t.Errorf("excluded entry archived: %s", name)
|
||||
}
|
||||
}
|
||||
w.wantFile("x/proj/sub/deeper/deep.txt", "deep\n")
|
||||
})
|
||||
}
|
||||
|
||||
func TestCExcludeRejectsPaths(t *testing.T) {
|
||||
w := newWork(t)
|
||||
w.write("in", "")
|
||||
w.c("--exclude", "a/b", "out.tar", "in").wantStatus(2).wantErr("must not contain '/'")
|
||||
}
|
||||
|
||||
func TestCZipWithEverythingExcludedFails(t *testing.T) {
|
||||
require(t, "zip")
|
||||
w := newWork(t)
|
||||
w.write("proj/a", "")
|
||||
w.c("--exclude", "proj", "out.zip", "proj").wantStatus(1).wantErr("nothing to archive in 'proj'")
|
||||
w.wantListing(".", "proj")
|
||||
}
|
||||
|
||||
// --- output -------------------------------------------------------------------------
|
||||
|
||||
func TestCQuietAndStdout(t *testing.T) {
|
||||
require(t, "tar", "gzip")
|
||||
w := newWork(t)
|
||||
w.write("in", "")
|
||||
w.c("out.tar.gz", "in").wantStatus(0).wantNoStdout().wantErr("created 'out.tar.gz'")
|
||||
w.c("-q", "out2.tar.gz", "in").wantStatus(0).wantNoStdout().wantNoStderr()
|
||||
}
|
||||
|
||||
func TestCVerboseListsEntries(t *testing.T) {
|
||||
for _, typ := range []string{"tar.gz", "zip", "7z"} {
|
||||
t.Run(typ, func(t *testing.T) {
|
||||
require(t, toolsFor(typ)...)
|
||||
w := newWork(t)
|
||||
w.makeTree("src/proj")
|
||||
w.c("-v", "out."+typ, "src/proj").wantStatus(0).wantNoStdout().wantErr("deep.txt")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCRunsFromZsh(t *testing.T) {
|
||||
require(t, "zsh", "tar")
|
||||
w := newWork(t)
|
||||
w.write("a file", "")
|
||||
w.sh("zsh", "-fc", `"$1" -q "out file.tar" "a file"`, "zsh", cBin)
|
||||
w.wantTarList("out file.tar", "a file")
|
||||
}
|
||||
|
||||
func lastElem(path string) string { return path[strings.LastIndex(path, "/")+1:] }
|
||||
Reference in new issue
Block a user