mirror of
https://github.com/arun11299/cpp-subprocess.git
synced 2025-08-05 12:56:23 -04:00
New test case for cat and env
This commit is contained in:
parent
943c6ee0de
commit
fd9efecf05
@ -746,17 +746,17 @@ namespace detail {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ArgumentDeducer::set_option(input&& inp) {
|
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_;
|
if (inp.wr_ch_ != -1) popen_->stream_.write_to_child_ = inp.wr_ch_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArgumentDeducer::set_option(output&& out) {
|
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_;
|
if (out.rd_ch_ != -1) popen_->stream_.read_from_child_ = out.rd_ch_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArgumentDeducer::set_option(error&& err) {
|
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_;
|
if (err.rd_ch_ != -1) popen_->stream_.err_read_ = err.rd_ch_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
test/env_script.sh
Executable file
5
test/env_script.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "ENV1 = ${NEW_ENV1}"
|
||||||
|
echo "ENV2 = ${NEW_ENV2}"
|
||||||
|
echo "ENV3 = ${NEW_ENV3}"
|
BIN
test/test_cat
Executable file
BIN
test/test_cat
Executable file
Binary file not shown.
31
test/test_cat.cc
Normal file → Executable file
31
test/test_cat.cc
Normal file → Executable file
@ -1,15 +1,30 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "../subprocess.hpp"
|
#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() {
|
int main() {
|
||||||
auto p = Popen({"cat", "-"},
|
test_cat_pipe_redirection();
|
||||||
input{PIPE},
|
test_cat_file_redirection();
|
||||||
output{PIPE});
|
|
||||||
const char* msg = "through stdin to stdout";
|
|
||||||
auto res = p.communicate(msg, strlen(msg)).first;
|
|
||||||
std::cout << res.buf.data() << std::endl;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
BIN
test/test_env
Executable file
BIN
test/test_env
Executable file
Binary file not shown.
19
test/test_env.cc
Normal file
19
test/test_env.cc
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user