test_os.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**************************************************************************/
  2. /* test_os.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_OS_H
  31. #define TEST_OS_H
  32. #include "core/os/os.h"
  33. #include "thirdparty/doctest/doctest.h"
  34. namespace TestOS {
  35. TEST_CASE("[OS] Environment variables") {
  36. #ifdef WINDOWS_ENABLED
  37. CHECK_MESSAGE(
  38. OS::get_singleton()->has_environment("USERPROFILE"),
  39. "The USERPROFILE environment variable should be present.");
  40. #else
  41. CHECK_MESSAGE(
  42. OS::get_singleton()->has_environment("HOME"),
  43. "The HOME environment variable should be present.");
  44. #endif
  45. OS::get_singleton()->set_environment("HELLO", "world");
  46. CHECK_MESSAGE(
  47. OS::get_singleton()->get_environment("HELLO") == "world",
  48. "The previously-set HELLO environment variable should return the expected value.");
  49. }
  50. TEST_CASE("[OS] Command line arguments") {
  51. List<String> arguments = OS::get_singleton()->get_cmdline_args();
  52. bool found = false;
  53. for (int i = 0; i < arguments.size(); i++) {
  54. if (arguments[i] == "--test") {
  55. found = true;
  56. break;
  57. }
  58. }
  59. CHECK_MESSAGE(
  60. found,
  61. "The `--test` option must be present in the list of command line arguments.");
  62. }
  63. TEST_CASE("[OS] Executable and data paths") {
  64. CHECK_MESSAGE(
  65. OS::get_singleton()->get_executable_path().is_absolute_path(),
  66. "The executable path returned should be an absolute path.");
  67. CHECK_MESSAGE(
  68. OS::get_singleton()->get_data_path().is_absolute_path(),
  69. "The user data path returned should be an absolute path.");
  70. CHECK_MESSAGE(
  71. OS::get_singleton()->get_config_path().is_absolute_path(),
  72. "The user configuration path returned should be an absolute path.");
  73. CHECK_MESSAGE(
  74. OS::get_singleton()->get_cache_path().is_absolute_path(),
  75. "The cache path returned should be an absolute path.");
  76. }
  77. TEST_CASE("[OS] Ticks") {
  78. CHECK_MESSAGE(
  79. OS::get_singleton()->get_ticks_usec() > 1000,
  80. "The returned ticks (in microseconds) must be greater than 1,000.");
  81. CHECK_MESSAGE(
  82. OS::get_singleton()->get_ticks_msec() > 1,
  83. "The returned ticks (in milliseconds) must be greater than 1.");
  84. }
  85. TEST_CASE("[OS] Feature tags") {
  86. CHECK_MESSAGE(
  87. OS::get_singleton()->has_feature("editor"),
  88. "The binary has the \"editor\" feature tag.");
  89. CHECK_MESSAGE(
  90. !OS::get_singleton()->has_feature("template"),
  91. "The binary does not have the \"template\" feature tag.");
  92. CHECK_MESSAGE(
  93. !OS::get_singleton()->has_feature("template_debug"),
  94. "The binary does not have the \"template_debug\" feature tag.");
  95. CHECK_MESSAGE(
  96. !OS::get_singleton()->has_feature("template_release"),
  97. "The binary does not have the \"template_release\" feature tag.");
  98. }
  99. TEST_CASE("[OS] Process ID") {
  100. CHECK_MESSAGE(
  101. OS::get_singleton()->get_process_id() >= 1,
  102. "The returned process ID should be greater than zero.");
  103. }
  104. TEST_CASE("[OS] Processor count and memory information") {
  105. CHECK_MESSAGE(
  106. OS::get_singleton()->get_processor_count() >= 1,
  107. "The returned processor count should be greater than zero.");
  108. CHECK_MESSAGE(
  109. OS::get_singleton()->get_static_memory_usage() >= 1,
  110. "The returned static memory usage should be greater than zero.");
  111. CHECK_MESSAGE(
  112. OS::get_singleton()->get_static_memory_peak_usage() >= 1,
  113. "The returned static memory peak usage should be greater than zero.");
  114. }
  115. TEST_CASE("[OS] Execute") {
  116. #ifdef WINDOWS_ENABLED
  117. List<String> arguments;
  118. arguments.push_back("/C");
  119. arguments.push_back("dir > NUL");
  120. int exit_code;
  121. const Error err = OS::get_singleton()->execute("cmd", arguments, nullptr, &exit_code);
  122. CHECK_MESSAGE(
  123. err == OK,
  124. "(Running the command `cmd /C \"dir > NUL\"` returns the expected Godot error code (OK).");
  125. CHECK_MESSAGE(
  126. exit_code == 0,
  127. "Running the command `cmd /C \"dir > NUL\"` returns a zero (successful) exit code.");
  128. #else
  129. List<String> arguments;
  130. arguments.push_back("-c");
  131. arguments.push_back("ls > /dev/null");
  132. int exit_code;
  133. const Error err = OS::get_singleton()->execute("sh", arguments, nullptr, &exit_code);
  134. CHECK_MESSAGE(
  135. err == OK,
  136. "(Running the command `sh -c \"ls > /dev/null\"` returns the expected Godot error code (OK).");
  137. CHECK_MESSAGE(
  138. exit_code == 0,
  139. "Running the command `sh -c \"ls > /dev/null\"` returns a zero (successful) exit code.");
  140. #endif
  141. }
  142. } // namespace TestOS
  143. #endif // TEST_OS_H