diff --git a/subprocess.hpp b/subprocess.hpp index 948002c..5861a2f 100755 --- a/subprocess.hpp +++ b/subprocess.hpp @@ -746,17 +746,17 @@ namespace detail { } void ArgumentDeducer::set_option(input&& inp) { - popen_->stream_.read_from_parent_ = inp.rd_ch_; + if (inp.rd_ch_ != -1) popen_->stream_.read_from_parent_ = inp.rd_ch_; if (inp.wr_ch_ != -1) popen_->stream_.write_to_child_ = inp.wr_ch_; } void ArgumentDeducer::set_option(output&& out) { - popen_->stream_.write_to_parent_ = out.wr_ch_; + if (out.wr_ch_ != -1) popen_->stream_.write_to_parent_ = out.wr_ch_; if (out.rd_ch_ != -1) popen_->stream_.read_from_child_ = out.rd_ch_; } void ArgumentDeducer::set_option(error&& err) { - popen_->stream_.err_write_ = err.wr_ch_; + if (err.wr_ch_ != -1) popen_->stream_.err_write_ = err.wr_ch_; if (err.rd_ch_ != -1) popen_->stream_.err_read_ = err.rd_ch_; } diff --git a/test/env_script.sh b/test/env_script.sh new file mode 100755 index 0000000..809f56b --- /dev/null +++ b/test/env_script.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +echo "ENV1 = ${NEW_ENV1}" +echo "ENV2 = ${NEW_ENV2}" +echo "ENV3 = ${NEW_ENV3}" diff --git a/test/test_cat b/test/test_cat new file mode 100755 index 0000000..c7df47f Binary files /dev/null and b/test/test_cat differ diff --git a/test/test_cat.cc b/test/test_cat.cc old mode 100644 new mode 100755 index de8778f..3905b52 --- a/test/test_cat.cc +++ b/test/test_cat.cc @@ -1,15 +1,30 @@ #include #include "../subprocess.hpp" -using namespace subprocess; +namespace sp = subprocess; + +void test_cat_pipe_redirection() +{ + std::cout << "Test::test_cat_pipe_redirection" << std::endl; + auto p = sp::Popen({"cat", "-"}, sp::input{sp::PIPE}, sp::output{sp::PIPE}); + const char* msg = "through stdin to stdout"; + auto res_buf = p.communicate(msg, strlen(msg)).first; + assert(res_buf.length == strlen(msg)); + std::cout << "END_TEST" << std::endl; +} + +void test_cat_file_redirection() +{ + std::cout << "Test::test_cat_file_redirection" << std::endl; + auto p = sp::Popen({"cat", "-"}, sp::input{sp::PIPE}, sp::output{"cat_fredirect.txt"}); + const char* msg = "through stdin to stdout"; + int wr_bytes = p.send(msg, strlen(msg)); + assert (wr_bytes == strlen(msg)); + std::cout << "END_TEST" << std::endl; +} int main() { - auto p = Popen({"cat", "-"}, - input{PIPE}, - output{PIPE}); - const char* msg = "through stdin to stdout"; - auto res = p.communicate(msg, strlen(msg)).first; - std::cout << res.buf.data() << std::endl; - + test_cat_pipe_redirection(); + test_cat_file_redirection(); return 0; } diff --git a/test/test_env b/test/test_env new file mode 100755 index 0000000..7922692 Binary files /dev/null and b/test/test_env differ diff --git a/test/test_env.cc b/test/test_env.cc new file mode 100644 index 0000000..51f6029 --- /dev/null +++ b/test/test_env.cc @@ -0,0 +1,19 @@ +#include +#include "../subprocess.hpp" + +using namespace subprocess; + +void test_env() +{ + int st= Popen("./env_script.sh", environment{{ + {"NEW_ENV1", "VALUE-1"}, + {"NEW_ENV2", "VALUE-2"}, + {"NEW_ENV3", "VALUE-3"} + }}).wait(); + assert (st == 0); +} + +int main() { + test_env(); + return 0; +}