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

New feature implementation: Streaming results format to stdout #4665 #4668

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions googletest/include/gtest/gtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ GTEST_DECLARE_int32_(stack_trace_depth);
// non-zero code otherwise. For use with an external test framework.
GTEST_DECLARE_bool_(throw_on_failure);

// When this flag is set, results are streamed to stdout
// without pretty-print formatting
GTEST_DECLARE_bool_(machine_results);

// When this flag is set with a "host:port" string, on supported
// platforms test results are streamed to the specified port on
// the specified host machine.
Expand Down
9 changes: 6 additions & 3 deletions googletest/src/gtest-internal-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ class GTestFlagSaver {
GTEST_FLAG_GET(recreate_environments_when_repeating);
shuffle_ = GTEST_FLAG_GET(shuffle);
stack_trace_depth_ = GTEST_FLAG_GET(stack_trace_depth);
stream_result_to_ = GTEST_FLAG_GET(stream_result_to);
throw_on_failure_ = GTEST_FLAG_GET(throw_on_failure);
machine_results_ = GTEST_FLAG_GET(machine_results);
stream_result_to_ = GTEST_FLAG_GET(stream_result_to);
}

// The d'tor is not virtual. DO NOT INHERIT FROM THIS CLASS.
Expand All @@ -188,8 +189,9 @@ class GTestFlagSaver {
recreate_environments_when_repeating_);
GTEST_FLAG_SET(shuffle, shuffle_);
GTEST_FLAG_SET(stack_trace_depth, stack_trace_depth_);
GTEST_FLAG_SET(stream_result_to, stream_result_to_);
GTEST_FLAG_SET(throw_on_failure, throw_on_failure_);
GTEST_FLAG_SET(machine_results, machine_results_);
GTEST_FLAG_SET(stream_result_to, stream_result_to_);
}

private:
Expand All @@ -213,8 +215,9 @@ class GTestFlagSaver {
bool recreate_environments_when_repeating_;
bool shuffle_;
int32_t stack_trace_depth_;
std::string stream_result_to_;
bool throw_on_failure_;
bool machine_results_;
std::string stream_result_to_;
};

// Converts a Unicode code point to a narrow string in UTF-8 encoding.
Expand Down
29 changes: 21 additions & 8 deletions googletest/src/gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,20 +374,26 @@ GTEST_DEFINE_int32_(
"The maximum number of stack frames to print when an "
"assertion fails. The valid range is 0 through 100, inclusive.");

GTEST_DEFINE_string_(
stream_result_to,
testing::internal::StringFromGTestEnv("stream_result_to", ""),
"This flag specifies the host name and the port number on which to stream "
"test results. Example: \"localhost:555\". The flag is effective only on "
"Linux and macOS.");

GTEST_DEFINE_bool_(
throw_on_failure,
testing::internal::BoolFromGTestEnv("throw_on_failure", false),
"When this flag is specified, a failed assertion will throw an exception "
"if exceptions are enabled or exit the program with a non-zero code "
"otherwise. For use with an external test framework.");

GTEST_DEFINE_bool_(
machine_results,
testing::internal::BoolFromGTestEnv("machine_results", false),
"When this flag is specified, results are streamed to stdout "
"without pretty-printing. Uses the same format as stream_result_to");

GTEST_DEFINE_string_(
stream_result_to,
testing::internal::StringFromGTestEnv("stream_result_to", ""),
"This flag specifies the host name and the port number on which to stream "
"test results. Example: \"localhost:555\". The flag is effective only on "
"Linux and macOS.");

