#!/usr/bin/env bash

# MISE_ORIGINAL_CWD is the directory mise was started in, not the --cd target
mkdir -p caller project
cat >project/mise.toml <<'TOML'
[tasks.show]
quiet = true
run = 'printf "%s\n" "$MISE_ORIGINAL_CWD"'
TOML
mise trust project/mise.toml
cat >project/show-file <<'SH'
#!/usr/bin/env bash
#MISE quiet=true
printf '%s\n' "$MISE_ORIGINAL_CWD"
SH
chmod +x project/show-file

# Resolve the reported directory without interpolating it into shell code
reported_cwd() {
  local out
  out="$("$@")" || return 1
  case "$out" in /*) ;; *) return 1 ;; esac
  (cd "$out" && pwd -P)
}
export -f reported_cwd

caller="$(cd caller && pwd -P)"
assert "cd caller && reported_cwd mise -C ../project run show" "$caller"
assert "cd caller && reported_cwd mise run -C ../project show" "$caller"
assert "cd caller && MISE_CD=../project reported_cwd mise run show" "$caller"
assert "cd caller && reported_cwd mise -C ../project run ./show-file" "$caller"
assert "cd project && reported_cwd mise run show" "$(cd project && pwd -P)"
