test-js: Add a test helper to override the current time zone

This commit is contained in:
Timothy Flynn 2023-11-07 11:14:55 -05:00 committed by Andreas Kling
parent ee7ab5d053
commit 99233776e6

View file

@ -5,8 +5,11 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/System.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibTest/JavaScriptTestRunner.h>
#include <stdlib.h>
#include <time.h>
TEST_ROOT("Userland/Libraries/LibJS/Tests");
@ -88,6 +91,26 @@ TESTJS_GLOBAL_FUNCTION(detach_array_buffer, detachArrayBuffer)
return JS::js_null();
}
TESTJS_GLOBAL_FUNCTION(set_time_zone, setTimeZone)
{
auto current_time_zone = JS::js_null();
if (auto const* time_zone = getenv("TZ"))
current_time_zone = JS::PrimitiveString::create(vm, StringView { time_zone, strlen(time_zone) });
if (auto time_zone = vm.argument(0); time_zone.is_null()) {
if (auto result = Core::System::unsetenv("TZ"sv); result.is_error())
return vm.throw_completion<JS::InternalError>(MUST(String::formatted("Could not unset time zone: {}", result.error())));
} else {
if (auto result = Core::System::setenv("TZ"sv, TRY(time_zone.to_string(vm)), true); result.is_error())
return vm.throw_completion<JS::InternalError>(MUST(String::formatted("Could not set time zone: {}", result.error())));
}
tzset();
return current_time_zone;
}
TESTJS_RUN_FILE_FUNCTION(DeprecatedString const& test_file, JS::Realm& realm, JS::ExecutionContext&)
{
if (!test262_parser_tests)