JavaScriptTestRunnerMain.cpp 6.2 KB

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