#!/usr/bin/env bash

cat <<EOF >.mise.toml
[tasks.configtask]
run = 'echo "configtask:"'
[tasks.lint]
run = 'echo "linting!"'
[tasks.test]
run = 'echo "testing!"'
[tasks.test-with-args]
run = 'echo "{{arg()}} {{flag(name="force")}} {{option(name="user")}}"'
EOF

mkdir -p .mise/tasks
cat <<'EOF' >.mise/tasks/filetask
#!/usr/bin/env bash
#MISE description="This is a test build script"
#MISE depends=["lint", "test"]
#MISE sources=[".test-tool-versions"]
#MISE outputs=["$MISE_PROJECT_ROOT/test-e2e/test-build-output.txt"]
#MISE env={TEST_BUILDSCRIPT_ENV_VAR = "VALID"}

set -euxo pipefail
cd "$MISE_PROJECT_ROOT" || exit 1
echo "running test-build script"
mkdir -p test-e2e
echo "TEST_BUILDSCRIPT_ENV_VAR: $TEST_BUILDSCRIPT_ENV_VAR" >test-e2e/test-build-output.txt
echo "ARGS: $*" >>test-e2e/test-build-output.txt
EOF
chmod +x .mise/tasks/filetask

mise r --cd "$PWD" configtask arg1 arg2 ::: filetask arg1 arg2
assert "cat test-e2e/test-build-output.txt" "TEST_BUILDSCRIPT_ENV_VAR: VALID
ARGS: arg1 arg2"

assert "mise run test arg1 arg2 arg3" "testing! arg1 arg2 arg3"
assert "mise run test-with-args foo --force --user=user" "foo true user"

cat <<'EOF' >.mise/tasks/filetask
#!/usr/bin/env bash
#USAGE flag "-u --user <user>" help="User to run as"

echo "user=$usage_user"
EOF
assert "mise run filetask --user=jdx" "user=jdx"
