JavaScriptTestRunnerMain.cpp 6.1 KB

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