#!/usr/bin/env bash # The test suite. # # Runs inside the Docker `test` stage, which is what ./test.sh builds. Do not # expect it to work on a laptop directly - it wants the JDK, the extracted # MegaMek release and the patched jar the build stage produced: # # MM_HOME extracted MegaMek release # PATCHED_JAR the jar megamek/apply-patches.sh produced # JAVA_HOME a JDK # # ./scripts/test.sh from the repo root, the supported way # ./tests/run.sh lint one group, if you are already inside the stage # # Groups, fastest first, so a break surfaces early: # # lint shellcheck every shell script # syntax bash -n; the JSON and XML templates parse # unit the container's shell logic, against fixtures # compile javac against the stock MegaMek jar; patch verification set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$ROOT" PASS=0 FAIL=0 SKIP=0 ok() { printf ' \033[32mok\033[0m %s\n' "$1"; PASS=$((PASS + 1)); } bad() { printf ' \033[31mFAIL\033[0m %s\n' "$1"; FAIL=$((FAIL + 1)); } skip() { printf ' \033[33mskip\033[0m %s (%s)\n' "$1" "$2"; SKIP=$((SKIP + 1)); } group() { printf '\n\033[1m%s\033[0m\n' "$1"; } # check check() { local name="$1"; shift if out="$("$@" 2>&1)"; then ok "$name" else bad "$name" printf '%s\n' "$out" | sed 's/^/ /' fi } WANT="${1:-all}" want() { [ "$WANT" = "all" ] || [ "$WANT" = "$1" ]; } # The build context has no .git, so file discovery is a plain find. mapfile -t SH < <(find . -name '*.sh' -not -path './.git/*' | sort) # --- lint -------------------------------------------------------------------- if want lint; then group "lint" if command -v shellcheck >/dev/null; then check "shellcheck" ./tests/shell/lint.sh else skip "shellcheck" "not installed" fi fi # --- syntax ------------------------------------------------------------------ if want syntax; then group "syntax" for f in "${SH[@]}"; do check "bash -n $f" bash -n "$f" done # The suramadu template is JSON with @PLACEHOLDER@ markers inside string # values, so it must parse as JSON even before substitution. check "suramadu.config.template parses" jq -e . suramadu/suramadu.config.template check "clientsettings.xml.template parses" \ python3 -c "import xml.etree.ElementTree as E; E.parse('megamek/clientsettings.xml.template')" # Not an XML parse - index.html is HTML5, with void elements that no XML # parser accepts. What matters is that our stylesheet is loaded after # upstream's, because the overrides win on order, not specificity. check "theme.css is linked after the war's style.css" bash -c ' s=$(grep -n "css/style.css" suramadu/web/index.html | cut -d: -f1) o=$(grep -n "\"theme.css\"" suramadu/web/index.html | cut -d: -f1) [ -n "$s" ] && [ -n "$o" ] && [ "$o" -gt "$s" ]' check "the FOUC guard is inline, before any stylesheet" bash -c ' i=$(grep -n "