helpers.sh (1924B)
1 #!/bin/sh 2 # tests/helpers.sh - shared assertions, source with `. tests/helpers.sh` 3 # 4 # Assertions only. linkd is a standalone daemon and its tests build and run it 5 # directly, so none of the QEMU/SSH plumbing from the OS-assembly repo belongs 6 # here -- anything needing a booted system is an integration test and lives in 7 # a consuming OS repo. 8 set -eu 9 10 assert_eq() { 11 # assert_eq <got> <expected> <msg> 12 if [ "$1" != "$2" ]; then 13 echo "FAIL: $3: expected '$2', got '$1'" >&2 14 return 1 15 fi 16 echo "PASS: $3" 17 } 18 19 assert_contains() { 20 # assert_contains <haystack> <needle> <msg> 21 case "$1" in 22 *"$2"*) echo "PASS: $3" ;; 23 *) echo "FAIL: $3: expected to contain '$2', got '$1'" >&2; return 1 ;; 24 esac 25 } 26 27 assert_file_contains() { 28 # assert_file_contains <file> <needle> <msg> 29 # `--` matters: without it a needle starting with '-' (any command-line flag) 30 # is parsed by grep as options and never matches. 31 if grep -qF -- "$2" "$1" 2>/dev/null; then 32 echo "PASS: $3" 33 else 34 echo "FAIL: $3: $1 does not contain '$2'" >&2 35 return 1 36 fi 37 } 38 39 # Build linkd once, on demand. Every test needs the objects, and rebuilding per 40 # test would dominate the runtime. 41 ensure_built() { 42 # ensure_built <repo-root> 43 _root=$1 44 _bd="${_root}/build/${LINKD_TARGET:-linux-glibc-amd64}" 45 if [ ! -x "${_bd}/linkd" ]; then 46 echo "building linkd" >&2 47 make -C "${_root}" -j"$(nproc)" >/dev/null 48 fi 49 [ -x "${_bd}/linkd" ] || { echo "FAIL: linkd did not build" >&2; return 1; } 50 printf '%s\n' "${_bd}" 51 } 52 53 # Guard against greps that silently stop matching. A pattern that finds nothing 54 # because the file moved is indistinguishable from one that finds nothing 55 # because the code is correct, and the second is the only one we want to pass. 56 assert_path_exists() { 57 # assert_path_exists <path> <msg> 58 if [ -e "$1" ]; then 59 echo "PASS: $2" 60 else 61 echo "FAIL: $2: no such path: $1" >&2 62 return 1 63 fi 64 }