#!/usr/bin/env bash # Offline test: the QEMU initramfs scripts pass the same shellcheck the qemu-tools CI job runs # (issue #18: `for w in $(cat /proc/cmdline)` failed it with SC2013), and the check really fires. # Skips with a notice when shellcheck is not installed (CI installs it). set -uo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO="$(cd "$HERE/../.." && pwd)" TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT FAIL=0 ok() { printf '[PASS] %s\n' "$1"; } bad() { printf '[FAIL] %s\n' "$1"; FAIL=1; } if ! command -v shellcheck >/dev/null 2>&1; then echo "[SKIP] shellcheck is not installed"; exit 0 fi cd "$REPO" || exit 1 if shellcheck -x qemu/rootfs/init qemu/rootfs/etc/rc qemu/rootfs/sbin/init > "$TMP/out" 2>&1; then ok "qemu/rootfs scripts pass shellcheck" else bad "qemu/rootfs scripts fail shellcheck"; head -20 "$TMP/out" fi if grep -q 'read -r CMDLINE < /proc/cmdline' qemu/rootfs/init; then ok "init reads /proc/cmdline into a variable" else bad "init no longer reads /proc/cmdline into a variable" fi # the old form must be red # shellcheck disable=SC2016 # the text is the literal old source line old_loop='for w in $(cat /proc/cmdline); do' sed -e '/^read -r CMDLINE < \/proc\/cmdline$/d' -e "s|^for w in \\\$CMDLINE; do\$|$old_loop|" qemu/rootfs/init > "$TMP/init-old" grep -qF "$old_loop" "$TMP/init-old" || bad "the doctored copy was not built" out="$(shellcheck -x "$TMP/init-old" 2>&1 || true)" if printf '%s' "$out" | grep -q 'SC2013'; then ok "doctored: the cat-in-a-for-loop form is reported as SC2013" else bad "doctored: the old form was not caught" fi if [ "$FAIL" -eq 0 ]; then echo "ALL QEMU-INIT SHELLCHECK TESTS PASSED"; else echo "QEMU-INIT SHELLCHECK TESTS FAILED"; fi exit $FAIL