diff --git a/README.md b/README.md index 65f789c..41161d3 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,4 @@ In order to specify how to validate the error message, a struct `matcher` is ava Execute [`execute_tests.sh`](execute_tests.sh) after installing [`bazelisk`](https://github.com/bazelbuild/bazelisk). It executes `bazelisk test` and `bazelisk build` commands under the hood. -When writing tests, in principle, do the following things - -- Use `tags = ["manual", "fail-to-build"]` if a test case target must fail to build with `bazelisk build` -- Otherwise, do not use any of the tags above for the target. +When writing tests, in principle, use `tags = ["manual"]` if a test case target must fail with `bazelisk test`. In such a test case, confirm its failure in [`execute_tests.sh`](execute_tests.sh) one by one. diff --git a/execute_tests.sh b/execute_tests.sh index 854c8d8..a6d5d7a 100755 --- a/execute_tests.sh +++ b/execute_tests.sh @@ -3,4 +3,36 @@ # Execute all tests set -euo pipefail + +check_bazel_build_error() { + # Check bazel build error for a particular target + # + # Args: + # $1: label to check + local label + label=$1 + + # Before executing `bazelisk build`, check if the target exists with `bazelisk query` + bazelisk query "${label}" + + # Check build error + if bazelisk 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 fail at 'bazelisk 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 +check_bazel_build_error //tests/cc/cpp_successful_build:with_substr_matcher +check_bazel_build_error //tests/cc/cpp_successful_build_with_deps:plain +check_bazel_build_error //tests/cc/cpp_successful_build_with_deps:with_basic_regex_matcher +check_bazel_build_error //tests/cc/cpp_successful_build_with_deps:with_extended_regex_matcher +check_bazel_build_error //tests/cc/cpp_successful_build_with_deps:with_substr_matcher diff --git a/tests/cc/cpp_successful_build/BUILD.bazel b/tests/cc/cpp_successful_build/BUILD.bazel new file mode 100644 index 0000000..3d514ae --- /dev/null +++ b/tests/cc/cpp_successful_build/BUILD.bazel @@ -0,0 +1,29 @@ +load("//lang/cc:defs.bzl", "cc_build_error") +load("//matcher:defs.bzl", "matcher") + +cc_build_error( + name = "plain", + src = "cpp_successful_build.cpp", + tags = ["manual"], +) + +cc_build_error( + name = "with_substr_matcher", + src = "cpp_successful_build.cpp", + compile_stderr = matcher.has_substr("for cpp_successful_build.cpp"), + tags = ["manual"], +) + +cc_build_error( + name = "with_basic_regex_matcher", + src = "cpp_successful_build.cpp", + compile_stderr = matcher.contains_basic_regex(r"for[[:space:]]cpp_successful_build\.\(cpp\|cxx\)"), + tags = ["manual"], +) + +cc_build_error( + name = "with_extended_regex_matcher", + src = "cpp_successful_build.cpp", + compile_stderr = matcher.contains_extended_regex(r"for[[:space:]]cpp_successful_build\.(cpp|cxx)"), + tags = ["manual"], +) diff --git a/tests/cc/cpp_successful_build/cpp_successful_build.cpp b/tests/cc/cpp_successful_build/cpp_successful_build.cpp new file mode 100644 index 0000000..905869d --- /dev/null +++ b/tests/cc/cpp_successful_build/cpp_successful_build.cpp @@ -0,0 +1,4 @@ +int main() +{ + return 0; +} diff --git a/tests/cc/cpp_successful_build_with_deps/BUILD.bazel b/tests/cc/cpp_successful_build_with_deps/BUILD.bazel new file mode 100644 index 0000000..4b728ee --- /dev/null +++ b/tests/cc/cpp_successful_build_with_deps/BUILD.bazel @@ -0,0 +1,40 @@ +load("//lang/cc:defs.bzl", "cc_build_error") +load("//matcher:defs.bzl", "matcher") + +cc_library( + name = "library", + srcs = ["library.cpp"], + hdrs = ["library.hpp"], + tags = ["manual"], +) + +cc_build_error( + name = "plain", + src = "cpp_compile_error.cpp", + tags = ["manual"], + deps = [":library"], +) + +cc_build_error( + name = "with_substr_matcher", + src = "cpp_compile_error.cpp", + compile_stderr = matcher.has_substr("requires single argument"), + tags = ["manual"], + deps = [":library"], +) + +cc_build_error( + name = "with_basic_regex_matcher", + src = "cpp_compile_error.cpp", + compile_stderr = matcher.contains_basic_regex("requires single argument.*argument"), + tags = ["manual"], + deps = [":library"], +) + +cc_build_error( + name = "with_extended_regex_matcher", + src = "cpp_compile_error.cpp", + compile_stderr = matcher.contains_extended_regex("requires single argument.*argument"), + tags = ["manual"], + deps = [":library"], +) diff --git a/tests/cc/cpp_successful_build_with_deps/cpp_compile_error.cpp b/tests/cc/cpp_successful_build_with_deps/cpp_compile_error.cpp new file mode 100644 index 0000000..bdf1ef6 --- /dev/null +++ b/tests/cc/cpp_successful_build_with_deps/cpp_compile_error.cpp @@ -0,0 +1,6 @@ +#include "library.hpp" + +int main() +{ + return NormalFunction(1); +} diff --git a/tests/cc/cpp_successful_build_with_deps/library.cpp b/tests/cc/cpp_successful_build_with_deps/library.cpp new file mode 100644 index 0000000..f899529 --- /dev/null +++ b/tests/cc/cpp_successful_build_with_deps/library.cpp @@ -0,0 +1,6 @@ +#include "library.hpp" + +int NormalFunction(int value) +{ + return 123 + value; +} diff --git a/tests/cc/cpp_successful_build_with_deps/library.hpp b/tests/cc/cpp_successful_build_with_deps/library.hpp new file mode 100644 index 0000000..585edee --- /dev/null +++ b/tests/cc/cpp_successful_build_with_deps/library.hpp @@ -0,0 +1,6 @@ +#ifndef TESTS_CC_CPP_SUCCESSFUL_BUILD_WITH_DEPS_ASSERTION_HPP_ +#define TESTS_CC_CPP_SUCCESSFUL_BUILD_WITH_DEPS_ASSERTION_HPP_ + +int NormalFunction(int value); + +#endif // TESTS_CC_CPP_SUCCESSFUL_BUILD_WITH_DEPS_ASSERTION_HPP_