From 835fdc1d5f5982e70908e69e119d3dee8ca19508 Mon Sep 17 00:00:00 2001 From: Finn Ball Date: Wed, 24 Mar 2021 11:53:09 +0000 Subject: [PATCH] Adding rbe cc test for coverage. --- .../bazel/remote/remote_execution_test.sh | 148 +++++++++++++++++- 1 file changed, 147 insertions(+), 1 deletion(-) diff --git a/src/test/shell/bazel/remote/remote_execution_test.sh b/src/test/shell/bazel/remote/remote_execution_test.sh index dce00f2b848d26..ce7f0f862053ea 100755 --- a/src/test/shell/bazel/remote/remote_execution_test.sh +++ b/src/test/shell/bazel/remote/remote_execution_test.sh @@ -2343,7 +2343,7 @@ EOF # because the coverage post-processing tool escaped the sandbox to find its own # runfiles. The error we would see here without the flag would be "Cannot find # runfiles". See #4685. -function test_rbe_coverage_produces_report() { +function test_java_rbe_coverage_produces_report() { mkdir -p java/factorial JAVA_TOOLS_ZIP="released" @@ -2426,4 +2426,150 @@ end_of_record" assert_equals "$expected_result" "$(cat bazel-testlogs/java/factorial/fact-test/coverage.dat)" } +# Runs coverage with `cc_test` and RE then checks the coverage file is returned. +# See the above `test_java_rbe_coverage_produces_report` for more information. +function test_cc_rbe_coverage_produces_report() { + if [[ "$PLATFORM" == "darwin" ]]; then + # TODO(b/37355380): This test is disabled due to RemoteWorker not supporting + # setting SDKROOT and DEVELOPER_DIR appropriately, as is required of + # action executors in order to select the appropriate Xcode toolchain. + return 0 + fi + + local test_dir="a/cc/coverage_test" + mkdir -p $test_dir + + cat > "$test_dir"/BUILD <<'EOF' +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "hello-lib", + srcs = ["hello-lib.cc"], + hdrs = ["hello-lib.h"], +) + +cc_binary( + name = "hello-world", + srcs = ["hello-world.cc"], + deps = [":hello-lib"], +) + +cc_test( + name = "hello-test", + srcs = ["hello-world.cc"], + deps = [":hello-lib"], +) + +EOF + + cat > "$test_dir"/hello-lib.cc <<'EOF' +#include "hello-lib.h" + +#include + +using std::cout; +using std::endl; +using std::string; + +namespace hello { + +HelloLib::HelloLib(const string& greeting) : greeting_(new string(greeting)) { +} + +void HelloLib::greet(const string& thing) { + cout << *greeting_ << " " << thing << endl; +} + +} // namespace hello + +EOF + + cat > "$test_dir"/hello-lib.h <<'EOF' +#ifndef HELLO_LIB_H_ +#define HELLO_LIB_H_ + +#include +#include + +namespace hello { + +class HelloLib { + public: + explicit HelloLib(const std::string &greeting); + + void greet(const std::string &thing); + + private: + std::unique_ptr greeting_; +}; + +} // namespace hello + +#endif // HELLO_LIB_H_ + +EOF + + cat > "$test_dir"/hello-world.cc <<'EOF' +#include "hello-lib.h" + +#include + +using hello::HelloLib; +using std::string; + +int main(int argc, char** argv) { + HelloLib lib("Hello"); + string thing = "world"; + if (argc > 1) { + thing = argv[1]; + } + lib.greet(thing); + return 0; +} + +EOF + + bazel coverage \ + --test_output=all \ + --experimental_fetch_all_coverage_outputs \ + --experimental_split_coverage_postprocessing \ + --spawn_strategy=remote \ + --remote_executor=grpc://localhost:${worker_port} \ + //"$test_dir":hello-test >& $TEST_log \ + || fail "Failed to run coverage for cc_test" + + local expected_result="SF:a/cc/coverage_test/hello-lib.cc +FN:18,_GLOBAL__sub_I_hello_lib.cc +FN:18,_Z41__static_initialization_and_destruction_0ii +FN:14,_ZN5hello8HelloLib5greetERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE +FN:11,_ZN5hello8HelloLibC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE +FNDA:1,_GLOBAL__sub_I_hello_lib.cc +FNDA:1,_Z41__static_initialization_and_destruction_0ii +FNDA:1,_ZN5hello8HelloLib5greetERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE +FNDA:1,_ZN5hello8HelloLibC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE +FNF:4 +FNH:4 +DA:11,1 +DA:12,1 +DA:14,1 +DA:15,1 +DA:16,1 +DA:18,3 +LH:6 +LF:6 +end_of_record +SF:a/cc/coverage_test/hello-lib.h +FN:9,_ZN5hello8HelloLibD2Ev +FNDA:1,_ZN5hello8HelloLibD2Ev +FNF:1 +FNH:1 +DA:9,1 +LH:1 +LF:1 +end_of_record" + + cat bazel-testlogs/a/cc/coverage_test/hello-test/coverage.dat + assert_equals "$expected_result" "$(cat bazel-testlogs/a/cc/coverage_test/hello-test/coverage.dat)" +} + run_suite "Remote execution and remote cache tests"