start_dir.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.hpp>
  13. #include <boost/system/error_code.hpp>
  14. #include <boost/filesystem.hpp>
  15. #include <boost/algorithm/string/compare.hpp>
  16. #include <string>
  17. #include <iostream>
  18. namespace bp = boost::process;
  19. struct test_dir
  20. {
  21. std::string s_;
  22. test_dir(const std::string &s) : s_(s)
  23. { BOOST_REQUIRE_NO_THROW(boost::filesystem::create_directory(s)); }
  24. ~test_dir() { boost::filesystem::remove(s_); }
  25. };
  26. BOOST_AUTO_TEST_CASE(start_in_dir)
  27. {
  28. using boost::unit_test::framework::master_test_suite;
  29. test_dir dir("start_in_dir_test");
  30. bp::ipstream is;
  31. std::error_code ec;
  32. bp::child c(
  33. bp::exe=boost::filesystem::absolute(master_test_suite().argv[1]).string(),
  34. bp::args +={"test", "--pwd"},
  35. bp::start_dir = dir.s_,
  36. bp::std_out>is,
  37. ec
  38. );
  39. BOOST_REQUIRE(!ec);
  40. std::string s;
  41. std::getline(is, s);
  42. auto path_read = boost::filesystem::absolute(boost::filesystem::path(s)).string();
  43. auto path_set = boost::filesystem::absolute(dir.s_).string();
  44. if (path_read.size() > path_set.size())
  45. path_read.resize(path_set.size());
  46. else if (path_read.size() < path_set.size())
  47. path_set.resize(path_read.size());
  48. BOOST_CHECK_EQUAL_COLLECTIONS(path_read.begin(), path_read.end(),
  49. path_set.begin(), path_set.end());
  50. BOOST_REQUIRE_NO_THROW(c.wait());
  51. }