Process.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Tests/Framework/Framework.h>
  6. #include <AnKi/Util/Process.h>
  7. #include <AnKi/Util/File.h>
  8. #include <AnKi/Util/HighRezTimer.h>
  9. #include <AnKi/Util/Filesystem.h>
  10. namespace anki {
  11. static void createBashScript(CString code)
  12. {
  13. File file;
  14. ANKI_TEST_EXPECT_NO_ERR(file.open("process_test.sh", FileOpenFlag::WRITE));
  15. ANKI_TEST_EXPECT_NO_ERR(file.writeText("#!/bin/bash\n%s\n", code.cstr()));
  16. }
  17. } // end namespace anki
  18. ANKI_TEST(Util, Process)
  19. {
  20. // Simple test
  21. if(1)
  22. {
  23. createBashScript(R"(
  24. echo Hello from script
  25. exit 6
  26. )");
  27. Process proc;
  28. ANKI_TEST_EXPECT_NO_ERR(proc.start("bash", Array<CString, 1>{{"process_test.sh"}}, {}));
  29. ProcessStatus status;
  30. I32 exitCode;
  31. ANKI_TEST_EXPECT_NO_ERR(proc.wait(-1.0, &status, &exitCode));
  32. ANKI_TEST_EXPECT_EQ(status, ProcessStatus::NOT_RUNNING);
  33. ANKI_TEST_EXPECT_EQ(exitCode, 6);
  34. // Get stuff again, don't wait this time
  35. exitCode = 0;
  36. ANKI_TEST_EXPECT_NO_ERR(proc.wait(0.0, &status, &exitCode));
  37. ANKI_TEST_EXPECT_EQ(status, ProcessStatus::NOT_RUNNING);
  38. ANKI_TEST_EXPECT_EQ(exitCode, 6);
  39. StringAuto stdOut(HeapAllocator<U8>(allocAligned, nullptr));
  40. ANKI_TEST_EXPECT_NO_ERR(proc.readFromStdout(stdOut));
  41. ANKI_TEST_EXPECT_EQ(stdOut, "Hello from script\n");
  42. }
  43. // Stderr and stdOut
  44. if(1)
  45. {
  46. createBashScript(R"(
  47. x=1
  48. while [ $x -le 10 ]
  49. do
  50. echo "Stdout message no $x"
  51. echo "Stderr message no $x" 1>&2
  52. sleep 1
  53. x=$(( $x + 1 ))
  54. done
  55. )");
  56. Process proc;
  57. ANKI_TEST_EXPECT_NO_ERR(proc.start("bash", Array<CString, 1>{{"process_test.sh"}}, {}));
  58. ProcessStatus status;
  59. ANKI_TEST_EXPECT_NO_ERR(proc.getStatus(status));
  60. ANKI_TEST_EXPECT_EQ(status, ProcessStatus::RUNNING);
  61. while(true)
  62. {
  63. ANKI_TEST_EXPECT_NO_ERR(proc.getStatus(status));
  64. if(status == ProcessStatus::NOT_RUNNING)
  65. {
  66. break;
  67. }
  68. HeapAllocator<U8> alloc(allocAligned, nullptr);
  69. StringAuto stdOut(alloc);
  70. ANKI_TEST_EXPECT_NO_ERR(proc.readFromStdout(stdOut));
  71. if(stdOut.getLength())
  72. {
  73. ANKI_TEST_LOGI("%s", stdOut.cstr());
  74. }
  75. StringAuto stderrStr(alloc);
  76. ANKI_TEST_EXPECT_NO_ERR(proc.readFromStderr(stderrStr));
  77. if(stderrStr.getLength())
  78. {
  79. ANKI_TEST_LOGI("%s", stderrStr.cstr());
  80. }
  81. }
  82. ANKI_TEST_EXPECT_NO_ERR(proc.wait(0.0, &status));
  83. }
  84. // Read after complete wait & env
  85. if(1)
  86. {
  87. createBashScript(R"(
  88. echo $ENV_VAR
  89. sleep 1
  90. )");
  91. Process proc;
  92. ANKI_TEST_EXPECT_NO_ERR(
  93. proc.start("bash", Array<CString, 1>{{"process_test.sh"}}, Array<CString, 1>{{"ENV_VAR=Lala"}}));
  94. ANKI_TEST_EXPECT_NO_ERR(proc.wait());
  95. HighRezTimer::sleep(0.5_sec); // Wait a bit more for good measure
  96. StringAuto stdOut(HeapAllocator<U8>(allocAligned, nullptr));
  97. ANKI_TEST_EXPECT_NO_ERR(proc.readFromStdout(stdOut));
  98. ANKI_TEST_EXPECT_EQ(stdOut, "Lala\n");
  99. }
  100. ANKI_TEST_EXPECT_NO_ERR(removeFile("process_test.sh"));
  101. }