|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" |
| 6 | +SCRIPT="${SCRIPT_DIR}/calculate-release-version.sh" |
| 7 | + |
| 8 | +fail() { |
| 9 | + echo "FAIL: $*" >&2 |
| 10 | + exit 1 |
| 11 | +} |
| 12 | + |
| 13 | +assert_output() { |
| 14 | + local file="$1" |
| 15 | + local key="$2" |
| 16 | + local expected="$3" |
| 17 | + local actual |
| 18 | + |
| 19 | + actual="$(awk -F= -v key="${key}" '$1 == key { print substr($0, index($0, "=") + 1) }' "${file}" | tail -n1)" |
| 20 | + [[ "${actual}" == "${expected}" ]] \ |
| 21 | + || fail "${key}: expected ${expected:-<empty>}, got ${actual:-<empty>}" |
| 22 | +} |
| 23 | + |
| 24 | +assert_version() { |
| 25 | + local name="$1" |
| 26 | + local release_type="$2" |
| 27 | + local current_version="$3" |
| 28 | + local version_prefix="$4" |
| 29 | + local expected_current="$5" |
| 30 | + local expected_new="$6" |
| 31 | + local tmp_dir output_file channel_name |
| 32 | + |
| 33 | + tmp_dir="$(mktemp -d)" |
| 34 | + output_file="${tmp_dir}/github-output" |
| 35 | + channel_name="Nightly" |
| 36 | + if [[ "${release_type}" == "glean" ]]; then |
| 37 | + channel_name="Glean-Stable" |
| 38 | + fi |
| 39 | + |
| 40 | + RELEASE_TYPE="${release_type}" \ |
| 41 | + RELEASE_CHANNEL_NAME="${channel_name}" \ |
| 42 | + CURRENT_VERSION="${current_version}" \ |
| 43 | + VERSION_PREFIX="${version_prefix}" \ |
| 44 | + GITHUB_OUTPUT="${output_file}" \ |
| 45 | + bash "${SCRIPT}" >"${tmp_dir}/stdout" 2>"${tmp_dir}/stderr" \ |
| 46 | + || fail "${name}: script exited non-zero" |
| 47 | + |
| 48 | + assert_output "${output_file}" current "${expected_current}" |
| 49 | + assert_output "${output_file}" new "${expected_new}" |
| 50 | + rm -rf "${tmp_dir}" |
| 51 | +} |
| 52 | + |
| 53 | +assert_version_fails() { |
| 54 | + local name="$1" |
| 55 | + local release_type="$2" |
| 56 | + local current_version="$3" |
| 57 | + local version_prefix="$4" |
| 58 | + local expected_error="$5" |
| 59 | + local tmp_dir channel_name |
| 60 | + |
| 61 | + tmp_dir="$(mktemp -d)" |
| 62 | + channel_name="Nightly" |
| 63 | + if [[ "${release_type}" == "glean" ]]; then |
| 64 | + channel_name="Glean-Stable" |
| 65 | + fi |
| 66 | + |
| 67 | + if RELEASE_TYPE="${release_type}" \ |
| 68 | + RELEASE_CHANNEL_NAME="${channel_name}" \ |
| 69 | + CURRENT_VERSION="${current_version}" \ |
| 70 | + VERSION_PREFIX="${version_prefix}" \ |
| 71 | + bash "${SCRIPT}" >"${tmp_dir}/stdout" 2>"${tmp_dir}/stderr"; then |
| 72 | + fail "${name}: script unexpectedly succeeded" |
| 73 | + fi |
| 74 | + |
| 75 | + grep -Fq "${expected_error}" "${tmp_dir}/stderr" \ |
| 76 | + || fail "${name}: expected error was not emitted" |
| 77 | + rm -rf "${tmp_dir}" |
| 78 | +} |
| 79 | + |
| 80 | +assert_version "increments standard version" \ |
| 81 | + standard "0.2.140" "" "0.2.140" "0.2.141" |
| 82 | +assert_version "bootstraps empty Glean version" \ |
| 83 | + glean "" "0.2.87" "" "0.2.87.1" |
| 84 | +assert_version "increments existing Glean version" \ |
| 85 | + glean "0.2.87.1" "0.2.87" "0.2.87.1" "0.2.87.2" |
| 86 | +assert_version "increments a multi-digit Glean sequence" \ |
| 87 | + glean "0.2.87.19" "0.2.87" "0.2.87.19" "0.2.87.20" |
| 88 | + |
| 89 | +assert_version_fails "rejects empty standard version" \ |
| 90 | + standard "" "" "Nightly channel returned an invalid currentVersion: <empty>" |
| 91 | +assert_version_fails "rejects a different Glean family" \ |
| 92 | + glean "0.2.88.1" "0.2.87" "Glean-Stable currentVersion must match 0.2.87.x: 0.2.88.1" |
| 93 | +assert_version_fails "requires the Glean version prefix" \ |
| 94 | + glean "" "" "VERSION_PREFIX is required." |
| 95 | + |
| 96 | +echo "calculate-release-version tests passed" |
0 commit comments