2021-05-18 11:15:12 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
|
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2025-05-30 05:50:55 -03:00
|
|
|
#include <AK/LexicalPath.h>
|
2021-10-26 16:06:35 -03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2025-05-30 05:50:55 -03:00
|
|
|
#include <LibCore/Environment.h>
|
|
|
|
|
#include <LibCore/System.h>
|
2023-03-21 12:35:30 -03:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2026-04-13 06:54:04 -03:00
|
|
|
#include <LibJS/Bytecode/Debug.h>
|
2021-05-18 11:15:12 -03:00
|
|
|
#include <LibTest/JavaScriptTestRunner.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2021-06-27 15:57:35 -03:00
|
|
|
namespace Test {
|
|
|
|
|
|
|
|
|
|
TestRunner* ::Test::TestRunner::s_the = nullptr;
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
2021-05-18 11:15:12 -03:00
|
|
|
|
2026-01-18 14:42:26 -03:00
|
|
|
GC_DEFINE_ALLOCATOR(TestRunnerGlobalObject);
|
|
|
|
|
|
2021-05-18 11:15:12 -03:00
|
|
|
RefPtr<::JS::VM> g_vm;
|
|
|
|
|
bool g_collect_on_every_allocation = false;
|
2023-12-16 11:19:34 -03:00
|
|
|
ByteString g_currently_running_test;
|
2025-08-02 20:27:29 -03:00
|
|
|
HashMap<Utf16String, FunctionWithLength> s_exposed_global_functions;
|
2021-05-18 11:15:12 -03:00
|
|
|
Function<void()> g_main_hook;
|
2023-12-16 11:19:34 -03:00
|
|
|
HashMap<bool*, Tuple<ByteString, ByteString, char>> g_extra_args;
|
|
|
|
|
IntermediateRunFileResult (*g_run_file)(ByteString const&, JS::Realm&, JS::ExecutionContext&) = nullptr;
|
|
|
|
|
ByteString g_test_root;
|
2021-05-18 11:15:12 -03:00
|
|
|
int g_test_argc;
|
|
|
|
|
char** g_test_argv;
|
|
|
|
|
|
2021-06-27 15:57:35 -03:00
|
|
|
} // namespace JS
|
|
|
|
|
} // namespace Test
|
2021-05-18 11:15:12 -03:00
|
|
|
|
|
|
|
|
using namespace Test::JS;
|
|
|
|
|
|
|
|
|
|
static StringView g_program_name { "test-js"sv };
|
|
|
|
|
|
2025-05-30 05:50:55 -03:00
|
|
|
static bool set_abort_action(void (*function)(int))
|
2021-05-18 11:15:12 -03:00
|
|
|
{
|
2025-05-30 05:50:55 -03:00
|
|
|
#if defined(AK_OS_WINDOWS)
|
|
|
|
|
auto rc = signal(SIGABRT, function);
|
|
|
|
|
if (rc == SIG_ERR) {
|
|
|
|
|
perror("sigaction");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
#else
|
2021-05-26 15:33:58 -03:00
|
|
|
struct sigaction act;
|
|
|
|
|
memset(&act, 0, sizeof(act));
|
2023-09-03 17:13:37 -03:00
|
|
|
act.sa_flags = 0;
|
2025-05-30 05:50:55 -03:00
|
|
|
act.sa_handler = function;
|
2021-05-26 15:33:58 -03:00
|
|
|
int rc = sigaction(SIGABRT, &act, nullptr);
|
|
|
|
|
if (rc < 0) {
|
|
|
|
|
perror("sigaction");
|
2025-05-30 05:50:55 -03:00
|
|
|
return false;
|
2021-05-26 15:33:58 -03:00
|
|
|
}
|
2025-05-30 05:50:55 -03:00
|
|
|
return true;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void handle_sigabrt(int)
|
|
|
|
|
{
|
|
|
|
|
dbgln("{}: SIGABRT received, cleaning up.", g_program_name);
|
|
|
|
|
Test::cleanup();
|
|
|
|
|
if (!set_abort_action(SIG_DFL))
|
|
|
|
|
exit(1);
|
2021-05-26 15:33:58 -03:00
|
|
|
abort();
|
2021-05-18 11:15:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
|
{
|
2023-02-21 08:44:41 -03:00
|
|
|
Vector<StringView> arguments;
|
|
|
|
|
arguments.ensure_capacity(argc);
|
|
|
|
|
for (auto i = 0; i < argc; ++i)
|
|
|
|
|
arguments.append({ argv[i], strlen(argv[i]) });
|
|
|
|
|
|
2021-05-18 11:15:12 -03:00
|
|
|
g_test_argc = argc;
|
|
|
|
|
g_test_argv = argv;
|
2021-06-29 11:46:16 -03:00
|
|
|
auto program_name = LexicalPath::basename(argv[0]);
|
2021-05-18 11:15:12 -03:00
|
|
|
g_program_name = program_name;
|
|
|
|
|
|
2025-05-30 05:50:55 -03:00
|
|
|
if (!set_abort_action(handle_sigabrt))
|
2021-05-18 11:15:12 -03:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
#ifdef SIGINFO
|
|
|
|
|
signal(SIGINFO, [](int) {
|
|
|
|
|
static char buffer[4096];
|
2021-06-27 15:57:35 -03:00
|
|
|
auto& counts = ::Test::TestRunner::the()->counts();
|
2021-05-18 11:15:12 -03:00
|
|
|
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());
|
|
|
|
|
write(STDOUT_FILENO, buffer, len);
|
|
|
|
|
});
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
bool print_times = false;
|
2024-06-09 07:10:28 -03:00
|
|
|
bool print_progress = false;
|
2021-06-04 04:32:43 -03:00
|
|
|
bool print_json = false;
|
2022-03-08 00:39:41 -03:00
|
|
|
bool per_file = false;
|
2026-04-29 06:59:13 -03:00
|
|
|
bool print_each_test = false;
|
2023-02-28 17:41:43 -03:00
|
|
|
StringView specified_test_root;
|
2023-12-16 11:19:34 -03:00
|
|
|
ByteString common_path;
|
2025-02-04 16:23:11 -03:00
|
|
|
Vector<ByteString> test_globs;
|
2021-05-18 11:15:12 -03:00
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
|
args_parser.add_option(print_times, "Show duration of each test", "show-time", 't');
|
|
|
|
|
args_parser.add_option(Core::ArgsParser::Option {
|
2022-07-12 17:13:38 -03:00
|
|
|
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
|
2021-05-18 11:15:12 -03:00
|
|
|
.help_string = "Show progress with OSC 9 (true, false)",
|
|
|
|
|
.long_name = "show-progress",
|
|
|
|
|
.short_name = 'p',
|
2023-02-21 08:44:41 -03:00
|
|
|
.accept_value = [&](StringView str) {
|
2021-07-04 06:08:46 -03:00
|
|
|
if ("true"sv == str)
|
2021-05-18 11:15:12 -03:00
|
|
|
print_progress = true;
|
2021-07-04 06:08:46 -03:00
|
|
|
else if ("false"sv == str)
|
2021-05-18 11:15:12 -03:00
|
|
|
print_progress = false;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-06-22 10:59:18 -03:00
|
|
|
|
2021-06-04 04:32:43 -03:00
|
|
|
args_parser.add_option(print_json, "Show results as JSON", "json", 'j');
|
2024-04-20 17:34:56 -03:00
|
|
|
args_parser.add_option(per_file, "Show detailed per-file results as JSON (implies -j)", "per-file");
|
2026-04-29 06:59:13 -03:00
|
|
|
args_parser.add_option(print_each_test, "Print each test file before running it", "verbose", 'v');
|
2021-05-18 11:15:12 -03:00
|
|
|
args_parser.add_option(g_collect_on_every_allocation, "Collect garbage after every allocation", "collect-often", 'g');
|
2021-10-24 08:34:46 -03:00
|
|
|
args_parser.add_option(JS::Bytecode::g_dump_bytecode, "Dump the bytecode", "dump-bytecode", 'd');
|
2025-02-04 16:23:11 -03:00
|
|
|
args_parser.add_option(test_globs, "Only run tests matching the given glob", "filter", 'f', "glob");
|
2021-05-30 03:05:44 -03:00
|
|
|
for (auto& entry : g_extra_args)
|
|
|
|
|
args_parser.add_option(*entry.key, entry.value.get<0>().characters(), entry.value.get<1>().characters(), entry.value.get<2>());
|
2021-05-18 11:15:12 -03:00
|
|
|
args_parser.add_positional_argument(specified_test_root, "Tests root directory", "path", Core::ArgsParser::Required::No);
|
|
|
|
|
args_parser.add_positional_argument(common_path, "Path to tests-common.js", "common-path", Core::ArgsParser::Required::No);
|
2023-02-21 08:44:41 -03:00
|
|
|
args_parser.parse(arguments);
|
2021-05-18 11:15:12 -03:00
|
|
|
|
2022-03-08 00:39:41 -03:00
|
|
|
if (per_file)
|
|
|
|
|
print_json = true;
|
|
|
|
|
|
2025-02-04 16:23:11 -03:00
|
|
|
for (auto& glob : test_globs)
|
|
|
|
|
glob = ByteString::formatted("*{}*", glob);
|
|
|
|
|
if (test_globs.is_empty())
|
|
|
|
|
test_globs.append("*"sv);
|
2021-05-18 11:15:12 -03:00
|
|
|
|
2025-05-30 05:50:55 -03:00
|
|
|
if (Core::Environment::has("DISABLE_DBG_OUTPUT"sv)) {
|
2021-05-18 11:15:12 -03:00
|
|
|
AK::set_debug_enabled(false);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-16 11:19:34 -03:00
|
|
|
ByteString test_root;
|
2021-05-18 11:15:12 -03:00
|
|
|
|
2023-02-28 17:41:43 -03:00
|
|
|
if (!specified_test_root.is_empty()) {
|
2023-12-16 11:19:34 -03:00
|
|
|
test_root = ByteString { specified_test_root };
|
2021-05-18 11:15:12 -03:00
|
|
|
} else {
|
2025-05-30 05:50:55 -03:00
|
|
|
auto ladybird_source_dir = Core::Environment::get("LADYBIRD_SOURCE_DIR"sv);
|
|
|
|
|
if (!ladybird_source_dir.has_value()) {
|
2024-06-03 09:40:19 -03:00
|
|
|
warnln("No test root given, {} requires the LADYBIRD_SOURCE_DIR environment variable to be set", g_program_name);
|
2021-05-18 11:15:12 -03:00
|
|
|
return 1;
|
|
|
|
|
}
|
2025-05-30 05:50:55 -03:00
|
|
|
test_root = LexicalPath::join(*ladybird_source_dir, g_test_root_fragment).string();
|
2026-01-22 07:58:59 -03:00
|
|
|
common_path = LexicalPath::join(*ladybird_source_dir, "Tests"sv, "LibJS"sv, "Runtime"sv, "test-common.js"sv).string();
|
2021-05-18 11:15:12 -03:00
|
|
|
}
|
2023-03-21 12:35:30 -03:00
|
|
|
if (!FileSystem::is_directory(test_root)) {
|
2021-05-18 11:15:12 -03:00
|
|
|
warnln("Test root is not a directory: {}", test_root);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (common_path.is_empty()) {
|
2025-05-30 05:50:55 -03:00
|
|
|
auto ladybird_source_dir = Core::Environment::get("LADYBIRD_SOURCE_DIR"sv);
|
|
|
|
|
if (!ladybird_source_dir.has_value()) {
|
2024-06-03 09:40:19 -03:00
|
|
|
warnln("No test root given, {} requires the LADYBIRD_SOURCE_DIR environment variable to be set", g_program_name);
|
2021-05-18 11:15:12 -03:00
|
|
|
return 1;
|
|
|
|
|
}
|
2026-01-22 07:58:59 -03:00
|
|
|
common_path = LexicalPath::join(*ladybird_source_dir, "Tests"sv, "LibJS"sv, "Runtime"sv, "test-common.js"sv).string();
|
2021-05-18 11:15:12 -03:00
|
|
|
}
|
|
|
|
|
|
2023-03-24 06:57:53 -03:00
|
|
|
auto test_root_or_error = FileSystem::real_path(test_root);
|
|
|
|
|
if (test_root_or_error.is_error()) {
|
|
|
|
|
warnln("Failed to resolve test root: {}", test_root_or_error.error());
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2024-01-15 13:23:24 -03:00
|
|
|
test_root = test_root_or_error.release_value();
|
2023-03-24 06:57:53 -03:00
|
|
|
|
|
|
|
|
auto common_path_or_error = FileSystem::real_path(common_path);
|
|
|
|
|
if (common_path_or_error.is_error()) {
|
|
|
|
|
warnln("Failed to resolve common path: {}", common_path_or_error.error());
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2024-01-15 13:23:24 -03:00
|
|
|
common_path = common_path_or_error.release_value();
|
2021-05-30 03:05:44 -03:00
|
|
|
|
2025-05-30 05:50:55 -03:00
|
|
|
if (auto err = Core::System::chdir(test_root); err.is_error()) {
|
|
|
|
|
warnln("chdir failed: {}", err.error());
|
2021-05-18 11:15:12 -03:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (g_main_hook)
|
|
|
|
|
g_main_hook();
|
|
|
|
|
|
2022-01-18 15:39:36 -03:00
|
|
|
if (!g_vm) {
|
2025-04-24 00:46:51 -03:00
|
|
|
g_vm = JS::VM::create();
|
2023-12-02 18:56:47 -03:00
|
|
|
g_vm->set_dynamic_imports_allowed(true);
|
2025-09-22 20:46:22 -03:00
|
|
|
|
|
|
|
|
// Configure the test VM to support additional import attributes
|
|
|
|
|
// This allows tests to use import attributes beyond just "type"
|
|
|
|
|
Test::JS::g_vm->host_get_supported_import_attributes = []() -> Vector<Utf16String> {
|
|
|
|
|
return {
|
|
|
|
|
"type"_utf16,
|
|
|
|
|
"key"_utf16, // Used in modules/import-with-attributes.mjs test
|
|
|
|
|
"key1"_utf16, // Used in modules/basic-modules.js
|
|
|
|
|
"key2"_utf16, // Used in modules/import-with-attributes.mjs test
|
|
|
|
|
"default"_utf16, // Used in modules/import-with-attributes.mjs test
|
|
|
|
|
};
|
|
|
|
|
};
|
2022-01-18 15:39:36 -03:00
|
|
|
}
|
2021-05-18 11:15:12 -03:00
|
|
|
|
2026-04-29 06:59:13 -03:00
|
|
|
Test::JS::TestRunner test_runner(test_root, common_path, print_times, print_progress, print_json, per_file, print_each_test);
|
2025-02-04 16:23:11 -03:00
|
|
|
test_runner.run(test_globs);
|
2021-05-18 11:15:12 -03:00
|
|
|
|
|
|
|
|
g_vm = nullptr;
|
|
|
|
|
|
|
|
|
|
return test_runner.counts().tests_failed > 0 ? 1 : 0;
|
|
|
|
|
}
|