JavaScriptTestRunnerMain.cpp 6.3 KB

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