Skip to content

Commit

Permalink
Add dependency on googletest, so we can use gmock along with gtest.
Browse files Browse the repository at this point in the history
third_party/gtest can go away after this.

RELNOTES: None.
PiperOrigin-RevId: 190221581
  • Loading branch information
cvcal authored and Copybara-Service committed Mar 23, 2018
1 parent 23e1c5d commit 8e9f4a8
Show file tree
Hide file tree
Showing 35 changed files with 91 additions and 95 deletions.
9 changes: 9 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ http_archive(
sha256 = "62797e7cd7cc959419710cd25b075b5f5b247da0e8214d47bf5af9b32128fb0d",
)

http_archive(
name = "com_google_googletest",
urls = [
"https://github.com/google/googletest/archive/dfa853b63d17c787914b663b50c2095a0c5b706e.tar.gz",
],
strip_prefix = "googletest-dfa853b63d17c787914b663b50c2095a0c5b706e",
sha256 = "313a16fba8f0be8ee20ba9883e044556044cbd1ae6cea532473d163a843ef991",
)

# For src/test/shell/bazel:bazel_sandboxing_test
http_file(
name = 'mount_path_toolchain',
Expand Down
10 changes: 5 additions & 5 deletions src/test/cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ cc_test(
deps = [
"//src/main/cpp:blaze_util",
"//src/main/cpp/util",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
],
)

Expand All @@ -36,7 +36,7 @@ cc_test(
"//src/main/cpp:option_processor",
"//src/main/cpp:workspace_layout",
"//src/main/cpp/util",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
"@com_googlesource_code_re2//:re2",
],
)
Expand All @@ -50,7 +50,7 @@ cc_test(
"//src/main/cpp:option_processor",
"//src/main/cpp:workspace_layout",
"//src/main/cpp/util",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
],
)

Expand All @@ -62,7 +62,7 @@ cc_test(
"//src/main/cpp:blaze_util",
"//src/main/cpp:startup_options",
"//src/main/cpp:workspace_layout",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
],
)

Expand All @@ -74,7 +74,7 @@ cc_test(
"//src/main/cpp:blaze_util",
"//src/main/cpp:workspace_layout",
"//src/main/cpp/util",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
],
)

Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/blaze_util_posix_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include "src/main/cpp/blaze_util.h"
#include "src/main/cpp/blaze_util_platform.h"
#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze {

Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/blaze_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "src/main/cpp/blaze_util.h"
#include "src/main/cpp/blaze_util_platform.h"
#include "src/main/cpp/util/file.h"
#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze {

Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/blaze_util_windows_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "src/main/cpp/blaze_util_platform.h"
#include "src/main/cpp/util/file.h"
#include "src/main/cpp/util/strings.h"
#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze {

Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/option_processor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "src/main/cpp/util/file.h"
#include "src/main/cpp/util/file_platform.h"
#include "src/main/cpp/workspace_layout.h"
#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"
#include "re2/re2.h"

