#!/bin/bash
# Prove the ShellCheck gate never inherits an indefinitely open stdin.
#
# `shellcheck -x` follows a `source=/dev/stdin` directive by opening that path.
# This repo's scripts use exactly that directive to load the Bash kit that
# `afdata shell bash` writes at runtime, so an analyzer handed a stdin that
# never reaches EOF waits on it forever — silently, with no output and no
# timeout. CI happens to hand the gate a closed stdin, so the failure only
# appears on a developer's terminal, where it reads as "the tests hang".
#
# The check runs the gate against `scripts/fixtures/shellcheck-open-stdin.sh`
# with a pipe held open and requires it to finish within a deadline.
#
# Usage: validate_shellcheck_stdin.sh
set -euo pipefail

ROOTPATH="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SCRIPT_DIR="$ROOTPATH/scripts"

# shellcheck source-path=SCRIPTDIR source=in_tree_afdata.sh
. "$ROOTPATH/scripts/in_tree_afdata.sh"

afdata_args_begin "validate_shellcheck_stdin.sh [OPTIONS]"
afdata_args_parse "$@"

INSTALLER="$SCRIPT_DIR/install-shellcheck.sh"
FIXTURE_DIR="$SCRIPT_DIR/fixtures"
DEADLINE_S=20

if [ ! -f "$INSTALLER" ]; then
  afdata_error shellcheck_installer_missing "missing $INSTALLER" || exit 1
fi
if [ ! -f "$FIXTURE_DIR/shellcheck-open-stdin.sh" ]; then
  afdata_error shellcheck_fixture_missing "missing the open-stdin fixture under $FIXTURE_DIR" || exit 1
fi

afdata_mktemp -d WORK

# Name the interpreter rather than relying on the shebang: Windows resolves a
# program through `CreateProcess`, which has no idea what a `#!` line is.
#
# The gate's stdin is a pipe whose writer, a `sleep`, writes nothing and never
# closes it: an interactive terminal's shape, not a redirect the gate could
# accidentally satisfy. The sleep outlives the deadline. The command is one
# process group (afdata_timeout starts it as one): when the deadline passes
# afdata_timeout stops the gate and the sleep together and reports 124, and
# when the gate finishes first the group is signalled so the sleep is not left
# behind.
status=0
# shellcheck disable=SC2016
afdata_timeout "$DEADLINE_S" bash -c '
  trap : TERM
  gate_status=0
  bash "$2" --check "$3" < <(sleep "$1") || gate_status=$?
  kill -TERM -- "-$$" 2>/dev/null || :
  exit "$gate_status"
' _ $((DEADLINE_S + 30)) "$INSTALLER" "$FIXTURE_DIR" \
  >"$WORK/out" 2>"$WORK/err" || status=$?

if [ "$status" -eq 124 ]; then
  afdata_error shellcheck_stdin_inherited \
    "the ShellCheck gate inherited an open stdin and did not finish within ${DEADLINE_S}s" || exit 1
fi

if [ "$status" -ne 0 ]; then
  cat "$WORK/out" "$WORK/err" >&2
  afdata_error shellcheck_gate_failed "the ShellCheck gate exited $status on its own fixture" || exit 1
fi

# Finishing proves nothing where no analyzer ran: the deadline is about
# ShellCheck's own stdin, and on a platform upstream publishes no build for
# there is none to hold open. Say that, rather than report a pass earned by the
# gate having had nothing to do.
if grep -q "ShellCheck skipped" "$WORK/out"; then
  afdata_log warn "no pinned ShellCheck on this platform, so the gate has no stdin to inherit; the Linux and macOS runs carry this check"
fi
afdata_result "ShellCheck gate finishes with an open stdin"
