1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-18 13:22:55 -05:00
denoland-deno/tests/integration/js_unit_tests.rs

228 lines
5.1 KiB
Rust
Raw Normal View History

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::io::BufRead;
use std::io::BufReader;
use std::time::Duration;
use std::time::Instant;
use test_util as util;
util::unit_test_factory!(
js_unit_test,
refactor: split integration tests from CLI (part 1) (#22308) This PR separates integration tests from CLI tests into a new project named `cli_tests`. This is a prerequisite for an integration test runner that can work with either the CLI binary in the current project, or one that is built ahead of time. ## Background Rust does not have the concept of artifact dependencies yet (https://github.com/rust-lang/cargo/issues/9096). Because of this, the only way we can ensure a binary is built before running associated tests is by hanging tests off the crate with the binary itself. Unfortunately this means that to run those tests, you _must_ build the binary and in the case of the deno executable that might be a 10 minute wait in release mode. ## Implementation To allow for tests to run with and without the requirement that the binary is up-to-date, we split the integration tests into a project of their own. As these tests would not require the binary to build itself before being run as-is, we add a stub integration `[[test]]` target in the `cli` project that invokes these tests using `cargo test`. The stub test runner we add has `harness = false` so that we can get access to a `main` function. This `main` function's sole job is to `execvp` the command `cargo test -p deno_cli`, effectively "calling" another cargo target. This ensures that the deno executable is always correctly rebuilt before running the stub test runner from `cli`, and gets us closer to be able to run the entire integration test suite on arbitrary deno executables (and therefore split the build into multiple phases). The new `cli_tests` project lives within `cli` to avoid a large PR. In later PRs, the test data will be split from the `cli` project. As there are a few thousand files, it'll be better to do this as a completely separate PR to avoid noise.
2024-02-09 15:33:05 -05:00
"../tests/unit",
"*.ts",
[
abort_controller_test,
blob_test,
body_test,
broadcast_channel_test,
build_test,
cache_api_test,
chmod_test,
chown_test,
command_test,
console_test,
copy_file_test,
custom_event_test,
cron_test,
dir_test,
dom_exception_test,
error_stack_test,
error_test,
esnext_test,
event_source_test,
event_target_test,
event_test,
fetch_test,
ffi_test,
file_test,
filereader_test,
files_test,
fs_events_test,
get_random_values_test,
globals_test,
headers_test,
http_test,
2024-01-22 06:08:01 -05:00
image_bitmap_test,
image_data_test,
internals_test,
intl_test,
jupyter_test,
kv_test,
kv_queue_test_no_db_close,
kv_queue_test,
kv_queue_undelivered_test,
link_test,
make_temp_test,
message_channel_test,
mkdir_test,
navigator_test,
net_test,
network_interfaces_test,
os_test,
ops_test,
path_from_url_test,
performance_test,
permissions_test,
process_test,
progressevent_test,
promise_hooks_test,
read_dir_test,
read_file_test,
read_link_test,
read_text_file_test,
real_path_test,
ref_unref_test,
remove_test,
rename_test,
request_test,
response_test,
serve_test,
signal_test,
stat_test,
stdio_test,
streams_test,
structured_clone_test,
symbol_test,
symlink_test,
test_util,
testing_test,
text_encoding_test,
timers_test,
tls_test,
tls_sni_test,
truncate_test,
tty_color_test,
tty_test,
umask_test,
url_search_params_test,
url_test,
urlpattern_test,
utime_test,
version_test,
wasm_test,
webcrypto_test,
webgpu_test,
websocket_test,
webstorage_test,
worker_permissions_test,
worker_test,
write_file_test,
write_text_file_test,
]
);
fn js_unit_test(test: String) {
let _g = util::http_server();
let mut deno = util::deno_cmd()
.current_dir(util::root_path())
.arg("test")
.arg("--config")
.arg(util::deno_config_path())
.arg("--no-lock")
// TODO(bartlomieju): would be better if we could apply this unstable
// flag to particular files, but there's many of them that rely on unstable
// net APIs (`reusePort` in `listen` and `listenTls`; `listenDatagram`)
.arg("--unstable-net")
.arg("--location=http://127.0.0.1:4545/")
.arg("--no-prompt");
if test == "broadcast_channel_test" {
deno = deno.arg("--unstable-broadcast-channel");
}
if test == "cron_test" {
deno = deno.arg("--unstable-cron");
}
if test.contains("kv_") {
deno = deno.arg("--unstable-kv");
}
if test == "worker_permissions_test" || test == "worker_test" {
deno = deno.arg("--unstable-worker-options");
}
// Some tests require the root CA cert file to be loaded.
if test == "websocket_test" {
deno = deno.arg(format!(
"--cert={}",
util::testdata_path()
.join("tls")
.join("RootCA.pem")
.to_string_lossy()
));
};
if test == "tls_sni_test" {
// TODO(lucacasonato): fix the SNI in the certs so that this is not needed
deno = deno.arg("--unsafely-ignore-certificate-errors");
}
let mut deno = deno
.arg("-A")
.arg(util::tests_path().join("unit").join(format!("{test}.ts")))
.piped_output()
.spawn()
.expect("failed to spawn script");
let now = Instant::now();
let stdout = deno.stdout.take().unwrap();
let test_name = test.clone();
let stdout = std::thread::spawn(move || {
let reader = BufReader::new(stdout);
for line in reader.lines() {
if let Ok(line) = line {
println!("[{test_name} {:0>6.2}] {line}", now.elapsed().as_secs_f32());
} else {
break;
}
}
});
let now = Instant::now();
let stderr = deno.stderr.take().unwrap();
let test_name = test.clone();
let stderr = std::thread::spawn(move || {
let reader = BufReader::new(stderr);
for line in reader.lines() {
if let Ok(line) = line {
eprintln!("[{test_name} {:0>6.2}] {line}", now.elapsed().as_secs_f32());
} else {
break;
}
}
});
const PER_TEST_TIMEOUT: Duration = Duration::from_secs(3 * 60);
let now = Instant::now();
let status = loop {
if now.elapsed() > PER_TEST_TIMEOUT {
// Last-ditch kill
_ = deno.kill();
panic!("Test {test} failed to complete in time");
}
if let Some(status) = deno
.try_wait()
.expect("failed to wait for the child process")
{
break status;
}
std::thread::sleep(Duration::from_millis(100));
};
#[cfg(unix)]
assert_eq!(
std::os::unix::process::ExitStatusExt::signal(&status),
None,
"Deno should not have died with a signal"
);
assert_eq!(Some(0), status.code(), "Deno should have exited cleanly");
stdout.join().unwrap();
stderr.join().unwrap();
assert!(status.success());
}