testUVProcessChain.cxx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include "cmUVProcessChain.h"
  2. #include "cmGetPipes.h"
  3. #include "cmUVHandlePtr.h"
  4. #include "cmUVStreambuf.h"
  5. #include "cm_uv.h"
  6. #include <algorithm>
  7. #include <functional>
  8. #include <iostream>
  9. #include <memory>
  10. #include <sstream>
  11. #include <string>
  12. #include <vector>
  13. #include <csignal>
  14. #include "cm_memory.hxx"
  15. struct ExpectedStatus
  16. {
  17. bool Finished;
  18. bool MatchExitStatus;
  19. bool MatchTermSignal;
  20. cmUVProcessChain::Status Status;
  21. };
  22. static const std::vector<ExpectedStatus> status1 = {
  23. { false, false, false, { 0, 0 } },
  24. { false, false, false, { 0, 0 } },
  25. { false, false, false, { 0, 0 } },
  26. };
  27. static const std::vector<ExpectedStatus> status2 = {
  28. { true, true, true, { 0, 0 } },
  29. { false, false, false, { 0, 0 } },
  30. { false, false, false, { 0, 0 } },
  31. };
  32. static const std::vector<ExpectedStatus> status3 = {
  33. { true, true, true, { 0, 0 } },
  34. { true, true, true, { 1, 0 } },
  35. #ifdef _WIN32
  36. { true, true, true, { 2, 0 } },
  37. #else
  38. { true, false, true, { 0, SIGABRT } },
  39. #endif
  40. };
  41. bool operator==(const cmUVProcessChain::Status* actual,
  42. const ExpectedStatus& expected)
  43. {
  44. if (!expected.Finished) {
  45. return !actual;
  46. } else if (!actual) {
  47. return false;
  48. }
  49. if (expected.MatchExitStatus &&
  50. expected.Status.ExitStatus != actual->ExitStatus) {
  51. return false;
  52. }
  53. if (expected.MatchTermSignal &&
  54. expected.Status.TermSignal != actual->TermSignal) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. bool resultsMatch(const std::vector<const cmUVProcessChain::Status*>& actual,
  60. const std::vector<ExpectedStatus>& expected)
  61. {
  62. return actual.size() == expected.size() &&
  63. std::equal(actual.begin(), actual.end(), expected.begin());
  64. }
  65. std::string getInput(std::istream& input)
  66. {
  67. char buffer[1024];
  68. std::ostringstream str;
  69. do {
  70. input.read(buffer, 1024);
  71. str.write(buffer, input.gcount());
  72. } while (input.gcount() > 0);
  73. return str.str();
  74. }
  75. template <typename T>
  76. std::function<std::ostream&(std::ostream&)> printExpected(bool match,
  77. const T& value)
  78. {
  79. return [match, value](std::ostream& stream) -> std::ostream& {
  80. if (match) {
  81. stream << value;
  82. } else {
  83. stream << "*";
  84. }
  85. return stream;
  86. };
  87. }
  88. std::ostream& operator<<(
  89. std::ostream& stream,
  90. const std::function<std::ostream&(std::ostream&)>& func)
  91. {
  92. return func(stream);
  93. }
  94. void printResults(const std::vector<const cmUVProcessChain::Status*>& actual,
  95. const std::vector<ExpectedStatus>& expected)
  96. {
  97. std::cout << "Expected: " << std::endl;
  98. for (auto const& e : expected) {
  99. if (e.Finished) {
  100. std::cout << " ExitStatus: "
  101. << printExpected(e.MatchExitStatus, e.Status.ExitStatus)
  102. << ", TermSignal: "
  103. << printExpected(e.MatchTermSignal, e.Status.TermSignal)
  104. << std::endl;
  105. } else {
  106. std::cout << " null" << std::endl;
  107. }
  108. }
  109. std::cout << "Actual:" << std::endl;
  110. for (auto const& a : actual) {
  111. if (a) {
  112. std::cout << " ExitStatus: " << a->ExitStatus
  113. << ", TermSignal: " << a->TermSignal << std::endl;
  114. } else {
  115. std::cout << " null" << std::endl;
  116. }
  117. }
  118. }
  119. bool checkExecution(cmUVProcessChainBuilder& builder,
  120. std::unique_ptr<cmUVProcessChain>& chain)
  121. {
  122. std::vector<const cmUVProcessChain::Status*> status;
  123. chain = cm::make_unique<cmUVProcessChain>(builder.Start());
  124. if (!chain->Valid()) {
  125. std::cout << "Valid() returned false, should be true" << std::endl;
  126. return false;
  127. }
  128. status = chain->GetStatus();
  129. if (!resultsMatch(status, status1)) {
  130. std::cout << "GetStatus() did not produce expected output" << std::endl;
  131. printResults(status, status1);
  132. return false;
  133. }
  134. if (chain->Wait(6000)) {
  135. std::cout << "Wait() returned true, should be false" << std::endl;
  136. return false;
  137. }
  138. status = chain->GetStatus();
  139. if (!resultsMatch(status, status2)) {
  140. std::cout << "GetStatus() did not produce expected output" << std::endl;
  141. printResults(status, status2);
  142. return false;
  143. }
  144. if (!chain->Wait()) {
  145. std::cout << "Wait() returned false, should be true" << std::endl;
  146. return false;
  147. }
  148. status = chain->GetStatus();
  149. if (!resultsMatch(status, status3)) {
  150. std::cout << "GetStatus() did not produce expected output" << std::endl;
  151. printResults(status, status3);
  152. return false;
  153. }
  154. return true;
  155. }
  156. bool checkOutput(std::istream& outputStream, std::istream& errorStream)
  157. {
  158. std::string output = getInput(outputStream);
  159. if (output != "HELO WRD!") {
  160. std::cout << "Output was \"" << output << "\", expected \"HELO WRD!\""
  161. << std::endl;
  162. return false;
  163. }
  164. std::string error = getInput(errorStream);
  165. if (error.length() != 3 || error.find('1') == std::string::npos ||
  166. error.find('2') == std::string::npos ||
  167. error.find('3') == std::string::npos) {
  168. std::cout << "Error was \"" << error << "\", expected \"123\""
  169. << std::endl;
  170. return false;
  171. }
  172. return true;
  173. }
  174. bool testUVProcessChainBuiltin(const char* helperCommand)
  175. {
  176. cmUVProcessChainBuilder builder;
  177. std::unique_ptr<cmUVProcessChain> chain;
  178. builder.AddCommand({ helperCommand, "echo" })
  179. .AddCommand({ helperCommand, "capitalize" })
  180. .AddCommand({ helperCommand, "dedup" })
  181. .SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
  182. .SetBuiltinStream(cmUVProcessChainBuilder::Stream_ERROR);
  183. if (!checkExecution(builder, chain)) {
  184. return false;
  185. }
  186. if (!chain->OutputStream()) {
  187. std::cout << "OutputStream() was null, expecting not null" << std::endl;
  188. return false;
  189. }
  190. if (!chain->ErrorStream()) {
  191. std::cout << "ErrorStream() was null, expecting not null" << std::endl;
  192. return false;
  193. }
  194. if (!checkOutput(*chain->OutputStream(), *chain->ErrorStream())) {
  195. return false;
  196. }
  197. return true;
  198. }
  199. bool testUVProcessChainExternal(const char* helperCommand)
  200. {
  201. cmUVProcessChainBuilder builder;
  202. std::unique_ptr<cmUVProcessChain> chain;
  203. int outputPipe[2], errorPipe[2];
  204. cm::uv_pipe_ptr outputInPipe, outputOutPipe, errorInPipe, errorOutPipe;
  205. if (cmGetPipes(outputPipe) < 0) {
  206. std::cout << "Error creating pipes" << std::endl;
  207. return false;
  208. }
  209. if (cmGetPipes(errorPipe) < 0) {
  210. std::cout << "Error creating pipes" << std::endl;
  211. return false;
  212. }
  213. builder.AddCommand({ helperCommand, "echo" })
  214. .AddCommand({ helperCommand, "capitalize" })
  215. .AddCommand({ helperCommand, "dedup" })
  216. .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, outputPipe[1])
  217. .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, errorPipe[1]);
  218. if (!checkExecution(builder, chain)) {
  219. return false;
  220. }
  221. if (chain->OutputStream()) {
  222. std::cout << "OutputStream() was not null, expecting null" << std::endl;
  223. return false;
  224. }
  225. if (chain->ErrorStream()) {
  226. std::cout << "ErrorStream() was not null, expecting null" << std::endl;
  227. return false;
  228. }
  229. outputOutPipe.init(chain->GetLoop(), 0);
  230. uv_pipe_open(outputOutPipe, outputPipe[1]);
  231. outputOutPipe.reset();
  232. errorOutPipe.init(chain->GetLoop(), 0);
  233. uv_pipe_open(errorOutPipe, errorPipe[1]);
  234. errorOutPipe.reset();
  235. outputInPipe.init(chain->GetLoop(), 0);
  236. uv_pipe_open(outputInPipe, outputPipe[0]);
  237. cmUVStreambuf outputBuf;
  238. outputBuf.open(outputInPipe);
  239. std::istream outputStream(&outputBuf);
  240. errorInPipe.init(chain->GetLoop(), 0);
  241. uv_pipe_open(errorInPipe, errorPipe[0]);
  242. cmUVStreambuf errorBuf;
  243. errorBuf.open(errorInPipe);
  244. std::istream errorStream(&errorBuf);
  245. if (!checkOutput(outputStream, errorStream)) {
  246. return false;
  247. }
  248. return true;
  249. }
  250. bool testUVProcessChainNone(const char* helperCommand)
  251. {
  252. cmUVProcessChainBuilder builder;
  253. std::unique_ptr<cmUVProcessChain> chain;
  254. builder.AddCommand({ helperCommand, "echo" })
  255. .AddCommand({ helperCommand, "capitalize" })
  256. .AddCommand({ helperCommand, "dedup" });
  257. if (!checkExecution(builder, chain)) {
  258. return false;
  259. }
  260. if (chain->OutputStream()) {
  261. std::cout << "OutputStream() was not null, expecting null" << std::endl;
  262. return false;
  263. }
  264. if (chain->ErrorStream()) {
  265. std::cout << "ErrorStream() was not null, expecting null" << std::endl;
  266. return false;
  267. }
  268. return true;
  269. }
  270. int testUVProcessChain(int argc, char** const argv)
  271. {
  272. if (argc < 2) {
  273. std::cout << "Invalid arguments.\n";
  274. return -1;
  275. }
  276. if (!testUVProcessChainBuiltin(argv[1])) {
  277. std::cout << "While executing testUVProcessChainBuiltin().\n";
  278. return -1;
  279. }
  280. if (!testUVProcessChainExternal(argv[1])) {
  281. std::cout << "While executing testUVProcessChainExternal().\n";
  282. return -1;
  283. }
  284. if (!testUVProcessChainNone(argv[1])) {
  285. std::cout << "While executing testUVProcessChainNone().\n";
  286. return -1;
  287. }
  288. return 0;
  289. }