#!/usr/bin/env bash set -euo pipefail ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) HELPER="$ROOT/tools/in-dev-shell" REAL_JUST=$(command -v just) TMP=$(mktemp -d) trap 'rm -rf "$TMP"' EXIT fail() { echo "FAIL: $*" >&2 exit 1 } assert_args() { local actual=$1 shift printf '%s\0' "$@" > "$TMP/expected.args" cmp "$TMP/expected.args" "$actual" || fail "unexpected argv in $actual" } mkdir -p "$TMP/bin" "$TMP/no-nix" "$TMP/ldraw" "$TMP/runtime" ln -s "$(command -v bash)" "$TMP/bin/bash" ln -s "$(command -v jj)" "$TMP/bin/jj" ln -s "$(command -v tar)" "$TMP/bin/tar" ln -s "$(command -v bash)" "$TMP/no-nix/bash" cat > "$TMP/bin/just" <<'EOF' #!/usr/bin/env bash set -euo pipefail if [[ "${1:-}" == _child && -n "${FAKE_JUST_NESTED_ARGS:-}" ]]; then printf '%s\0' "$@" > "$FAKE_JUST_NESTED_ARGS" elif (($#)); then printf '%s\0' "$@" > "$FAKE_JUST_ARGS" else : > "$FAKE_JUST_ARGS" fi if [[ "${1:-}" == _environment ]]; then [[ "${POLYMODEL_NIX_SHELL:-}" == 1 ]] [[ -d "${LDRAWDIR:-}" ]] [[ "${LD_LIBRARY_PATH:-}" == *"$FAKE_RUNTIME"* ]] [[ "${DISPLAY:-}" == :77 ]] [[ "${WAYLAND_DISPLAY:-}" == wayland-test ]] fi if [[ "${1:-}" == _nested ]]; then exec "$HELPER" _child 'nested value; $HOME' fi exit "${FAKE_JUST_EXIT:-0}" EOF chmod +x "$TMP/bin/just" ln -s "$TMP/bin/just" "$TMP/no-nix/just" cat > "$TMP/bin/nix" <<'EOF' #!/usr/bin/env bash set -euo pipefail printf '%s\0' "$@" > "$FAKE_NIX_ARGS" [[ ${1:-} == develop && -n ${2:-} && ${3:-} == --command ]] || exit 64 shift 3 export POLYMODEL_NIX_SHELL=1 export LDRAWDIR="$FAKE_LDRAWDIR" export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}:$FAKE_RUNTIME" exec "$@" EOF chmod +x "$TMP/bin/nix" run_outside() { : > "$TMP/nix.args" : > "$TMP/just.args" : > "$TMP/nested.args" env -u POLYMODEL_NIX_SHELL -u LDRAWDIR -u LD_LIBRARY_PATH \ PATH="$TMP/bin:$PATH" \ DISPLAY=:77 \ WAYLAND_DISPLAY=wayland-test \ FAKE_NIX_ARGS="$TMP/nix.args" \ FAKE_JUST_ARGS="$TMP/just.args" \ FAKE_JUST_NESTED_ARGS="$TMP/nested.args" \ FAKE_JUST="$TMP/bin/just" \ FAKE_LDRAWDIR="$TMP/ldraw" \ FAKE_RUNTIME="$TMP/runtime" \ HELPER="$HELPER" \ "$HELPER" "$@" } run_outside _demo 'path with spaces; $(printf injected)' '--flag=value' mapfile -d '' -t nix_args < "$TMP/nix.args" [[ ${nix_args[0]} == develop && -n ${nix_args[1]} && ${nix_args[2]} == --command ]] || fail 'outside Nix dispatch prefix changed' printf '%s\0' "$TMP/bin/just" _demo 'path with spaces; $(printf injected)' '--flag=value' > "$TMP/expected.args" printf '%s\0' "${nix_args[@]:3}" > "$TMP/actual-command.args" cmp "$TMP/expected.args" "$TMP/actual-command.args" || fail 'outside Nix command argv changed' printf '%s\0' _demo 'path with spaces; $(printf injected)' '--flag=value' > "$TMP/expected.args" cmp "$TMP/expected.args" "$TMP/just.args" || fail 'outside Just argv changed' run_outside _environment set +e env -u POLYMODEL_NIX_SHELL -u LDRAWDIR -u LD_LIBRARY_PATH \ PATH="$TMP/bin" \ FAKE_NIX_ARGS="$TMP/nix.args" \ FAKE_JUST_ARGS="$TMP/just.args" \ FAKE_JUST="$TMP/bin/just" \ FAKE_LDRAWDIR="$TMP/ldraw" \ FAKE_RUNTIME="$TMP/runtime" \ HELPER="$HELPER" \ FAKE_JUST_EXIT=37 \ "$HELPER" _exit-test status=$? set -e [[ $status -eq 37 ]] || fail "exit status was $status, expected 37" : > "$TMP/just.args" env POLYMODEL_NIX_SHELL=1 \ LDRAWDIR="$TMP/ldraw" \ PATH="$TMP/bin" \ FAKE_JUST_ARGS="$TMP/just.args" \ "$HELPER" _direct 'inside value' assert_args "$TMP/just.args" _direct 'inside value' : > "$TMP/nix.args" : > "$TMP/just.args" run_outside _nested assert_args "$TMP/just.args" _nested assert_args "$TMP/nested.args" _child 'nested value; $HOME' [[ $(wc -c < "$TMP/nix.args") -gt 0 ]] || fail 'nested public/private dispatch skipped outer shell entry' set +e env -u POLYMODEL_NIX_SHELL PATH="$TMP/no-nix" "$HELPER" _missing-nix 2>"$TMP/missing.stderr" status=$? set -e [[ $status -eq 127 ]] || fail "missing nix status was $status, expected 127" grep -F 'nix is required' "$TMP/missing.stderr" >/dev/null || fail 'missing nix diagnostic missing' TEST_JUSTFILE="$TMP/justfile" cat > "$TEST_JUSTFILE" </dev/null || fail 'workspace-create does not allow the new workspace envrc' grep -F 'direnv exec "$workspace_path" true' "$ROOT/justfile" >/dev/null || fail 'workspace-create does not verify the new workspace envrc' printf 'PASS: in-dev-shell dispatch boundary\n'