* commit '4600bdac709969408ac446507d85881db55b0ca7': Make all output of child test go to parent process in gtest_main.
This commit is contained in:
		@@ -124,15 +124,15 @@ class Test {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  int64_t GetTestTime() const { return elapsed_time_ns_; }
 | 
					  int64_t GetTestTime() const { return elapsed_time_ns_; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void AppendFailureMessage(const std::string& s) { failure_message_ += s; }
 | 
					  void AppendTestOutput(const std::string& s) { output_ += s; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const std::string& GetFailureMessage() const { return failure_message_; }
 | 
					  const std::string& GetTestOutput() const { return output_; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 private:
 | 
					 private:
 | 
				
			||||||
  const std::string name_;
 | 
					  const std::string name_;
 | 
				
			||||||
  TestResult result_;
 | 
					  TestResult result_;
 | 
				
			||||||
  int64_t elapsed_time_ns_;
 | 
					  int64_t elapsed_time_ns_;
 | 
				
			||||||
  std::string failure_message_;
 | 
					  std::string output_;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestCase {
 | 
					class TestCase {
 | 
				
			||||||
@@ -196,10 +196,6 @@ class TestCase {
 | 
				
			|||||||
  std::vector<Test> test_list_;
 | 
					  std::vector<Test> test_list_;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// This is the file descriptor used by the child process to write failure message.
 | 
					 | 
				
			||||||
// The parent process will collect the information and dump to stdout / xml file.
 | 
					 | 
				
			||||||
static int child_output_fd;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class TestResultPrinter : public testing::EmptyTestEventListener {
 | 
					class TestResultPrinter : public testing::EmptyTestEventListener {
 | 
				
			||||||
 public:
 | 
					 public:
 | 
				
			||||||
  TestResultPrinter() : pinfo_(NULL) {}
 | 
					  TestResultPrinter() : pinfo_(NULL) {}
 | 
				
			||||||
@@ -219,25 +215,9 @@ void TestResultPrinter::OnTestPartResult(const testing::TestPartResult& result)
 | 
				
			|||||||
    return;
 | 
					    return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Print failure message from the assertion (e.g. expected this and got that).
 | 
					  // Print failure message from the assertion (e.g. expected this and got that).
 | 
				
			||||||
  char buf[1024];
 | 
					  printf("%s:(%d) Failure in test %s.%s\n%s\n", result.file_name(), result.line_number(),
 | 
				
			||||||
  snprintf(buf, sizeof(buf), "%s:(%d) Failure in test %s.%s\n%s\n", result.file_name(),
 | 
					         pinfo_->test_case_name(), pinfo_->name(), result.message());
 | 
				
			||||||
                                                                    result.line_number(),
 | 
					  fflush(stdout);
 | 
				
			||||||
                                                                    pinfo_->test_case_name(),
 | 
					 | 
				
			||||||
                                                                    pinfo_->name(),
 | 
					 | 
				
			||||||
                                                                    result.message());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  int towrite = strlen(buf);
 | 
					 | 
				
			||||||
  char* p = buf;
 | 
					 | 
				
			||||||
  while (towrite > 0) {
 | 
					 | 
				
			||||||
    ssize_t bytes_written = TEMP_FAILURE_RETRY(write(child_output_fd, p, towrite));
 | 
					 | 
				
			||||||
    if (bytes_written == -1) {
 | 
					 | 
				
			||||||
      fprintf(stderr, "failed to write child_output_fd: %s\n", strerror(errno));
 | 
					 | 
				
			||||||
      exit(1);
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      towrite -= bytes_written;
 | 
					 | 
				
			||||||
      p += bytes_written;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int64_t NanoTime() {
 | 
					static int64_t NanoTime() {
 | 
				
			||||||
@@ -332,8 +312,8 @@ static void OnTestEndPrint(const TestCase& testcase, size_t test_id) {
 | 
				
			|||||||
    printf("\n");
 | 
					    printf("\n");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const std::string& failure_message = testcase.GetTest(test_id).GetFailureMessage();
 | 
					  const std::string& test_output = testcase.GetTest(test_id).GetTestOutput();
 | 
				
			||||||
  printf("%s", failure_message.c_str());
 | 
					  printf("%s", test_output.c_str());
 | 
				
			||||||
  fflush(stdout);
 | 
					  fflush(stdout);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -481,8 +461,8 @@ void OnTestIterationEndXmlPrint(const std::string& xml_output_filename,
 | 
				
			|||||||
        fputs(" />\n", fp);
 | 
					        fputs(" />\n", fp);
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        fputs(">\n", fp);
 | 
					        fputs(">\n", fp);
 | 
				
			||||||
        const std::string& failure_message = testcase.GetTest(j).GetFailureMessage();
 | 
					        const std::string& test_output = testcase.GetTest(j).GetTestOutput();
 | 
				
			||||||
        fprintf(fp, "      <failure message=\"%s\" type=\"\">\n", failure_message.c_str());
 | 
					        fprintf(fp, "      <failure message=\"%s\" type=\"\">\n", test_output.c_str());
 | 
				
			||||||
        fputs("      </failure>\n", fp);
 | 
					        fputs("      </failure>\n", fp);
 | 
				
			||||||
        fputs("    </testcase>\n", fp);
 | 
					        fputs("    </testcase>\n", fp);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@@ -538,7 +518,10 @@ static ChildProcInfo RunChildProcess(const std::string& test_name, int testcase_
 | 
				
			|||||||
  } else if (pid == 0) {
 | 
					  } else if (pid == 0) {
 | 
				
			||||||
    // In child process, run a single test.
 | 
					    // In child process, run a single test.
 | 
				
			||||||
    close(pipefd[0]);
 | 
					    close(pipefd[0]);
 | 
				
			||||||
    child_output_fd = pipefd[1];
 | 
					    close(STDOUT_FILENO);
 | 
				
			||||||
 | 
					    close(STDERR_FILENO);
 | 
				
			||||||
 | 
					    dup2(pipefd[1], STDOUT_FILENO);
 | 
				
			||||||
 | 
					    dup2(pipefd[1], STDERR_FILENO);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (sigprocmask(SIG_SETMASK, &sigmask, NULL) == -1) {
 | 
					    if (sigprocmask(SIG_SETMASK, &sigmask, NULL) == -1) {
 | 
				
			||||||
      perror("sigprocmask SIG_SETMASK");
 | 
					      perror("sigprocmask SIG_SETMASK");
 | 
				
			||||||
@@ -692,7 +675,7 @@ static void CollectChildTestResult(const ChildProcInfo& child_proc, TestCase& te
 | 
				
			|||||||
    ssize_t bytes_read = TEMP_FAILURE_RETRY(read(child_proc.child_read_fd, buf, sizeof(buf) - 1));
 | 
					    ssize_t bytes_read = TEMP_FAILURE_RETRY(read(child_proc.child_read_fd, buf, sizeof(buf) - 1));
 | 
				
			||||||
    if (bytes_read > 0) {
 | 
					    if (bytes_read > 0) {
 | 
				
			||||||
      buf[bytes_read] = '\0';
 | 
					      buf[bytes_read] = '\0';
 | 
				
			||||||
      testcase.GetTest(test_id).AppendFailureMessage(buf);
 | 
					      testcase.GetTest(test_id).AppendTestOutput(buf);
 | 
				
			||||||
    } else if (bytes_read == 0) {
 | 
					    } else if (bytes_read == 0) {
 | 
				
			||||||
      break; // Read end.
 | 
					      break; // Read end.
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
@@ -713,7 +696,7 @@ static void CollectChildTestResult(const ChildProcInfo& child_proc, TestCase& te
 | 
				
			|||||||
    char buf[1024];
 | 
					    char buf[1024];
 | 
				
			||||||
    snprintf(buf, sizeof(buf), "%s killed because of timeout at %" PRId64 " ms.\n",
 | 
					    snprintf(buf, sizeof(buf), "%s killed because of timeout at %" PRId64 " ms.\n",
 | 
				
			||||||
             testcase.GetTestName(test_id).c_str(), testcase.GetTestTime(test_id) / 1000000);
 | 
					             testcase.GetTestName(test_id).c_str(), testcase.GetTestTime(test_id) / 1000000);
 | 
				
			||||||
    testcase.GetTest(test_id).AppendFailureMessage(buf);
 | 
					    testcase.GetTest(test_id).AppendTestOutput(buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  } else if (WIFSIGNALED(child_proc.exit_status)) {
 | 
					  } else if (WIFSIGNALED(child_proc.exit_status)) {
 | 
				
			||||||
    // Record signal terminated test as failed.
 | 
					    // Record signal terminated test as failed.
 | 
				
			||||||
@@ -721,7 +704,7 @@ static void CollectChildTestResult(const ChildProcInfo& child_proc, TestCase& te
 | 
				
			|||||||
    char buf[1024];
 | 
					    char buf[1024];
 | 
				
			||||||
    snprintf(buf, sizeof(buf), "%s terminated by signal: %s.\n",
 | 
					    snprintf(buf, sizeof(buf), "%s terminated by signal: %s.\n",
 | 
				
			||||||
             testcase.GetTestName(test_id).c_str(), strsignal(WTERMSIG(child_proc.exit_status)));
 | 
					             testcase.GetTestName(test_id).c_str(), strsignal(WTERMSIG(child_proc.exit_status)));
 | 
				
			||||||
    testcase.GetTest(test_id).AppendFailureMessage(buf);
 | 
					    testcase.GetTest(test_id).AppendTestOutput(buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    testcase.SetTestResult(test_id, WEXITSTATUS(child_proc.exit_status) == 0 ?
 | 
					    testcase.SetTestResult(test_id, WEXITSTATUS(child_proc.exit_status) == 0 ?
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user