JavaScriptTestRunner.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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/ByteBuffer.h>
  11. #include <AK/JsonObject.h>
  12. #include <AK/JsonValue.h>
  13. #include <AK/LexicalPath.h>
  14. #include <AK/QuickSort.h>
  15. #include <AK/Result.h>
  16. #include <AK/Tuple.h>
  17. #include <LibCore/ArgsParser.h>
  18. #include <LibCore/DirIterator.h>
  19. #include <LibCore/File.h>
  20. #include <LibJS/Bytecode/Interpreter.h>
  21. #include <LibJS/Interpreter.h>
  22. #include <LibJS/Lexer.h>
  23. #include <LibJS/Parser.h>
  24. #include <LibJS/Runtime/Array.h>
  25. #include <LibJS/Runtime/GlobalObject.h>
  26. #include <LibJS/Runtime/JSONObject.h>
  27. #include <LibJS/Runtime/TypedArray.h>
  28. #include <LibJS/Runtime/WeakSet.h>
  29. #include <LibTest/Results.h>
  30. #include <fcntl.h>
  31. #include <sys/time.h>
  32. #include <unistd.h>
  33. #define STRCAT(x, y) __STRCAT(x, y)
  34. #define STRSTRCAT(x, y) __STRSTRCAT(x, y)
  35. #define __STRCAT(x, y) x #y
  36. #define __STRSTRCAT(x, y) x y
  37. // Note: This is a little weird, so here's an explanation:
  38. // If the vararg isn't given, the tuple initializer will simply expand to `fn, ::Test::JS::__testjs_last<1>()`
  39. // and if it _is_ given (say as `A`), the tuple initializer will expand to `fn, ::Test::JS::__testjs_last<1, A>()`, which will end up being evaluated as `A`
  40. // and if multiple args are given, the static_assert will be sad.
  41. #define __TESTJS_REGISTER_GLOBAL_FUNCTION(name, fn, ...) \
  42. struct __TestJS_register_##fn { \
  43. static_assert( \
  44. ::Test::JS::__testjs_count(__VA_ARGS__) <= 1, \
  45. STRCAT(STRSTRCAT(STRCAT("Expected at most three arguments to TESTJS_GLOBAL_FUNCTION at line", __LINE__), ", in file "), __FILE__)); \
  46. __TestJS_register_##fn() noexcept \
  47. { \
  48. ::Test::JS::s_exposed_global_functions.set( \
  49. name, \
  50. { fn, ::Test::JS::__testjs_last<1, ##__VA_ARGS__>() }); \
  51. } \
  52. } __testjs_register_##fn {};
  53. #define TESTJS_GLOBAL_FUNCTION(function, exposed_name, ...) \
  54. JS_DECLARE_NATIVE_FUNCTION(function); \
  55. __TESTJS_REGISTER_GLOBAL_FUNCTION(#exposed_name, function, ##__VA_ARGS__); \
  56. JS_DEFINE_NATIVE_FUNCTION(function)
  57. #define TESTJS_MAIN_HOOK() \
  58. struct __TestJS_main_hook { \
  59. __TestJS_main_hook() \
  60. { \
  61. ::Test::JS::g_main_hook = hook; \
  62. } \
  63. static void hook(); \
  64. } __testjs_common_register_##name {}; \
  65. void __TestJS_main_hook::hook()
  66. #define TESTJS_PROGRAM_FLAG(flag, help_string, long_name, short_name) \
  67. bool flag { false }; \
  68. struct __TestJS_flag_hook_##flag { \
  69. __TestJS_flag_hook_##flag() \
  70. { \
  71. ::Test::JS::g_extra_args.set(&(flag), { help_string, long_name, short_name }); \
  72. }; \
  73. } __testjs_flag_hook_##flag;
  74. #define TEST_ROOT(path) \
  75. String Test::JS::g_test_root_fragment = path
  76. #define TESTJS_RUN_FILE_FUNCTION(...) \
  77. struct __TestJS_run_file { \
  78. __TestJS_run_file() \
  79. { \
  80. ::Test::JS::g_run_file = hook; \
  81. } \
  82. static ::Test::JS::IntermediateRunFileResult hook(const String&, JS::Interpreter&); \
  83. } __testjs_common_run_file {}; \
  84. ::Test::JS::IntermediateRunFileResult __TestJS_run_file::hook(__VA_ARGS__)
  85. namespace Test::JS {
  86. namespace JS = ::JS;
  87. template<typename... Args>
  88. static consteval size_t __testjs_count(Args...) { return sizeof...(Args); }
  89. template<auto... Values>
  90. static consteval size_t __testjs_last() { return (AK::Detail::IntegralConstant<size_t, Values> {}, ...).value; }
  91. static constexpr auto TOP_LEVEL_TEST_NAME = "__$$TOP_LEVEL$$__";
  92. extern RefPtr<JS::VM> g_vm;
  93. extern bool g_collect_on_every_allocation;
  94. extern bool g_run_bytecode;
  95. extern bool g_dump_bytecode;
  96. extern String g_currently_running_test;
  97. extern String g_test_glob;
  98. struct FunctionWithLength {
  99. JS::Value (*function)(JS::VM&, JS::GlobalObject&);
  100. size_t length { 0 };
  101. };
  102. extern HashMap<String, FunctionWithLength> s_exposed_global_functions;
  103. extern String g_test_root_fragment;
  104. extern String g_test_root;
  105. extern int g_test_argc;
  106. extern char** g_test_argv;
  107. extern Function<void()> g_main_hook;
  108. extern HashMap<bool*, Tuple<String, String, char>> g_extra_args;
  109. struct ParserError {
  110. JS::Parser::Error error;
  111. String hint;
  112. };
  113. struct JSFileResult {
  114. String name;
  115. Optional<ParserError> error {};
  116. double time_taken { 0 };
  117. // A failed test takes precedence over a skipped test, which both have
  118. // precedence over a passed test
  119. Test::Result most_severe_test_result { Test::Result::Pass };
  120. Vector<Test::Suite> suites {};
  121. Vector<String> logged_messages {};
  122. };
  123. enum class RunFileHookResult {
  124. RunAsNormal,
  125. SkipFile,
  126. };
  127. using IntermediateRunFileResult = AK::Result<JSFileResult, RunFileHookResult>;
  128. extern IntermediateRunFileResult (*g_run_file)(const String&, JS::Interpreter&);
  129. class TestRunner {
  130. public:
  131. static TestRunner* the()
  132. {
  133. return s_the;
  134. }
  135. TestRunner(String test_root, String common_path, bool print_times, bool print_progress, bool print_json)
  136. : m_common_path(move(common_path))
  137. , m_test_root(move(test_root))
  138. , m_print_times(print_times)
  139. , m_print_progress(print_progress)
  140. , m_print_json(print_json)
  141. {
  142. VERIFY(!s_the);
  143. s_the = this;
  144. g_test_root = m_test_root;
  145. }
  146. virtual ~TestRunner() = default;
  147. void run();
  148. const Test::Counts& counts() const { return m_counts; }
  149. bool is_printing_progress() const { return m_print_progress; }
  150. protected:
  151. static TestRunner* s_the;
  152. virtual Vector<String> get_test_paths() const;
  153. virtual JSFileResult run_file_test(const String& test_path);
  154. void print_file_result(const JSFileResult& file_result) const;
  155. void print_test_results() const;
  156. void print_test_results_as_json() const;
  157. String m_common_path;
  158. String m_test_root;
  159. bool m_print_times;
  160. bool m_print_progress;
  161. bool m_print_json;
  162. double m_total_elapsed_time_in_ms { 0 };
  163. Test::Counts m_counts;
  164. RefPtr<JS::Program> m_test_program;
  165. };
  166. class TestRunnerGlobalObject final : public JS::GlobalObject {
  167. JS_OBJECT(TestRunnerGlobalObject, JS::GlobalObject);
  168. public:
  169. TestRunnerGlobalObject() = default;
  170. virtual ~TestRunnerGlobalObject() override = default;
  171. virtual void initialize_global_object() override;
  172. };
  173. inline void TestRunnerGlobalObject::initialize_global_object()
  174. {
  175. Base::initialize_global_object();
  176. define_property("global", this, JS::Attribute::Enumerable);
  177. for (auto& entry : s_exposed_global_functions) {
  178. define_native_function(
  179. entry.key, [fn = entry.value.function](auto& vm, auto& global_object) {
  180. return fn(vm, global_object);
  181. },
  182. entry.value.length);
  183. }
  184. }
  185. inline void cleanup()
  186. {
  187. // Clear the taskbar progress.
  188. if (TestRunner::the() && TestRunner::the()->is_printing_progress())
  189. warn("\033]9;-1;\033\\");
  190. }
  191. inline void cleanup_and_exit()
  192. {
  193. cleanup();
  194. exit(1);
  195. }
  196. inline double get_time_in_ms()
  197. {
  198. struct timeval tv1;
  199. auto return_code = gettimeofday(&tv1, nullptr);
  200. VERIFY(return_code >= 0);
  201. return static_cast<double>(tv1.tv_sec) * 1000.0 + static_cast<double>(tv1.tv_usec) / 1000.0;
  202. }
  203. template<typename Callback>
  204. inline void iterate_directory_recursively(const String& directory_path, Callback callback)
  205. {
  206. Core::DirIterator directory_iterator(directory_path, Core::DirIterator::Flags::SkipDots);
  207. while (directory_iterator.has_next()) {
  208. auto name = directory_iterator.next_path();
  209. struct stat st = {};
  210. if (fstatat(directory_iterator.fd(), name.characters(), &st, AT_SYMLINK_NOFOLLOW) < 0)
  211. continue;
  212. bool is_directory = S_ISDIR(st.st_mode);
  213. auto full_path = String::formatted("{}/{}", directory_path, name);
  214. if (is_directory && name != "/Fixtures"sv) {
  215. iterate_directory_recursively(full_path, callback);
  216. } else if (!is_directory) {
  217. callback(full_path);
  218. }
  219. }
  220. }
  221. inline Vector<String> TestRunner::get_test_paths() const
  222. {
  223. Vector<String> paths;
  224. iterate_directory_recursively(m_test_root, [&](const String& file_path) {
  225. if (!file_path.ends_with(".js"))
  226. return;
  227. if (!file_path.ends_with("test-common.js"))
  228. paths.append(file_path);
  229. });
  230. quick_sort(paths);
  231. return paths;
  232. }
  233. inline void TestRunner::run()
  234. {
  235. size_t progress_counter = 0;
  236. auto test_paths = get_test_paths();
  237. for (auto& path : test_paths) {
  238. if (!path.matches(g_test_glob))
  239. continue;
  240. ++progress_counter;
  241. auto file_result = run_file_test(path);
  242. if (!m_print_json)
  243. print_file_result(file_result);
  244. if (m_print_progress)
  245. warn("\033]9;{};{};\033\\", progress_counter, test_paths.size());
  246. }
  247. if (m_print_progress)
  248. warn("\033]9;-1;\033\\");
  249. if (!m_print_json)
  250. print_test_results();
  251. else
  252. print_test_results_as_json();
  253. }
  254. inline AK::Result<NonnullRefPtr<JS::Program>, ParserError> parse_file(const String& file_path)
  255. {
  256. auto file = Core::File::construct(file_path);
  257. auto result = file->open(Core::OpenMode::ReadOnly);
  258. if (!result) {
  259. warnln("Failed to open the following file: \"{}\"", file_path);
  260. cleanup_and_exit();
  261. }
  262. auto contents = file->read_all();
  263. String test_file_string(reinterpret_cast<const char*>(contents.data()), contents.size());
  264. file->close();
  265. auto parser = JS::Parser(JS::Lexer(test_file_string));
  266. auto program = parser.parse_program();
  267. if (parser.has_errors()) {
  268. auto error = parser.errors()[0];
  269. return AK::Result<NonnullRefPtr<JS::Program>, ParserError>(ParserError { error, error.source_location_hint(test_file_string) });
  270. }
  271. return AK::Result<NonnullRefPtr<JS::Program>, ParserError>(program);
  272. }
  273. inline Optional<JsonValue> get_test_results(JS::Interpreter& interpreter)
  274. {
  275. auto result = g_vm->get_variable("__TestResults__", interpreter.global_object());
  276. auto json_string = JS::JSONObject::stringify_impl(interpreter.global_object(), result, JS::js_undefined(), JS::js_undefined());
  277. auto json = JsonValue::from_string(json_string);
  278. if (!json.has_value())
  279. return {};
  280. return json.value();
  281. }
  282. inline JSFileResult TestRunner::run_file_test(const String& test_path)
  283. {
  284. g_currently_running_test = test_path;
  285. double start_time = get_time_in_ms();
  286. auto interpreter = JS::Interpreter::create<TestRunnerGlobalObject>(*g_vm);
  287. // FIXME: This is a hack while we're refactoring Interpreter/VM stuff.
  288. JS::VM::InterpreterExecutionScope scope(*interpreter);
  289. interpreter->heap().set_should_collect_on_every_allocation(g_collect_on_every_allocation);
  290. if (g_run_file) {
  291. auto result = g_run_file(test_path, *interpreter);
  292. if (result.is_error() && result.error() == RunFileHookResult::SkipFile) {
  293. return {
  294. test_path,
  295. {},
  296. 0,
  297. Test::Result::Skip,
  298. {},
  299. {}
  300. };
  301. }
  302. if (!result.is_error()) {
  303. auto value = result.release_value();
  304. for (auto& suite : value.suites) {
  305. if (suite.most_severe_test_result == Result::Pass)
  306. m_counts.suites_passed++;
  307. else if (suite.most_severe_test_result == Result::Fail)
  308. m_counts.suites_failed++;
  309. for (auto& test : suite.tests) {
  310. if (test.result == Result::Pass)
  311. m_counts.tests_passed++;
  312. else if (test.result == Result::Fail)
  313. m_counts.tests_failed++;
  314. else if (test.result == Result::Skip)
  315. m_counts.tests_skipped++;
  316. }
  317. }
  318. ++m_counts.files_total;
  319. m_total_elapsed_time_in_ms += value.time_taken;
  320. return value;
  321. }
  322. }
  323. if (!m_test_program) {
  324. auto result = parse_file(m_common_path);
  325. if (result.is_error()) {
  326. warnln("Unable to parse test-common.js");
  327. warnln("{}", result.error().error.to_string());
  328. warnln("{}", result.error().hint);
  329. cleanup_and_exit();
  330. }
  331. m_test_program = result.value();
  332. }
  333. if (g_run_bytecode) {
  334. auto unit = JS::Bytecode::Generator::generate(*m_test_program);
  335. if (g_dump_bytecode) {
  336. for (auto& block : unit.basic_blocks)
  337. block.dump(unit);
  338. if (!unit.string_table->is_empty()) {
  339. outln();
  340. unit.string_table->dump();
  341. }
  342. }
  343. JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object());
  344. bytecode_interpreter.run(unit);
  345. } else {
  346. interpreter->run(interpreter->global_object(), *m_test_program);
  347. }
  348. VERIFY(!g_vm->exception());
  349. auto file_program = parse_file(test_path);
  350. if (file_program.is_error())
  351. return { test_path, file_program.error() };
  352. if (g_run_bytecode) {
  353. auto unit = JS::Bytecode::Generator::generate(*file_program.value());
  354. if (g_dump_bytecode) {
  355. for (auto& block : unit.basic_blocks)
  356. block.dump(unit);
  357. if (!unit.string_table->is_empty()) {
  358. outln();
  359. unit.string_table->dump();
  360. }
  361. }
  362. JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object());
  363. bytecode_interpreter.run(unit);
  364. } else {
  365. interpreter->run(interpreter->global_object(), *file_program.value());
  366. }
  367. if (g_vm->exception())
  368. g_vm->clear_exception();
  369. auto test_json = get_test_results(*interpreter);
  370. if (!test_json.has_value()) {
  371. warnln("Received malformed JSON from test \"{}\"", test_path);
  372. cleanup_and_exit();
  373. }
  374. JSFileResult file_result { test_path.substring(m_test_root.length() + 1, test_path.length() - m_test_root.length() - 1) };
  375. // Collect logged messages
  376. auto& arr = interpreter->vm().get_variable("__UserOutput__", interpreter->global_object()).as_array();
  377. for (auto& entry : arr.indexed_properties()) {
  378. auto message = entry.value_and_attributes(&interpreter->global_object()).value;
  379. file_result.logged_messages.append(message.to_string_without_side_effects());
  380. }
  381. test_json.value().as_object().for_each_member([&](const String& suite_name, const JsonValue& suite_value) {
  382. Test::Suite suite { suite_name };
  383. VERIFY(suite_value.is_object());
  384. suite_value.as_object().for_each_member([&](const String& test_name, const JsonValue& test_value) {
  385. Test::Case test { test_name, Test::Result::Fail, "" };
  386. VERIFY(test_value.is_object());
  387. VERIFY(test_value.as_object().has("result"));
  388. auto result = test_value.as_object().get("result");
  389. VERIFY(result.is_string());
  390. auto result_string = result.as_string();
  391. if (result_string == "pass") {
  392. test.result = Test::Result::Pass;
  393. m_counts.tests_passed++;
  394. } else if (result_string == "fail") {
  395. test.result = Test::Result::Fail;
  396. m_counts.tests_failed++;
  397. suite.most_severe_test_result = Test::Result::Fail;
  398. VERIFY(test_value.as_object().has("details"));
  399. auto details = test_value.as_object().get("details");
  400. VERIFY(result.is_string());
  401. test.details = details.as_string();
  402. } else {
  403. test.result = Test::Result::Skip;
  404. if (suite.most_severe_test_result == Test::Result::Pass)
  405. suite.most_severe_test_result = Test::Result::Skip;
  406. m_counts.tests_skipped++;
  407. }
  408. suite.tests.append(test);
  409. });
  410. if (suite.most_severe_test_result == Test::Result::Fail) {
  411. m_counts.suites_failed++;
  412. file_result.most_severe_test_result = Test::Result::Fail;
  413. } else {
  414. if (suite.most_severe_test_result == Test::Result::Skip && file_result.most_severe_test_result == Test::Result::Pass)
  415. file_result.most_severe_test_result = Test::Result::Skip;
  416. m_counts.suites_passed++;
  417. }
  418. file_result.suites.append(suite);
  419. });
  420. m_counts.files_total++;
  421. file_result.time_taken = get_time_in_ms() - start_time;
  422. m_total_elapsed_time_in_ms += file_result.time_taken;
  423. return file_result;
  424. }
  425. enum Modifier {
  426. BG_RED,
  427. BG_GREEN,
  428. FG_RED,
  429. FG_GREEN,
  430. FG_ORANGE,
  431. FG_GRAY,
  432. FG_BLACK,
  433. FG_BOLD,
  434. ITALIC,
  435. CLEAR,
  436. };
  437. inline void print_modifiers(Vector<Modifier> modifiers)
  438. {
  439. for (auto& modifier : modifiers) {
  440. auto code = [&] {
  441. switch (modifier) {
  442. case BG_RED:
  443. return "\033[48;2;255;0;102m";
  444. case BG_GREEN:
  445. return "\033[48;2;102;255;0m";
  446. case FG_RED:
  447. return "\033[38;2;255;0;102m";
  448. case FG_GREEN:
  449. return "\033[38;2;102;255;0m";
  450. case FG_ORANGE:
  451. return "\033[38;2;255;102;0m";
  452. case FG_GRAY:
  453. return "\033[38;2;135;139;148m";
  454. case FG_BLACK:
  455. return "\033[30m";
  456. case FG_BOLD:
  457. return "\033[1m";
  458. case ITALIC:
  459. return "\033[3m";
  460. case CLEAR:
  461. return "\033[0m";
  462. }
  463. VERIFY_NOT_REACHED();
  464. }();
  465. out("{}", code);
  466. }
  467. }
  468. inline void TestRunner::print_file_result(const JSFileResult& file_result) const
  469. {
  470. if (file_result.most_severe_test_result == Test::Result::Fail || file_result.error.has_value()) {
  471. print_modifiers({ BG_RED, FG_BLACK, FG_BOLD });
  472. out(" FAIL ");
  473. print_modifiers({ CLEAR });
  474. } else {
  475. if (m_print_times || file_result.most_severe_test_result != Test::Result::Pass) {
  476. print_modifiers({ BG_GREEN, FG_BLACK, FG_BOLD });
  477. out(" PASS ");
  478. print_modifiers({ CLEAR });
  479. } else {
  480. return;
  481. }
  482. }
  483. out(" {}", file_result.name);
  484. if (m_print_times) {
  485. print_modifiers({ CLEAR, ITALIC, FG_GRAY });
  486. if (file_result.time_taken < 1000) {
  487. outln(" ({}ms)", static_cast<int>(file_result.time_taken));
  488. } else {
  489. outln(" ({:3}s)", file_result.time_taken / 1000.0);
  490. }
  491. print_modifiers({ CLEAR });
  492. } else {
  493. outln();
  494. }
  495. if (!file_result.logged_messages.is_empty()) {
  496. print_modifiers({ FG_GRAY, FG_BOLD });
  497. #ifdef __serenity__
  498. outln(" ℹ Console output:");
  499. #else
  500. // This emoji has a second invisible byte after it. The one above does not
  501. outln(" ℹ️ Console output:");
  502. #endif
  503. print_modifiers({ CLEAR, FG_GRAY });
  504. for (auto& message : file_result.logged_messages)
  505. outln(" {}", message);
  506. }
  507. if (file_result.error.has_value()) {
  508. auto test_error = file_result.error.value();
  509. print_modifiers({ FG_RED });
  510. #ifdef __serenity__
  511. outln(" ❌ The file failed to parse");
  512. #else
  513. // No invisible byte here, but the spacing still needs to be altered on the host
  514. outln(" ❌ The file failed to parse");
  515. #endif
  516. outln();
  517. print_modifiers({ FG_GRAY });
  518. for (auto& message : test_error.hint.split('\n', true)) {
  519. outln(" {}", message);
  520. }
  521. print_modifiers({ FG_RED });
  522. outln(" {}", test_error.error.to_string());
  523. outln();
  524. return;
  525. }
  526. if (file_result.most_severe_test_result != Test::Result::Pass) {
  527. for (auto& suite : file_result.suites) {
  528. if (suite.most_severe_test_result == Test::Result::Pass)
  529. continue;
  530. bool failed = suite.most_severe_test_result == Test::Result::Fail;
  531. print_modifiers({ FG_GRAY, FG_BOLD });
  532. if (failed) {
  533. #ifdef __serenity__
  534. out(" ❌ Suite: ");
  535. #else
  536. // No invisible byte here, but the spacing still needs to be altered on the host
  537. out(" ❌ Suite: ");
  538. #endif
  539. } else {
  540. #ifdef __serenity__
  541. out(" ⚠ Suite: ");
  542. #else
  543. // This emoji has a second invisible byte after it. The one above does not
  544. out(" ⚠️ Suite: ");
  545. #endif
  546. }
  547. print_modifiers({ CLEAR, FG_GRAY });
  548. if (suite.name == TOP_LEVEL_TEST_NAME) {
  549. outln("<top-level>");
  550. } else {
  551. outln("{}", suite.name);
  552. }
  553. print_modifiers({ CLEAR });
  554. for (auto& test : suite.tests) {
  555. if (test.result == Test::Result::Pass)
  556. continue;
  557. print_modifiers({ FG_GRAY, FG_BOLD });
  558. out(" Test: ");
  559. if (test.result == Test::Result::Fail) {
  560. print_modifiers({ CLEAR, FG_RED });
  561. outln("{} (failed):", test.name);
  562. outln(" {}", test.details);
  563. } else {
  564. print_modifiers({ CLEAR, FG_ORANGE });
  565. outln("{} (skipped)", test.name);
  566. }
  567. print_modifiers({ CLEAR });
  568. }
  569. }
  570. }
  571. }
  572. inline void TestRunner::print_test_results() const
  573. {
  574. out("\nTest Suites: ");
  575. if (m_counts.suites_failed) {
  576. print_modifiers({ FG_RED });
  577. out("{} failed, ", m_counts.suites_failed);
  578. print_modifiers({ CLEAR });
  579. }
  580. if (m_counts.suites_passed) {
  581. print_modifiers({ FG_GREEN });
  582. out("{} passed, ", m_counts.suites_passed);
  583. print_modifiers({ CLEAR });
  584. }
  585. outln("{} total", m_counts.suites_failed + m_counts.suites_passed);
  586. out("Tests: ");
  587. if (m_counts.tests_failed) {
  588. print_modifiers({ FG_RED });
  589. out("{} failed, ", m_counts.tests_failed);
  590. print_modifiers({ CLEAR });
  591. }
  592. if (m_counts.tests_skipped) {
  593. print_modifiers({ FG_ORANGE });
  594. out("{} skipped, ", m_counts.tests_skipped);
  595. print_modifiers({ CLEAR });
  596. }
  597. if (m_counts.tests_passed) {
  598. print_modifiers({ FG_GREEN });
  599. out("{} passed, ", m_counts.tests_passed);
  600. print_modifiers({ CLEAR });
  601. }
  602. outln("{} total", m_counts.tests_failed + m_counts.tests_skipped + m_counts.tests_passed);
  603. outln("Files: {} total", m_counts.files_total);
  604. out("Time: ");
  605. if (m_total_elapsed_time_in_ms < 1000.0) {
  606. outln("{}ms", static_cast<int>(m_total_elapsed_time_in_ms));
  607. } else {
  608. outln("{:>.3}s", m_total_elapsed_time_in_ms / 1000.0);
  609. }
  610. outln();
  611. }
  612. inline void TestRunner::print_test_results_as_json() const
  613. {
  614. JsonObject suites;
  615. suites.set("failed", m_counts.suites_failed);
  616. suites.set("passed", m_counts.suites_passed);
  617. suites.set("total", m_counts.suites_failed + m_counts.suites_passed);
  618. JsonObject tests;
  619. tests.set("failed", m_counts.tests_failed);
  620. tests.set("passed", m_counts.tests_passed);
  621. tests.set("skipped", m_counts.tests_skipped);
  622. tests.set("total", m_counts.tests_failed + m_counts.tests_passed + m_counts.tests_skipped);
  623. JsonObject results;
  624. results.set("suites", suites);
  625. results.set("tests", tests);
  626. JsonObject root;
  627. root.set("results", results);
  628. root.set("files_total", m_counts.files_total);
  629. root.set("duration", m_total_elapsed_time_in_ms / 1000.0);
  630. outln("{}", root.to_string());
  631. }
  632. }