| 📦 Turbo JS 91999d1 k33g 11h ago | 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Check that a freshly built binary reports the version the build meant to put |
| 4 | # into it. |
| 5 | # |
| 6 | # scripts/check-version.sh bin/turbo-js v0.2.0 88a4c38 |
| 7 | # scripts/check-version.sh bin/turbo-js # unstamped build |
| 8 | # |
| 9 | # Linker flags are a string: a typo in one produces a binary that builds, links |
| 10 | # and runs, and quietly reports the wrong version — or "unknown". Nothing but |
| 11 | # running the binary catches that, so the build runs it. |
| 12 | # |
| 13 | # With a version to expect, the reported number must **equal** it. A substring |
| 14 | # test is not enough: "0.2.0" is a substring of "10.2.0" and of a commit hash |
| 15 | # that happens to contain it, and the case this exists to catch is a stamp that |
| 16 | # is nearly right. |
| 17 | # |
| 18 | # With no version to expect — a build outside a git checkout, where there is |
| 19 | # nothing to describe — the only claim left is that some source named it, so |
| 20 | # "unknown" is the failure. |
| 21 | |
| 22 | set -euo pipefail |
| 23 | |
| 24 | if [ $# -lt 1 ]; then |
| 25 | echo "usage: $0 <binary> [expected-version] [expected-commit]" >&2 |
| 26 | exit 2 |
| 27 | fi |
| 28 | |
| 29 | readonly BINARY="$1" |
| 30 | readonly EXPECTED_VERSION="${2:-}" |
| 31 | readonly EXPECTED_COMMIT="${3:-}" |
| 32 | |
| 33 | if [ ! -x "${BINARY}" ]; then |
| 34 | echo "check-version: ${BINARY} is not an executable file" >&2 |
| 35 | exit 1 |
| 36 | fi |
| 37 | |
| 38 | if ! reported="$("${BINARY}" -version 2>&1)"; then |
| 39 | echo "check-version: ${BINARY} does not run:" >&2 |
| 40 | echo "${reported}" >&2 |
| 41 | exit 1 |
| 42 | fi |
| 43 | |
| 44 | # The binary prints "<Name> <number>" or "<Name> <number> (<commit>, built …)", |
| 45 | # so the number is the last field before the parenthesis, if there is one. The |
| 46 | # name is two words in every editor built on turbo-core and one word in some |
| 47 | # future one, which is why it is read from the right rather than the left. |
| 48 | head="${reported%% (*}" |
| 49 | number="${head##* }" |
| 50 | |
| 51 | if [ -n "${EXPECTED_VERSION}" ]; then |
| 52 | # The version package drops the leading v of a tag: the tag is v0.2.0 and |
| 53 | # what a person reads is 0.2.0. |
| 54 | want="${EXPECTED_VERSION#v}" |
| 55 | if [ "${number}" != "${want}" ]; then |
| 56 | echo "check-version: the build meant to stamp ${want} and the binary reports ${number}" >&2 |
| 57 | echo " ${reported}" >&2 |
| 58 | exit 1 |
| 59 | fi |
| 60 | elif [ "${number}" = "unknown" ]; then |
| 61 | echo "check-version: the binary cannot name its own version" >&2 |
| 62 | echo " ${reported}" >&2 |
| 63 | exit 1 |
| 64 | fi |
| 65 | |
| 66 | if [ -n "${EXPECTED_COMMIT}" ] && [ "${reported}" = "${reported#*"${EXPECTED_COMMIT}"}" ]; then |
| 67 | echo "check-version: the build meant to stamp commit ${EXPECTED_COMMIT} and the binary reports:" >&2 |
| 68 | echo " ${reported}" >&2 |
| 69 | exit 1 |
| 70 | fi |
| 71 | |
| 72 | echo "${reported}" |