#!/usr/bin/env bash

# Regression test for #7776: --ignore / --ignore-file / --print-events are
# parsed by `mise watch` but must be forwarded onto the watchexec command line
# it builds. We assert on the `$ watchexec ...` line that mise logs (with
# MISE_DEBUG=1) right before spawning watchexec.
#
# watchexec is a stub that exits immediately, so `mise watch` runs to completion
# in the foreground. A real watchexec never exits on its own, and killing
# `mise watch` would orphan it (and its nested `mise run`) to write into the
# isolated dir during teardown.

mkdir -p "$HOME/bin"
cat >"$HOME/bin/watchexec" <<'STUB'
#!/usr/bin/env bash
exit 0
STUB
chmod +x "$HOME/bin/watchexec"
export PATH="$HOME/bin:$PATH"

mise tasks add example -- echo 'running example'

echo 'spec/**/*.rb' >.mise-watch-ignore

output_file=.watch_ignore_output
MISE_DEBUG=1 mise watch --ignore 'spec/**/*.rb' --ignore-file .mise-watch-ignore --print-events example >"${output_file}" 2>&1

assert_contains "cat ${output_file}" '$ watchexec '
assert_contains "cat ${output_file}" "--ignore spec/**/*.rb"
assert_contains "cat ${output_file}" "--ignore-file .mise-watch-ignore"
assert_contains "cat ${output_file}" "--print-events"
