TestRunner.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  5. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <AK/Format.h>
  11. #include <AK/JsonObject.h>
  12. #include <AK/JsonValue.h>
  13. #include <AK/QuickSort.h>
  14. #include <AK/String.h>
  15. #include <AK/Vector.h>
  16. #include <LibCore/DirIterator.h>
  17. #include <LibTest/Results.h>
  18. #include <fcntl.h>
  19. #include <sys/time.h>
  20. namespace Test {
  21. class TestRunner {
  22. public:
  23. static TestRunner* the()
  24. {
  25. return s_the;
  26. }
  27. TestRunner(String test_root, bool print_times, bool print_progress, bool print_json)
  28. : m_test_root(move(test_root))
  29. , m_print_times(print_times)
  30. , m_print_progress(print_progress)
  31. , m_print_json(print_json)
  32. {
  33. VERIFY(!s_the);
  34. s_the = this;
  35. }
  36. virtual ~TestRunner() { s_the = nullptr; };
  37. virtual void run(String test_glob);
  38. const Test::Counts& counts() const { return m_counts; }
  39. bool is_printing_progress() const { return m_print_progress; }
  40. protected:
  41. static TestRunner* s_the;
  42. void print_test_results() const;
  43. void print_test_results_as_json() const;
  44. virtual Vector<String> get_test_paths() const = 0;
  45. virtual void do_run_single_test(const String&) = 0;
  46. String m_test_root;
  47. bool m_print_times;
  48. bool m_print_progress;
  49. bool m_print_json;
  50. double m_total_elapsed_time_in_ms { 0 };
  51. Test::Counts m_counts;
  52. };
  53. inline void cleanup()
  54. {
  55. // Clear the taskbar progress.
  56. if (TestRunner::the() && TestRunner::the()->is_printing_progress())
  57. warn("\033]9;-1;\033\\");
  58. }
  59. inline void cleanup_and_exit()
  60. {
  61. cleanup();
  62. exit(1);
  63. }
  64. inline double get_time_in_ms()
  65. {
  66. struct timeval tv1;
  67. auto return_code = gettimeofday(&tv1, nullptr);
  68. VERIFY(return_code >= 0);
  69. return static_cast<double>(tv1.tv_sec) * 1000.0 + static_cast<double>(tv1.tv_usec) / 1000.0;
  70. }
  71. template<typename Callback>
  72. inline void iterate_directory_recursively(const String& directory_path, Callback callback)
  73. {
  74. Core::DirIterator directory_iterator(directory_path, Core::DirIterator::Flags::SkipDots);
  75. while (directory_iterator.has_next()) {
  76. auto name = directory_iterator.next_path();
  77. struct stat st = {};
  78. if (fstatat(directory_iterator.fd(), name.characters(), &st, AT_SYMLINK_NOFOLLOW) < 0)
  79. continue;
  80. bool is_directory = S_ISDIR(st.st_mode);
  81. auto full_path = String::formatted("{}/{}", directory_path, name);
  82. if (is_directory && name != "/Fixtures"sv) {
  83. iterate_directory_recursively(full_path, callback);
  84. } else if (!is_directory) {
  85. callback(full_path);
  86. }
  87. }
  88. }
  89. inline void TestRunner::run(String test_glob)
  90. {
  91. size_t progress_counter = 0;
  92. auto test_paths = get_test_paths();
  93. for (auto& path : test_paths) {
  94. if (!path.matches(test_glob))
  95. continue;
  96. ++progress_counter;
  97. do_run_single_test(path);
  98. if (m_print_progress)
  99. warn("\033]9;{};{};\033\\", progress_counter, test_paths.size());
  100. }
  101. if (m_print_progress)
  102. warn("\033]9;-1;\033\\");
  103. if (!m_print_json)
  104. print_test_results();
  105. else
  106. print_test_results_as_json();
  107. }
  108. enum Modifier {
  109. BG_RED,
  110. BG_GREEN,
  111. FG_RED,
  112. FG_GREEN,
  113. FG_ORANGE,
  114. FG_GRAY,
  115. FG_BLACK,
  116. FG_BOLD,
  117. ITALIC,
  118. CLEAR,
  119. };
  120. inline void print_modifiers(Vector<Modifier> modifiers)
  121. {
  122. for (auto& modifier : modifiers) {
  123. auto code = [&] {
  124. switch (modifier) {
  125. case BG_RED:
  126. return "\033[48;2;255;0;102m";
  127. case BG_GREEN:
  128. return "\033[48;2;102;255;0m";
  129. case FG_RED:
  130. return "\033[38;2;255;0;102m";
  131. case FG_GREEN:
  132. return "\033[38;2;102;255;0m";
  133. case FG_ORANGE:
  134. return "\033[38;2;255;102;0m";
  135. case FG_GRAY:
  136. return "\033[38;2;135;139;148m";
  137. case FG_BLACK:
  138. return "\033[30m";
  139. case FG_BOLD:
  140. return "\033[1m";
  141. case ITALIC:
  142. return "\033[3m";
  143. case CLEAR:
  144. return "\033[0m";
  145. }
  146. VERIFY_NOT_REACHED();
  147. }();
  148. out("{}", code);
  149. }
  150. }
  151. inline void TestRunner::print_test_results() const
  152. {
  153. out("\nTest Suites: ");
  154. if (m_counts.suites_failed) {
  155. print_modifiers({ FG_RED });
  156. out("{} failed, ", m_counts.suites_failed);
  157. print_modifiers({ CLEAR });
  158. }
  159. if (m_counts.suites_passed) {
  160. print_modifiers({ FG_GREEN });
  161. out("{} passed, ", m_counts.suites_passed);
  162. print_modifiers({ CLEAR });
  163. }
  164. outln("{} total", m_counts.suites_failed + m_counts.suites_passed);
  165. out("Tests: ");
  166. if (m_counts.tests_failed) {
  167. print_modifiers({ FG_RED });
  168. out("{} failed, ", m_counts.tests_failed);
  169. print_modifiers({ CLEAR });
  170. }
  171. if (m_counts.tests_skipped) {
  172. print_modifiers({ FG_ORANGE });
  173. out("{} skipped, ", m_counts.tests_skipped);
  174. print_modifiers({ CLEAR });
  175. }
  176. if (m_counts.tests_passed) {
  177. print_modifiers({ FG_GREEN });
  178. out("{} passed, ", m_counts.tests_passed);
  179. print_modifiers({ CLEAR });
  180. }
  181. outln("{} total", m_counts.tests_failed + m_counts.tests_skipped + m_counts.tests_passed);
  182. outln("Files: {} total", m_counts.files_total);
  183. out("Time: ");
  184. if (m_total_elapsed_time_in_ms < 1000.0) {
  185. outln("{}ms", static_cast<int>(m_total_elapsed_time_in_ms));
  186. } else {
  187. outln("{:>.3}s", m_total_elapsed_time_in_ms / 1000.0);
  188. }
  189. outln();
  190. }
  191. inline void TestRunner::print_test_results_as_json() const
  192. {
  193. JsonObject suites;
  194. suites.set("failed", m_counts.suites_failed);
  195. suites.set("passed", m_counts.suites_passed);
  196. suites.set("total", m_counts.suites_failed + m_counts.suites_passed);
  197. JsonObject tests;
  198. tests.set("failed", m_counts.tests_failed);
  199. tests.set("passed", m_counts.tests_passed);
  200. tests.set("skipped", m_counts.tests_skipped);
  201. tests.set("total", m_counts.tests_failed + m_counts.tests_passed + m_counts.tests_skipped);
  202. JsonObject results;
  203. results.set("suites", suites);
  204. results.set("tests", tests);
  205. JsonObject root;
  206. root.set("results", results);
  207. root.set("files_total", m_counts.files_total);
  208. root.set("duration", m_total_elapsed_time_in_ms / 1000.0);
  209. outln("{}", root.to_string());
  210. }
  211. }