namespace blaze {
Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/rc_options_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "src/main/cpp/util/file_platform.h"
#include "src/main/cpp/util/strings.h"
#include "src/main/cpp/workspace_layout.h"
#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze {
using std::string;
Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/startup_options_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "src/main/cpp/blaze_util_platform.h"
#include "src/main/cpp/workspace_layout.h"
#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze {

Expand Down
10 changes: 5 additions & 5 deletions src/test/cpp/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cc_test(
deps = [
"//src/main/cpp/util",
"//src/main/cpp/util:md5",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
],
)

Expand All @@ -32,7 +32,7 @@ cc_test(
deps = [
":test_util",
"//src/main/cpp/util:file",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
] + select({
"//src/conditions:windows": [
":windows_test_util",
Expand All @@ -49,7 +49,7 @@ cc_test(
"//src/main/cpp:blaze_util",
"//src/main/cpp/util:bazel_log_handler",
"//src/main/cpp/util:logging",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
],
)

Expand All @@ -58,7 +58,7 @@ cc_test(
srcs = ["numbers_test.cc"],
deps = [
"//src/main/cpp/util",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
],
)

Expand All @@ -68,7 +68,7 @@ cc_test(
shard_count = 2,
deps = [
"//src/main/cpp/util:strings",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
],
)

Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/util/file_posix_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "src/main/cpp/util/file.h"
#include "src/main/cpp/util/file_platform.h"
#include "src/test/cpp/util/test_util.h"
#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze_util {

Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/util/file_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "src/main/cpp/util/file.h"
#include "src/test/cpp/util/test_util.h"
#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze_util {

Expand Down
67 changes: 26 additions & 41 deletions src/test/cpp/util/logging_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
#include "src/main/cpp/blaze_util_platform.h"
#include "src/main/cpp/util/bazel_log_handler.h"
#include "src/main/cpp/util/logging.h"
#include "gtest/gtest.h"
#include "googlemock/include/gmock/gmock.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze_util {
using ::testing::HasSubstr;
using ::testing::Not;
// Note: gmock uses different regex syntax on different platforms. MatchesRegex
// is still useful since the '.' wildcard can help match formatted log lines
// like `[bazel INFO filename:134] message`
// but should not be used for more fine grained testing.
using ::testing::MatchesRegex;

class LoggingTest : public ::testing::Test {
protected:
void SetUp() {
Expand All @@ -40,27 +49,23 @@ TEST(LoggingTest, BazelLogHandlerDumpsToCerrAtFail) {
blaze_util::SetLogHandler(std::move(handler));

// Log something.
std::string teststring = "test that the log messages get dumped to stderr";
std::string teststring = "test that log messages get dumped to stderr";
BAZEL_LOG(INFO) << teststring;

// Check that stderr isn't getting anything yet.
std::string stderr_output = testing::internal::GetCapturedStderr();
EXPECT_TRUE(stderr_output.find(teststring) == std::string::npos)
<< "stderr unexpectedly contains the log message, before log output was "
"set. Stderr contained: "
<< stderr_output;
EXPECT_THAT(stderr_output, Not(HasSubstr(teststring)));
testing::internal::CaptureStderr();

// Destruct the log handler and get the stderr remains.
blaze_util::SetLogHandler(nullptr);
stderr_output = testing::internal::GetCapturedStderr();
EXPECT_TRUE(stderr_output.find(teststring) != std::string::npos)
<< "stderr does not contain the expected message. Stderr contained: "
<< stderr_output;
EXPECT_THAT(stderr_output, HasSubstr(teststring));
}

TEST(LoggingTest, LogLevelNamesMatch) {
EXPECT_STREQ("INFO", LogLevelName(LOGLEVEL_INFO));
EXPECT_STREQ("USER", LogLevelName(LOGLEVEL_USER));
EXPECT_STREQ("WARNING", LogLevelName(LOGLEVEL_WARNING));
EXPECT_STREQ("ERROR", LogLevelName(LOGLEVEL_ERROR));
EXPECT_STREQ("FATAL", LogLevelName(LOGLEVEL_FATAL));
Expand All @@ -74,17 +79,14 @@ TEST(LoggingTest, BazelLogDoesNotDumpToStderrIfOuputStreamSetToNull) {
blaze_util::SetLogHandler(std::move(handler));

// Log something.
std::string teststring = "test that the log message is lost.";
std::string teststring = "test that this log message is lost.";
BAZEL_LOG(INFO) << teststring;
blaze_util::SetLoggingOutputStream(nullptr);

// Destruct the log handler and check if stderr got anything.
blaze_util::SetLogHandler(nullptr);
std::string stderr_output = testing::internal::GetCapturedStderr();
EXPECT_TRUE(stderr_output.find(teststring) == std::string::npos)
<< "stderr unexpectedly contains the log message, even though log output "
"was explicitly set to null. Stderr contained: "
<< stderr_output;
EXPECT_THAT(stderr_output, Not(HasSubstr(teststring)));
}

TEST(LoggingTest, DirectLogsToBufferStreamWorks) {
Expand All @@ -105,15 +107,11 @@ TEST(LoggingTest, DirectLogsToBufferStreamWorks) {

// Check that output went to the buffer.
std::string output(stringbuf_ptr->str());
EXPECT_TRUE(output.find(teststring) != std::string::npos)
<< "log output is missing the log message, the output is: " << output;
EXPECT_THAT(output, HasSubstr(teststring));

// Check that the output never went to stderr.
std::string stderr_output = testing::internal::GetCapturedStderr();
EXPECT_TRUE(stderr_output.find(teststring) == std::string::npos)
<< "stderr unexpectedly contains the log message that should have gone "
"to the specified string buffer. Stderr contained: "
<< stderr_output;
EXPECT_THAT(stderr_output, Not(HasSubstr(teststring)));
}

TEST(LoggingTest, BufferedLogsSentToSpecifiedStream) {
Expand All @@ -127,12 +125,9 @@ TEST(LoggingTest, BufferedLogsSentToSpecifiedStream) {
"test sending logs to the buffer before setting the output stream";
BAZEL_LOG(INFO) << teststring;

// Check that stderr isn't getting anything yet.
// Check that stderr isn't getting anything.
std::string stderr_output = testing::internal::GetCapturedStderr();
EXPECT_TRUE(stderr_output.find(teststring) == std::string::npos)
<< "stderr unexpectedly contains the log message, before log output was "
"set. Stderr contained: "
<< stderr_output;
EXPECT_THAT(stderr_output, Not(HasSubstr(teststring)));
testing::internal::CaptureStderr();

// Ask that the logs get output to a string buffer (keep a ptr to it so we can
Expand All @@ -143,15 +138,11 @@ TEST(LoggingTest, BufferedLogsSentToSpecifiedStream) {

// Check that the buffered logs were sent.
std::string output(stringbuf_ptr->str());
EXPECT_TRUE(output.find(teststring) != std::string::npos)
<< "log output is missing the log message, the output is: " << output;
EXPECT_THAT(output, HasSubstr(teststring));

// Check that the output did not go to stderr.
stderr_output = testing::internal::GetCapturedStderr();
EXPECT_TRUE(stderr_output.find(teststring) == std::string::npos)
<< "stderr unexpectedly contains the log message that should have gone "
"to the specified string buffer. Stderr contained: "
<< stderr_output;
EXPECT_THAT(stderr_output, Not(HasSubstr(teststring)));
}

TEST(LoggingTest, DirectLogsToCerrWorks) {
Expand All @@ -171,9 +162,7 @@ TEST(LoggingTest, DirectLogsToCerrWorks) {
// Cause the logs to be flushed, and capture them.
blaze_util::SetLogHandler(nullptr);
std::string stderr_output = testing::internal::GetCapturedStderr();
EXPECT_TRUE(stderr_output.find(teststring) != std::string::npos)
<< "stderr does not contain the expected log message. Stderr contained: "
<< stderr_output;
EXPECT_THAT(stderr_output, HasSubstr(teststring));
}

TEST(LoggingTest, BufferedLogsGetDirectedToCerr) {
Expand All @@ -193,9 +182,7 @@ TEST(LoggingTest, BufferedLogsGetDirectedToCerr) {
// Cause the logs to be flushed, and capture them.
blaze_util::SetLogHandler(nullptr);
std::string stderr_output = testing::internal::GetCapturedStderr();
EXPECT_TRUE(stderr_output.find(teststring) != std::string::npos)
<< "stderr does not contain the expected log message. Stderr contained: "
<< stderr_output;
EXPECT_THAT(stderr_output, HasSubstr(teststring));
}

TEST(LoggingTest, ImpossibleFile) {
Expand All @@ -213,9 +200,7 @@ TEST(LoggingTest, ImpossibleFile) {
// Cause the logs to be flushed, and capture them.
blaze_util::SetLogHandler(nullptr);
std::string stderr_output = testing::internal::GetCapturedStderr();
EXPECT_TRUE(stderr_output.find("ERROR") != std::string::npos);
EXPECT_TRUE(stderr_output.find("Provided stream failed") != std::string::npos)
<< "stderr does not contain the expected error. Stderr contained: "
<< stderr_output;
EXPECT_THAT(stderr_output,
MatchesRegex(".bazel ERROR.* Provided stream failed.\n"));
}
} // namespace blaze_util
2 changes: 1 addition & 1 deletion src/test/cpp/util/md5_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
#include "src/main/cpp/util/md5.h"
#include "src/main/cpp/util/port.h"
#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze_util {

Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/util/numbers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
#include "src/main/cpp/util/numbers.h"
#include "src/main/cpp/util/port.h"
#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze_util {

Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/util/strings_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <string>
#include <vector>

#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze_util {

Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/workspace_layout_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "src/main/cpp/blaze_util_platform.h"
#include "src/main/cpp/util/file.h"
#include "gtest/gtest.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze {

Expand Down
2 changes: 1 addition & 1 deletion src/test/native/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ cc_test(
"//src/conditions:windows": [
"//src/main/native/windows:lib-file",
"//src/test/cpp/util:windows_test_util",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
],
"//conditions:default": [],
}),
Expand Down
4 changes: 2 additions & 2 deletions src/tools/launcher/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cc_test(
srcs = ["launcher_util_test.cc"],
deps = [
":util",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
],
)

Expand All @@ -36,7 +36,7 @@ cc_test(
srcs = ["data_parser_test.cc"],
deps = [
":data_parser",
"//third_party:gtest",
"@com_google_googletest//:gtest_main",
],
)

Expand Down
Loading

0 comments on commit 8e9f4a8

Please sign in to comment.