split-util.sh raw

   1  #!/bin/bash
   2  # Unit layer for the test suite.
   3  #
   4  # The suite used to be one process per phase: a phase ran its fixtures in a
   5  # loop, and anything that made it stop - a fixture that hung the compiler, a
   6  # crash that took the runner with it, a ctrl-C because 40 minutes had passed -
   7  # threw away every fixture already run, including the 60 that had passed. The
   8  # phase was the unit of work AND the unit of loss.
   9  #
  10  # A phase now offers its fixtures as units. Each unit is one compiler
  11  # invocation in its own temp dir with its own log and its own exit code, and
  12  # the driver (tests/unitrun.sh) runs them independently, in parallel, with a
  13  # per-unit timeout, keeping each unit's result on disk. A unit that fails or
  14  # hangs costs exactly that unit; the rest still run and still report.
  15  #
  16  # A phase script supporting units handles:
  17  #   --list          print one unit name per line
  18  #   --name IDX      print unit IDX (0-based, the --list order)
  19  #   --unit IDX      run unit IDX; exit 0 pass, 1 fail, 2 skip
  20  # Inside --unit the phase script sees UNIT_NAME already set, so it dispatches
  21  # on the name and never re-derives its own ordering.
  22  
  23  # ---------------------------------------------------------------- unit lists
  24  # A phase's units are the entries of its UNITS array, in order. The driver asks
  25  # for the list, then addresses a unit by its index; the phase script turns that
  26  # index back into a name and dispatches on the name.
  27  unit_list() {
  28  	local u
  29  	for u in "${UNITS[@]}"; do echo "$u"; done
  30  }
  31  
  32  unit_name() {
  33  	local i="${1:-}"
  34  	[ -n "$i" ] || { echo "usage: $0 --name IDX" >&2; return 1; }
  35  	[ "$i" -ge 0 ] && [ "$i" -lt "${#UNITS[@]}" ] || {
  36  		echo "unit index $i out of range (0..$((${#UNITS[@]} - 1)))" >&2
  37  		return 1
  38  	}
  39  	echo "${UNITS[$i]}"
  40  }
  41  
  42  # Dispatch shared by every phase script:
  43  #   unit_args "$@"
  44  # returns 0 when it handled the arguments and the caller must exit with $?.
  45  unit_args() {
  46  	local mode="${1:-}" arg="${2:-}"
  47  	case "$mode" in
  48  	--list) unit_list; exit 0 ;;
  49  	--name) unit_name "$arg"; exit $? ;;
  50  	--unit)
  51  		[ -n "$arg" ] || { echo "usage: $0 --unit IDX" >&2; exit 1; }
  52  		UNIT_INDEX="$arg"
  53  		UNIT_NAME="$(unit_name "$arg")" || exit 1
  54  		export UNIT_INDEX UNIT_NAME
  55  		return 0
  56  		;;
  57  	esac
  58  	if [ -z "$mode" ]; then
  59  		UNIT_NAME="${UNIT_NAME:-}"
  60  		return 0
  61  	fi
  62  	echo "unknown phase argument: $mode" >&2
  63  	exit 1
  64  }
  65  
  66  # ------------------------------------------------------------------ one unit
  67  # Everything one unit needs: an isolated work dir plus the status file the
  68  # shared log_* helpers write to. UNIT_NAME is the fixture; two fixture names
  69  # that differ only in characters unusable in a path are the same fixture.
  70  #
  71  # The work dir is NOT removed here. It holds the build log, which is the
  72  # evidence for a unit that failed; the driver removes it only for a unit that
  73  # passed, and reports the path for one that did not.
  74  unit_begin() {
  75  	UNIT_WORK="$(tempdir "u_$(echo "$UNIT_NAME" | tr -c 'A-Za-z0-9_.-' '_')")"
  76  	rm -rf "$UNIT_WORK"
  77  	mkdir -p "$UNIT_WORK"
  78  	# The verdict is written beside the STATUSFILE the driver set, before
  79  	# unit_begin replaces it with the work dir's own. The driver reports what is
  80  	# in ITS state dir; the phase script's work dir is the fixture's, not the
  81  	# driver's.
  82  	UNIT_STATE="$(dirname "$STATUSFILE")"
  83  	export STATUSFILE="$UNIT_WORK/status"
  84  	: >"$STATUSFILE"
  85  }
  86  
  87  # ------------------------------------------------------------------- verdict
  88  # The verdict is written to two places on purpose. The shared log_* helpers
  89  # write human lines to stdout and append to STATUSFILE, which is how the driver
  90  # tells pass from fail; the `verdict`/`reason` fields beside STATUSFILE carry
  91  # the one-line summary the driver reports. Parsing the phase script's stdout for
  92  # it would make the summary depend on log formatting, which is not a contract.
  93  unit_verdict() {
  94  	local v="$1" reason="$2" dir="${UNIT_STATE:-$(dirname "$STATUSFILE")}"
  95  	printf '%s\n' "$v" >"$dir/verdict"
  96  	printf '%s\n' "$reason" | tr '\n\t' '  ' >"$dir/reason"
  97  	# Bind the verdict to the tree it describes. A verdict that can outlive its
  98  	# commit is the pass/fail version of a dead diagnostic sink: it made the
  99  	# driver report a FAIL from before the fixture was repaired.
 100  	local c st
 101  	c="$(git -C "$ROOT" rev-parse HEAD 2>/dev/null || true)"
 102  	st="$(git -C "$ROOT" status --porcelain 2>/dev/null | sha256sum | cut -d' ' -f1)"
 103  	printf '%s %s\n' "$c" "$st" >"$dir/commit"
 104  }
 105  
 106  # unit_ok NAME REASON / unit_no NAME REASON / unit_skipped NAME REASON: the only
 107  # way a unit reports. One call per unit run.
 108  unit_ok() {
 109  	log_pass "$1" "$2"
 110  	unit_verdict PASS "$2"
 111  }
 112  unit_no() {
 113  	log_fail "$1" "$2"
 114  	unit_verdict FAIL "$2"
 115  }
 116  unit_skipped() {
 117  	log_skip "$1" "$2"
 118  	unit_verdict SKIP "$2"
 119  }
 120  
 121  # unit_exit: derive the exit code the driver records from the verdict. 0 pass,
 122  # 1 fail, 2 skip.
 123  unit_exit() {
 124  	local v
 125  	v="$(cat "${UNIT_STATE:-$(dirname "$STATUSFILE")}/verdict" 2>/dev/null || echo FAIL)"
 126  	case "$v" in
 127  	PASS) return 0 ;;
 128  	SKIP) return 2 ;;
 129  	*) return 1 ;;
 130  	esac
 131  }
 132  
 133  # first_error LOG [EXTRA_GREP]: the first line of a build log that says what
 134  # went wrong. The compiler prefixes real failures with "; ", "mxc:" or
 135  # "error:"; an LLVM verifier complaint is prefixed with neither and only shows
 136  # up in a tail, which is why the tail fallback exists.
 137  first_error() {
 138  	local log="$1" extra="${2:-}" line=""
 139  	if [ "$extra" != "" ]; then
 140  		line="$(grep -E "$extra" "$log" 2>/dev/null | head -1 || true)"
 141  	fi
 142  	if [ -z "$line" ]; then
 143  		line="$(grep -E '^; |^mxc:|error:' "$log" 2>/dev/null |
 144  			grep -v '^  compile errors$' | head -1 || true)"
 145  	fi
 146  	if [ -z "$line" ]; then
 147  		line="$(grep -E 'panic:|caught signal|Assertion|defined with type' "$log" 2>/dev/null | head -1 || true)"
 148  	fi
 149  	if [ -z "$line" ]; then
 150  		line="$(tail -3 "$log" 2>/dev/null | head -1 || true)"
 151  	fi
 152  	echo "$line"
 153  }
 154