| #!/usr/bin/env bash | |
| # | |
| # Copyright 2007 The Bazel Authors. All rights reserved. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| # | |
| # Test of Bazel's version command. | |
| # --- begin runfiles.bash initialization --- | |
| # Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). | |
| set -euo pipefail | |
| if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then | |
| if [[ -f "$0.runfiles_manifest" ]]; then | |
| export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" | |
| elif [[ -f "$0.runfiles/MANIFEST" ]]; then | |
| export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" | |
| elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then | |
| export RUNFILES_DIR="$0.runfiles" | |
| fi | |
| fi | |
| if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then | |
| source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" | |
| elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then | |
| source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ | |
| "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" | |
| else | |
| echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" | |
| exit 1 | |
| fi | |
| # --- end runfiles.bash initialization --- | |
| # Do not reuse a shared CI install_base because | |
| # test_dashdash_version_doesnt_start_server deletes it. | |
| unset TEST_INSTALL_BASE | |
| source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \ | |
| || { echo "integration_test_setup.sh not found!" >&2; exit 1; } | |
| readonly RAW_BAZEL="${PATH_TO_RAW_BAZEL_BIN:-$PATH_TO_BAZEL_BIN}" | |
| function _assert_contains() { | |
| local pattern="$1" | |
| local contents="$2" | |
| echo "$contents" | grep -q -- "$pattern" \ | |
| || fail "Didn't find '$pattern' in '$contents'" | |
| } | |
| function test_gnu_format() { | |
| local version_gnu_format | |
| version_gnu_format="$(bazel --batch version --gnu_format)" \ | |
| || fail "Couldn't get version" | |
| _assert_contains "^${PRODUCT_NAME} ..*$" "$version_gnu_format" | |
| [[ "$(echo "$version_gnu_format" | wc -l)" -eq 1 ]] \ | |
| || fail "Expected only one line of output, got '$version_gnu_format'" | |
| } | |
| function test_dashdash_version_matches_gnu_format() { | |
| local version_gnu_format | |
| version_gnu_format="$(bazel --batch version --gnu_format)" \ | |
| || fail "Couldn't get version" | |
| _assert_contains "^${PRODUCT_NAME} ..*$" "$version_gnu_format" | |
| local dashdash_version | |
| dashdash_version="$("$RAW_BAZEL" --version)" | |
| [[ "$dashdash_version" = "$version_gnu_format" ]] \ | |
| || fail "--version ($dashdash_version)" \ | |
| "!= --gnu_format ($version_gnu_format)" | |
| } | |
| function test_dashdash_version_doesnt_start_server() { | |
| # Test that `bazel --version` doesn't start a server by using the existence of | |
| # install_base as a proxy for a server having been started. It's expected that | |
| # install_base is consistent across servers, which should be tested elsewhere. | |
| local install_base | |
| install_base="$(bazel info install_base)" \ | |
| || fail "couldn't get install base" | |
| [[ -e "$install_base" ]] || fail "$install_base didn't exist" | |
| bazel shutdown || fail "failed to shut down server" | |
| if is_windows; then | |
| try_with_timeout rm -rf "$install_base" | |
| else | |
| rm -rf "$install_base" || fail "failed to rm $install_base" | |
| fi | |
| [[ ! -e "$install_base" ]] || fail "failed to rm $install_base" | |
| "$RAW_BAZEL" --version || fail "bazel --version failed" | |
| [[ ! -e "$install_base" ]] || fail "$install_base shouldn't exist" | |
| } | |
| function test_dashdash_version_tidy_output() { | |
| local version_stderr="$TEST_TMPDIR/version_stderr" | |
| "$RAW_BAZEL" --version 2>"$version_stderr" || \ | |
| fail "bazel --version failed, stderr: $(cat "$version_stderr")" | |
| assert_equals "" "$(cat "$version_stderr")" | |
| } | |
| run_suite "Bazel version stamping tests" |