New test case for cat and env

This commit is contained in:
arunmu 2016-03-17 15:23:11 +05:30
parent 943c6ee0de
commit fd9efecf05
6 changed files with 50 additions and 11 deletions

View File

@ -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_;
}

5
test/env_script.sh Executable file
View 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

Binary file not shown.

31
test/test_cat.cc Normal file → Executable file
View File

@ -1,15 +1,30 @@
#include <iostream>
#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;
}

BIN
test/test_env Executable file

Binary file not shown.

19
test/test_env.cc Normal file
View 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;
}