Process.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2009-2021, 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. ANKI_TEST(Util, Process)
  10. {
  11. // Simple test
  12. if(1)
  13. {
  14. Process proc;
  15. ANKI_TEST_EXPECT_NO_ERR(proc.start("ls", Array<CString, 1>{{"-lt"}}, {}));
  16. ANKI_TEST_EXPECT_NO_ERR(proc.wait());
  17. HighRezTimer::sleep(1.0);
  18. HeapAllocator<U8> alloc(allocAligned, nullptr);
  19. StringAuto stdOut(alloc);
  20. ANKI_TEST_EXPECT_NO_ERR(proc.readFromStdout(stdOut));
  21. ANKI_TEST_LOGI("%s", stdOut.cstr());
  22. }
  23. // Stderr and stdOut
  24. if(1)
  25. {
  26. File file;
  27. ANKI_TEST_EXPECT_NO_ERR(file.open("process_test.sh", FileOpenFlag::WRITE));
  28. ANKI_TEST_EXPECT_NO_ERR(file.writeText(R"(#!/bin/bash
  29. x=1
  30. while [ $x -le 10 ]
  31. do
  32. echo "Stdout message no $x"
  33. echo "Stderr message no $x" 1>&2
  34. sleep 1
  35. x=$(( $x + 1 ))
  36. done
  37. )"));
  38. file.close();
  39. Process proc;
  40. ANKI_TEST_EXPECT_NO_ERR(proc.start("bash", Array<CString, 1>{{"process_test.sh"}}, {}));
  41. ProcessStatus status;
  42. while(true)
  43. {
  44. ANKI_TEST_EXPECT_NO_ERR(proc.getStatus(status));
  45. if(status == ProcessStatus::NORMAL_EXIT)
  46. {
  47. break;
  48. }
  49. HeapAllocator<U8> alloc(allocAligned, nullptr);
  50. StringAuto stdOut(alloc);
  51. ANKI_TEST_EXPECT_NO_ERR(proc.readFromStdout(stdOut));
  52. if(stdOut.getLength())
  53. {
  54. ANKI_TEST_LOGI("%s", stdOut.cstr());
  55. }
  56. StringAuto stderrStr(alloc);
  57. ANKI_TEST_EXPECT_NO_ERR(proc.readFromStderr(stderrStr));
  58. if(stderrStr.getLength())
  59. {
  60. ANKI_TEST_LOGI("%s", stderrStr.cstr());
  61. }
  62. }
  63. ANKI_TEST_EXPECT_NO_ERR(proc.wait(0.0, &status));
  64. }
  65. }