JavaScriptTestRunnerMain.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibCore/ArgsParser.h>
  9. #include <LibFileSystem/FileSystem.h>
  10. #include <LibTest/JavaScriptTestRunner.h>
  11. #include <signal.h>
  12. #include <stdio.h>
  13. namespace Test {
  14. TestRunner* ::Test::TestRunner::s_the = nullptr;
  15. namespace JS {
  16. RefPtr<::JS::VM> g_vm;
  17. bool g_collect_on_every_allocation = false;
  18. DeprecatedString g_currently_running_test;
  19. HashMap<DeprecatedString, FunctionWithLength> s_exposed_global_functions;
  20. Function<void()> g_main_hook;
  21. HashMap<bool*, Tuple<DeprecatedString, DeprecatedString, char>> g_extra_args;
  22. IntermediateRunFileResult (*g_run_file)(DeprecatedString const&, JS::Realm&, JS::ExecutionContext&) = nullptr;
  23. DeprecatedString g_test_root;
  24. int g_test_argc;
  25. char** g_test_argv;
  26. } // namespace JS
  27. } // namespace Test
  28. using namespace Test::JS;
  29. static StringView g_program_name { "test-js"sv };
  30. static void handle_sigabrt(int)
  31. {
  32. dbgln("{}: SIGABRT received, cleaning up.", g_program_name);
  33. Test::cleanup();
  34. struct sigaction act;
  35. memset(&act, 0, sizeof(act));
  36. act.sa_flags = SA_NOCLDWAIT;
  37. act.sa_handler = SIG_DFL;
  38. int rc = sigaction(SIGABRT, &act, nullptr);
  39. if (rc < 0) {
  40. perror("sigaction");
  41. exit(1);
  42. }
  43. abort();
  44. }
  45. int main(int argc, char** argv)
  46. {
  47. Vector<StringView> arguments;
  48. arguments.ensure_capacity(argc);
  49. for (auto i = 0; i < argc; ++i)
  50. arguments.append({ argv[i], strlen(argv[i]) });
  51. g_test_argc = argc;
  52. g_test_argv = argv;
  53. auto program_name = LexicalPath::basename(argv[0]);
  54. g_program_name = program_name;
  55. struct sigaction act;
  56. memset(&act, 0, sizeof(act));
  57. act.sa_flags = SA_NOCLDWAIT;
  58. act.sa_handler = handle_sigabrt;
  59. int rc = sigaction(SIGABRT, &act, nullptr);
  60. if (rc < 0) {
  61. perror("sigaction");
  62. return 1;
  63. }
  64. #ifdef SIGINFO
  65. signal(SIGINFO, [](int) {
  66. static char buffer[4096];
  67. auto& counts = ::Test::TestRunner::the()->counts();
  68. int len = snprintf(buffer, sizeof(buffer), "Pass: %d, Fail: %d, Skip: %d\nCurrent test: %s\n", counts.tests_passed, counts.tests_failed, counts.tests_skipped, g_currently_running_test.characters());
  69. write(STDOUT_FILENO, buffer, len);
  70. });
  71. #endif
  72. bool print_times = false;
  73. bool print_progress =
  74. #ifdef AK_OS_SERENITY
  75. true; // Use OSC 9 to print progress
  76. #else
  77. false;
  78. #endif
  79. bool print_json = false;
  80. bool per_file = false;
  81. StringView specified_test_root;
  82. DeprecatedString common_path;
  83. DeprecatedString test_glob;
  84. Core::ArgsParser args_parser;
  85. args_parser.add_option(print_times, "Show duration of each test", "show-time", 't');
  86. args_parser.add_option(Core::ArgsParser::Option {
  87. .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
  88. .help_string = "Show progress with OSC 9 (true, false)",
  89. .long_name = "show-progress",
  90. .short_name = 'p',
  91. .accept_value = [&](StringView str) {
  92. if ("true"sv == str)
  93. print_progress = true;
  94. else if ("false"sv == str)
  95. print_progress = false;
  96. else
  97. return false;
  98. return true;
  99. },
  100. });
  101. args_parser.add_option(print_json, "Show results as JSON", "json", 'j');
  102. args_parser.add_option(per_file, "Show detailed per-file results as JSON (implies -j)", "per-file", 0);
  103. args_parser.add_option(g_collect_on_every_allocation, "Collect garbage after every allocation", "collect-often", 'g');
  104. args_parser.add_option(JS::Bytecode::g_dump_bytecode, "Dump the bytecode", "dump-bytecode", 'd');
  105. args_parser.add_option(test_glob, "Only run tests matching the given glob", "filter", 'f', "glob");
  106. for (auto& entry : g_extra_args)
  107. args_parser.add_option(*entry.key, entry.value.get<0>().characters(), entry.value.get<1>().characters(), entry.value.get<2>());
  108. args_parser.add_positional_argument(specified_test_root, "Tests root directory", "path", Core::ArgsParser::Required::No);
  109. args_parser.add_positional_argument(common_path, "Path to tests-common.js", "common-path", Core::ArgsParser::Required::No);
  110. args_parser.parse(arguments);
  111. if (per_file)
  112. print_json = true;
  113. test_glob = DeprecatedString::formatted("*{}*", test_glob);
  114. if (getenv("DISABLE_DBG_OUTPUT")) {
  115. AK::set_debug_enabled(false);
  116. }
  117. DeprecatedString test_root;
  118. if (!specified_test_root.is_empty()) {
  119. test_root = DeprecatedString { specified_test_root };
  120. } else {
  121. #ifdef AK_OS_SERENITY
  122. test_root = LexicalPath::join("/home/anon/Tests"sv, DeprecatedString::formatted("{}-tests", program_name.split_view('-').last())).string();
  123. #else
  124. char* serenity_source_dir = getenv("SERENITY_SOURCE_DIR");
  125. if (!serenity_source_dir) {
  126. warnln("No test root given, {} requires the SERENITY_SOURCE_DIR environment variable to be set", g_program_name);
  127. return 1;
  128. }
  129. test_root = DeprecatedString::formatted("{}/{}", serenity_source_dir, g_test_root_fragment);
  130. common_path = DeprecatedString::formatted("{}/Userland/Libraries/LibJS/Tests/test-common.js", serenity_source_dir);
  131. #endif
  132. }
  133. if (!FileSystem::is_directory(test_root)) {
  134. warnln("Test root is not a directory: {}", test_root);
  135. return 1;
  136. }
  137. if (common_path.is_empty()) {
  138. #ifdef AK_OS_SERENITY
  139. common_path = "/home/anon/Tests/js-tests/test-common.js";
  140. #else
  141. char* serenity_source_dir = getenv("SERENITY_SOURCE_DIR");
  142. if (!serenity_source_dir) {
  143. warnln("No test root given, {} requires the SERENITY_SOURCE_DIR environment variable to be set", g_program_name);
  144. return 1;
  145. }
  146. common_path = DeprecatedString::formatted("{}/Userland/Libraries/LibJS/Tests/test-common.js", serenity_source_dir);
  147. #endif
  148. }
  149. auto test_root_or_error = FileSystem::real_path(test_root);
  150. if (test_root_or_error.is_error()) {
  151. warnln("Failed to resolve test root: {}", test_root_or_error.error());
  152. return 1;
  153. }
  154. test_root = test_root_or_error.release_value().to_deprecated_string();
  155. auto common_path_or_error = FileSystem::real_path(common_path);
  156. if (common_path_or_error.is_error()) {
  157. warnln("Failed to resolve common path: {}", common_path_or_error.error());
  158. return 1;
  159. }
  160. common_path = common_path_or_error.release_value().to_deprecated_string();
  161. if (chdir(test_root.characters()) < 0) {
  162. auto saved_errno = errno;
  163. warnln("chdir failed: {}", strerror(saved_errno));
  164. return 1;
  165. }
  166. if (g_main_hook)
  167. g_main_hook();
  168. if (!g_vm) {
  169. g_vm = MUST(JS::VM::create());
  170. g_vm->enable_default_host_import_module_dynamically_hook();
  171. }
  172. Test::JS::TestRunner test_runner(test_root, common_path, print_times, print_progress, print_json, per_file);
  173. test_runner.run(test_glob);
  174. g_vm = nullptr;
  175. return test_runner.counts().tests_failed > 0 ? 1 : 0;
  176. }