#!/bin/sh
# Pre-commit hook: run the Battle for 'Tismo test suite and refuse the
# commit when anything fails. Installed by scripts/install-git-hooks.sh, which
# points the repository at this directory via core.hooksPath.
#
# Skip it once with: git commit --no-verify

set -e

# Locate the project root from this hook's own path, so the hook works no
# matter which directory the commit was invoked from.
PROJECT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$PROJECT_DIR"

echo "pre-commit: running the test suite..."

if command -v node >/dev/null 2>&1; then
  node tests/run_tests.js
elif command -v nodejs >/dev/null 2>&1; then
  nodejs tests/run_tests.js
elif command -v nix-shell >/dev/null 2>&1; then
  # Node is not on PATH; use the pinned nixpkgs one like the dev environment.
  nix-shell -p nodejs --run "node tests/run_tests.js"
else
  echo "pre-commit: no Node.js runtime found (looked for node, nodejs, nix-shell)." >&2
  echo "pre-commit: install Node.js, or commit with --no-verify to skip the tests." >&2
  exit 1
fi
