JavaScriptTestRunnerMain.cpp 5.7 KB

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