package runner
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
// A premerge pair is checked out as an archive, so the workspace it
// leaves is a bare tree with no .zit. The next check-in reclones into
// that directory, and zit refuses to open a checkout over files it does
// not own: the first attempt failed every time and only the retry, which
// found the half-made .zit, got through. The workspace is cleared first.
func TestACheckinAfterAPairReclonesIntoAnEmptyWorkspace(t *testing.T) {
dir := filepath.Join(t.TempDir(), "ws")
if err := os.MkdirAll(filepath.Join(dir, "src"), 0o755); err != nil {
t.Fatal(err)
}
// What the pair's archive left behind.
for _, f := range []string{"README.md", "src/main.go"} {
if err := os.WriteFile(filepath.Join(dir, f), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
}
// zit as the runner meets it: clone into "." refuses a directory
// holding anything, and update needs the checkout clone made.
zit := filepath.Join(t.TempDir(), "zit")
script := `#!/bin/sh
case "$1" in
clone) [ -z "$(ls -A .)" ] || { echo "zit open: an untracked file or directory is in the way"; exit 1; }; : > .zit ;;
update) [ -f .zit ] || exit 1 ;;
esac
`
if err := os.WriteFile(zit, []byte(script), 0o755); err != nil {
t.Fatal(err)
}
err := ZitCheckout{Zit: zit, URL: "https://forge.example/r/demo"}.Checkout(context.Background(), dir, strings.Repeat("a", 64))
if err != nil {
t.Fatalf("FAIL: the first checkout after a pair failed: %v", err)
}
for _, f := range []string{"README.md", "src"} {
if _, err := os.Stat(filepath.Join(dir, f)); err == nil {
t.Errorf("FAIL: %s from the pair's tree survived the reclone", f)
}
}
t.Log("ok: a workspace with no .zit is cleared and recloned on the first attempt")
}