JavaScriptTestRunnerMain.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <LibTest/JavaScriptTestRunner.h>
  10. #include <signal.h>
  11. #include <stdio.h>
  12. namespace Test {
  13. TestRunner* ::Test::TestRunner::s_the = nullptr;
  14. namespace JS {
  15. RefPtr<::JS::VM> g_vm;
  16. bool g_collect_on_every_allocation = false;
  17. bool g_run_bytecode = false;
  18. String g_currently_running_test;
  19. HashMap<String, FunctionWithLength> s_exposed_global_functions;
  20. Function<void()> g_main_hook;
  21. Function<NonnullOwnPtr<JS::Interpreter>()> g_create_interpreter_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_run_bytecode, "Use the bytecode interpreter", "run-bytecode", 'b');
  100. args_parser.add_option(JS::Bytecode::g_dump_bytecode, "Dump the bytecode", "dump-bytecode", 'd');
  101. args_parser.add_option(test_glob, "Only run tests matching the given glob", "filter", 'f', "glob");
  102. for (auto& entry : g_extra_args)
  103. args_parser.add_option(*entry.key, entry.value.get<0>().characters(), entry.value.get<1>().characters(), entry.value.get<2>());
  104. args_parser.add_positional_argument(specified_test_root, "Tests root directory", "path", Core::ArgsParser::Required::No);
  105. args_parser.add_positional_argument(common_path, "Path to tests-common.js", "common-path", Core::ArgsParser::Required::No);
  106. args_parser.parse(argc, argv);
  107. test_glob = String::formatted("*{}*", test_glob);
  108. if (getenv("DISABLE_DBG_OUTPUT")) {
  109. AK::set_debug_enabled(false);
  110. }
  111. if (JS::Bytecode::g_dump_bytecode && !g_run_bytecode) {
  112. warnln("--dump-bytecode can only be used when --run-bytecode is specified.");
  113. return 1;
  114. }
  115. String test_root;
  116. if (specified_test_root) {
  117. test_root = String { specified_test_root };
  118. } else {
  119. #ifdef __serenity__
  120. test_root = LexicalPath::join("/home/anon", String::formatted("{}-tests", program_name.split_view('-').last())).string();
  121. #else
  122. char* serenity_source_dir = getenv("SERENITY_SOURCE_DIR");
  123. if (!serenity_source_dir) {
  124. warnln("No test root given, {} requires the SERENITY_SOURCE_DIR environment variable to be set", g_program_name);
  125. return 1;
  126. }
  127. test_root = String::formatted("{}/{}", serenity_source_dir, g_test_root_fragment);
  128. common_path = String::formatted("{}/Userland/Libraries/LibJS/Tests/test-common.js", serenity_source_dir);
  129. #endif
  130. }
  131. if (!Core::File::is_directory(test_root)) {
  132. warnln("Test root is not a directory: {}", test_root);
  133. return 1;
  134. }
  135. if (common_path.is_empty()) {
  136. #ifdef __serenity__
  137. common_path = "/home/anon/js-tests/test-common.js";
  138. #else
  139. char* serenity_source_dir = getenv("SERENITY_SOURCE_DIR");
  140. if (!serenity_source_dir) {
  141. warnln("No test root given, {} requires the SERENITY_SOURCE_DIR environment variable to be set", g_program_name);
  142. return 1;
  143. }
  144. common_path = String::formatted("{}/Userland/Libraries/LibJS/Tests/test-common.js", serenity_source_dir);
  145. #endif
  146. }
  147. test_root = Core::File::real_path_for(test_root);
  148. common_path = Core::File::real_path_for(common_path);
  149. if (chdir(test_root.characters()) < 0) {
  150. auto saved_errno = errno;
  151. warnln("chdir failed: {}", strerror(saved_errno));
  152. return 1;
  153. }
  154. if (g_main_hook)
  155. g_main_hook();
  156. if (!g_vm) {
  157. g_vm = JS::VM::create();
  158. g_vm->enable_default_host_import_module_dynamically_hook();
  159. }
  160. Test::JS::TestRunner test_runner(test_root, common_path, print_times, print_progress, print_json);
  161. test_runner.run(test_glob);
  162. g_vm = nullptr;
  163. return test_runner.counts().tests_failed > 0 ? 1 : 0;
  164. }