Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: restrict environment variables #4

Merged
merged 3 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading