cmd_test.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #define BOOST_TEST_MAIN
  10. #define BOOST_TEST_IGNORE_SIGCHLD
  11. #include <boost/test/included/unit_test.hpp>
  12. #include <boost/process/error.hpp>
  13. #include <boost/process/system.hpp>
  14. #include <boost/process/env.hpp>
  15. #include <boost/process/cmd.hpp>
  16. #include <boost/filesystem/path.hpp>
  17. #include <boost/filesystem/operations.hpp>
  18. #include <system_error>
  19. #include <boost/algorithm/string/case_conv.hpp>
  20. #include <boost/system/error_code.hpp>
  21. #include <cstdlib>
  22. namespace bp = boost::process;
  23. namespace fs = boost::filesystem;
  24. BOOST_AUTO_TEST_CASE(excplicit)
  25. {
  26. using boost::unit_test::framework::master_test_suite;
  27. std::error_code ec;
  28. fs::path pth = master_test_suite().argv[1];
  29. auto env = boost::this_process::environment();
  30. auto itr = std::find_if(env.begin(), env.end(),
  31. [](const bp::native_environment::entry_type & e){return boost::to_upper_copy(e.get_name()) == "PATH";});
  32. BOOST_REQUIRE(itr != env.end());
  33. (*itr) += fs::canonical(fs::absolute(pth.parent_path())).string();
  34. int ret = bp::system(
  35. bp::cmd="sparring_partner --exit-code 42",
  36. ec
  37. );
  38. BOOST_CHECK(!ec);
  39. if (ec)
  40. BOOST_TEST_MESSAGE(ec.message());
  41. BOOST_CHECK_EQUAL(ret, 42);
  42. }
  43. BOOST_AUTO_TEST_CASE(implicit)
  44. {
  45. using boost::unit_test::framework::master_test_suite;
  46. std::error_code ec;
  47. fs::path pth = master_test_suite().argv[1];
  48. auto env = boost::this_process::environment();
  49. auto itr = std::find_if(env.begin(), env.end(),
  50. [](const bp::native_environment::entry_type & e){return boost::to_upper_copy(e.get_name()) == "PATH";});
  51. BOOST_REQUIRE(itr != env.end());
  52. (*itr) += fs::canonical(fs::absolute(pth.parent_path())).string();
  53. int ret = bp::system(
  54. "sparring_partner --exit-code 21",
  55. ec
  56. );
  57. BOOST_CHECK(!ec);
  58. if (ec)
  59. BOOST_TEST_MESSAGE(ec.message());
  60. BOOST_CHECK_EQUAL(ret, 21);
  61. }