#!/usr/bin/env bash # # Check that a freshly built binary reports the version the build meant to put # into it. # # scripts/check-version.sh bin/turbo-js v0.2.0 88a4c38 # scripts/check-version.sh bin/turbo-js # unstamped build # # Linker flags are a string: a typo in one produces a binary that builds, links # and runs, and quietly reports the wrong version — or "unknown". Nothing but # running the binary catches that, so the build runs it. # # With a version to expect, the reported number must **equal** it. A substring # test is not enough: "0.2.0" is a substring of "10.2.0" and of a commit hash # that happens to contain it, and the case this exists to catch is a stamp that # is nearly right. # # With no version to expect — a build outside a git checkout, where there is # nothing to describe — the only claim left is that some source named it, so # "unknown" is the failure. set -euo pipefail if [ $# -lt 1 ]; then echo "usage: $0 [expected-version] [expected-commit]" >&2 exit 2 fi readonly BINARY="$1" readonly EXPECTED_VERSION="${2:-}" readonly EXPECTED_COMMIT="${3:-}" if [ ! -x "${BINARY}" ]; then echo "check-version: ${BINARY} is not an executable file" >&2 exit 1 fi if ! reported="$("${BINARY}" -version 2>&1)"; then echo "check-version: ${BINARY} does not run:" >&2 echo "${reported}" >&2 exit 1 fi # The binary prints " " or " (, built …)", # so the number is the last field before the parenthesis, if there is one. The # name is two words in every editor built on turbo-core and one word in some # future one, which is why it is read from the right rather than the left. head="${reported%% (*}" number="${head##* }" if [ -n "${EXPECTED_VERSION}" ]; then # The version package drops the leading v of a tag: the tag is v0.2.0 and # what a person reads is 0.2.0. want="${EXPECTED_VERSION#v}" if [ "${number}" != "${want}" ]; then echo "check-version: the build meant to stamp ${want} and the binary reports ${number}" >&2 echo " ${reported}" >&2 exit 1 fi elif [ "${number}" = "unknown" ]; then echo "check-version: the binary cannot name its own version" >&2 echo " ${reported}" >&2 exit 1 fi if [ -n "${EXPECTED_COMMIT}" ] && [ "${reported}" = "${reported#*"${EXPECTED_COMMIT}"}" ]; then echo "check-version: the build meant to stamp commit ${EXPECTED_COMMIT} and the binary reports:" >&2 echo " ${reported}" >&2 exit 1 fi echo "${reported}"