コード例 #1
0
void test_input() 
{ 
    std::vector<std::string> args; 
    args.push_back("helpers"); 
    args.push_back("stdin-to-stdout"); 

    bp::posix_context ctx; 
    ctx.input_behavior.insert(bp::behavior_map::value_type(STDIN_FILENO, bp::capture_stream())); 
    ctx.output_behavior.insert(bp::behavior_map::value_type(STDOUT_FILENO, bp::capture_stream())); 
    ctx.environment = bp::self::get_environment(); 

    bp::posix_child c = bp::posix_launch(get_helpers_path(), args, ctx); 

    bp::postream &os = c.get_input(STDIN_FILENO); 
    bp::pistream &is = c.get_output(STDOUT_FILENO); 

    os << "message-to-process" << std::endl; 
    os.close(); 

    std::string word; 
    is >> word; 
    BOOST_CHECK_EQUAL(word, "message-to-process"); 

    const bp::status s = c.wait(); 
    BOOST_REQUIRE(s.exited()); 
    BOOST_CHECK_EQUAL(s.exit_status(), EXIT_SUCCESS); 
} 
コード例 #2
0
void check_redirect(int desc1, int desc2, const std::string &msg) 
{ 
    std::vector<std::string> args; 
    args.push_back("helpers"); 
    args.push_back("posix-echo-two"); 
    args.push_back(boost::str(boost::format("%1%") % desc1)); 
    args.push_back(boost::str(boost::format("%1%") % desc2)); 
    args.push_back(msg); 

    bp::posix_context ctx; 
    ctx.output_behavior.insert(bp::behavior_map::value_type(desc1, bp::capture_stream())); 
    ctx.output_behavior.insert(bp::behavior_map::value_type(desc2, bp::posix_redirect_stream(desc1))); 
    ctx.environment = bp::self::get_environment(); 

    bp::posix_child c = posix_launch(get_helpers_path(), args, ctx); 

    bp::pistream &is = c.get_output(desc1); 
    int dtmp; 
    std::string word; 
    is >> dtmp; 
    BOOST_CHECK_EQUAL(dtmp, desc1); 
    is >> word; 
    BOOST_CHECK_EQUAL(word, msg); 
    is >> dtmp; 
    BOOST_CHECK_EQUAL(dtmp, desc2); 
    is >> word; 
    BOOST_CHECK_EQUAL(word, msg); 

    const bp::status s = c.wait(); 
    BOOST_REQUIRE(s.exited()); 
    BOOST_CHECK_EQUAL(s.exit_status(), EXIT_SUCCESS); 
} 
コード例 #3
0
static void test_shell_execution() 
{ 
    std::string command; 
    if (bp_api_type == posix_api) 
        command = "echo test | sed 's,^,LINE-,'"; 
    else if (bp_api_type == win32_api) 
        command = "if foo==foo echo LINE-test"; 
    else 
        BOOST_REQUIRE(false); 

    bp::context ctx; 
    ctx.stdout_behavior = bp::capture_stream(); 
    // Julio: Without the following line, bash returns an exit status of 4, 
    // which makes the test fail...  Why?  I don't know. 
    ctx.stderr_behavior = bp::redirect_stream_to_stdout(); 
    bp::child c = bp::launch_shell(command, ctx); 

    bp::pistream &is = c.get_stdout(); 
    std::string word; 
    is >> word; 
    BOOST_CHECK_EQUAL(word, "LINE-test"); 

    const bp::status s = c.wait(); 
    BOOST_REQUIRE(s.exited()); 
    BOOST_CHECK_EQUAL(s.exit_status(), EXIT_SUCCESS); 
} 
コード例 #4
0
static std::string get_argument(const std::string &word) 
{ 
    std::vector<std::string> args; 
    args.push_back("helpers"); 
    args.push_back("echo-quoted"); 
    args.push_back(word); 

    bp::context ctx; 
    ctx.stdout_behavior = bp::capture_stream(); 
    ctx.environment = bp::self::get_environment(); 

    bp::child c = bp::launch(get_helpers_path(), args, ctx); 
    bp::pistream &is = c.get_stdout(); 

    std::string result; 
    portable_getline(is, result); 

    const bp::status s = c.wait(); 
    BOOST_REQUIRE(s.exited()); 
    BOOST_CHECK_EQUAL(s.exit_status(), EXIT_SUCCESS); 

    return result; 
}