testUVProcessChain.cxx 8.6 KB

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