|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# Argument-handling tests for build_and_package.sh. They only exercise |
| 19 | +# --print-name, which resolves the package name and exits before any build step, |
| 20 | +# so no toolchain is needed. |
| 21 | + |
| 22 | +# No `set -e`: every check runs the script under test and inspects its exit |
| 23 | +# status, so a non-zero status is data here, not a reason to abort. |
| 24 | +set -uo pipefail |
| 25 | + |
| 26 | +source_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) |
| 27 | +script="${source_dir}/build_and_package.sh" |
| 28 | + |
| 29 | +checks=0 |
| 30 | +failures=0 |
| 31 | + |
| 32 | +# A private directory: a fixed /tmp path would collide with a concurrent run and |
| 33 | +# could make the script delete a file it did not create. |
| 34 | +work_dir=$(mktemp -d) |
| 35 | +trap 'rm -rf "${work_dir}"' EXIT |
| 36 | +sentinel="${work_dir}/must-not-exist" |
| 37 | + |
| 38 | +# --print-name is passed FIRST, never last: the arguments under test have to keep |
| 39 | +# their own position, otherwise a trailing --print-name would become the value of |
| 40 | +# a preceding --platform and the argument-exhaustion branch could never be |
| 41 | +# reached. Parsing is order independent, so leading it changes nothing else. |
| 42 | +expect_name() { |
| 43 | + local description=$1 expected=$2 |
| 44 | + shift 2 |
| 45 | + local actual |
| 46 | + checks=$((checks + 1)) |
| 47 | + if ! actual=$("${script}" --print-name "$@" 2>&1); then |
| 48 | + failures=$((failures + 1)) |
| 49 | + echo "FAIL ${description}: exited non-zero: ${actual}" |
| 50 | + elif [[ "${actual}" != "${expected}" ]]; then |
| 51 | + failures=$((failures + 1)) |
| 52 | + echo "FAIL ${description}: expected '${expected}', got '${actual}'" |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +# Asserting the diagnostic, not just a non-zero exit: the script runs under |
| 57 | +# `set -u`, so a missing validation branch would still abort -- with an "unbound |
| 58 | +# variable" crash instead of a usable message. |
| 59 | +expect_rejected() { |
| 60 | + local description=$1 |
| 61 | + shift |
| 62 | + local output status |
| 63 | + checks=$((checks + 1)) |
| 64 | + output=$("${script}" --print-name "$@" 2>&1) |
| 65 | + status=$? |
| 66 | + if [[ "${status}" -eq 0 ]]; then |
| 67 | + failures=$((failures + 1)) |
| 68 | + echo "FAIL ${description}: expected a non-zero exit, but the value was accepted" |
| 69 | + elif [[ "${output}" != *"--platform requires a label"* ]]; then |
| 70 | + failures=$((failures + 1)) |
| 71 | + echo "FAIL ${description}: expected the --platform diagnostic, got: ${output}" |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +# The default label is derived from `uname`, so stub it on PATH to check the |
| 76 | +# derivation for hosts this machine is not, including the Darwin -> macos mapping. |
| 77 | +expect_host_default() { |
| 78 | + local description=$1 uname_s=$2 uname_m=$3 expected=$4 |
| 79 | + shift 4 |
| 80 | + local stub_dir actual status |
| 81 | + checks=$((checks + 1)) |
| 82 | + stub_dir=$(mktemp -d "${work_dir}/stub.XXXXXX") |
| 83 | + cat > "${stub_dir}/uname" <<EOF |
| 84 | +#!/bin/sh |
| 85 | +case "\$1" in |
| 86 | + -s) echo "${uname_s}" ;; |
| 87 | + -m) echo "${uname_m}" ;; |
| 88 | + *) echo "unexpected uname argument: \$1" >&2; exit 1 ;; |
| 89 | +esac |
| 90 | +EOF |
| 91 | + chmod +x "${stub_dir}/uname" |
| 92 | + actual=$(PATH="${stub_dir}:${PATH}" "${script}" --print-name "$@" 2>&1) |
| 93 | + status=$? |
| 94 | + if [[ "${status}" -ne 0 ]]; then |
| 95 | + failures=$((failures + 1)) |
| 96 | + echo "FAIL ${description}: exited ${status}: ${actual}" |
| 97 | + elif [[ "${actual}" != "${expected}" ]]; then |
| 98 | + failures=$((failures + 1)) |
| 99 | + echo "FAIL ${description}: expected '${expected}', got '${actual}'" |
| 100 | + fi |
| 101 | +} |
| 102 | + |
| 103 | +host_platform="$(uname -s | tr '[:upper:]' '[:lower:]')" |
| 104 | +if [[ "${host_platform}" == "darwin" ]]; then |
| 105 | + host_platform="macos" |
| 106 | +fi |
| 107 | +host_platform="${host_platform}-$(uname -m)" |
| 108 | + |
| 109 | +expect_name "release defaults to the host platform" "paimon-cpp-${host_platform}" |
| 110 | +expect_name "debug keeps its own prefix" "paimon-cpp-debug-${host_platform}" --debug |
| 111 | +expect_name "release is the default build type" "paimon-cpp-${host_platform}" --release |
| 112 | +expect_name "--platform overrides the host" "paimon-cpp-linux-aarch64" --platform linux-aarch64 |
| 113 | +expect_name "--platform applies to debug too" "paimon-cpp-debug-macos-arm64" -d --platform \ |
| 114 | + macos-arm64 |
| 115 | +expect_name "the last --platform wins" "paimon-cpp-linux-aarch64" --platform linux-x86_64 \ |
| 116 | + --platform linux-aarch64 |
| 117 | + |
| 118 | +# The documented pattern admits dots, underscores and hyphens; keep that a contract. |
| 119 | +expect_name "a dotted label is accepted" "paimon-cpp-linux.arm64" --platform linux.arm64 |
| 120 | +expect_name "an underscored label is accepted" "paimon-cpp-linux_musl-aarch64" --platform \ |
| 121 | + linux_musl-aarch64 |
| 122 | + |
| 123 | +# Default labels for hosts other than this one. |
| 124 | +expect_host_default "linux aarch64 host" Linux aarch64 "paimon-cpp-linux-aarch64" |
| 125 | +expect_host_default "darwin arm64 host maps to macos" Darwin arm64 "paimon-cpp-macos-arm64" |
| 126 | +expect_host_default "darwin x86_64 host maps to macos" Darwin x86_64 "paimon-cpp-macos-x86_64" |
| 127 | +expect_host_default "debug on a linux aarch64 host" Linux aarch64 \ |
| 128 | + "paimon-cpp-debug-linux-aarch64" --debug |
| 129 | +expect_host_default "--platform still wins over the host" Linux aarch64 \ |
| 130 | + "paimon-cpp-linux-x86_64" --platform linux-x86_64 |
| 131 | + |
| 132 | +# --platform as the final argument must be reported as a missing value rather than |
| 133 | +# consuming whatever follows. |
| 134 | +expect_rejected "--platform without a value" --platform |
| 135 | + |
| 136 | +# Nothing that turns the package name into a path, or that a shell would treat as |
| 137 | +# anything but a literal, may be accepted. |
| 138 | +expect_rejected "--platform followed by an option" --platform --debug |
| 139 | +expect_rejected "bare parent directory" --platform ".." |
| 140 | +expect_rejected "parent directory traversal" --platform "../evil" |
| 141 | +expect_rejected "absolute path" --platform "/etc/passwd" |
| 142 | +expect_rejected "nested path" --platform "linux/x86_64" |
| 143 | +expect_rejected "leading dash" --platform "-linux" |
| 144 | +expect_rejected "leading dot" --platform ".linux" |
| 145 | +expect_rejected "empty label" --platform "" |
| 146 | +expect_rejected "command separator" --platform "linux;touch ${sentinel}" |
| 147 | +expect_rejected "command substitution" --platform 'linux$(touch '"${sentinel}"')' |
| 148 | +expect_rejected "whitespace" --platform "linux x86_64" |
| 149 | + |
| 150 | +if [[ -e "${sentinel}" ]]; then |
| 151 | + failures=$((failures + 1)) |
| 152 | + echo "FAIL a rejected label was still evaluated by a shell" |
| 153 | +fi |
| 154 | + |
| 155 | +if [[ "${failures}" -gt 0 ]]; then |
| 156 | + echo "${failures} of ${checks} packaging argument checks failed" |
| 157 | + exit 1 |
| 158 | +fi |
| 159 | +echo "All ${checks} packaging argument checks passed" |
0 commit comments