Calls subprocess, pipes output to an open file descriptor (and specifically a file descriptor for a file, rather than a socket).
| 77 | // Calls subprocess, pipes output to an open file descriptor (and specifically |
| 78 | // a file descriptor for a file, rather than a socket). |
| 79 | TEST_F(SubprocessTest, PipeOutputToFileDescriptor) |
| 80 | { |
| 81 | // Create temporary files to pipe `stdin` to, and open it. We will pipe |
| 82 | // output into this file. |
| 83 | const string outfile_name = "out.txt"; |
| 84 | const string outfile = path::join(sandbox.get(), outfile_name); |
| 85 | ASSERT_SOME(os::touch(outfile)); |
| 86 | |
| 87 | Try<int_fd> outfile_fd = os::open(outfile, O_RDWR); |
| 88 | ASSERT_SOME(outfile_fd); |
| 89 | |
| 90 | // Create temporary files to pipe `stderr` to, and open it. We will pipe |
| 91 | // error into this file. |
| 92 | const string errorfile_name = "error.txt"; |
| 93 | const string errorfile = path::join(sandbox.get(), errorfile_name); |
| 94 | ASSERT_SOME(os::touch(errorfile)); |
| 95 | |
| 96 | Try<int_fd> errorfile_fd = os::open(errorfile, O_RDWR); |
| 97 | ASSERT_SOME(errorfile_fd); |
| 98 | |
| 99 | // RAII handle for the file descriptor increases chance that we clean up |
| 100 | // after ourselves. |
| 101 | const auto close_fd = [](int_fd* fd) { os::close(*fd); }; |
| 102 | |
| 103 | shared_ptr<int_fd> safe_out_fd(&outfile_fd.get(), close_fd); |
| 104 | shared_ptr<int_fd> safe_err_fd(&errorfile_fd.get(), close_fd); |
| 105 | |
| 106 | // Pipe simple string to output file. |
| 107 | run_subprocess( |
| 108 | [outfile_fd]() -> Try<Subprocess> { |
| 109 | return subprocess( |
| 110 | "echo hello", |
| 111 | Subprocess::FD(STDIN_FILENO), |
| 112 | Subprocess::FD(outfile_fd.get()), |
| 113 | Subprocess::FD(STDERR_FILENO)); |
| 114 | }); |
| 115 | |
| 116 | // Pipe simple string to error file. |
| 117 | run_subprocess( |
| 118 | [errorfile_fd]() -> Try<Subprocess> { |
| 119 | return subprocess( |
| 120 | "echo goodbye 1>&2", |
| 121 | Subprocess::FD(STDIN_FILENO), |
| 122 | Subprocess::FD(STDOUT_FILENO), |
| 123 | Subprocess::FD(errorfile_fd.get())); |
| 124 | }); |
| 125 | |
| 126 | // Finally, read output and error files, and make sure messages are inside. |
| 127 | const Result<string> output = os::read(outfile); |
| 128 | ASSERT_SOME(output); |
| 129 | EXPECT_EQ("hello\n", output.get()); |
| 130 | |
| 131 | const Result<string> error = os::read(errorfile); |
| 132 | ASSERT_SOME(error); |
| 133 | #ifdef __WINDOWS__ |
| 134 | EXPECT_EQ("goodbye \n", error.get()); |
| 135 | #else |
| 136 | EXPECT_EQ("goodbye\n", error.get()); |
nothing calls this directly
no test coverage detected