Skip to content

Commit

Permalink
Use a sh_test instead of a py_test to test Bazelisk
Browse files Browse the repository at this point in the history
  • Loading branch information
philwo committed Jan 19, 2019
1 parent 99d1b46 commit 8998c03
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 57 deletions.
1 change: 1 addition & 0 deletions .bazelci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ platforms:
test_targets:
- //:bazelisk_test
test_flags:
- --test_env=PATH
- --test_env=PROCESSOR_ARCHITECTURE
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore all bazel-* symlinks. There is no full list since this can change
# based on the name of the directory bazel is cloned into.
/bazel-*

11 changes: 6 additions & 5 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
py_library(
name = "bazelisk_lib",
py_binary(
name = "bazelisk",
srcs = ["bazelisk.py"],
)

py_test(
sh_test(
name = "bazelisk_test",
srcs = ["bazelisk_test.py"],
deps = [":bazelisk_lib"],
srcs = ["bazelisk_test.sh"],
data = [":bazelisk"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
1 change: 0 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

51 changes: 0 additions & 51 deletions bazelisk_test.py

This file was deleted.

100 changes: 100 additions & 0 deletions bazelisk_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

# Copyright 2018 Google Inc. 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.

set -euo pipefail
# --- begin runfiles.bash initialization ---
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 ---

function setup() {
cd "$(mktemp -d $TEST_TMPDIR/workspace.XXXXXX)"
touch WORKSPACE BUILD
}

function bazelisk() {
case "$(uname -s)" in
MINGW*|MSYS*)
# We can change this to just call bazelisk.exe when this is fixed:
# https://github.com/bazelbuild/bazel/issues/7190
python "$(rlocation __main__/bazelisk.py)" "$@"
;;
*)
"$(rlocation __main__/bazelisk)" "$@"
;;
esac
}

function test_bazel_version() {
setup

BAZELISK_HOME="$(mktemp -d $TEST_TMPDIR/home.XXXXXX)" \
bazelisk version 2>&1 | tee log

grep "Build label" log || \
(echo "FAIL: Expected to find 'Build label' in the output of 'bazelisk version'"; exit 1)
}

function test_bazel_version_from_environment() {
setup

USE_BAZEL_VERSION="0.20.0" \
BAZELISK_HOME="$(mktemp -d $TEST_TMPDIR/home.XXXXXX)" \
bazelisk version 2>&1 | tee log

grep "Build label: 0.20.0" log || \
(echo "FAIL: Expected to find 'Build label: 0.20.0' in the output of 'bazelisk version'"; exit 1)
}

function test_bazel_version_from_file() {
setup

echo "0.19.0" > .bazelversion

BAZELISK_HOME="$(mktemp -d $TEST_TMPDIR/home.XXXXXX)" \
bazelisk version 2>&1 | tee log

grep "Build label: 0.19.0" log || \
(echo "FAIL: Expected to find 'Build label: 0.19.0' in the output of 'bazelisk version'"; exit 1)
}

echo "# test_bazel_version"
test_bazel_version
echo

echo "# test_bazel_version_from_environment"
test_bazel_version_from_environment
echo

echo "# test_bazel_version_from_file"
test_bazel_version_from_file
echo

0 comments on commit 8998c03

Please sign in to comment.