#!/usr/bin/env bash

cat <<EOF >mise.toml
[tasks.echo-args]
run = 'echo args:'

[tasks.hello]
run = 'echo hello'

[tasks.check-tiny]
# tiny is not installed, so this would fail without --skip-tools
# if tools were being installed, mise would try to install tiny
tools = { tiny = "1.0.0" }
run = 'echo ran'
EOF

# --skip-tools before task name is a mise flag (skips tool install, task runs)
assert "mise run --skip-tools hello" "hello"

# --skip-tools after task name is a task arg (passed through to task)
assert "mise run echo-args --skip-tools" "args: --skip-tools"

# verify --skip-tools skips task-level tool installation
# task declares tools = { tiny = "1.0.0" } but --skip-tools prevents install attempt
assert "mise run --skip-tools check-tiny" "ran"

credential_marker="$HOME/github-credential-command-called"
credential_command="$HOME/fake-github-credential-command"
cat >"$credential_command" <<EOF
#!/usr/bin/env bash
touch "$credential_marker"
echo fake-token
EOF
chmod +x "$credential_command"

mkdir -p "$MISE_CONFIG_DIR"
cat >"$MISE_CONFIG_DIR/config.toml" <<EOF
[settings]
auto_install = false
github.credential_command = "$credential_command"
minimum_release_age = "3d"
EOF

cat >mise.toml <<EOF
[tools]
"github:cli/cli" = "latest"

[tasks.nop-test]
run = "echo Hello World!"
EOF

assert "mise run --skip-tools nop-test" "Hello World!"
assert_fail "test -f '$credential_marker'"

rm -f "$credential_marker"

assert "mise run nop-test" "Hello World!"
assert_fail "test -f '$credential_marker'"
