diff --git a/.mise/tasks/lint/caller-pwd-contract b/.mise/tasks/lint/caller-pwd-contract index 919a896..1d3d25f 100755 --- a/.mise/tasks/lint/caller-pwd-contract +++ b/.mise/tasks/lint/caller-pwd-contract @@ -4,8 +4,8 @@ set -euo pipefail -# shellcheck source=../../../lib/shell-files.sh -source "$MISE_CONFIG_ROOT/lib/shell-files.sh" +# shellcheck source=../../../lib/codebase-config.sh +source "$MISE_CONFIG_ROOT/lib/codebase-config.sh" IFS=' ' read -ra TARGETS <<< "${usage_targets}" @@ -21,11 +21,7 @@ done caller_var_for_target() { local target="$1" local name var - target="${target%/}" - if [[ "$(basename "$target")" == "." || "$(basename "$target")" == ".." ]]; then - target=$(cd "$target" && pwd -P) - fi - name=$(basename "$target") + name=$(codebase_name "$target") var=$(printf '%s' "$name" | tr '[:lower:]' '[:upper:]' | sed 's/[^A-Z0-9_]/_/g') case "$var" in [A-Z_]*) ;; diff --git a/lib/codebase-config.sh b/lib/codebase-config.sh index 2d43820..14c5052 100644 --- a/lib/codebase-config.sh +++ b/lib/codebase-config.sh @@ -54,6 +54,27 @@ codebase_resolve_repo() { (cd "$(dirname "$resolved")" && pwd -P) } +# codebase_name +# +# Emit the stable machine name configured at [_.codebase].name. Fall back to +# the repository directory name for codebases that have not declared one. +codebase_name() { + local target="$1" + local repo_root toml value + + repo_root=$(codebase_resolve_repo "$target") || return 1 + toml="$repo_root/mise.toml" + + if [[ -f "$toml" ]] && value=$(mise config get -f "$toml" _.codebase.name 2>/dev/null); then + if [[ -n "$value" ]]; then + printf '%s\n' "$value" + return 0 + fi + fi + + basename "$repo_root" +} + # codebase_configured_lint_rules # # Emit configured [_.codebase].lint rules, one per line. Rule names are expected diff --git a/test/lint/caller-pwd-contract/caller-pwd-contract.bats b/test/lint/caller-pwd-contract/caller-pwd-contract.bats index f79fa73..7ecf635 100644 --- a/test/lint/caller-pwd-contract/caller-pwd-contract.bats +++ b/test/lint/caller-pwd-contract/caller-pwd-contract.bats @@ -9,19 +9,19 @@ setup() { } make_pkg() { - local name="$1" - local body="$2" - local dir="$WORK/$name" + local dir="$1" mkdir -p "$dir/.mise/tasks" - printf '%s\n' "$body" > "$dir/.mise/tasks/demo" + cat > "$dir/.mise/tasks/demo" chmod +x "$dir/.mise/tasks/demo" - echo "$dir" } @test "passes when package-specific caller var precedes legacy fallback" { - target=$(make_pkg clean '#!/usr/bin/env bash + target="$WORK/clean" + make_pkg "$target" <<'BASH' +#!/usr/bin/env bash TARGET="${CLEAN_CALLER_PWD:-${CALLER_PWD:-.}}" -printf "%s\n" "$TARGET"') +printf "%s\n" "$TARGET" +BASH run codebase lint:caller-pwd-contract "$target" [ "$status" -eq 0 ] @@ -29,19 +29,42 @@ printf "%s\n" "$TARGET"') } @test "dot target derives package name from physical repo directory" { - target=$(make_pkg shimmer '#!/usr/bin/env bash + target="$WORK/shimmer" + make_pkg "$target" <<'BASH' +#!/usr/bin/env bash TARGET="${SHIMMER_CALLER_PWD:-${CALLER_PWD:-.}}" -printf "%s\n" "$TARGET"') +printf "%s\n" "$TARGET" +BASH CODEBASE_CALLER_PWD="$target" run codebase lint:caller-pwd-contract . [ "$status" -eq 0 ] [[ "$output" == *"expects SHIMMER_CALLER_PWD"* ]] } +@test "configured codebase name survives a renamed checkout directory" { + target="$WORK/notes-review" + make_pkg "$target" <<'BASH' +#!/usr/bin/env bash +TARGET="${NOTES_CALLER_PWD:-${CALLER_PWD:-.}}" +printf "%s\n" "$TARGET" +BASH + cat > "$target/mise.toml" <<'TOML' +[_.codebase] +name = "notes" +TOML + + run codebase lint:caller-pwd-contract "$target" + [ "$status" -eq 0 ] + [[ "$output" == *"expects NOTES_CALLER_PWD"* ]] +} + @test "fails on SHIV_CALLER_PWD in a non-shiv package" { - target=$(make_pkg shimmer '#!/usr/bin/env bash + target="$WORK/shimmer" + make_pkg "$target" <<'BASH' +#!/usr/bin/env bash TARGET="${SHIV_CALLER_PWD:-${CALLER_PWD:-.}}" -printf "%s\n" "$TARGET"') +printf "%s\n" "$TARGET" +BASH run codebase lint:caller-pwd-contract "$target" [ "$status" -eq 1 ] @@ -50,9 +73,12 @@ printf "%s\n" "$TARGET"') } @test "fails on any non-package caller var, not just SHIV_CALLER_PWD" { - target=$(make_pkg modules '#!/usr/bin/env bash + target="$WORK/modules" + make_pkg "$target" <<'BASH' +#!/usr/bin/env bash TARGET="${THREADS_CALLER_PWD:-.}" -printf "%s\n" "$TARGET"') +printf "%s\n" "$TARGET" +BASH run codebase lint:caller-pwd-contract "$target" [ "$status" -eq 1 ] @@ -60,9 +86,12 @@ printf "%s\n" "$TARGET"') } @test "fails when runtime exports legacy CALLER_PWD" { - target=$(make_pkg bad-export '#!/usr/bin/env bash + target="$WORK/bad-export" + make_pkg "$target" <<'BASH' +#!/usr/bin/env bash export CALLER_PWD="$PWD" -exec mise run "$@"') +exec mise run "$@" +BASH run codebase lint:caller-pwd-contract "$target" [ "$status" -eq 1 ] @@ -70,8 +99,11 @@ exec mise run "$@"') } @test "fails when runtime assigns legacy CALLER_PWD" { - target=$(make_pkg bad-assign '#!/usr/bin/env bash -CALLER_PWD="$PWD" exec mise run "$@"') + target="$WORK/bad-assign" + make_pkg "$target" <<'BASH' +#!/usr/bin/env bash +CALLER_PWD="$PWD" exec mise run "$@" +BASH run codebase lint:caller-pwd-contract "$target" [ "$status" -eq 1 ] @@ -79,9 +111,12 @@ CALLER_PWD="$PWD" exec mise run "$@"') } @test "allows SHIV_CALLER_PWD inside the shiv package itself" { - target=$(make_pkg shiv '#!/usr/bin/env bash + target="$WORK/shiv" + make_pkg "$target" <<'BASH' +#!/usr/bin/env bash TARGET="${SHIV_CALLER_PWD:-${CALLER_PWD:-.}}" -printf "%s\n" "$TARGET"') +printf "%s\n" "$TARGET" +BASH run codebase lint:caller-pwd-contract "$target" [ "$status" -eq 0 ]