Skip to content
Merged
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
2 changes: 1 addition & 1 deletion tools/test/TestCSVFileImport.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
120 changes: 9 additions & 111 deletions tools/test/ToolTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions tools/test/ToolTest.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tools/test/gzip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down