Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

5.1 KB
Raw Blame
#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TMP_ROOT="$(mktemp -d "${TMPDIR:-/tmp}/ods-cli-link.XXXXXX")"
trap 'rm -rf "$TMP_ROOT"' EXIT

fail() {
    printf '[FAIL] %s\n' "$1" >&2
    exit 1
}

# ods_bind_cli_command consults `command -v ods` to decide whether an existing
# command already points at this install. On a machine that actually runs ODS
# that ambient command answers instead of the fixture, the first binding
# returns 1, and `set -e` ends the run with no output at all. Drop only the
# PATH entries that carry an ods executable so the fixture decides.
drop_ambient_ods_from_path() {
    local cleaned="" entry
    local IFS=:
    for entry in $PATH; do
        [[ -n "$entry" && -x "$entry/ods" ]] && continue
        cleaned+="${cleaned:+:}$entry"
    done
    PATH="$cleaned"
}
drop_ambient_ods_from_path

pass() {
    printf '[PASS] %s\n' "$1"
}

ods_sudo() {
    "$@"
}

ods_sudo_available() {
    [[ "${TEST_SUDO_AVAILABLE:-true}" == "true" ]]
}

# shellcheck source=../installers/lib/cli-link.sh
source "$ROOT_DIR/installers/lib/cli-link.sh"

INSTALL_DIR="$TMP_ROOT/current"
OLD_DIR="$TMP_ROOT/old"
OWNER_HOME="$TMP_ROOT/home"
SYSTEM_LINK="$TMP_ROOT/system/ods"
mkdir -p "$INSTALL_DIR" "$OLD_DIR" "$OWNER_HOME" "${SYSTEM_LINK%/*}"
printf '#!/usr/bin/env bash\n' >"$INSTALL_DIR/ods-cli"
printf '#!/usr/bin/env bash\n' >"$OLD_DIR/ods-cli"
chmod 0755 "$INSTALL_DIR/ods-cli" "$OLD_DIR/ods-cli"

ln -s "$OLD_DIR/ods-cli" "$SYSTEM_LINK"
result="$(ODS_CLI_SYSTEM_LINK="$SYSTEM_LINK" ods_bind_cli_command "$INSTALL_DIR" "$OWNER_HOME")" \
    || fail "binding refused to refresh the stale system link"
[[ "$result" == "system:$SYSTEM_LINK" ]] || fail "stale system link was not reported as refreshed"
ods_cli_path_matches_install "$SYSTEM_LINK" "$INSTALL_DIR/ods-cli" \
    || fail "stale system link was not rebound to the current install"
pass "stale system link is rebound to the current install"

rm "$SYSTEM_LINK"
result="$(
    TEST_SUDO_AVAILABLE=false ODS_CLI_SYSTEM_LINK="$SYSTEM_LINK" \
        ods_bind_cli_command "$INSTALL_DIR" "$OWNER_HOME"
)" || fail "binding refused the rootless fallback when sudo is unavailable"
[[ "$result" == "user:$OWNER_HOME/.local/bin/ods" ]] \
    || fail "sudo-unavailable install did not use the rootless fallback"
ods_cli_path_matches_install "$OWNER_HOME/.local/bin/ods" "$INSTALL_DIR/ods-cli" \
    || fail "sudo-unavailable fallback does not target the current install"
pass "sudo-unavailable install uses the exact rootless fallback"

rm "$OWNER_HOME/.local/bin/ods"
printf '#!/usr/bin/env bash\n' >"$SYSTEM_LINK"
chmod 0755 "$SYSTEM_LINK"
result="$(ODS_CLI_SYSTEM_LINK="$SYSTEM_LINK" ods_bind_cli_command "$INSTALL_DIR" "$OWNER_HOME")" \
    || fail "binding refused to fall back while a regular system command exists"
[[ "$result" == "user:$OWNER_HOME/.local/bin/ods" ]] \
    || fail "regular system command did not use the rootless fallback"
[[ -f "$SYSTEM_LINK" && ! -L "$SYSTEM_LINK" ]] \
    || fail "regular system command was overwritten"
ods_cli_path_matches_install "$OWNER_HOME/.local/bin/ods" "$INSTALL_DIR/ods-cli" \
    || fail "rootless fallback does not target the current install"
pass "regular system command is preserved and rootless fallback is exact"

rm "$OWNER_HOME/.local/bin/ods"
printf '#!/usr/bin/env bash\n' >"$OWNER_HOME/.local/bin/ods"
chmod 0755 "$OWNER_HOME/.local/bin/ods"
if ODS_CLI_SYSTEM_LINK="$SYSTEM_LINK" ods_bind_cli_command "$INSTALL_DIR" "$OWNER_HOME" >/dev/null 2>&1; then
    fail "unsafe regular user command was overwritten"
fi
[[ -f "$OWNER_HOME/.local/bin/ods" && ! -L "$OWNER_HOME/.local/bin/ods" ]] \
    || fail "unsafe regular user command changed"
pass "unmanaged regular command paths fail closed"

# Exercise the BSD fallback contract without claiming macOS installation QA.
(
    stat() {
        [[ "$1" == "-f" ]] || return 1
        if [[ "$(uname -s)" == Darwin ]]; then
            command stat "$@"
        else
            command stat -c "$2" "$3"
        fi
    }
    install() {
        for argument in "$@"; do [[ "$argument" != "--" ]] || return 1; done
        command install "$@"
    }
    ln() {
        for argument in "$@"; do [[ "$argument" != "--" ]] || return 1; done
        command ln "$@"
    }
    bsd_owner="$TMP_ROOT/bsd home"
    mkdir -p "$bsd_owner"
    result="$(TEST_SUDO_AVAILABLE=false ODS_CLI_SYSTEM_LINK="$SYSTEM_LINK" ods_bind_cli_command "$INSTALL_DIR" "$bsd_owner")" \
        || fail "BSD rootless creation refused to bind"
    [[ "$result" == "user:$bsd_owner/.local/bin/ods" ]] || fail "BSD rootless creation failed"
    result="$(TEST_SUDO_AVAILABLE=false ODS_CLI_SYSTEM_LINK="$SYSTEM_LINK" ods_bind_cli_command "$INSTALL_DIR" "$bsd_owner")" \
        || fail "BSD ownership fallback refused to bind"
    [[ "$result" == "user:$bsd_owner/.local/bin/ods" ]] || fail "BSD ownership fallback failed"
    realpath() { return 1; }
    ods_cli_path_matches_install "$bsd_owner/.local/bin/ods" "$INSTALL_DIR/ods-cli" || fail "readlink fallback failed"
)
pass "BSD-style options and ownership fallback retain exact CLI binding"

printf 'CLI link refresh tests passed.\n'