xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Examples / test.sh
test.sh118 lines · 3.1 KiBRaw
| 1 | #!/bin/bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | shopt -s nullglob |
| 5 | |
| 6 | usage() { |
| 7 | echo "Usage: $0 [--build | --run] [DemoName]" |
| 8 | } |
| 9 | |
| 10 | mode=both |
| 11 | case "${1:-}" in |
| 12 | --build) mode=build; shift ;; |
| 13 | --run) mode=run; shift ;; |
| 14 | -h|--help) usage; exit 0 ;; |
| 15 | esac |
| 16 | |
| 17 | if [[ $# -gt 1 || (${1:-} != "" && (${1:-} != Demo* || ${1:-} == */*)) ]]; then |
| 18 | usage >&2 |
| 19 | exit 1 |
| 20 | fi |
| 21 | |
| 22 | cd "$(dirname "$0")" |
| 23 | examples_dir="$PWD" |
| 24 | out_dir="$examples_dir/out" |
| 25 | |
| 26 | if [[ -n "${1:-}" ]]; then |
| 27 | demos=("$1") |
| 28 | elif [[ "$mode" == run ]]; then |
| 29 | demos=() |
| 30 | for app in "$out_dir"/*.app; do |
| 31 | name="${app##*/}" |
| 32 | demos+=("${name%.app}") |
| 33 | done |
| 34 | else |
| 35 | demos=() |
| 36 | for demo in "$examples_dir"/Demo*/; do |
| 37 | [[ -f "$demo/Package.swift" ]] || continue |
| 38 | demo="${demo%/}" |
| 39 | demos+=("${demo##*/}") |
| 40 | done |
| 41 | fi |
| 42 | |
| 43 | if [[ ${#demos[@]} -eq 0 ]]; then |
| 44 | echo "No demos found. Run $0 --build first if using --run." >&2 |
| 45 | exit 1 |
| 46 | fi |
| 47 | |
| 48 | logs_dir="$(mktemp -d)" |
| 49 | trap 'rm -rf "$logs_dir"' EXIT |
| 50 | trap 'exit 130' INT |
| 51 | trap 'exit 143' TERM |
| 52 | |
| 53 | build_demo() { |
| 54 | local demo="$1" |
| 55 | echo "Building $demo..." |
| 56 | mkdir -p "$out_dir" || return 1 |
| 57 | # Do not leave an older successful build available after a failed rebuild. |
| 58 | rm -rf "$out_dir/$demo.app" || return 1 |
| 59 | (cd "$examples_dir/$demo" && xtool dev build --triple arm64-apple-ios-simulator) || return 1 |
| 60 | cp -R "$examples_dir/$demo/xtool/$demo.app" "$out_dir/$demo.app" |
| 61 | } |
| 62 | |
| 63 | run_demo() ( |
| 64 | demo="$1" |
| 65 | app="$out_dir/$demo.app" |
| 66 | log="$logs_dir/$demo-run.log" |
| 67 | if [[ ! -d "$app" ]]; then |
| 68 | echo "Missing $app. Run $0 --build $demo first." >&2 |
| 69 | exit 1 |
| 70 | fi |
| 71 | bundle_id=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$app/Info.plist") || exit 1 |
| 72 | echo "Running $demo..." |
| 73 | xcrun simctl install booted "$app" || exit 1 |
| 74 | xcrun simctl launch --terminate-running-process --console booted "$bundle_id" >"$log" 2>&1 & |
| 75 | launch_pid=$! |
| 76 | # A demo should exit shortly after launch. Bound failures that hang instead. |
| 77 | ( |
| 78 | sleep_pid="" |
| 79 | trap '[[ -z "$sleep_pid" ]] || kill "$sleep_pid" 2>/dev/null || true' EXIT |
| 80 | sleep 60 & |
| 81 | sleep_pid=$! |
| 82 | wait "$sleep_pid" |
| 83 | sleep_pid="" |
| 84 | touch "$log.timeout" |
| 85 | kill -TERM "$launch_pid" 2>/dev/null || exit 0 |
| 86 | sleep 5 & |
| 87 | sleep_pid=$! |
| 88 | wait "$sleep_pid" |
| 89 | sleep_pid="" |
| 90 | kill -KILL "$launch_pid" 2>/dev/null || true |
| 91 | ) >/dev/null 2>&1 & |
| 92 | timeout_pid=$! |
| 93 | trap 'kill "$timeout_pid" "$launch_pid" 2>/dev/null || true' EXIT |
| 94 | status=0 |
| 95 | wait "$launch_pid" || status=$? |
| 96 | kill "$timeout_pid" 2>/dev/null || true |
| 97 | wait "$timeout_pid" 2>/dev/null || true |
| 98 | cat "$log" |
| 99 | if [[ -f "$log.timeout" ]]; then |
| 100 | echo "$demo did not terminate within 60 seconds." >&2 |
| 101 | exit 1 |
| 102 | fi |
| 103 | [[ "$status" -eq 0 ]] && grep -Fxq 'xtool test succeeded' "$log" |
| 104 | ) |
| 105 | |
| 106 | status=0 |
| 107 | for demo in "${demos[@]}"; do |
| 108 | if [[ "$mode" != run ]] && ! build_demo "$demo"; then |
| 109 | echo "Build failed: $demo" >&2 |
| 110 | status=1 |
| 111 | continue |
| 112 | fi |
| 113 | if [[ "$mode" != build ]] && ! run_demo "$demo"; then |
| 114 | echo "Test failed: $demo" >&2 |
| 115 | status=1 |
| 116 | fi |
| 117 | done |
| 118 | exit "$status" |