zircon/system/utest/runtests-utils/runtests-utils-test.cc
// Passing an --output argument should result in output being written to that
// location.
Passing an --output argument should result in output being written to that
location.
Passing an -- output argument should result in output being written to that location .
TEST(DiscoverAndRunTests, DiscoverAndRunTestsWithOutput) {
  fbl::String all_scripts_dir = packaged_script_dir();
  fbl::String script_dir = JoinPath(all_scripts_dir, "testwithoutput");
  ScopedTestDir test_dir;
  const fbl::String succeed_file_name = JoinPath(script_dir, "test-with-output-succeed.sh");
  const fbl::String fail_file_name = JoinPath(script_dir, "test-with-output-fail.sh");
  const fbl::String output_dir = JoinPath(test_dir.path(), "run-all-tests-output-1");
  EXPECT_EQ(0, MkDirAll(output_dir));
  const char* const argv[] = {"./runtests", "--all", "--output", output_dir.c_str()};
  TestStopwatch stopwatch;
  EXPECT_EQ(EXIT_FAILURE, DiscoverAndRunTests(4, argv, {script_dir.c_str()}, &stopwatch, ""));
  fbl::String success_output_rel_path;
  ASSERT_TRUE(GetOutputFileRelPath(output_dir, succeed_file_name, &success_output_rel_path));
  fbl::String failure_output_rel_path;
  ASSERT_TRUE(GetOutputFileRelPath(output_dir, fail_file_name, &failure_output_rel_path));
  char expected_pass_output_buf[1024];
  sprintf(expected_pass_output_buf,
          R"(    \{
      "name": "%s",
      "output_file": "%s",
      "result": "PASS",
      "duration_milliseconds": \d+
    \})",
          succeed_file_name.c_str(),
          success_output_rel_path.c_str() + 1);  
  std::regex expected_pass_output_regex(expected_pass_output_buf);
  char expected_fail_output_buf[1024];
  sprintf(expected_fail_output_buf,
          R"(    \{
      "name": "%s",
      "output_file": "%s",
      "result": "FAIL",
      "duration_milliseconds": \d+
    \})",
          fail_file_name.c_str(),
          failure_output_rel_path.c_str() + 1);  
  std::regex expected_fail_output_regex(expected_fail_output_buf);
  const fbl::String output_path = JoinPath(output_dir, "summary.json");
  FILE* output_file = fopen(output_path.c_str(), "r");
  ASSERT_TRUE(output_file);
  char buf[1024];
  memset(buf, 0, sizeof(buf));
  EXPECT_LT(0, fread(buf, sizeof(buf[0]), sizeof(buf), output_file));
  fclose(output_file);
  EXPECT_EQ(0, strncmp(kExpectedJSONOutputPrefix, &buf[0], kExpectedJSONOutputPrefixSize));
  std::cmatch fail_output_match;
  std::cmatch pass_output_match;
  EXPECT_TRUE(std::regex_search(buf, pass_output_match, expected_pass_output_regex));
  EXPECT_TRUE(std::regex_search(buf, fail_output_match, expected_fail_output_regex));
  auto outputs_end_index = std::max(pass_output_match.position() + pass_output_match.length(),
                                    fail_output_match.position() + fail_output_match.length());
  EXPECT_STR_EQ("\n  ]\n}\n", &buf[outputs_end_index]);
}
TEST ( DiscoverAndRunTests , DiscoverAndRunTestsWithOutput ) { fbl :: String all_scripts_dir = packaged_script_dir ( ) ; fbl :: String script_dir = JoinPath ( all_scripts_dir , " " ) ; ScopedTestDir test_dir ; const fbl :: String succeed_file_name = JoinPath ( script_dir , " " ) ; const fbl :: String fail_file_name = JoinPath ( script_dir , " " ) ; const fbl :: String output_dir = JoinPath ( test_dir . path ( ) , " " ) ; EXPECT_EQ ( 0 , MkDirAll ( output_dir ) ) ; const char * const argv [ ] = { " " , " " , " " , output_dir . c_str ( ) } ; TestStopwatch stopwatch ; EXPECT_EQ ( EXIT_FAILURE , DiscoverAndRunTests ( 4 , argv , { script_dir . c_str ( ) } , & stopwatch , " " ) ) ; fbl :: String success_output_rel_path ; ASSERT_TRUE ( GetOutputFileRelPath ( output_dir , succeed_file_name , & success_output_rel_path ) ) ; fbl :: String failure_output_rel_path ; ASSERT_TRUE ( GetOutputFileRelPath ( output_dir , fail_file_name , & failure_output_rel_path ) ) ; char expected_pass_output_buf [ 1024 ] ; sprintf ( expected_pass_output_buf , R" (     \{
      "name": "%s",
      "output_file": "%s",
      "result": "PASS",
      "duration_milliseconds": \d+
    \} ) " , succeed_file_name . c_str ( ) , success_output_rel_path . c_str ( ) + 1 ) ; std :: regex expected_pass_output_regex ( expected_pass_output_buf ) ; char expected_fail_output_buf [ 1024 ] ; sprintf ( expected_fail_output_buf , R" (     \{
      "name": "%s",
      "output_file": "%s",
      "result": "FAIL",
      "duration_milliseconds": \d+
    \} ) " , fail_file_name . c_str ( ) , failure_output_rel_path . c_str ( ) + 1 ) ; std :: regex expected_fail_output_regex ( expected_fail_output_buf ) ; const fbl :: String output_path = JoinPath ( output_dir , " " ) ; FILE * output_file = fopen ( output_path . c_str ( ) , " " ) ; ASSERT_TRUE ( output_file ) ; char buf [ 1024 ] ; memset ( buf , 0 , sizeof ( buf ) ) ; EXPECT_LT ( 0 , fread ( buf , sizeof ( buf [ 0 ] ) , sizeof ( buf ) , output_file ) ) ; fclose ( output_file ) ; EXPECT_EQ ( 0 , strncmp ( kExpectedJSONOutputPrefix , & buf [ 0 ] , kExpectedJSONOutputPrefixSize ) ) ; std :: cmatch fail_output_match ; std :: cmatch pass_output_match ; EXPECT_TRUE ( std :: regex_search ( buf , pass_output_match , expected_pass_output_regex ) ) ; EXPECT_TRUE ( std :: regex_search ( buf , fail_output_match , expected_fail_output_regex ) ) ; auto outputs_end_index = std :: max ( pass_output_match . position ( ) + pass_output_match . length ( ) , fail_output_match . position ( ) + fail_output_match . length ( ) ) ; EXPECT_STR_EQ ( " \n \n \n " , & buf [ outputs_end_index ] ) ; }
Passing an --output argument should result in output being written to that
location.
Passing an -- output argument should result in output being written to that location .
// Prepare the expected output. // +1 to discard the leading slash. // +1 to discared the leading slash. // Extract the actual output. // The order of the tests in summary.json is not defined, so first check the // prefix, then be permissive about order of the actual tests.