diff --git a/tools/test/TestCSVFileImport.cc b/tools/test/TestCSVFileImport.cc index 23136d640f..79bc3de246 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 8008a5638e..2344d3f976 100644 --- a/tools/test/ToolTest.cc +++ b/tools/test/ToolTest.cc @@ -25,13 +25,9 @@ #include "wrap/gtest-wrapper.h" #include -#include -#include -#include #include #include #include -#include #include 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& command, +int runProgram(const std::vector &args, std::string &out, std::string &err) { + std::ostringstream command; + std::copy(args.begin(), args.end(), + std::ostream_iterator(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(stdoutStr.data()); - char *stderrName = const_cast(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 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(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 8e926ac0e6..e43bde8077 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& command, - std::string &stdout, - std::string &stderr); +int runProgram(const std::vector &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 c5ada8b3b6..7161111f0d 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(fread(input, 1, sizeof(input), - file)); + stream.avail_in = static_cast(fread(input, 1, sizeof(input), + file)); if (ferror(file)) { throw std::runtime_error("failure reading " + filename); }