return fds;
}
-template<class Runnable>
+template <class Runnable>
void checkFdLeak(const Runnable& r) {
// Get the currently open fds. Check that they are the same both before and
// after calling the specified function. We read the open fds from /proc.
});
// Normal execution with pipes
checkFdLeak([] {
- Subprocess proc("echo foo; echo bar >&2",
- Subprocess::pipeStdout() | Subprocess::pipeStderr());
+ Subprocess proc(
+ "echo foo; echo bar >&2",
+ Subprocess::Options().pipeStdout().pipeStderr());
auto p = proc.communicate();
EXPECT_EQ("foo\n", p.first);
EXPECT_EQ("bar\n", p.second);
// Test where the exec call fails() with pipes
checkFdLeak([] {
try {
- Subprocess proc(std::vector<std::string>({"/no/such/file"}),
- Subprocess::pipeStdout().stderrFd(Subprocess::PIPE));
+ Subprocess proc(
+ std::vector<std::string>({"/no/such/file"}),
+ Subprocess::Options().pipeStdout().stderrFd(Subprocess::PIPE));
ADD_FAILURE() << "expected an error when running /no/such/file";
} catch (const SubprocessSpawnError& ex) {
EXPECT_EQ(ENOENT, ex.errnoValue());
}
TEST(PopenSubprocessTest, PopenRead) {
- Subprocess proc("ls /", Subprocess::pipeStdout());
+ Subprocess proc("ls /", Subprocess::Options().pipeStdout());
int found = 0;
gen::byLine(File(proc.stdoutFd())) |
[&] (StringPiece line) {
: public Subprocess::DangerousPostForkPreExecCallback {
explicit WriteFileAfterFork(std::string filename)
: filename_(std::move(filename)) {}
- virtual ~WriteFileAfterFork() {}
+ ~WriteFileAfterFork() override {}
int operator()() override {
return writeFile(std::string("ok"), filename_.c_str()) ? 0 : errno;
}
}
TEST(CommunicateSubprocessTest, SimpleRead) {
- Subprocess proc(std::vector<std::string>{ "/bin/echo", "-n", "foo", "bar"},
- Subprocess::pipeStdout());
+ Subprocess proc(
+ std::vector<std::string>{"/bin/echo", "-n", "foo", "bar"},
+ Subprocess::Options().pipeStdout());
auto p = proc.communicate();
EXPECT_EQ("foo bar", p.first);
proc.waitChecked();
data.append(line);
}
- Subprocess proc("wc -l", Subprocess::pipeStdin() | Subprocess::pipeStdout());
+ Subprocess proc("wc -l", Subprocess::Options().pipeStdin().pipeStdout());
auto p = proc.communicate(data);
EXPECT_EQ(folly::format("{}\n", numLines).str(), p.first);
proc.waitChecked();
const int bytes = 10 << 20;
std::string line(bytes, 'x');
- Subprocess proc("tr a-z A-Z",
- Subprocess::pipeStdin() | Subprocess::pipeStdout());
+ Subprocess proc("tr a-z A-Z", Subprocess::Options().pipeStdin().pipeStdout());
auto p = proc.communicate(line);
EXPECT_EQ(bytes, p.first.size());
EXPECT_EQ(std::string::npos, p.first.find_first_not_of('X'));
"-e", "s/a test/a successful test/",
"-e", "/^another line/w/dev/stderr",
});
- auto options = Subprocess::pipeStdin().pipeStdout().pipeStderr().usePath();
+ auto options =
+ Subprocess::Options().pipeStdin().pipeStdout().pipeStderr().usePath();
Subprocess proc(cmd, options);
auto out = proc.communicateIOBuf(std::move(input));
proc.waitChecked();
return (n == 0);
}
-} // namespace
+} // namespace
TEST(CommunicateSubprocessTest, Chatty) {
checkFdLeak([] {
int wcount = 0;
int rcount = 0;
- auto options = Subprocess::pipeStdin().pipeStdout().pipeStderr().usePath();
+ auto options =
+ Subprocess::Options().pipeStdin().pipeStdout().pipeStderr().usePath();
std::vector<std::string> cmd {
"sed",
"-u",
std::vector<Subprocess::ChildPipe> pipes;
{
Subprocess proc(
- "echo $'oh\\nmy\\ncat' | wc -l &", Subprocess::pipeStdout()
- );
+ "echo $'oh\\nmy\\ncat' | wc -l &", Subprocess::Options().pipeStdout());
pipes = proc.takeOwnershipOfPipes();
proc.waitChecked();
}