JavaScriptTestRunner.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2020-2022, 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/DirIterator.h>
  18. #include <LibCore/File.h>
  19. #include <LibJS/Bytecode/Interpreter.h>
  20. #include <LibJS/Interpreter.h>
  21. #include <LibJS/Lexer.h>
  22. #include <LibJS/Parser.h>
  23. #include <LibJS/Runtime/Array.h>
  24. #include <LibJS/Runtime/GlobalObject.h>
  25. #include <LibJS/Runtime/JSONObject.h>
  26. #include <LibJS/Runtime/TypedArray.h>
  27. #include <LibJS/Runtime/WeakMap.h>
  28. #include <LibJS/Runtime/WeakSet.h>
  29. #include <LibJS/Script.h>
  30. #include <LibJS/SourceTextModule.h>
  31. #include <LibTest/Results.h>
  32. #include <LibTest/TestRunner.h>
  33. #include <fcntl.h>
  34. #include <sys/time.h>
  35. #include <unistd.h>
  36. #ifdef AK_OS_SERENITY
  37. # include <serenity.h>
  38. #endif
  39. #define STRCAT(x, y) __STRCAT(x, y)
  40. #define STRSTRCAT(x, y) __STRSTRCAT(x, y)
  41. #define __STRCAT(x, y) x #y
  42. #define __STRSTRCAT(x, y) x y
  43. // Note: This is a little weird, so here's an explanation:
  44. // If the vararg isn't given, the tuple initializer will simply expand to `fn, ::Test::JS::__testjs_last<1>()`
  45. // 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`
  46. // and if multiple args are given, the static_assert will be sad.
  47. #define __TESTJS_REGISTER_GLOBAL_FUNCTION(name, fn, ...) \
  48. struct __TestJS_register_##fn { \
  49. static_assert( \
  50. ::Test::JS::__testjs_count(__VA_ARGS__) <= 1, \
  51. STRCAT(STRSTRCAT(STRCAT("Expected at most three arguments to TESTJS_GLOBAL_FUNCTION at line", __LINE__), ", in file "), __FILE__)); \
  52. __TestJS_register_##fn() noexcept \
  53. { \
  54. ::Test::JS::s_exposed_global_functions.set( \
  55. name, \
  56. { fn, ::Test::JS::__testjs_last<1, ##__VA_ARGS__>() }); \
  57. } \
  58. } __testjs_register_##fn {};
  59. #define TESTJS_GLOBAL_FUNCTION(function, exposed_name, ...) \
  60. JS_DECLARE_NATIVE_FUNCTION(function); \
  61. __TESTJS_REGISTER_GLOBAL_FUNCTION(#exposed_name, function, ##__VA_ARGS__); \
  62. JS_DEFINE_NATIVE_FUNCTION(function)
  63. #define TESTJS_MAIN_HOOK() \
  64. struct __TestJS_main_hook { \
  65. __TestJS_main_hook() \
  66. { \
  67. ::Test::JS::g_main_hook = hook; \
  68. } \
  69. static void hook(); \
  70. } __testjs_common_register_##name {}; \
  71. void __TestJS_main_hook::hook()
  72. #define TESTJS_PROGRAM_FLAG(flag, help_string, long_name, short_name) \
  73. bool flag { false }; \
  74. struct __TestJS_flag_hook_##flag { \
  75. __TestJS_flag_hook_##flag() \
  76. { \
  77. ::Test::JS::g_extra_args.set(&(flag), { help_string, long_name, short_name }); \
  78. }; \
  79. } __testjs_flag_hook_##flag;
  80. #define TEST_ROOT(path) \
  81. DeprecatedString Test::JS::g_test_root_fragment = path
  82. #define TESTJS_RUN_FILE_FUNCTION(...) \
  83. struct __TestJS_run_file { \
  84. __TestJS_run_file() \
  85. { \
  86. ::Test::JS::g_run_file = hook; \
  87. } \
  88. static ::Test::JS::IntermediateRunFileResult hook(DeprecatedString const&, JS::Interpreter&, JS::ExecutionContext&); \
  89. } __testjs_common_run_file {}; \
  90. ::Test::JS::IntermediateRunFileResult __TestJS_run_file::hook(__VA_ARGS__)
  91. #define TESTJS_CREATE_INTERPRETER_HOOK(...) \
  92. struct __TestJS_create_interpreter_hook { \
  93. __TestJS_create_interpreter_hook() \
  94. { \
  95. ::Test::JS::g_create_interpreter_hook = hook; \
  96. } \
  97. static NonnullOwnPtr<JS::Interpreter> hook(); \
  98. } __testjs_create_interpreter_hook {}; \
  99. NonnullOwnPtr<JS::Interpreter> __TestJS_create_interpreter_hook::hook(__VA_ARGS__)
  100. namespace Test::JS {
  101. namespace JS = ::JS;
  102. template<typename... Args>
  103. static consteval size_t __testjs_count(Args...) { return sizeof...(Args); }
  104. template<auto... Values>
  105. static consteval size_t __testjs_last()
  106. {
  107. Array values { Values... };
  108. return values[values.size() - 1U];
  109. }
  110. static constexpr auto TOP_LEVEL_TEST_NAME = "__$$TOP_LEVEL$$__";
  111. extern RefPtr<JS::VM> g_vm;
  112. extern bool g_collect_on_every_allocation;
  113. extern DeprecatedString g_currently_running_test;
  114. struct FunctionWithLength {
  115. JS::ThrowCompletionOr<JS::Value> (*function)(JS::VM&);
  116. size_t length { 0 };
  117. };
  118. extern HashMap<DeprecatedString, FunctionWithLength> s_exposed_global_functions;
  119. extern DeprecatedString g_test_root_fragment;
  120. extern DeprecatedString g_test_root;
  121. extern int g_test_argc;
  122. extern char** g_test_argv;
  123. extern Function<void()> g_main_hook;
  124. extern Function<NonnullOwnPtr<JS::Interpreter>()> g_create_interpreter_hook;
  125. extern HashMap<bool*, Tuple<DeprecatedString, DeprecatedString, char>> g_extra_args;
  126. struct ParserError {
  127. JS::ParserError error;
  128. DeprecatedString hint;
  129. };
  130. struct JSFileResult {
  131. DeprecatedString name;
  132. Optional<ParserError> error {};
  133. double time_taken { 0 };
  134. // A failed test takes precedence over a skipped test, which both have
  135. // precedence over a passed test
  136. Test::Result most_severe_test_result { Test::Result::Pass };
  137. Vector<Test::Suite> suites {};
  138. Vector<DeprecatedString> logged_messages {};
  139. };
  140. enum class RunFileHookResult {
  141. RunAsNormal,
  142. SkipFile,
  143. };
  144. using IntermediateRunFileResult = AK::Result<JSFileResult, RunFileHookResult>;
  145. extern IntermediateRunFileResult (*g_run_file)(DeprecatedString const&, JS::Interpreter&, JS::ExecutionContext&);
  146. class TestRunner : public ::Test::TestRunner {
  147. public:
  148. TestRunner(DeprecatedString test_root, DeprecatedString common_path, bool print_times, bool print_progress, bool print_json, bool detailed_json)
  149. : ::Test::TestRunner(move(test_root), print_times, print_progress, print_json, detailed_json)
  150. , m_common_path(move(common_path))
  151. {
  152. g_test_root = m_test_root;
  153. }
  154. virtual ~TestRunner() = default;
  155. protected:
  156. virtual void do_run_single_test(DeprecatedString const& test_path, size_t, size_t) override;
  157. virtual Vector<DeprecatedString> get_test_paths() const override;
  158. virtual JSFileResult run_file_test(DeprecatedString const& test_path);
  159. void print_file_result(JSFileResult const& file_result) const;
  160. DeprecatedString m_common_path;
  161. };
  162. class TestRunnerGlobalObject final : public JS::GlobalObject {
  163. JS_OBJECT(TestRunnerGlobalObject, JS::GlobalObject);
  164. public:
  165. TestRunnerGlobalObject(JS::Realm& realm)
  166. : JS::GlobalObject(realm)
  167. {
  168. }
  169. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  170. virtual ~TestRunnerGlobalObject() override = default;
  171. };
  172. inline JS::ThrowCompletionOr<void> TestRunnerGlobalObject::initialize(JS::Realm& realm)
  173. {
  174. MUST_OR_THROW_OOM(Base::initialize(realm));
  175. define_direct_property("global", this, JS::Attribute::Enumerable);
  176. for (auto& entry : s_exposed_global_functions) {
  177. define_native_function(
  178. realm,
  179. entry.key, [fn = entry.value.function](auto& vm) {
  180. return fn(vm);
  181. },
  182. entry.value.length, JS::default_attributes);
  183. }
  184. return {};
  185. }
  186. inline ByteBuffer load_entire_file(StringView path)
  187. {
  188. auto try_load_entire_file = [](StringView const& path) -> ErrorOr<ByteBuffer> {
  189. auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
  190. auto file_size = TRY(file->size());
  191. auto content = TRY(ByteBuffer::create_uninitialized(file_size));
  192. TRY(file->read_until_filled(content.bytes()));
  193. return content;
  194. };
  195. auto buffer_or_error = try_load_entire_file(path);
  196. if (buffer_or_error.is_error()) {
  197. warnln("Failed to open the following file: \"{}\", error: {}", path, buffer_or_error.release_error());
  198. cleanup_and_exit();
  199. }
  200. return buffer_or_error.release_value();
  201. }
  202. inline AK::Result<JS::NonnullGCPtr<JS::Script>, ParserError> parse_script(StringView path, JS::Realm& realm)
  203. {
  204. auto contents = load_entire_file(path);
  205. auto script_or_errors = JS::Script::parse(contents, realm, path);
  206. if (script_or_errors.is_error()) {
  207. auto errors = script_or_errors.release_error();
  208. return ParserError { errors[0], errors[0].source_location_hint(contents) };
  209. }
  210. return script_or_errors.release_value();
  211. }
  212. inline AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, ParserError> parse_module(StringView path, JS::Realm& realm)
  213. {
  214. auto contents = load_entire_file(path);
  215. auto script_or_errors = JS::SourceTextModule::parse(contents, realm, path);
  216. if (script_or_errors.is_error()) {
  217. auto errors = script_or_errors.release_error();
  218. return ParserError { errors[0], errors[0].source_location_hint(contents) };
  219. }
  220. return script_or_errors.release_value();
  221. }
  222. inline ErrorOr<JsonValue> get_test_results(JS::Interpreter& interpreter)
  223. {
  224. auto results = MUST(interpreter.realm().global_object().get("__TestResults__"));
  225. auto json_string = MUST(JS::JSONObject::stringify_impl(*g_vm, results, JS::js_undefined(), JS::js_undefined()));
  226. return JsonValue::from_string(json_string);
  227. }
  228. inline void TestRunner::do_run_single_test(DeprecatedString const& test_path, size_t, size_t)
  229. {
  230. auto file_result = run_file_test(test_path);
  231. if (!m_print_json)
  232. print_file_result(file_result);
  233. if (needs_detailed_suites())
  234. ensure_suites().extend(file_result.suites);
  235. }
  236. inline Vector<DeprecatedString> TestRunner::get_test_paths() const
  237. {
  238. Vector<DeprecatedString> paths;
  239. iterate_directory_recursively(m_test_root, [&](DeprecatedString const& file_path) {
  240. if (!file_path.ends_with(".js"sv))
  241. return;
  242. if (!file_path.ends_with("test-common.js"sv))
  243. paths.append(file_path);
  244. });
  245. quick_sort(paths);
  246. return paths;
  247. }
  248. inline JSFileResult TestRunner::run_file_test(DeprecatedString const& test_path)
  249. {
  250. g_currently_running_test = test_path;
  251. #ifdef AK_OS_SERENITY
  252. auto string_id = perf_register_string(test_path.characters(), test_path.length());
  253. perf_event(PERF_EVENT_SIGNPOST, string_id, 0);
  254. #endif
  255. double start_time = get_time_in_ms();
  256. auto interpreter = JS::Interpreter::create<TestRunnerGlobalObject>(*g_vm);
  257. // Since g_vm is reused for each new interpreter, Interpreter::create will end up pushing multiple
  258. // global execution contexts onto the VM's execution context stack. To prevent this, we immediately
  259. // pop the global execution context off the execution context stack and manually handle pushing
  260. // and popping it. Since the global execution context should be the only thing on the stack
  261. // at interpreter creation, let's assert there is only one.
  262. VERIFY(g_vm->execution_context_stack().size() == 1);
  263. auto& global_execution_context = *g_vm->execution_context_stack().take_first();
  264. // FIXME: This is a hack while we're refactoring Interpreter/VM stuff.
  265. JS::VM::InterpreterExecutionScope scope(*interpreter);
  266. interpreter->heap().set_should_collect_on_every_allocation(g_collect_on_every_allocation);
  267. if (g_run_file) {
  268. auto result = g_run_file(test_path, *interpreter, global_execution_context);
  269. if (result.is_error() && result.error() == RunFileHookResult::SkipFile) {
  270. return {
  271. test_path,
  272. {},
  273. 0,
  274. Test::Result::Skip,
  275. {},
  276. {}
  277. };
  278. }
  279. if (!result.is_error()) {
  280. auto value = result.release_value();
  281. for (auto& suite : value.suites) {
  282. if (suite.most_severe_test_result == Result::Pass)
  283. m_counts.suites_passed++;
  284. else if (suite.most_severe_test_result == Result::Fail)
  285. m_counts.suites_failed++;
  286. for (auto& test : suite.tests) {
  287. if (test.result == Result::Pass)
  288. m_counts.tests_passed++;
  289. else if (test.result == Result::Fail)
  290. m_counts.tests_failed++;
  291. else if (test.result == Result::Skip)
  292. m_counts.tests_skipped++;
  293. }
  294. }
  295. ++m_counts.files_total;
  296. m_total_elapsed_time_in_ms += value.time_taken;
  297. return value;
  298. }
  299. }
  300. // FIXME: Since a new interpreter is created every time with a new realm, we no longer cache the test-common.js file as scripts are parsed for the current realm only.
  301. // Find a way to cache this.
  302. auto result = parse_script(m_common_path, interpreter->realm());
  303. if (result.is_error()) {
  304. warnln("Unable to parse test-common.js");
  305. warnln("{}", result.error().error.to_deprecated_string());
  306. warnln("{}", result.error().hint);
  307. cleanup_and_exit();
  308. }
  309. auto test_script = result.release_value();
  310. if (auto* bytecode_interpreter = g_vm->bytecode_interpreter_if_exists()) {
  311. g_vm->push_execution_context(global_execution_context);
  312. MUST(bytecode_interpreter->run(*test_script));
  313. g_vm->pop_execution_context();
  314. } else {
  315. g_vm->push_execution_context(global_execution_context);
  316. MUST(interpreter->run(*test_script));
  317. g_vm->pop_execution_context();
  318. }
  319. auto file_script = parse_script(test_path, interpreter->realm());
  320. JS::ThrowCompletionOr<JS::Value> top_level_result { JS::js_undefined() };
  321. if (file_script.is_error())
  322. return { test_path, file_script.error() };
  323. g_vm->push_execution_context(global_execution_context);
  324. if (auto* bytecode_interpreter = g_vm->bytecode_interpreter_if_exists()) {
  325. top_level_result = bytecode_interpreter->run(file_script.value());
  326. } else {
  327. top_level_result = interpreter->run(file_script.value());
  328. }
  329. g_vm->pop_execution_context();
  330. g_vm->push_execution_context(global_execution_context);
  331. auto test_json = get_test_results(*interpreter);
  332. g_vm->pop_execution_context();
  333. if (test_json.is_error()) {
  334. warnln("Received malformed JSON from test \"{}\"", test_path);
  335. cleanup_and_exit();
  336. }
  337. JSFileResult file_result { test_path.substring(m_test_root.length() + 1, test_path.length() - m_test_root.length() - 1) };
  338. // Collect logged messages
  339. auto user_output = MUST(interpreter->realm().global_object().get("__UserOutput__"));
  340. auto& arr = user_output.as_array();
  341. for (auto& entry : arr.indexed_properties()) {
  342. auto message = MUST(arr.get(entry.index()));
  343. file_result.logged_messages.append(message.to_string_without_side_effects().release_value_but_fixme_should_propagate_errors().to_deprecated_string());
  344. }
  345. test_json.value().as_object().for_each_member([&](DeprecatedString const& suite_name, JsonValue const& suite_value) {
  346. Test::Suite suite { test_path, suite_name };
  347. VERIFY(suite_value.is_object());
  348. suite_value.as_object().for_each_member([&](const DeprecatedString& test_name, const JsonValue& test_value) {
  349. Test::Case test { test_name, Test::Result::Fail, "", 0 };
  350. VERIFY(test_value.is_object());
  351. VERIFY(test_value.as_object().has("result"sv));
  352. auto result = test_value.as_object().get_deprecated_string("result"sv);
  353. VERIFY(result.has_value());
  354. auto result_string = result.value();
  355. if (result_string == "pass") {
  356. test.result = Test::Result::Pass;
  357. m_counts.tests_passed++;
  358. } else if (result_string == "fail") {
  359. test.result = Test::Result::Fail;
  360. m_counts.tests_failed++;
  361. suite.most_severe_test_result = Test::Result::Fail;
  362. VERIFY(test_value.as_object().has("details"sv));
  363. auto details = test_value.as_object().get_deprecated_string("details"sv);
  364. VERIFY(result.has_value());
  365. test.details = details.value();
  366. } else {
  367. test.result = Test::Result::Skip;
  368. if (suite.most_severe_test_result == Test::Result::Pass)
  369. suite.most_severe_test_result = Test::Result::Skip;
  370. m_counts.tests_skipped++;
  371. }
  372. test.duration_us = test_value.as_object().get_u64("duration"sv).value_or(0);
  373. suite.tests.append(test);
  374. });
  375. if (suite.most_severe_test_result == Test::Result::Fail) {
  376. m_counts.suites_failed++;
  377. file_result.most_severe_test_result = Test::Result::Fail;
  378. } else {
  379. if (suite.most_severe_test_result == Test::Result::Skip && file_result.most_severe_test_result == Test::Result::Pass)
  380. file_result.most_severe_test_result = Test::Result::Skip;
  381. m_counts.suites_passed++;
  382. }
  383. file_result.suites.append(suite);
  384. });
  385. if (top_level_result.is_error()) {
  386. Test::Suite suite { test_path, "<top-level>" };
  387. suite.most_severe_test_result = Result::Crashed;
  388. Test::Case test_case { "<top-level>", Test::Result::Fail, "", 0 };
  389. auto error = top_level_result.release_error().release_value().release_value();
  390. if (error.is_object()) {
  391. StringBuilder detail_builder;
  392. auto& error_object = error.as_object();
  393. auto name = error_object.get_without_side_effects(g_vm->names.name).value_or(JS::js_undefined());
  394. auto message = error_object.get_without_side_effects(g_vm->names.message).value_or(JS::js_undefined());
  395. if (name.is_accessor() || message.is_accessor()) {
  396. detail_builder.append(error.to_string_without_side_effects().release_value_but_fixme_should_propagate_errors());
  397. } else {
  398. detail_builder.append(name.to_string_without_side_effects().release_value_but_fixme_should_propagate_errors());
  399. detail_builder.append(": "sv);
  400. detail_builder.append(message.to_string_without_side_effects().release_value_but_fixme_should_propagate_errors());
  401. }
  402. if (is<JS::Error>(error_object)) {
  403. auto& error_as_error = static_cast<JS::Error&>(error_object);
  404. detail_builder.append('\n');
  405. detail_builder.append(error_as_error.stack_string(*g_vm).release_allocated_value_but_fixme_should_propagate_errors());
  406. }
  407. test_case.details = detail_builder.to_deprecated_string();
  408. } else {
  409. test_case.details = error.to_string_without_side_effects().release_value_but_fixme_should_propagate_errors().to_deprecated_string();
  410. }
  411. suite.tests.append(move(test_case));
  412. file_result.suites.append(suite);
  413. m_counts.suites_failed++;
  414. file_result.most_severe_test_result = Test::Result::Fail;
  415. }
  416. m_counts.files_total++;
  417. file_result.time_taken = get_time_in_ms() - start_time;
  418. m_total_elapsed_time_in_ms += file_result.time_taken;
  419. return file_result;
  420. }
  421. inline void TestRunner::print_file_result(JSFileResult const& file_result) const
  422. {
  423. if (file_result.most_severe_test_result == Test::Result::Fail || file_result.error.has_value()) {
  424. print_modifiers({ BG_RED, FG_BOLD });
  425. out(" FAIL ");
  426. print_modifiers({ CLEAR });
  427. } else {
  428. if (m_print_times || file_result.most_severe_test_result != Test::Result::Pass) {
  429. print_modifiers({ BG_GREEN, FG_BLACK, FG_BOLD });
  430. out(" PASS ");
  431. print_modifiers({ CLEAR });
  432. } else {
  433. return;
  434. }
  435. }
  436. out(" {}", file_result.name);
  437. if (m_print_times) {
  438. print_modifiers({ CLEAR, ITALIC, FG_GRAY });
  439. if (file_result.time_taken < 1000) {
  440. outln(" ({}ms)", static_cast<int>(file_result.time_taken));
  441. } else {
  442. outln(" ({:3}s)", file_result.time_taken / 1000.0);
  443. }
  444. print_modifiers({ CLEAR });
  445. } else {
  446. outln();
  447. }
  448. if (!file_result.logged_messages.is_empty()) {
  449. print_modifiers({ FG_GRAY, FG_BOLD });
  450. #ifdef AK_OS_SERENITY
  451. outln(" ℹ Console output:");
  452. #else
  453. // This emoji has a second invisible byte after it. The one above does not
  454. outln(" ℹ️ Console output:");
  455. #endif
  456. print_modifiers({ CLEAR, FG_GRAY });
  457. for (auto& message : file_result.logged_messages)
  458. outln(" {}", message);
  459. }
  460. if (file_result.error.has_value()) {
  461. auto test_error = file_result.error.value();
  462. print_modifiers({ FG_RED });
  463. #ifdef AK_OS_SERENITY
  464. outln(" ❌ The file failed to parse");
  465. #else
  466. // No invisible byte here, but the spacing still needs to be altered on the host
  467. outln(" ❌ The file failed to parse");
  468. #endif
  469. outln();
  470. print_modifiers({ FG_GRAY });
  471. for (auto& message : test_error.hint.split('\n', SplitBehavior::KeepEmpty)) {
  472. outln(" {}", message);
  473. }
  474. print_modifiers({ FG_RED });
  475. outln(" {}", test_error.error.to_deprecated_string());
  476. outln();
  477. return;
  478. }
  479. if (file_result.most_severe_test_result != Test::Result::Pass) {
  480. for (auto& suite : file_result.suites) {
  481. if (suite.most_severe_test_result == Test::Result::Pass)
  482. continue;
  483. bool failed = suite.most_severe_test_result == Test::Result::Fail;
  484. print_modifiers({ FG_GRAY, FG_BOLD });
  485. if (failed) {
  486. #ifdef AK_OS_SERENITY
  487. out(" ❌ Suite: ");
  488. #else
  489. // No invisible byte here, but the spacing still needs to be altered on the host
  490. out(" ❌ Suite: ");
  491. #endif
  492. } else {
  493. #ifdef AK_OS_SERENITY
  494. out(" ⚠ Suite: ");
  495. #else
  496. // This emoji has a second invisible byte after it. The one above does not
  497. out(" ⚠️ Suite: ");
  498. #endif
  499. }
  500. print_modifiers({ CLEAR, FG_GRAY });
  501. if (suite.name == TOP_LEVEL_TEST_NAME) {
  502. outln("<top-level>");
  503. } else {
  504. outln("{}", suite.name);
  505. }
  506. print_modifiers({ CLEAR });
  507. for (auto& test : suite.tests) {
  508. if (test.result == Test::Result::Pass)
  509. continue;
  510. print_modifiers({ FG_GRAY, FG_BOLD });
  511. out(" Test: ");
  512. if (test.result == Test::Result::Fail) {
  513. print_modifiers({ CLEAR, FG_RED });
  514. outln("{} (failed):", test.name);
  515. outln(" {}", test.details);
  516. } else {
  517. print_modifiers({ CLEAR, FG_ORANGE });
  518. outln("{} (skipped)", test.name);
  519. }
  520. print_modifiers({ CLEAR });
  521. }
  522. }
  523. }
  524. }
  525. }