2
0

Process.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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::kWrite));
  15. ANKI_TEST_EXPECT_NO_ERR(file.writeTextf("#!/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::kNotRunning);
  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::kNotRunning);
  38. ANKI_TEST_EXPECT_EQ(exitCode, 6);
  39. HeapMemoryPool pool(allocAligned, nullptr);
  40. StringRaii stdOut(&pool);
  41. ANKI_TEST_EXPECT_NO_ERR(proc.readFromStdout(stdOut));
  42. ANKI_TEST_EXPECT_EQ(stdOut, "Hello from script\n");
  43. }
  44. // Stderr and stdOut
  45. if(1)
  46. {
  47. createBashScript(R"(
  48. x=1
  49. while [ $x -le 10 ]
  50. do
  51. echo "Stdout message no $x"
  52. echo "Stderr message no $x" 1>&2
  53. sleep 1
  54. x=$(( $x + 1 ))
  55. done
  56. )");
  57. Process proc;
  58. ANKI_TEST_EXPECT_NO_ERR(proc.start("bash", Array<CString, 1>{{"process_test.sh"}}, {}));
  59. ProcessStatus status;
  60. ANKI_TEST_EXPECT_NO_ERR(proc.getStatus(status));
  61. ANKI_TEST_EXPECT_EQ(status, ProcessStatus::kRunning);
  62. while(true)
  63. {
  64. ANKI_TEST_EXPECT_NO_ERR(proc.getStatus(status));
  65. if(status == ProcessStatus::kNotRunning)
  66. {
  67. break;
  68. }
  69. HeapMemoryPool pool(allocAligned, nullptr);
  70. StringRaii stdOut(&pool);
  71. ANKI_TEST_EXPECT_NO_ERR(proc.readFromStdout(stdOut));
  72. if(stdOut.getLength())
  73. {
  74. ANKI_TEST_LOGI("%s", stdOut.cstr());
  75. }
  76. StringRaii stderrStr(&pool);
  77. ANKI_TEST_EXPECT_NO_ERR(proc.readFromStderr(stderrStr));
  78. if(stderrStr.getLength())
  79. {
  80. ANKI_TEST_LOGI("%s", stderrStr.cstr());
  81. }
  82. }
  83. ANKI_TEST_EXPECT_NO_ERR(proc.wait(0.0, &status));
  84. }
  85. // Read after complete wait & env
  86. if(1)
  87. {
  88. createBashScript(R"(
  89. echo $ENV_VAR
  90. sleep 1
  91. )");
  92. Process proc;
  93. ANKI_TEST_EXPECT_NO_ERR(
  94. proc.start("bash", Array<CString, 1>{{"process_test.sh"}}, Array<CString, 1>{{"ENV_VAR=Lala"}}));
  95. ANKI_TEST_EXPECT_NO_ERR(proc.wait());
  96. HighRezTimer::sleep(0.5_sec); // Wait a bit more for good measure
  97. HeapMemoryPool pool(allocAligned, nullptr);
  98. StringRaii stdOut(&pool);
  99. ANKI_TEST_EXPECT_NO_ERR(proc.readFromStdout(stdOut));
  100. ANKI_TEST_EXPECT_EQ(stdOut, "Lala\n");
  101. }
  102. ANKI_TEST_EXPECT_NO_ERR(removeFile("process_test.sh"));
  103. }