#if GTEST_USE_OWN_FLAGFILE_FLAG_
GTEST_DEFINE_string_(
flagfile, testing::internal::StringFromGTestEnv("flagfile", ""),
Expand Down Expand Up @@ -5723,6 +5729,12 @@ void UnitTestImpl::ConfigureXmlOutput() {
#endif // GTEST_HAS_FILE_SYSTEM
}

#if GTEST_HAS_TERMINAL
void PrintToTerminal() {
// Unsure of whether to use listener system, or to try and print events directly to terminal
}
#endif // GTEST_HAS_TERMINAL

#if GTEST_CAN_STREAM_RESULTS_
// Initializes event listeners for streaming test results in string form.
// Must not be called before InitGoogleTest.
Expand Down Expand Up @@ -6667,8 +6679,9 @@ static bool ParseGoogleTestFlag(const char* const arg) {
GTEST_INTERNAL_PARSE_FLAG(recreate_environments_when_repeating);
GTEST_INTERNAL_PARSE_FLAG(shuffle);
GTEST_INTERNAL_PARSE_FLAG(stack_trace_depth);
GTEST_INTERNAL_PARSE_FLAG(stream_result_to);
GTEST_INTERNAL_PARSE_FLAG(throw_on_failure);
GTEST_INTERNAL_PARSE_FLAG(machine_results);
GTEST_INTERNAL_PARSE_FLAG(stream_result_to);
return false;
}

Expand Down
75 changes: 49 additions & 26 deletions googletest/test/gtest_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) {
GTEST_FLAG_GET(recreate_environments_when_repeating) ||
GTEST_FLAG_GET(show_internal_stack_frames) || GTEST_FLAG_GET(shuffle) ||
GTEST_FLAG_GET(stack_trace_depth) > 0 ||
GTEST_FLAG_GET(stream_result_to) != "unknown" ||
GTEST_FLAG_GET(throw_on_failure);
GTEST_FLAG_GET(throw_on_failure) ||
GTEST_FLAG_GET(machine_results) ||
GTEST_FLAG_GET(stream_result_to) != "unknown";
EXPECT_TRUE(dummy || !dummy); // Suppresses warning that dummy is unused.
}

Expand Down Expand Up @@ -1618,8 +1619,9 @@ class GTestFlagSaverTest : public Test {
GTEST_FLAG_SET(recreate_environments_when_repeating, true);
GTEST_FLAG_SET(shuffle, false);
GTEST_FLAG_SET(stack_trace_depth, kMaxStackTraceDepth);
GTEST_FLAG_SET(stream_result_to, "");
GTEST_FLAG_SET(throw_on_failure, false);
GTEST_FLAG_SET(machine_results, false);
GTEST_FLAG_SET(stream_result_to, "");
}

