clboss/tests/ev/test_runcmd.cpp
2022-05-02 04:56:47 +00:00

47 lines
1.1 KiB
C++

#undef NDEBUG
#include"Ev/Io.hpp"
#include"Ev/start.hpp"
#include"Ev/runcmd.hpp"
#include<assert.h>
#include<stdexcept>
int main() {
auto flag = false;
auto action = Ev::lift().then([]() {
return Ev::runcmd("cat", {});
}).then([](std::string cat) {
/* Should be an empty string, since input is /dev/null. */
assert(cat == "");
/* Test on large output. */
return Ev::runcmd("sh", {"-c", "ls -la *"});
}).then([&flag](std::string result) {
/* Should be nonempty. */
assert(result.size() != 0);
/* Test on non-existent command. */
return Ev::runcmd("test-on-nonexistent-command", {})
.catching<std::runtime_error>([&flag](std::runtime_error const& e) {
/* Ignore error, set flag. */
flag = true;
return Ev::lift(std::string(""));
});
}).then([&flag](std::string result) {
assert(flag);
assert(result == "");
/* Test on capturing stderr. */
return Ev::runcmd( "sh", {"-c", "echo 'foo' >&2"}
, true
);
}).then([](std::string result) {
assert(result == "foo\n");
return Ev::lift();
}).then([]() {
return Ev::lift(0);
});
return Ev::start(action);
}