#!/usr/bin/env bash

cat <<'EOF' >mise.toml
[tasks.interrupt]
run = """
touch "$READY_FILE"
sleep 30
"""
depends_post = ["cleanup"]

[tasks.cleanup]
run = [{ task = "cleanup-child" }]

[tasks.cleanup-child]
run = 'touch "$CLEANUP_FILE"'
EOF

ready_file="$TMPDIR/task-interrupt-cleanup-ready"
cleanup_file="$TMPDIR/task-interrupt-cleanup-ran"
output_file="$TMPDIR/task-interrupt-cleanup-output"
READY_FILE="$ready_file" CLEANUP_FILE="$cleanup_file" \
  mise run interrupt >"$output_file" 2>&1 &
mise_pid=$!
wait_for_file "$ready_file" "interruptible task child" 30 "$mise_pid"
kill -INT "$mise_pid"

status=0
wait "$mise_pid" || status=$?
output="$(cat "$output_file")"
if [[ $status -ne 130 ]]; then
  fail "mise run expected exit code 130 after Ctrl-C, got $status: $output"
fi
if [[ ! -e $cleanup_file ]]; then
  fail "post-dependency cleanup did not run after Ctrl-C: $output"
fi
