JavaScriptTestRunnerMain.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <LibTest/JavaScriptTestRunner.h>
  9. #include <signal.h>
  10. #include <stdio.h>
  11. namespace Test {
  12. TestRunner* ::Test::TestRunner::s_the = nullptr;
  13. namespace JS {
  14. RefPtr<::JS::VM> g_vm;
  15. bool g_collect_on_every_allocation = false;
  16. #ifdef JS_TRACK_ZOMBIE_CELLS
  17. bool g_zombify_dead_cells = false;
  18. #endif
  19. bool g_run_bytecode = false;
  20. String g_currently_running_test;
  21. HashMap<String, FunctionWithLength> s_exposed_global_functions;
  22. Function<void()> g_main_hook;
  23. HashMap<bool*, Tuple<String, String, char>> g_extra_args;
  24. IntermediateRunFileResult (*g_run_file)(const String&, JS::Interpreter&) = nullptr;
  25. String g_test_root;
  26. int g_test_argc;
  27. char** g_test_argv;
  28. } // namespace JS
  29. } // namespace Test
  30. using namespace Test::JS;
  31. static StringView g_program_name { "test-js"sv };
  32. static void handle_sigabrt(int)
  33. {
  34. dbgln("{}: SIGABRT received, cleaning up.", g_program_name);
  35. Test::cleanup();
  36. struct sigaction act;
  37. memset(&act, 0, sizeof(act));
  38. act.sa_flags = SA_NOCLDWAIT;
  39. act.sa_handler = SIG_DFL;
  40. int rc = sigaction(SIGABRT, &act, nullptr);
  41. if (rc < 0) {
  42. perror("sigaction");
  43. exit(1);
  44. }
  45. abort();
  46. }
  47. int main(int argc, char** argv)
  48. {
  49. g_test_argc = argc;
  50. g_test_argv = argv;
  51. auto program_name = LexicalPath::basename(argv[0]);
  52. g_program_name = program_name;
  53. struct sigaction act;
  54. memset(&act, 0, sizeof(act));
  55. act.sa_flags = SA_NOCLDWAIT;
  56. act.sa_handler = handle_sigabrt;
  57. int rc = sigaction(SIGABRT, &act, nullptr);
  58. if (rc < 0) {
  59. perror("sigaction");
  60. return 1;
  61. }
  62. #ifdef SIGINFO
  63. signal(SIGINFO, [](int) {
  64. static char buffer[4096];
  65. auto& counts = ::Test::TestRunner::the()->counts();
  66. 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());
  67. write(STDOUT_FILENO, buffer, len);
  68. });
  69. #endif
  70. bool print_times = false;
  71. bool print_progress =
  72. #ifdef __serenity__
  73. true; // Use OSC 9 to print progress
  74. #else
  75. false;
  76. #endif
  77. bool print_json = false;
  78. const char* specified_test_root = nullptr;
  79. String common_path;
  80. String test_glob;
  81. Core::ArgsParser args_parser;
  82. args_parser.add_option(print_times, "Show duration of each test", "show-time", 't');
  83. args_parser.add_option(Core::ArgsParser::Option {
  84. .requires_argument = true,
  85. .help_string = "Show progress with OSC 9 (true, false)",
  86. .long_name = "show-progress",
  87. .short_name = 'p',
  88. .accept_value = [&](auto* str) {
  89. if ("true"sv == str)
  90. print_progress = true;
  91. else if ("false"sv == str)
  92. print_progress = false;
  93. else
  94. return false;
  95. return true;
  96. },
  97. });
  98. args_parser.add_option(print_json, "Show results as JSON", "json", 'j');
  99. args_parser.add_option(g_collect_on_every_allocation, "Collect garbage after every allocation", "collect-often", 'g');
  100. #ifdef JS_TRACK_ZOMBIE_CELLS
  101. args_parser.add_option(g_zombify_dead_cells, "Zombify dead cells (to catch missing GC marks)", "zombify-dead-cells", 'z');
  102. #endif
  103. args_parser.add_option(g_run_bytecode, "Use the bytecode interpreter", "run-bytecode", 'b');
  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(argc, argv);
  111. test_glob = String::formatted("*{}*", test_glob);
  112. if (getenv("DISABLE_DBG_OUTPUT")) {
  113. AK::set_debug_enabled(false);
  114. }
  115. if (JS::Bytecode::g_dump_bytecode && !g_run_bytecode) {
  116. warnln("--dump-bytecode can only be used when --run-bytecode is specified.");
  117. return 1;
  118. }
  119. String test_root;
  120. if (specified_test_root) {
  121. test_root = String { specified_test_root };
  122. } else {
  123. #ifdef __serenity__
  124. test_root = LexicalPath::join("/home/anon", String::formatted("{}-tests", program_name.split_view('-').last())).string();
  125. #else
  126. char* serenity_source_dir = getenv("SERENITY_SOURCE_DIR");
  127. if (!serenity_source_dir) {
  128. warnln("No test root given, {} requires the SERENITY_SOURCE_DIR environment variable to be set", g_program_name);
  129. return 1;
  130. }
  131. test_root = String::formatted("{}/{}", serenity_source_dir, g_test_root_fragment);
  132. common_path = String::formatted("{}/Userland/Libraries/LibJS/Tests/test-common.js", serenity_source_dir);
  133. #endif
  134. }
  135. if (!Core::File::is_directory(test_root)) {
  136. warnln("Test root is not a directory: {}", test_root);
  137. return 1;
  138. }
  139. if (common_path.is_empty()) {
  140. #ifdef __serenity__
  141. common_path = "/home/anon/js-tests/test-common.js";
  142. #else
  143. char* serenity_source_dir = getenv("SERENITY_SOURCE_DIR");
  144. if (!serenity_source_dir) {
  145. warnln("No test root given, {} requires the SERENITY_SOURCE_DIR environment variable to be set", g_program_name);
  146. return 1;
  147. }
  148. common_path = String::formatted("{}/Userland/Libraries/LibJS/Tests/test-common.js", serenity_source_dir);
  149. #endif
  150. }
  151. test_root = Core::File::real_path_for(test_root);
  152. common_path = Core::File::real_path_for(common_path);
  153. if (chdir(test_root.characters()) < 0) {
  154. auto saved_errno = errno;
  155. warnln("chdir failed: {}", strerror(saved_errno));
  156. return 1;
  157. }
  158. if (g_main_hook)
  159. g_main_hook();
  160. if (!g_vm)
  161. g_vm = JS::VM::create();
  162. Test::JS::TestRunner test_runner(test_root, common_path, print_times, print_progress, print_json);
  163. test_runner.run(test_glob);
  164. g_vm = nullptr;
  165. return test_runner.counts().tests_failed > 0 ? 1 : 0;
  166. }