test-test262.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteString.h>
  7. #include <AK/Format.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/JsonObject.h>
  10. #include <AK/JsonParser.h>
  11. #include <AK/LexicalPath.h>
  12. #include <AK/QuickSort.h>
  13. #include <AK/Vector.h>
  14. #include <LibCore/ArgsParser.h>
  15. #include <LibCore/Command.h>
  16. #include <LibCore/File.h>
  17. #include <LibFileSystem/FileSystem.h>
  18. #include <LibMain/Main.h>
  19. #include <LibTest/TestRunnerUtil.h>
  20. enum class TestResult {
  21. Passed,
  22. Failed,
  23. Skipped,
  24. MetadataError,
  25. HarnessError,
  26. TimeoutError,
  27. ProcessError,
  28. RunnerFailure,
  29. TodoError,
  30. };
  31. static StringView name_for_result(TestResult result)
  32. {
  33. switch (result) {
  34. case TestResult::Passed:
  35. return "PASSED"sv;
  36. case TestResult::Failed:
  37. return "FAILED"sv;
  38. case TestResult::Skipped:
  39. return "SKIPPED"sv;
  40. case TestResult::MetadataError:
  41. return "METADATA_ERROR"sv;
  42. case TestResult::HarnessError:
  43. return "HARNESS_ERROR"sv;
  44. case TestResult::TimeoutError:
  45. return "TIMEOUT_ERROR"sv;
  46. case TestResult::ProcessError:
  47. return "PROCESS_ERROR"sv;
  48. case TestResult::RunnerFailure:
  49. return "RUNNER_EXCEPTION"sv;
  50. case TestResult::TodoError:
  51. return "TODO_ERROR"sv;
  52. }
  53. VERIFY_NOT_REACHED();
  54. return ""sv;
  55. }
  56. static TestResult result_from_string(StringView string_view)
  57. {
  58. if (string_view == "passed"sv)
  59. return TestResult::Passed;
  60. if (string_view == "failed"sv)
  61. return TestResult::Failed;
  62. if (string_view == "skipped"sv)
  63. return TestResult::Skipped;
  64. if (string_view == "metadata_error"sv)
  65. return TestResult::MetadataError;
  66. if (string_view == "harness_error"sv)
  67. return TestResult::HarnessError;
  68. if (string_view == "timeout"sv)
  69. return TestResult::TimeoutError;
  70. if (string_view == "process_error"sv || string_view == "assert_fail"sv)
  71. return TestResult::ProcessError;
  72. if (string_view == "todo_error"sv)
  73. return TestResult::TodoError;
  74. return TestResult::RunnerFailure;
  75. }
  76. static StringView emoji_for_result(TestResult result)
  77. {
  78. switch (result) {
  79. case TestResult::Passed:
  80. return "✅"sv;
  81. case TestResult::Failed:
  82. return "❌"sv;
  83. case TestResult::Skipped:
  84. return "⚠"sv;
  85. case TestResult::MetadataError:
  86. return "📄"sv;
  87. case TestResult::HarnessError:
  88. return "⚙"sv;
  89. case TestResult::TimeoutError:
  90. return "💀"sv;
  91. case TestResult::ProcessError:
  92. return "💥"sv;
  93. case TestResult::RunnerFailure:
  94. return "🐍"sv;
  95. case TestResult::TodoError:
  96. return "📝"sv;
  97. }
  98. VERIFY_NOT_REACHED();
  99. return ""sv;
  100. }
  101. static constexpr StringView total_test_emoji = "🧪"sv;
  102. static ErrorOr<HashMap<size_t, TestResult>> run_test_files(Span<ByteString> files, size_t offset, StringView command, char const* const arguments[])
  103. {
  104. HashMap<size_t, TestResult> results {};
  105. TRY(results.try_ensure_capacity(files.size()));
  106. size_t test_index = 0;
  107. auto fail_all_after = [&] {
  108. for (; test_index < files.size(); ++test_index)
  109. results.set(offset + test_index, TestResult::RunnerFailure);
  110. };
  111. while (test_index < files.size()) {
  112. auto runner_process_or_error = Core::Command::create(command, arguments);
  113. if (runner_process_or_error.is_error()) {
  114. fail_all_after();
  115. return results;
  116. }
  117. auto& runner_process = runner_process_or_error.value();
  118. if (auto maybe_error = runner_process->write_lines(files.slice(test_index)); maybe_error.is_error()) {
  119. warnln("Runner process failed writing writing file input: {}", maybe_error.error());
  120. fail_all_after();
  121. return results;
  122. }
  123. auto output_or_error = runner_process->read_all();
  124. ByteString output;
  125. if (output_or_error.is_error())
  126. warnln("Got error: {} while reading runner output", output_or_error.error());
  127. else
  128. output = ByteString(output_or_error.release_value().standard_error.bytes(), Chomp);
  129. auto status_or_error = runner_process->status();
  130. bool failed = false;
  131. if (!status_or_error.is_error()) {
  132. VERIFY(status_or_error.value() != Core::Command::ProcessResult::Running);
  133. failed = status_or_error.value() != Core::Command::ProcessResult::DoneWithZeroExitCode;
  134. }
  135. for (StringView line : output.split_view('\n')) {
  136. if (!line.starts_with("RESULT "sv))
  137. break;
  138. auto test_for_line = test_index;
  139. ++test_index;
  140. if (test_for_line >= files.size())
  141. break;
  142. line = line.substring_view(7).trim("\n\0 "sv);
  143. JsonParser parser { line };
  144. TestResult result = TestResult::RunnerFailure;
  145. auto result_object_or_error = parser.parse();
  146. if (!result_object_or_error.is_error() && result_object_or_error.value().is_object()) {
  147. auto& result_object = result_object_or_error.value().as_object();
  148. if (auto result_string = result_object.get_byte_string("result"sv); result_string.has_value()) {
  149. auto const& view = result_string.value();
  150. // Timeout and assert fail already are the result of the stopping test
  151. if (view == "timeout"sv || view == "assert_fail"sv) {
  152. failed = false;
  153. }
  154. result = result_from_string(view);
  155. }
  156. }
  157. results.set(test_for_line + offset, result);
  158. }
  159. if (failed) {
  160. TestResult result = TestResult::ProcessError;
  161. if (!status_or_error.is_error() && status_or_error.value() == Core::Command::ProcessResult::FailedFromTimeout) {
  162. result = TestResult::TimeoutError;
  163. }
  164. // assume the last test failed, if by SIGALRM signal it's a timeout
  165. results.set(test_index + offset, result);
  166. ++test_index;
  167. }
  168. }
  169. return results;
  170. }
  171. void write_per_file(HashMap<size_t, TestResult> const& result_map, Vector<ByteString> const& paths, StringView per_file_name, double time_taken_in_ms);
  172. ErrorOr<int> serenity_main(Main::Arguments arguments)
  173. {
  174. size_t batch_size = 50;
  175. StringView per_file_location;
  176. StringView pass_through_parameters;
  177. StringView runner_command = "test262-runner"sv;
  178. StringView test_directory;
  179. bool dont_print_progress = false;
  180. bool dont_disable_core_dump = false;
  181. Core::ArgsParser args_parser;
  182. args_parser.add_positional_argument(test_directory, "Directory to search for tests", "tests");
  183. args_parser.add_option(per_file_location, "Output a per-file containing all results", "per-file", 'o', "filename");
  184. args_parser.add_option(batch_size, "Size of batches send to runner at once", "batch-size", 'b', "batch size");
  185. args_parser.add_option(runner_command, "Command to run", "runner-command", 'r', "command");
  186. args_parser.add_option(pass_through_parameters, "Parameters to pass through to the runner, will split on spaces", "pass-through", 'p', "parameters");
  187. args_parser.add_option(dont_print_progress, "Hide progress information", "quiet", 'q');
  188. args_parser.add_option(dont_disable_core_dump, "Enabled core dumps for runner (i.e. don't pass --disable-core-dump)", "enable-core-dumps");
  189. args_parser.parse(arguments);
  190. // Normalize the path to ensure filenames are consistent
  191. Vector<ByteString> paths;
  192. if (!FileSystem::is_directory(test_directory)) {
  193. paths.append(test_directory);
  194. } else {
  195. Test::iterate_directory_recursively(LexicalPath::canonicalized_path(test_directory), [&](ByteString const& file_path) {
  196. if (file_path.contains("_FIXTURE"sv))
  197. return;
  198. // FIXME: Add ignored file set
  199. paths.append(file_path);
  200. });
  201. quick_sort(paths);
  202. }
  203. outln("Found {} tests", paths.size());
  204. auto parameters = pass_through_parameters.split_view(' ');
  205. Vector<ByteString> args;
  206. args.ensure_capacity(parameters.size() + 2);
  207. args.append(runner_command);
  208. if (!dont_disable_core_dump)
  209. args.append("--disable-core-dump"sv);
  210. for (auto parameter : parameters)
  211. args.append(parameter);
  212. Vector<char const*> raw_args;
  213. raw_args.ensure_capacity(args.size() + 1);
  214. for (auto& arg : args)
  215. raw_args.append(arg.characters());
  216. raw_args.append(nullptr);
  217. dbgln("test262 runner command: {}", args);
  218. HashMap<size_t, TestResult> results;
  219. Array<size_t, 9> result_counts {};
  220. static_assert(result_counts.size() == static_cast<size_t>(TestResult::TodoError) + 1u);
  221. size_t index = 0;
  222. double start_time = Test::get_time_in_ms();
  223. auto print_progress = [&] {
  224. if (!dont_print_progress) {
  225. warn("\033]9;{};{};\033\\", index, paths.size());
  226. double percentage_done = (100. * index) / paths.size();
  227. warn("{:04.2f}% {:3.1f}s ", percentage_done, (Test::get_time_in_ms() - start_time) / 1000.);
  228. for (size_t i = 0; i < result_counts.size(); ++i) {
  229. auto result_type = static_cast<TestResult>(i);
  230. warn("{} {} ", emoji_for_result(result_type), result_counts[i]);
  231. }
  232. warn("\r");
  233. }
  234. };
  235. while (index < paths.size()) {
  236. print_progress();
  237. auto this_batch_size = min(batch_size, paths.size() - index);
  238. auto batch_results = TRY(run_test_files(paths.span().slice(index, this_batch_size), index, args[0], raw_args.data()));
  239. TRY(results.try_ensure_capacity(results.size() + batch_results.size()));
  240. for (auto& [key, value] : batch_results) {
  241. results.set(key, value);
  242. ++result_counts[static_cast<size_t>(value)];
  243. }
  244. index += this_batch_size;
  245. }
  246. double time_taken_in_ms = Test::get_time_in_ms() - start_time;
  247. print_progress();
  248. if (!dont_print_progress)
  249. warn("\n\033]9;-1;\033\\");
  250. outln("Took {} seconds", time_taken_in_ms / 1000.);
  251. outln("{}: {}", total_test_emoji, paths.size());
  252. for (size_t i = 0; i < result_counts.size(); ++i) {
  253. auto result_type = static_cast<TestResult>(i);
  254. outln("{}: {} ({:3.2f}%)", emoji_for_result(result_type), result_counts[i], 100. * static_cast<double>(result_counts[i]) / paths.size());
  255. }
  256. if (!per_file_location.is_empty())
  257. write_per_file(results, paths, per_file_location, time_taken_in_ms);
  258. return 0;
  259. }
  260. void write_per_file(HashMap<size_t, TestResult> const& result_map, Vector<ByteString> const& paths, StringView per_file_name, double time_taken_in_ms)
  261. {
  262. auto file_or_error = Core::File::open(per_file_name, Core::File::OpenMode::Write);
  263. if (file_or_error.is_error()) {
  264. warnln("Failed to open per file for writing at {}: {}", per_file_name, file_or_error.error().string_literal());
  265. return;
  266. }
  267. auto& file = file_or_error.value();
  268. JsonObject result_object;
  269. for (auto& [test, value] : result_map)
  270. result_object.set(paths[test], name_for_result(value));
  271. JsonObject complete_results {};
  272. complete_results.set("duration", time_taken_in_ms / 1000.);
  273. complete_results.set("results", result_object);
  274. if (file->write_until_depleted(complete_results.to_byte_string()).is_error())
  275. warnln("Failed to write per-file");
  276. file->close();
  277. }