cpp-subprocess/test/test_ret_code.cc
xoviat 64f4dfc2e5
remove changes to quote_argument and bring back ci for all platforms (#91)
* revert channges to quote argument

* ci: add cmake draft

* ci: enable suprocess tests

* cleanup some warnings

* ci: disable nonworking windows tests

* ci: set timeout

* ci: disable nonworking tests on windows

* ci: remove travis

---------

Co-authored-by: xoviat <xoviat@users.noreply.github.com>
2023-08-28 09:54:20 +05:30

54 lines
1.2 KiB
C++

#include <iostream>
#include <subprocess.hpp>
namespace sp = subprocess;
void test_ret_code()
{
std::cout << "Test::test_poll_ret_code" << std::endl;
auto p = sp::Popen({"/usr/bin/false"});
while (p.poll() == -1) {
#ifndef _MSC_VER
usleep(1000 * 100);
#endif
}
assert (p.retcode() == 1);
}
void test_ret_code_comm()
{
using namespace sp;
auto cat = Popen({"cat", "../subprocess.hpp"}, output{PIPE});
auto grep = Popen({"grep", "template"}, input{cat.output()}, output{PIPE});
auto cut = Popen({"cut", "-d,", "-f", "1"}, input{grep.output()}, output{PIPE});
auto res = cut.communicate().first;
std::cout << res.buf.data() << std::endl;
std::cout << "retcode: " << cut.retcode() << std::endl;
}
void test_ret_code_check_output()
{
using namespace sp;
bool caught = false;
try {
auto obuf = check_output({"/bin/false"}, shell{false});
assert(false); // Expected to throw
} catch (CalledProcessError &e) {
std::cout << "retcode: " << e.retcode << std::endl;
assert (e.retcode == 1);
caught = true;
}
assert(caught);
}
int main() {
// test_ret_code();
#ifndef __USING_WINDOWS__
test_ret_code_comm();
test_ret_code_check_output();
#endif
return 0;
}