Skip to content

Commit

Permalink
add tests for build success
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyawk committed Apr 7, 2024
1 parent 05a61b8 commit 6546b0e
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 4 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
32 changes: 32 additions & 0 deletions execute_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions tests/cc/cpp_successful_build/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
4 changes: 4 additions & 0 deletions tests/cc/cpp_successful_build/cpp_successful_build.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int main()
{
return 0;
}
40 changes: 40 additions & 0 deletions tests/cc/cpp_successful_build_with_deps/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
6 changes: 6 additions & 0 deletions tests/cc/cpp_successful_build_with_deps/cpp_compile_error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "library.hpp"

int main()
{
return NormalFunction(1);
}
6 changes: 6 additions & 0 deletions tests/cc/cpp_successful_build_with_deps/library.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "library.hpp"

int NormalFunction(int value)
{
return 123 + value;
}
6 changes: 6 additions & 0 deletions tests/cc/cpp_successful_build_with_deps/library.hpp
Original file line number Diff line number Diff line change
@@ -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_

0 comments on commit 6546b0e

Please sign in to comment.