helpers.sh (2445B)
1 #!/bin/sh 2 # tests/helpers.sh - shared assertions, source with `. tests/helpers.sh` 3 # 4 # Assertions only. This repo builds and signs packages; it never boots a 5 # machine, so the QEMU and SSH helpers that used to live alongside these 6 # belong to the unos OS-assembly repo instead. 7 set -eu 8 9 assert_eq() { 10 # assert_eq <got> <expected> <msg> 11 if [ "$1" != "$2" ]; then 12 echo "FAIL: $3: expected '$2', got '$1'" >&2 13 return 1 14 fi 15 echo "PASS: $3" 16 } 17 18 assert_contains() { 19 # assert_contains <haystack> <needle> <msg> 20 case "$1" in 21 *"$2"*) echo "PASS: $3" ;; 22 *) echo "FAIL: $3: expected to contain '$2', got '$1'" >&2; return 1 ;; 23 esac 24 } 25 26 assert_file_contains() { 27 # assert_file_contains <file> <needle> <msg> 28 # `--` matters: without it a needle starting with '-' (any command-line flag, 29 # e.g. -Dmeson-docs=disabled) is parsed by grep as options and never matches. 30 if grep -qF -- "$2" "$1" 2>/dev/null; then 31 echo "PASS: $3" 32 else 33 echo "FAIL: $3: $1 does not contain '$2'" >&2 34 return 1 35 fi 36 } 37 38 assert_path_exists() { 39 # assert_path_exists <path> <msg> 40 # Use before grepping a file. A grep that matches nothing because the file 41 # moved looks exactly like one that matches nothing because the code is 42 # correct, and only the latter should pass. 43 if [ -e "$1" ]; then 44 echo "PASS: $2" 45 else 46 echo "FAIL: $2: no such path: $1" >&2 47 return 1 48 fi 49 } 50 51 # Newest built .apk for a package, or empty if none. Tests must never hardcode 52 # a revision: doing so silently tests a stale artifact and then silently skips 53 # once that artifact is pruned. 54 newest_apk() { 55 # newest_apk <pkg> [arch] 56 _p=$1; _a=${2:-x86_64}; _root=${ROOT:?ROOT must be set} 57 _found="" 58 for f in "${_root}/build/repo/${_a}/${_p}"-*.apk; do 59 [ -e "${f}" ] || continue 60 # `zlib-1.3.1-r0.apk` belongs to zlib; `zlib-doc-...` does not, so require 61 # the character after the name to begin a version field. 62 case "$(basename "${f}")" in 63 "${_p}"-[0-9]*) _found=${f} ;; 64 esac 65 done 66 printf '%s\n' "${_found}" 67 } 68 69 # Revision a template currently declares, so tests follow the template rather 70 # than a copy of it that rots. 71 template_pkgver() { 72 # template_pkgver <pkg> 73 _p=$1; _root=${ROOT:?ROOT must be set} 74 _t="${_root}/packages/${_p}/template" 75 [ -f "${_t}" ] || return 1 76 _v=$(sed -n 's/^version=//p' "${_t}" | head -1) 77 _r=$(sed -n 's/^revision=//p' "${_t}" | head -1) 78 printf '%s-r%s\n' "${_v}" "${_r}" 79 }