You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@orc.apache.org by ga...@apache.org on 2019/08/06 23:28:53 UTC

[orc] branch master updated: ORC-533: [C++] Simplify ToolTest and make it portable

This is an automated email from the ASF dual-hosted git repository.

gangwu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/orc.git


The following commit(s) were added to refs/heads/master by this push:
     new f766d3a  ORC-533: [C++] Simplify ToolTest and make it portable
f766d3a is described below

commit f766d3a77617294ba2af34957992e30f9214ecd6
Author: Renat Valiullin <ri...@gmail.com>
AuthorDate: Tue Aug 6 16:28:49 2019 -0700

    ORC-533: [C++] Simplify ToolTest and make it portable
    
    This closes #413
---
 tools/test/TestCSVFileImport.cc |   2 +-
 tools/test/ToolTest.cc          | 120 +++-------------------------------------
 tools/test/ToolTest.hh          |   6 +-
 tools/test/gzip.cc              |   4 +-
 4 files changed, 15 insertions(+), 117 deletions(-)

diff --git a/tools/test/TestCSVFileImport.cc b/tools/test/TestCSVFileImport.cc
index 23136d6..79bc3de 100644
--- a/tools/test/TestCSVFileImport.cc
+++ b/tools/test/TestCSVFileImport.cc
@@ -29,7 +29,7 @@ TEST (TestCSVFileImport, test10rows) {
   const std::string pgm1 = findProgram("tools/src/csv-import");
   const std::string csvFile = findExample("TestCSVFileImport.test10rows.csv");
   const std::string orcFile = "/tmp/test_csv_import_test_10_rows.orc";
-  const std::string schema = "struct<_a:bigint,b_:string,c_col:double>";
+  const std::string schema = "'struct<_a:bigint,b_:string,c_col:double>'";
   std::string output;
   std::string error;
 
diff --git a/tools/test/ToolTest.cc b/tools/test/ToolTest.cc
index 8008a56..2344d3f 100644
--- a/tools/test/ToolTest.cc
+++ b/tools/test/ToolTest.cc
@@ -25,13 +25,9 @@
 #include "wrap/gtest-wrapper.h"
 
 #include <cerrno>
-#include <cstdio>
-#include <fcntl.h>
-#include <fstream>
 #include <iostream>
 #include <sstream>
 #include <string>
-#include <unistd.h>
 #include <vector>
 
 namespace {
@@ -62,123 +58,25 @@ GTEST_API_ int main(int argc, char **argv) {
   return result;
 }
 
-std::string getFileContents(const char *filename) {
-  std::ifstream in(filename, std::ios::in | std::ios::binary);
-  if (in) {
-    std::ostringstream contents;
-    contents << in.rdbuf();
-    in.close();
-    return contents.str();
-  }
-  std::cerr << "Can't read " << filename << "\n";
-  exit(1);
-}
-
 /**
  * Run the given program and set the stdout and stderr parameters to
  * the output on each of the streams. The return code of the program is
  * returned as the result.
  */
-int runProgram(const std::vector<std::string>& command,
+int runProgram(const std::vector<std::string> &args,
                std::string &out,
                std::string &err) {
+  std::ostringstream command;
+  std::copy(args.begin(), args.end(),
+            std::ostream_iterator<std::string>(command, " "));
 
-  // create temporary filenames for stdout and stderr
-  std::string stdoutStr = "/tmp/orc-test-stdout-XXXXXXXX";
-  std::string stderrStr = "/tmp/orc-test-stderr-XXXXXXXX";
-  char *stdoutName = const_cast<char*>(stdoutStr.data());
-  char *stderrName = const_cast<char*>(stderrStr.data());
-  if (mkstemp(stdoutName) == -1) {
-    std::cerr << "Failed to make unique name " << stdoutName
-              << " - " << strerror(errno) << "\n";
-    exit(1);
-  }
-  if (mkstemp(stderrName) == -1) {
-    std::cerr << "Failed to make unique name " << stderrName
-              << " - " << strerror(errno) << "\n";
-    exit(1);
-  }
-
-  // flush stdout and stderr to make sure they aren't duplicated.
-  // ignore errors since pipes don't support fsync
-  fsync(1);
-  fsync(2);
-
-  // actuall fork
-  pid_t child = fork();
-  if (child == -1) {
-    std::cerr << "Failed to fork - " << strerror(errno) << "\n";
-    exit(1);
-  } else if (child == 0) {
+  testing::internal::CaptureStdout();
+  testing::internal::CaptureStderr();
 
-    // build the parameters
-    std::unique_ptr<const char*> argv(new const char*[command.size() + 1]);
-    for(uint64_t i=0; i < command.size(); ++i) {
-      argv.get()[i] = command[i].c_str();
-    }
-    argv.get()[command.size()] = 0;
+  int status = system(command.str().c_str());
 
-    // do the stdout & stderr redirection
-    int stdoutFd = open(stdoutName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
-    if (stdoutFd == -1) {
-      std::cerr << "Failed to open " << stdoutName << " - "
-                << strerror(errno) << "\n";
-      exit(1);
-    }
-    if (dup2(stdoutFd, 1) == -1) {
-      std::cerr << "Failed to redirect stdout - " << strerror(errno) << "\n";
-      exit(1);
-    }
-    close(stdoutFd);
-    int stderrFd = open(stderrName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
-    if (stderrFd == -1) {
-      std::cerr << "Failed to open " << stderrName << " - "
-                << strerror(errno) << "\n";
-      exit(1);
-    }
-    if (dup2(stderrFd, 2) == -1) {
-      std::cerr << "Failed to redirect stderr - " << strerror(errno) << "\n";
-      exit(1);
-    }
-    close(stderrFd);
-
-    // run the program
-    execvp(argv.get()[0], const_cast<char * const *>(argv.get()));
-
-    // can only reach here if the exec fails
-    std::cerr << "Can't run -";
-    for(uint64_t i=0; i < command.size(); ++i) {
-      std::cerr << " " << command[i];
-    }
-    std::cerr << "\n";
-    std::cerr << "Exec failed with " << strerror(errno) << "\n";
-    exit(1);
-  }
-  int status = 0;
-  pid_t result = waitpid(child, &status, 0);
-  if (result == -1 || !WIFEXITED(status)) {
-    std::cerr << "Can't run -";
-    for(uint64_t i=0; i < command.size(); ++i) {
-      std::cerr << " " << command[i];
-    }
-    std::cerr << "\n";
-    std::cerr << "stdout: " << stdoutName << ", stderr: "
-              << stderrName << "\n";
-    if (result == -1) {
-      std::cerr << "Error: " << strerror(errno) << "\n";
-    } else if (WIFSIGNALED(status)) {
-      std::cerr << "Fatal signal: " << WTERMSIG(status) << "\n";
-    }
-    exit(1);
-  }
-  out = getFileContents(stdoutName);
-  if (std::remove(stdoutName) != 0) {
-    std::cerr << "Failed to remove " << stdoutName << "\n";
-  }
-  err = getFileContents(stderrName);
-  if (std::remove(stderrName) != 0) {
-    std::cerr << "Failed to remove " << stderrName << "\n";
-  }
+  out = testing::internal::GetCapturedStdout();
+  err = testing::internal::GetCapturedStderr();
 
   return WEXITSTATUS(status);
 }
diff --git a/tools/test/ToolTest.hh b/tools/test/ToolTest.hh
index 8e926ac..e43bde8 100644
--- a/tools/test/ToolTest.hh
+++ b/tools/test/ToolTest.hh
@@ -24,9 +24,9 @@
  * the output on each of the streams. The return code of the program is
  * returned as the result.
  */
-int runProgram(const std::vector<std::string>& command,
-               std::string &stdout,
-               std::string &stderr);
+int runProgram(const std::vector<std::string> &command,
+               std::string &out,
+               std::string &err);
 
 /**
  * Get the name of the given example file.
diff --git a/tools/test/gzip.cc b/tools/test/gzip.cc
index c5ada8b..7161111 100644
--- a/tools/test/gzip.cc
+++ b/tools/test/gzip.cc
@@ -57,8 +57,8 @@ namespace orc {
     // if the last read is done, read more
     if (stream.avail_in == 0 && stream.avail_out != 0) {
       stream.next_in = input;
-      stream.avail_in = static_cast<uint>(fread(input, 1, sizeof(input),
-                                                file));
+      stream.avail_in = static_cast<unsigned>(fread(input, 1, sizeof(input),
+                                                    file));
       if (ferror(file)) {
         throw std::runtime_error("failure reading " + filename);
       }