Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions .mise/tasks/lint/caller-pwd-contract
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand All @@ -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_]*) ;;
Expand Down
21 changes: 21 additions & 0 deletions lib/codebase-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ codebase_resolve_repo() {
(cd "$(dirname "$resolved")" && pwd -P)
}

# codebase_name <target>
#
# 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 <repo-root>
#
# Emit configured [_.codebase].lint rules, one per line. Rule names are expected
Expand Down
73 changes: 54 additions & 19 deletions test/lint/caller-pwd-contract/caller-pwd-contract.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,62 @@ 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 ]
[[ "$output" == *"OK"* ]]
}

@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 ]
Expand All @@ -50,38 +73,50 @@ 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 ]
[[ "$output" == *"uses THREADS_CALLER_PWD instead of MODULES_CALLER_PWD"* ]]
}

@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 ]
[[ "$output" == *"exports legacy CALLER_PWD at runtime"* ]]
}

@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 ]
[[ "$output" == *"assigns legacy CALLER_PWD at runtime"* ]]
}

@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 ]
Expand Down