package runner
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
// The runner is given a private repository's sync token as
// QUARRY_RUNNER_PASSWORD, and zit reads ZIT_PASSWORD: the clone ran with
// --user and no password, so a runner for a private repository never
// started (ticket 430). zit is now handed the token.
func TestZitIsHandedTheFetchPassword(t *testing.T) {
seen := filepath.Join(t.TempDir(), "seen")
zit := filepath.Join(t.TempDir(), "zit")
script := "#!/bin/sh\ncase \"$1\" in\nclone) printf '%s %s\\n' \"$ZIT_PASSWORD\" \"$*\" >> " + seen + "; : > .zit ;;\nupdate) [ -f .zit ] || exit 1 ;;\nesac\n"
if err := os.WriteFile(zit, []byte(script), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("ZIT_PASSWORD", "")
dir := filepath.Join(t.TempDir(), "ws")
co := ZitCheckout{Zit: zit, URL: "https://forge.example/r/demo", User: "fetcher", Password: "the-sync-token"}
if err := co.Checkout(context.Background(), dir, strings.Repeat("a", 64)); err != nil {
t.Fatalf("FAIL: checkout: %v", err)
}
got, _ := os.ReadFile(seen)
if !strings.HasPrefix(string(got), "the-sync-token ") {
t.Errorf("FAIL: zit clone did not receive the password in ZIT_PASSWORD")
}
if !strings.Contains(string(got), "--user fetcher") {
t.Errorf("FAIL: zit clone was not told the account: %q", got)
}
if !t.Failed() {
t.Log("ok")
}
}
// With no account named, a private repository's refusal says what the
// runner needs, where it used to repeat zit's line and restart forever.
func TestAPrivateRepositoryWithNoAccountNamesTheFlag(t *testing.T) {
zit := filepath.Join(t.TempDir(), "zit")
script := "#!/bin/sh\necho 'zit clone: the peer wants a login (HTTP 401)'; exit 1\n"
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(), filepath.Join(t.TempDir(), "ws"), "trunk")
if err == nil {
t.Fatal("FAIL: a refused clone was reported as a success")
}
for _, hook := range []string{"--user", "QUARRY_RUNNER_PASSWORD"} {
if !strings.Contains(err.Error(), hook) {
t.Errorf("FAIL: the refusal does not name %s", hook)
}
}
if !t.Failed() {
t.Log("ok")
}
}