| 1 | #!/usr/bin/env bash |
| 2 | # Tests for fonts.sh's checks and for the release gate that uses them. None |
| 3 | # needs the network: every case fails before a download would start. Run |
| 4 | # directly: |
| 5 | # hack/fonts_test.sh |
| 6 | set -uo pipefail |
| 7 | |
| 8 | here=$(cd -- "$(dirname -- "$0")" && pwd) |
| 9 | root=$(cd -- "$here/.." && pwd) |
| 10 | work=$(mktemp -d) |
| 11 | trap 'rm -rf "$work"' EXIT |
| 12 | fails=0 |
| 13 | |
| 14 | fail() { echo "FAIL: $*"; fails=$((fails + 1)); } |
| 15 | |
| 16 | # A copy of the script in a tree of its own, so nothing touches the real |
| 17 | # font directory. |
| 18 | mkdir -p "$work/hack" "$work/internal/look/static/fonts/timeless" |
| 19 | cp "$here/fonts.sh" "$work/hack/fonts.sh" |
| 20 | font=$work/internal/look/static/fonts/timeless/1.094/TimelessSansVF.woff2 |
| 21 | |
| 22 | # check: a missing font fails. |
| 23 | if out=$(sh "$work/hack/fonts.sh" check 2>&1); then |
| 24 | fail "check passed with no font: $out" |
| 25 | elif ! printf '%s' "$out" | grep -q "not there"; then |
| 26 | fail "check without a font said: $out" |
| 27 | fi |
| 28 | |
| 29 | # check: a file that is not the pinned font fails. |
| 30 | mkdir -p "$(dirname "$font")" |
| 31 | printf 'not a font' >"$font" |
| 32 | if out=$(sh "$work/hack/fonts.sh" check 2>&1); then |
| 33 | fail "check passed a wrong file: $out" |
| 34 | elif ! printf '%s' "$out" | grep -q "checksum mismatch"; then |
| 35 | fail "check of a wrong file said: $out" |
| 36 | fi |
| 37 | |
| 38 | # fetch: a wrong file already there is refused, not kept as the font. |
| 39 | if out=$(sh "$work/hack/fonts.sh" 2>&1); then |
| 40 | fail "fetch kept a wrong file: $out" |
| 41 | elif ! printf '%s' "$out" | grep -q "checksum mismatch"; then |
| 42 | fail "fetch with a wrong file said: $out" |
| 43 | fi |
| 44 | |
| 45 | # The real font, when this machine has fetched it, passes. |
| 46 | real=$root/internal/look/static/fonts/timeless/1.094/TimelessSansVF.woff2 |
| 47 | if [ -f "$real" ]; then |
| 48 | cp "$real" "$font" |
| 49 | if ! out=$(sh "$work/hack/fonts.sh" check 2>&1); then |
| 50 | fail "check refused the real font: $out" |
| 51 | fi |
| 52 | fi |
| 53 | |
| 54 | # The release target fetches and checks the font before it builds anything. |
| 55 | plan=$(make -C "$root" -n release 2>/dev/null) |
| 56 | fetch=$(printf '%s\n' "$plan" | grep -n 'hack/fonts.sh$' | head -1 | cut -d: -f1) |
| 57 | check=$(printf '%s\n' "$plan" | grep -n 'hack/fonts.sh check' | head -1 | cut -d: -f1) |
| 58 | build=$(printf '%s\n' "$plan" | grep -n 'go build' | head -1 | cut -d: -f1) |
| 59 | if [ -z "$fetch" ] || [ -z "$check" ] || [ -z "$build" ] || [ "$fetch" -gt "$build" ] || [ "$check" -gt "$build" ]; then |
| 60 | fail "make release does not fetch and check the font before building:" |
| 61 | printf '%s\n' "$plan" |
| 62 | fi |
| 63 | |
| 64 | if [ "$fails" -ne 0 ]; then |
| 65 | echo "fonts_test: $fails failed" |
| 66 | exit 1 |
| 67 | fi |
| 68 | echo "fonts_test: ok" |
| 69 | |