#!/usr/bin/env bash

# A script discovered in a task directory is used by the path it was found at:
# its name is not a template. Only a TOML file = "..." string renders. A
# template-free script named {{ exec(...) }} loads without trust (its contents
# are inert), so resolving its path for "mise tasks --usage" (what completing
# "mise run" runs), "tasks info" and "tasks validate" must not render the name
# and run the command.

export MISE_TRUSTED_CONFIG_PATHS=""
unset CI GITHUB_ACTIONS GITHUB_ACTION 2>/dev/null || true

mkdir -p mise-tasks
name="{{ exec(command='touch name-marker') }}"
printf '#!/usr/bin/env bash\necho hi\n' >"mise-tasks/$name"
chmod +x "mise-tasks/$name"

assert_contains "MISE_YES=0 mise tasks --usage" "$name"
assert_contains "MISE_YES=0 mise tasks info \"$name\"" "mise-tasks/$name"
assert_succeed "MISE_YES=0 mise tasks validate"
assert_contains_text "$(mise __complete_word__ --shell fish --line 'mise run ')" "$name"
[[ ! -f name-marker ]] || fail "template in a task file's name was rendered"

# A TOML file = "..." string is still a template.
mkdir -p scripts
printf '#!/usr/bin/env bash\necho from-toml-file\n' >scripts/real.sh
chmod +x scripts/real.sh
cat <<'EOF' >mise.toml
[tasks.templated]
file = "{{ config_root }}/scripts/real.sh"
EOF
mise trust
assert "mise run templated" "from-toml-file"