// Restores the Google Test flags that the tests have modified. This will
Expand Down Expand Up @@ -1648,8 +1650,9 @@ class GTestFlagSaverTest : public Test {
EXPECT_TRUE(GTEST_FLAG_GET(recreate_environments_when_repeating));
EXPECT_FALSE(GTEST_FLAG_GET(shuffle));
EXPECT_EQ(kMaxStackTraceDepth, GTEST_FLAG_GET(stack_trace_depth));
EXPECT_STREQ("", GTEST_FLAG_GET(stream_result_to).c_str());
EXPECT_FALSE(GTEST_FLAG_GET(throw_on_failure));
EXPECT_FALSE(GTEST_FLAG_GET(machine_results));
EXPECT_STREQ("", GTEST_FLAG_GET(stream_result_to).c_str());

GTEST_FLAG_SET(also_run_disabled_tests, true);
GTEST_FLAG_SET(break_on_failure, true);
Expand All @@ -1667,8 +1670,9 @@ class GTestFlagSaverTest : public Test {
GTEST_FLAG_SET(recreate_environments_when_repeating, false);
GTEST_FLAG_SET(shuffle, true);
GTEST_FLAG_SET(stack_trace_depth, 1);
GTEST_FLAG_SET(stream_result_to, "localhost:1234");
GTEST_FLAG_SET(throw_on_failure, true);
GTEST_FLAG_SET(machine_results, true);
GTEST_FLAG_SET(stream_result_to, "localhost:1234");
}

private:
Expand Down Expand Up @@ -5537,8 +5541,9 @@ struct Flags {
recreate_environments_when_repeating(true),
shuffle(false),
stack_trace_depth(kMaxStackTraceDepth),
stream_result_to(""),
throw_on_failure(false) {}
throw_on_failure(false),
machine_results(false),
stream_result_to("") {}

// Factory methods.

Expand Down Expand Up @@ -5664,22 +5669,31 @@ struct Flags {
return flags;
}

// Creates a Flags struct where the GTEST_FLAG(stream_result_to) flag has
// Creates a Flags struct where the GTEST_FLAG(throw_on_failure) flag has
// the given value.
static Flags StreamResultTo(const char* stream_result_to) {
static Flags ThrowOnFailure(bool throw_on_failure) {
Flags flags;
flags.stream_result_to = stream_result_to;
flags.throw_on_failure = throw_on_failure;
return flags;
}

// Creates a Flags struct where the gtest_throw_on_failure flag has
// Creates a Flags struct where GTEST_FLAG(machine_results) flag has
// the given value
static Flags MachineResults(bool machine_results) {
Flags flags;
flags.machine_results = machine_results;
return flags;
}

// Creates a Flags struct where the GTEST_FLAG(stream_result_to) flag has
// the given value.
static Flags ThrowOnFailure(bool throw_on_failure) {
static Flags StreamResultTo(const char* stream_result_to) {
Flags flags;
flags.throw_on_failure = throw_on_failure;
flags.stream_result_to = stream_result_to;
return flags;
}


// These fields store the flag values.
bool also_run_disabled_tests;
bool break_on_failure;
Expand All @@ -5696,8 +5710,9 @@ struct Flags {
bool recreate_environments_when_repeating;
bool shuffle;
int32_t stack_trace_depth;
const char* stream_result_to;
bool throw_on_failure;
bool machine_results;
const char* stream_result_to;
};

// Fixture for testing ParseGoogleTestFlagsOnly().
Expand All @@ -5720,8 +5735,9 @@ class ParseFlagsTest : public Test {
GTEST_FLAG_SET(recreate_environments_when_repeating, true);
GTEST_FLAG_SET(shuffle, false);
GTEST_FLAG_SET(stack_trace_depth, kMaxStackTraceDepth);
GTEST_FLAG_SET(stream_result_to, "");
GTEST_FLAG_SET(throw_on_failure, false);
GTEST_FLAG_SET(machine_results, false);
GTEST_FLAG_SET(stream_result_to, "");
}

// Asserts that two narrow or wide string arrays are equal.
Expand Down Expand Up @@ -5755,9 +5771,10 @@ class ParseFlagsTest : public Test {
GTEST_FLAG_GET(recreate_environments_when_repeating));
EXPECT_EQ(expected.shuffle, GTEST_FLAG_GET(shuffle));
EXPECT_EQ(expected.stack_trace_depth, GTEST_FLAG_GET(stack_trace_depth));
EXPECT_EQ(expected.throw_on_failure, GTEST_FLAG_GET(throw_on_failure));
EXPECT_EQ(expected.machine_results, GTEST_FLAG_GET(machine_results));
EXPECT_STREQ(expected.stream_result_to,
GTEST_FLAG_GET(stream_result_to).c_str());
EXPECT_EQ(expected.throw_on_failure, GTEST_FLAG_GET(throw_on_failure));
}

// Parses a command line (specified by argc1 and argv1), then
Expand Down Expand Up @@ -6195,16 +6212,6 @@ TEST_F(ParseFlagsTest, StackTraceDepth) {
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::StackTraceDepth(5), false);
}

TEST_F(ParseFlagsTest, StreamResultTo) {
const char* argv[] = {"foo.exe", "--gtest_stream_result_to=localhost:1234",
nullptr};

const char* argv2[] = {"foo.exe", nullptr};

GTEST_TEST_PARSING_FLAGS_(argv, argv2,
Flags::StreamResultTo("localhost:1234"), false);
}

// Tests parsing --gtest_throw_on_failure.
TEST_F(ParseFlagsTest, ThrowOnFailureWithoutValue) {
const char* argv[] = {"foo.exe", "--gtest_throw_on_failure", nullptr};
Expand Down Expand Up @@ -6233,6 +6240,22 @@ TEST_F(ParseFlagsTest, ThrowOnFailureTrue) {
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(true), false);
}

// Tests parsing --gtest_machine_results
TEST_F(ParseFlagsTest, MachineResultsTrue) {
// TODO
}

// Tests parsing --gtest_stream_result_to flag that is set to localhost:1234
TEST_F(ParseFlagsTest, StreamResultTo) {
const char* argv[] = {"foo.exe", "--gtest_stream_result_to=localhost:1234",
nullptr};

const char* argv2[] = {"foo.exe", nullptr};

GTEST_TEST_PARSING_FLAGS_(argv, argv2,
Flags::StreamResultTo("localhost:1234"), false);
}

// Tests parsing a bad --gtest_filter flag.
TEST_F(ParseFlagsTest, FilterBad) {
const char* argv[] = {"foo.exe", "--gtest_filter", nullptr};
Expand Down