|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Run only on a disposable Linux host with empty, writable /workspace and |
| 3 | +# /home/claude-user directories. The test needs Git, Bash and a container engine. |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" |
| 7 | +for dir in /workspace /home/claude-user; do |
| 8 | + if [[ ! -d "${dir}" || ! -w "${dir}" || -n "$(ls -A "${dir}")" ]]; then |
| 9 | + echo "Test requires an empty, writable ${dir}" >&2 |
| 10 | + exit 1 |
| 11 | + fi |
| 12 | +done |
| 13 | + |
| 14 | +tmp=$(mktemp -d) |
| 15 | +workspace_fixture=$(mktemp -d /workspace/claude-test.XXXXXX) |
| 16 | +home_fixture=$(mktemp -d /home/claude-user/claude-test.XXXXXX) |
| 17 | +trap 'rm -rf "${tmp}" "${workspace_fixture}" "${home_fixture}"' EXIT |
| 18 | + |
| 19 | +export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null |
| 20 | +export GIT_AUTHOR_NAME=test GIT_AUTHOR_EMAIL=test@example.invalid |
| 21 | +export GIT_COMMITTER_NAME=test GIT_COMMITTER_EMAIL=test@example.invalid |
| 22 | +# Resolve the real engine before adding the test shim to PATH. |
| 23 | +TEST_ENGINE=$(command -v "${CONTAINER_ENGINE:-docker}") |
| 24 | +export TEST_ENGINE |
| 25 | +export TEST_ENGINE_HOME="${HOME}" |
| 26 | +export TEST_ENTRYPOINT="${SCRIPT_DIR}/claude-entrypoint.sh" |
| 27 | +export TEST_SETUP="${tmp}/entrypoint" |
| 28 | +image="${TEST_IMAGE:-openroad/flow-ubuntu26.04-dev:latest}" |
| 29 | + |
| 30 | +mkdir -p "${tmp}/bin" "${tmp}/settings/.claude" "${tmp}/settings/.cache/bazel-claude" |
| 31 | +cat > "${TEST_SETUP}" <<'SH' |
| 32 | +#!/usr/bin/env bash |
| 33 | +set -e |
| 34 | +mkdir -p /etc/sudoers.d |
| 35 | +exec /test-entrypoint.sh "$@" |
| 36 | +SH |
| 37 | +cat > "${tmp}/bin/docker" <<'SH' |
| 38 | +#!/usr/bin/env bash |
| 39 | +engine_args=() |
| 40 | +if [[ "${TEST_ENGINE##*/}" == podman ]]; then |
| 41 | + export HOME="${TEST_ENGINE_HOME}" |
| 42 | + engine_args+=(--userns keep-id --security-opt label=disable) |
| 43 | +fi |
| 44 | +case "$1" in |
| 45 | + image) exec "${TEST_ENGINE}" "$@" ;; |
| 46 | + run) |
| 47 | + shift |
| 48 | + exec "${TEST_ENGINE}" run "${engine_args[@]}" --user 0:0 --network none \ |
| 49 | + --cap-drop ALL --cap-add CHOWN --cap-add SETUID --cap-add SETGID \ |
| 50 | + --cap-add DAC_OVERRIDE --security-opt no-new-privileges \ |
| 51 | + -v "${TEST_ENTRYPOINT}:/test-entrypoint.sh:ro" \ |
| 52 | + -v "${TEST_SETUP}:/test-setup.sh:ro" \ |
| 53 | + --entrypoint /test-setup.sh "$@" |
| 54 | + ;; |
| 55 | + *) exit 1 ;; |
| 56 | +esac |
| 57 | +SH |
| 58 | +chmod +x "${TEST_SETUP}" "${tmp}/bin/docker" |
| 59 | + |
| 60 | +cat > "${tmp}/settings/.gitconfig" <<'CONFIG' |
| 61 | +[user] |
| 62 | + name = test |
| 63 | + email = test@example.invalid |
| 64 | +CONFIG |
| 65 | +echo saved > "${tmp}/settings/.claude.json" |
| 66 | + |
| 67 | +git init -q "${tmp}/module" |
| 68 | +echo module > "${tmp}/module/content" |
| 69 | +git -C "${tmp}/module" add content |
| 70 | +git -C "${tmp}/module" commit -qm module |
| 71 | + |
| 72 | +make_main() { |
| 73 | + git init -q "$1" |
| 74 | + cp "${SCRIPT_DIR}/../claude.sh" "$1/claude.sh" |
| 75 | + echo initial > "$1/content" |
| 76 | + git -C "$1" -c protocol.file.allow=always submodule add -q "${tmp}/module" module |
| 77 | + git -C "$1" add claude.sh content |
| 78 | + git -C "$1" commit -qm initial |
| 79 | +} |
| 80 | + |
| 81 | +make_worktree() { |
| 82 | + git -C "$1" worktree add --detach -q "$2" |
| 83 | + git -C "$2" -c protocol.file.allow=always submodule update --init -q |
| 84 | +} |
| 85 | + |
| 86 | +run_case() { |
| 87 | + local checkout=$1 name=$2 original original_module ref git_dir |
| 88 | + original=$(cat "${checkout}/.git") || return 1 |
| 89 | + original_module=$(cat "${checkout}/module/.git") || return 1 |
| 90 | + git_dir=$(git -C "${checkout}" rev-parse --absolute-git-dir) || return 1 |
| 91 | + echo fixture > "${tmp}/settings/.claude/settings.json" |
| 92 | + rm -f "${tmp}/settings/.cache/bazel-claude/marker" |
| 93 | + if ! env -u SSH_AUTH_SOCK -u ANTHROPIC_API_KEY -u AWS_ACCESS_KEY_ID \ |
| 94 | + -u AWS_SECRET_ACCESS_KEY -u GOOGLE_APPLICATION_CREDENTIALS -u BAZEL_CACHE_DIR \ |
| 95 | + HOME="${tmp}/settings" PATH="${tmp}/bin:${PATH}" \ |
| 96 | + bash "${checkout}/claude.sh" --shell --image "${image}" --name "claude-git-test-$$" <<'SH' |
| 97 | +set -eu |
| 98 | +test "$(git rev-parse --show-toplevel)" = "$(pwd -P)" |
| 99 | +test "$(git -C module rev-parse --show-toplevel)" = "$(pwd -P)/module" |
| 100 | +status=$(git status --porcelain) |
| 101 | +test -z "${status}" |
| 102 | +test "$(git -C module show HEAD:content)" = module |
| 103 | +git switch -qc "test-$(basename "$(git rev-parse --absolute-git-dir)")" |
| 104 | +echo modified > content |
| 105 | +git add content |
| 106 | +git -c commit.gpgsign=false commit -qm modified |
| 107 | +test "$(cat /workspace/content)" = modified |
| 108 | +test "$(git config --get gc.worktreePruneExpire)" = never |
| 109 | +test "$(cat "${HOME}/.claude/settings.json")" = fixture |
| 110 | +test "$(cat "${HOME}/.claude.json")" = saved |
| 111 | +if (echo changed >> "${HOME}/.gitconfig") 2>/dev/null; then |
| 112 | + exit 1 |
| 113 | +fi |
| 114 | +echo updated > "${HOME}/.claude/settings.json" |
| 115 | +touch "${HOME}/.cache/bazel/marker" |
| 116 | +SH |
| 117 | + then |
| 118 | + echo "FAIL: ${name}" >&2 |
| 119 | + return 1 |
| 120 | + fi |
| 121 | + ref=$(git -C "${checkout}" symbolic-ref HEAD) || return 1 |
| 122 | + if compgen -G "${git_dir}/claude-link.*" > /dev/null; then |
| 123 | + echo "FAIL: ${name}: temporary Git pointer was left behind" >&2 |
| 124 | + return 1 |
| 125 | + fi |
| 126 | + if [[ "$(cat "${checkout}/.git")" != "${original}" || |
| 127 | + "$(cat "${checkout}/module/.git")" != "${original_module}" || |
| 128 | + "$(git -C "${checkout}" show "${ref}:content")" != modified || |
| 129 | + "$(cat "${tmp}/settings/.claude/settings.json")" != updated || |
| 130 | + ! -f "${tmp}/settings/.cache/bazel-claude/marker" ]]; then |
| 131 | + echo "FAIL: ${name}: unexpected host state" >&2 |
| 132 | + return 1 |
| 133 | + fi |
| 134 | + echo "PASS: ${name}" |
| 135 | +} |
| 136 | + |
| 137 | +make_main "${tmp}/main" |
| 138 | +make_main "${workspace_fixture}/main" |
| 139 | +make_main "${home_fixture}/main" |
| 140 | +make_worktree "${tmp}/main" "${workspace_fixture}/worktree" |
| 141 | +make_worktree "${tmp}/main" "${home_fixture}/worktree" |
| 142 | +git -C "${workspace_fixture}/main" -c worktree.useRelativePaths=true \ |
| 143 | + worktree add --detach -q "${tmp}/workspace-data" |
| 144 | +git -C "${tmp}/workspace-data" -c protocol.file.allow=always submodule update --init -q |
| 145 | +make_worktree "${home_fixture}/main" "${tmp}/home-data" |
| 146 | + |
| 147 | +# A linked checkout can contain its main repository after the main |
| 148 | +# repository moves and Git repairs the links. |
| 149 | +nested_checkout="${workspace_fixture}/nested" |
| 150 | +make_main "${tmp}/nested-main" |
| 151 | +git -C "${tmp}/nested-main" worktree add --detach -q "${nested_checkout}" |
| 152 | +mv "${tmp}/nested-main" "${nested_checkout}/main" |
| 153 | +git -C "${nested_checkout}/main" worktree repair "${nested_checkout}" |
| 154 | +echo /main/ >> "${nested_checkout}/main/.git/info/exclude" |
| 155 | +git -C "${nested_checkout}" -c protocol.file.allow=always submodule update --init -q |
| 156 | + |
| 157 | +failed=0 |
| 158 | +run_case "${workspace_fixture}/worktree" "worktree under /workspace" || failed=1 |
| 159 | +run_case "${home_fixture}/worktree" "worktree under /home/claude-user" || failed=1 |
| 160 | +run_case "${tmp}/workspace-data" "Git data under /workspace" || failed=1 |
| 161 | +run_case "${tmp}/home-data" "Git data under /home/claude-user" || failed=1 |
| 162 | +run_case "${nested_checkout}" "Git data inside the worktree" || failed=1 |
| 163 | + |
| 164 | +# Use /workspace itself only after removing this test's nested fixtures. |
| 165 | +rm -rf "${workspace_fixture}" |
| 166 | +make_worktree "${tmp}/main" /workspace |
| 167 | +run_case /workspace "worktree at /workspace" || failed=1 |
| 168 | +# The caller provides /workspace. Remove the fixture files and let the exit |
| 169 | +# trap remove the temporary repository that stores their Git data. |
| 170 | +rm -rf -- /workspace/{.git,.gitmodules,claude.sh,content,module} |
| 171 | +workspace_entries=$(ls -A /workspace) |
| 172 | +test -z "${workspace_entries}" |
| 173 | +exit "${failed}" |
0 commit comments