Skip to content

Commit

Permalink
test: restrict environment variables (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyawk authored Apr 7, 2024
1 parent 5bfc814 commit b05298e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Bazel implementations to test a build error.

There's a situation where a developer wants to test if particular code doesn't compile. However, when using ordinary testing rules, such as `cc_test`, `bazel test` results in an error if the test code doesn't compile.

`rules_build_error` is the repository to address such a problem. It provides some implementations to test the compilation error for each programming language. When the code written in a particular **does** compile, `bazel build` should fail for the associated target.
`rules_build_error` is the repository to address such a problem. It provides some implementations to test the compilation error for each programming language. When the code written in a particular programming language **does** compile, `bazel build` should fail for the associated target.

## Usage

Expand Down
22 changes: 16 additions & 6 deletions execute_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

set -euo pipefail

# Bazel executable with some arguments
BAZEL_EXECUTABLE=(
"env"
"-i"
BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
"HOME=${HOME}"
"PATH=${PATH}"
bazelisk
)

check_bazel_build_error() {
# Check bazel build error for a particular target
#
Expand All @@ -12,22 +22,22 @@ check_bazel_build_error() {
local label
label=$1

# Before executing `bazelisk build`, check if the target exists with `bazelisk query`
bazelisk query "${label}"
# Before executing `bazel build`, check if the target exists with `bazel query`
"${BAZEL_EXECUTABLE[@]}" query "${label}"

# Check build error
if bazelisk build "${label}"; then
if "${BAZEL_EXECUTABLE[@]}" build "${label}"; then
echo "Target '${label}' must fail to build, but succeeded" >&2
exit 1
else
echo "OK! It has failed as intended."
fi
}

echo "Executing the test cases which should pass straightforward 'bazelisk test'"
bazelisk test //...
echo "Executing the test cases which should succeed in straightforward 'bazel test'"
"${BAZEL_EXECUTABLE[@]}" test //...

echo "Executing the test cases which should fail at 'bazelisk build'"
echo "Executing the test cases which should fail at 'bazel build'"
check_bazel_build_error //tests/cc/cpp_successful_build:plain
check_bazel_build_error //tests/cc/cpp_successful_build:with_basic_regex_matcher
check_bazel_build_error //tests/cc/cpp_successful_build:with_extended_regex_matcher
Expand Down

0 comments on commit b05298e

Please sign in to comment.