Skip to content
suit Artifact [2d6859099c]
Login

Artifact [2d6859099c]

Artifact 2d6859099c7adf3e0af22263d525aada29872a4b07718c6b63cbdbd34d28d2f3:


#!/bin/sh
# Shell tests for finding projects: roots, the scan, each project's key, and
# keys set by hand. Every case works in a scratch HOME, SUIT_HOME and scratch
# project folders under build/tmp; no real repository or home folder is read.
# SUIT names the binary under test (default build/dev/suit).

SUIT=${SUIT:-build/dev/suit}
case $SUIT in /*) ;; *) SUIT=$PWD/$SUIT ;; esac
ROOT=$PWD
FOSSIL_FIX=$ROOT/test/fixtures/fossil-project
mkdir -p build/tmp
T=$(mktemp -d "$ROOT/build/tmp/project.XXXXXX") || exit 1
trap 'chmod -R u+rwx "$T" 2>/dev/null; rm -rf "$T"' EXIT INT TERM

total=0
failed=0
pass() { total=$((total + 1)); echo "ok $1"; }
bad() {
    total=$((total + 1)); failed=$((failed + 1))
    echo "FAIL $1${2:+: $2}"
}

# world: a new scratch HOME, SUIT_HOME and an empty folder of projects, $T/r
world() {
    chmod -R u+rwx "$T/r" 2>/dev/null
    rm -rf "$T/home" "$T/sh" "$T/r"
    mkdir -p "$T/home" "$T/sh" "$T/r"
    export HOME="$T/home" SUIT_HOME="$T/sh"
}

# check NAME WANT_RC [STDERR_SUBSTRING] -- COMMAND...   (output in $T/out, $T/err)
check() {
    cname=$1; cwant=$2; cmsg=$3; shift 4
    "$@" >"$T/out" 2>"$T/err"; crc=$?
    if [ "$crc" != "$cwant" ]; then bad "$cname" "exit $crc, want $cwant: $(cat "$T/err")"
    elif [ -n "$cmsg" ] && ! grep -qF -- "$cmsg" "$T/err"; then bad "$cname" "want '$cmsg' in: $(cat "$T/err")"
    else pass "$cname"; fi
}

# has NAME LINE: `suit projects` prints exactly LINE, with $T written as T
has() {
    "$SUIT" projects 2>/dev/null | sed "s|$T|T|g" >"$T/list"
    if grep -qxF -- "$2" "$T/list"; then pass "$1"; else bad "$1" "no '$2' in: $(head -12 "$T/list" | tr '\n' ';')"; fi
}
# lacks NAME TEXT: `suit projects` prints no line holding TEXT
lacks() {
    "$SUIT" projects 2>/dev/null | sed "s|$T|T|g" >"$T/list"
    if grep -qF -- "$2" "$T/list"; then bad "$1" "found '$2'"; else pass "$1"; fi
}

# git_project FOLDER [URL [REMOTE]]: a git project by hand, with one remote
git_project() {
    mkdir -p "$1/.git"
    if [ -n "${2:-}" ]; then
        printf '[core]\n\tbare = false\n[remote "%s"]\n\turl = %s\n\tfetch = +refs/heads/*:refs/remotes/x/*\n' \
            "${3:-origin}" "$2" >"$1/.git/config"
    else
        printf '[core]\n\tbare = false\n' >"$1/.git/config"
    fi
}

# The C library words errno 5 its own way. Ask the binary under test.
EIO=$(SUIT_FAULT=open:1 "$SUIT" check test/fixtures/example.conf 2>&1 | sed -n 's/.*cannot open: //p')
[ -n "$EIO" ] || { echo "FAIL cannot learn the errno wording"; exit 1; }

# -- roots
world
check roots-none-yet 0 "" -- "$SUIT" roots
[ ! -s "$T/out" ] && pass roots-none-prints-nothing || bad roots-none-prints-nothing "$(cat "$T/out")"
check roots-add 0 "" -- "$SUIT" roots add "$T/r"
check roots-add-again-says-so 1 "is a root already" -- "$SUIT" roots add "$T/r"
"$SUIT" roots >"$T/out"
[ "$(cat "$T/out")" = "$T/r" ] && pass roots-lists-one || bad roots-lists-one "$(cat "$T/out")"
check roots-add-relative-is-refused 2 "is not an absolute path" -- "$SUIT" roots add relative/path
check roots-rm 0 "" -- "$SUIT" roots rm "$T/r"
"$SUIT" roots >"$T/out"
[ ! -s "$T/out" ] && pass roots-rm-empties-the-list || bad roots-rm-empties-the-list "$(cat "$T/out")"
check roots-usage-verb 2 "" -- "$SUIT" roots frob "$T/r"
check roots-usage-extra 2 "" -- "$SUIT" roots add "$T/r" more

# -- the key, for every form of address
world
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/https"   'https://github.com/Owner/Name.git'
git_project "$T/r/scp"     '[email protected]:Owner/Name2.git'
git_project "$T/r/ssh"     'ssh://[email protected]/Owner/Name3'
git_project "$T/r/port"    'https://User:[email protected]:8443/x/y.git/'
git_project "$T/r/dots"    'https://zeitforge.app/r/dmestas/my.project.v2'
git_project "$T/r/upstream" 'https://example.org/first/remote.git' upstream
git_project "$T/r/bare"
check scan 0 "" -- "$SUIT" projects scan
has key-from-https          "github.com/Owner/Name T/r/https git"
has key-from-scp-style      "github.com/Owner/Name2 T/r/scp git"
has key-from-ssh-url        "github.com/Owner/Name3 T/r/ssh git"
has key-drops-user-port-slash "example.com/x/y T/r/port git"
has key-keeps-dots-in-the-path "zeitforge.app/r/dmestas/my.project.v2 T/r/dots git"
has first-remote-when-no-origin "example.org/first/remote T/r/upstream git"
has no-remote-has-no-key    "- T/r/bare git"
lacks no-secret-in-the-list "secret"

# -- what the scan descends into
world
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/outer" 'https://h.example/outer'
git_project "$T/r/outer/inner" 'https://h.example/inner'
git_project "$T/r/d1/d2/d3/deep4" 'https://h.example/deep4'
git_project "$T/r/d1/d2/d3/d4/deep5" 'https://h.example/deep5'
mkdir -p "$T/elsewhere"; git_project "$T/elsewhere/linked" 'https://h.example/linked'
ln -s "$T/elsewhere" "$T/r/link"
mkdir -p "$T/r/locked/sub"; git_project "$T/r/locked/sub" 'https://h.example/locked'; chmod 000 "$T/r/locked"
touch "$T/r/plain-file"
check scan-with-odd-folders 0 "" -- "$SUIT" projects scan
has a-project-is-found            "h.example/outer T/r/outer git"
lacks a-nested-project-is-not-descended-into "h.example/inner"
has depth-four-is-reached         "h.example/deep4 T/r/d1/d2/d3/deep4 git"
lacks depth-five-is-not           "h.example/deep5"
lacks a-symlinked-folder-is-not-followed "h.example/linked"
lacks an-unreadable-folder-is-skipped "h.example/locked"
chmod 755 "$T/r/locked"

# -- a linked worktree shares its project's key
world
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/main" 'https://h.example/main.git'
mkdir -p "$T/r/main/.git/worktrees/wt" "$T/r/main/trees/wt"
printf 'gitdir: %s\n' "$T/r/main/.git/worktrees/wt" >"$T/r/main/trees/wt/.git"
printf '%s\n' "$T/r/main/trees/wt/.git" >"$T/r/main/.git/worktrees/wt/gitdir"
printf '../..\n' >"$T/r/main/.git/worktrees/wt/commondir"
check scan-with-a-worktree 0 "" -- "$SUIT" projects scan
has the-main-project-is-listed "h.example/main T/r/main git"
has its-worktree-has-the-same-key "h.example/main T/r/main/trees/wt git"

# -- Fossil and zit
world
"$SUIT" roots add "$T/r" >/dev/null
mkdir -p "$T/r/fos/co"
cp "$FOSSIL_FIX/repo.fossil" "$T/r/fos/repo.fossil"
cp "$FOSSIL_FIX/ckout.db" "$T/r/fos/co/.fslckout"
mkdir -p "$T/r/zz"; : >"$T/r/zz/.zit-checkout"
check scan-fossil-and-zit 0 "" -- "$SUIT" projects scan
"$SUIT" projects | sed "s|$T|T|g" >"$T/list"
if grep -qE '^[a-z0-9.-]+/[^ ]+ T/r/fos/co fossil$' "$T/list"; then pass fossil-key-from-last-sync-url
else bad fossil-key-from-last-sync-url "$(grep fossil "$T/list")"; fi
lacks fossil-key-has-no-user "@"
has zit-has-no-key-from-a-scan "- T/r/zz zit"

# -- a key set by hand, kept across scans
check project-set 0 "" -- "$SUIT" project set "$T/r/zz" zeitforge.app/r/me/zz
# A key set by hand is applied by the next scan.
has the-set-key-waits-for-a-scan "- T/r/zz zit"
"$SUIT" projects scan >/dev/null
has the-set-key-shows-after-a-scan "zeitforge.app/r/me/zz T/r/zz zit"
"$SUIT" projects scan >/dev/null
has the-set-key-survives-another-scan "zeitforge.app/r/me/zz T/r/zz zit"
check project-set-a-bad-key-is-refused 2 "" -- "$SUIT" project set "$T/r/zz" 'https://not a key'
check project-unset 0 "" -- "$SUIT" project unset "$T/r/zz"
"$SUIT" projects scan >/dev/null
has unset-returns-to-no-key "- T/r/zz zit"
check project-usage 2 "" -- "$SUIT" project frob "$T/r/zz"
check projects-usage 2 "" -- "$SUIT" projects frob
check projects-usage-extra 2 "" -- "$SUIT" projects scan more
check project-usage-wrong-verb-with-a-key 2 "" -- "$SUIT" project frob "$T/r/zz" some.host/x/y
check project-usage-no-arguments 2 "" -- "$SUIT" project

# -- malformed files are errors that name the file
world
printf 'not-absolute\n' >"$T/sh/roots"
check a-bad-roots-file-is-an-error 1 "roots" -- "$SUIT" roots
world
printf 'one-field-only\n' >"$T/sh/projects"
check a-bad-projects-file-is-an-error 1 "projects" -- "$SUIT" projects

# -- roots: what is refused, and what fails
world
check roots-rm-of-a-non-root 1 "is not a root" -- "$SUIT" roots rm "$T/r"
check roots-add-a-missing-folder 1 "is not a folder" -- "$SUIT" roots add "$T/nowhere"
: >"$T/file"
check roots-add-a-file 1 "is not a folder" -- "$SUIT" roots add "$T/file"
check roots-add-ignores-trailing-slashes 0 "" -- "$SUIT" roots add "$T/r//"
[ "$(cat "$T/sh/roots")" = "$T/r" ] && pass roots-keep-no-trailing-slash || bad roots-keep-no-trailing-slash "$(cat "$T/sh/roots")"
check roots-add-a-slash-only-path 1 "is not a root" -- "$SUIT" roots rm ///
long=$(printf '%3001s' x | tr ' ' a)
check roots-add-too-long-a-path 2 "is not an absolute path" -- "$SUIT" roots add "/$long"
check roots-add-with-a-control-character 2 "is not an absolute path" -- "$SUIT" roots add "$(printf '/a\tb')"
check roots-write-fails 1 "cannot write $T/sh/roots: $EIO" -- env SUIT_FAULT=rename:1 "$SUIT" roots rm "$T/r"
check roots-without-home 1 "HOME is not set" -- env -u HOME "$SUIT" roots
world
for i in $(seq 1 64); do mkdir "$T/r/d$i"; "$SUIT" roots add "$T/r/d$i" >/dev/null 2>&1; done
check roots-full 1 "there are 64 roots" -- "$SUIT" roots add "$T/r"

# -- projects and project: what is refused, and what fails
world
check projects-without-home 1 "HOME is not set" -- env -u HOME "$SUIT" projects
check projects-scan-without-home 1 "HOME is not set" -- env -u HOME "$SUIT" projects scan
check project-without-home 1 "HOME is not set" -- env -u HOME "$SUIT" project set /x/y github.com/o/n
printf 'not-absolute\n' >"$T/sh/roots"
check scan-with-a-bad-roots-file 1 "roots:1" -- "$SUIT" projects scan
world
printf 'one-field-only\n' >"$T/sh/projects.set"
check scan-with-a-bad-set-file 1 "projects.set:1" -- "$SUIT" projects scan
check project-with-a-bad-set-file 1 "projects.set:1" -- "$SUIT" project set "$T/r" github.com/o/n
world
check project-unset-of-a-path-with-no-key 1 "no key was set for $T/r" -- "$SUIT" project unset "$T/r"
check project-set-a-relative-path 2 "is not an absolute path" -- "$SUIT" project set relative github.com/o/n
check project-set-write-fails 1 "cannot write $T/sh/projects.set: $EIO" -- env SUIT_FAULT=rename:1 "$SUIT" project set "$T/r" github.com/o/n
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/p" 'https://h.example/p'
check scan-write-fails 1 "cannot write $T/sh/projects: $EIO" -- env SUIT_FAULT=rename:1 "$SUIT" projects scan
world
seq 1 4096 | sed "s|.*|h.example/k& /p&|" >"$T/sh/projects.set"
check project-set-list-full 1 "too many hand-set keys" -- "$SUIT" project set /one-more h.example/more

# -- a linked worktree found by the scan, away from its main repository
world
rm -rf "$T/elsewhere"
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/elsewhere/main" 'https://h.example/linked-main'
mkdir -p "$T/elsewhere/main/.git/worktrees/wt"
printf '../..\n' >"$T/elsewhere/main/.git/worktrees/wt/commondir"
mkdir -p "$T/r/abs" "$T/r/rel" "$T/r/abscommon" "$T/r/nocommon"
printf 'gitdir: %s\n' "$T/elsewhere/main/.git/worktrees/wt" >"$T/r/abs/.git"
printf 'gitdir:   ../../elsewhere/main/.git/worktrees/wt\n' >"$T/r/rel/.git"
printf 'gitdir: %s\n' "$T/elsewhere/main/.git/worktrees/wt" >"$T/r/abscommon/.git"
mkdir -p "$T/elsewhere/ac/wt"
printf '%s\n' "$T/elsewhere/main/.git" >"$T/elsewhere/ac/wt/commondir"
printf 'gitdir: %s\n' "$T/elsewhere/ac/wt" >"$T/r/abscommon/.git"
git_project "$T/elsewhere/plain" 'https://h.example/linked-plain'
printf 'gitdir: %s\n' "$T/elsewhere/plain/.git" >"$T/r/nocommon/.git"
check scan-linked-worktrees 0 "" -- "$SUIT" projects scan
has linked-gitdir-absolute        "h.example/linked-main T/r/abs git"
has linked-gitdir-relative        "h.example/linked-main T/r/rel git"
has linked-commondir-absolute     "h.example/linked-main T/r/abscommon git"
has linked-without-a-commondir    "h.example/linked-plain T/r/nocommon git"

# -- a .git file or config that cannot be used is noted, and the project has no key
world
"$SUIT" roots add "$T/r" >/dev/null
big=$(printf '%5000s' x | tr ' ' a)
long=$(printf '%4095s' x | tr ' ' a)
mkdir -p "$T/r/notgitdir" "$T/r/bigfile" "$T/r/longgitdir" "$T/r/longcommon" "$T/r/bigcommon" "$T/r/bigconfig"
printf 'something else\n' >"$T/r/notgitdir/.git"
printf '%s\n' "$big" >"$T/r/bigfile/.git"
printf 'gitdir:/%s' "$long" >"$T/r/longgitdir/.git"
mkdir -p "$T/gd1" "$T/gd2"
printf 'gitdir: %s\n' "$T/gd1" >"$T/r/longcommon/.git"
printf '%s' "$big" | head -c 4090 >"$T/gd1/commondir"
printf 'gitdir: %s\n' "$T/gd2" >"$T/r/bigcommon/.git"
printf '%s\n' "$big" >"$T/gd2/commondir"
mkdir -p "$T/r/bigconfig/.git"
head -c 200000 /dev/zero >"$T/r/bigconfig/.git/config"
git_project "$T/r/noread" 'https://h.example/noread'
chmod 000 "$T/r/noread/.git/config"
check scan-with-unusable-git-files 0 "" -- "$SUIT" projects scan
for n in notgitdir bigfile longgitdir longcommon bigcommon bigconfig noread; do
    has "unusable-$n-is-listed-without-a-key" "- T/r/$n git"
done
grep -qF "is not a 'gitdir: PATH' file" "$T/err" && pass note-not-a-gitdir-file || bad note-not-a-gitdir-file "$(head -c 300 "$T/err")"
grep -qF "too large to read" "$T/err" && pass note-too-large || bad note-too-large "$(head -c 300 "$T/err")"
grep -qF "the path is too long" "$T/err" && pass note-path-too-long || bad note-path-too-long "$(head -c 300 "$T/err")"
grep -qF "note: $T/r/noread/.git/config:" "$T/err" && pass note-config-unreadable || bad note-config-unreadable "$(head -c 300 "$T/err")"
chmod 644 "$T/r/noread/.git/config"

# -- Fossil checkouts that cannot name a key
world
"$SUIT" roots add "$T/r" >/dev/null
mkdir -p "$T/r/f1" "$T/r/f2" "$T/r/f3" "$T/r/f4" "$T/r/f5" "$T/r/f6"
printf 'this is not a database' >"$T/r/f1/.fslckout"
sqlite3 "$T/r/f2/.fslckout" "CREATE TABLE vvar(name TEXT, value TEXT);"
sqlite3 "$T/r/f3/.fslckout" "CREATE TABLE vvar(name TEXT, value TEXT); INSERT INTO vvar VALUES('repository','$T/nowhere.fossil');"
sqlite3 "$T/r/f4/.fslckout" "CREATE TABLE vvar(name TEXT, value TEXT); INSERT INTO vvar VALUES('repository','$T/f4.fossil');"
sqlite3 "$T/f4.fossil" "CREATE TABLE config(name TEXT, value TEXT);"
longrel=$(printf '%4050s' x | tr ' ' a)
sqlite3 "$T/r/f5/.fslckout" "CREATE TABLE vvar(name TEXT, value TEXT); INSERT INTO vvar VALUES('repository','$longrel');"
sqlite3 "$T/r/f6/.fslckout" "CREATE TABLE vvar(name TEXT, value TEXT); INSERT INTO vvar VALUES('repository','../../f6.fossil');"
sqlite3 "$T/f6.fossil" "CREATE TABLE config(name TEXT, value TEXT); INSERT INTO config VALUES('last-sync-url','https://h.example/f6');"
check scan-with-odd-fossil-checkouts 0 "" -- "$SUIT" projects scan
for n in f1 f2 f3 f4 f5; do has "fossil-$n-has-no-key" "- T/r/$n fossil"; done
has fossil-with-a-relative-repository-path "h.example/f6 T/r/f6 fossil"
grep -qF "the repository path is too long" "$T/err" && pass note-fossil-path-too-long || bad note-fossil-path-too-long "$(head -c 300 "$T/err")"

# -- odd folders in the scan
world
"$SUIT" roots add "$T/r" >/dev/null
tab=$(printf '\t')
git_project "$T/r/with${tab}tab" 'https://h.example/tab'
mkdir -p "$T/r/with${tab}tab/.git/worktrees/w1"
check scan-with-a-control-character-in-a-path 0 "not recorded: the path has a control character" -- "$SUIT" projects scan
lacks a-control-character-path-is-not-listed "h.example/tab"

# -- the worktrees a main repository lists
world
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/main" 'https://h.example/wtmain'
w="$T/r/main/.git/worktrees"
mkdir -p "$w/gone" "$w/short" "$w/other" "$w/ctl" "$w/nofile" "$w/big" "$T/live"
printf '%s\n' "$T/live/.git" >"$w/live-name"
mkdir -p "$w/live"
printf '%s\n' "$T/live/.git" >"$w/live/gitdir"
printf '%s\n' "$T/vanished/.git" >"$w/gone/gitdir"
printf 'x\n' >"$w/short/gitdir"
printf '%s\n' "$T/live/notgit" >"$w/other/gitdir"
printf '%s\n' "$T/li${tab}ve/.git" >"$w/ctl/gitdir"
printf '%s\n' "$big" >"$w/big/gitdir"
check scan-with-odd-worktrees 0 "does not name the .git file of a worktree" -- "$SUIT" projects scan
has a-live-worktree-is-listed "h.example/wtmain T/live git"
lacks a-vanished-worktree-is-not-listed "vanished"
grep -qF "too large to read" "$T/err" && pass note-worktree-gitdir-too-large || bad note-worktree-gitdir-too-large "$(head -c 300 "$T/err")"
world
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/main2" 'https://h.example/main2'
: >"$T/r/main2/.git/worktrees"
check scan-with-a-worktrees-file 0 "note: $T/r/main2/.git/worktrees:" -- "$SUIT" projects scan
has the-main-is-listed-anyway "h.example/main2 T/r/main2 git"

# -- a folder that cannot be examined
world
"$SUIT" roots add "$T/r" >/dev/null
mkdir "$T/r/child"
check scan-child-lstat-fails 0 "note: $T/r/child: $EIO" -- env SUIT_FAULT=lstat:1 "$SUIT" projects scan

# -- a full list of projects
world
rm -rf "$T/big"
mkdir -p "$T/big"
seq 1 4095 | sed "s|.*|$T/big/p&/.git|" | xargs mkdir -p
"$SUIT" roots add "$T/big" >/dev/null
"$SUIT" roots add "$T/r" >/dev/null
git_project "$T/r/main" 'https://h.example/fullmain'
mkdir -p "$T/r/main/.git/worktrees/wt" "$T/r/main/wt"
printf '%s\n' "$T/r/main/wt/.git" >"$T/r/main/.git/worktrees/wt/gitdir"
check scan-full-at-a-worktree 1 "too many projects" -- "$SUIT" projects scan
world
"$SUIT" roots add "$T/big" >/dev/null
mkdir -p "$T/big/p4096/.git" "$T/solo/.git"
"$SUIT" roots add "$T/solo" >/dev/null
check scan-full-at-a-root 1 "too many projects" -- "$SUIT" projects scan
world
"$SUIT" roots add "$T/big" >/dev/null
mkdir -p "$T/big/p4097/.git"
check scan-full-in-a-folder 1 "too many projects" -- "$SUIT" projects scan
rm -rf "$T/big"

# -- keys and remotes that are not what they should be
world
for k in abc 'abc/' 'ho{st/x/y' 'Host/x/y' 'h.example/' '/x/y' 'h.example//x' 'h.example/x/'; do
    check "project-set-refuses-$k" 2 "is not a remote key" -- "$SUIT" project set "$T/r" "$k"
done
"$SUIT" roots add "$T/r" >/dev/null
mkdir -p "$T/r/q1/.git" "$T/r/q2/.git" "$T/r/q3/.git"
printf '[remote "a]b"]\n\turl = https://h.example/q1\n' >"$T/r/q1/.git/config"
printf '[remote "origon"]\n\turl = https://h.example/q2\n[remote "origin"]\n\turl = https://h.example/q3\n' >"$T/r/q2/.git/config"
printf '[remote "origon"]\n\turl = https://h.example/q4\n' >"$T/r/q3/.git/config"
check scan-with-odd-remote-sections 0 "" -- "$SUIT" projects scan
has a-quote-after-the-bracket-is-no-remote "- T/r/q1 git"
has origin-wins-over-a-near-name "h.example/q3 T/r/q2 git"
has a-near-name-is-still-a-remote "h.example/q4 T/r/q3 git"

echo "project.sh: $total cases, $failed failed"
[ "$failed" -eq 0 ]