2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-02-11 06:01:56 -05:00
|
|
|
|
2023-01-07 15:22:09 -05:00
|
|
|
use crate::args::CliOptions;
|
|
|
|
use crate::args::FilesConfig;
|
|
|
|
use crate::args::TestOptions;
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::TypeCheckMode;
|
2021-04-28 14:17:04 -04:00
|
|
|
use crate::colors;
|
2022-03-08 18:19:02 -05:00
|
|
|
use crate::display;
|
2021-04-28 14:17:04 -04:00
|
|
|
use crate::file_fetcher::File;
|
2021-12-16 05:45:41 -05:00
|
|
|
use crate::graph_util::graph_valid;
|
2021-08-17 06:08:39 -04:00
|
|
|
use crate::ops;
|
2021-09-24 11:10:42 -04:00
|
|
|
use crate::proc_state::ProcState;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::checksum;
|
|
|
|
use crate::util::file_watcher;
|
|
|
|
use crate::util::file_watcher::ResolutionResult;
|
|
|
|
use crate::util::fs::collect_specifiers;
|
|
|
|
use crate::util::path::get_extension;
|
|
|
|
use crate::util::path::is_supported_ext;
|
2022-11-21 08:36:26 -05:00
|
|
|
use crate::worker::create_main_worker_for_test_or_bench;
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::swc::common::comments::CommentKind;
|
|
|
|
use deno_ast::MediaType;
|
2022-05-20 16:40:55 -04:00
|
|
|
use deno_ast::SourceRangedForSpanned;
|
2021-07-22 07:34:29 -04:00
|
|
|
use deno_core::error::generic_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-04-16 13:51:12 -04:00
|
|
|
use deno_core::error::JsError;
|
2021-04-28 14:17:04 -04:00
|
|
|
use deno_core::futures::future;
|
|
|
|
use deno_core::futures::stream;
|
2021-05-26 11:47:33 -04:00
|
|
|
use deno_core::futures::FutureExt;
|
2021-04-28 14:17:04 -04:00
|
|
|
use deno_core::futures::StreamExt;
|
2022-05-04 17:01:51 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2022-04-11 12:27:17 -04:00
|
|
|
use deno_core::url::Url;
|
2021-04-28 14:17:04 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2022-09-02 16:53:23 -04:00
|
|
|
use deno_runtime::fmt_errors::format_js_error;
|
2022-04-26 19:00:04 -04:00
|
|
|
use deno_runtime::ops::io::Stdio;
|
|
|
|
use deno_runtime::ops::io::StdioPipe;
|
2021-04-28 14:17:04 -04:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2022-07-11 13:02:23 -04:00
|
|
|
use deno_runtime::tokio_util::run_local;
|
2022-07-15 13:09:22 -04:00
|
|
|
use indexmap::IndexMap;
|
2021-08-23 10:03:57 -04:00
|
|
|
use log::Level;
|
2021-07-05 21:20:33 -04:00
|
|
|
use rand::rngs::SmallRng;
|
|
|
|
use rand::seq::SliceRandom;
|
|
|
|
use rand::SeedableRng;
|
2021-05-10 19:54:39 -04:00
|
|
|
use regex::Regex;
|
2021-04-28 14:17:04 -04:00
|
|
|
use serde::Deserialize;
|
2023-01-10 10:28:10 -05:00
|
|
|
use std::cell::RefCell;
|
2022-05-09 05:44:50 -04:00
|
|
|
use std::collections::BTreeMap;
|
2021-08-26 15:21:58 -04:00
|
|
|
use std::collections::HashSet;
|
2022-07-01 09:28:06 -04:00
|
|
|
use std::fmt::Write as _;
|
2022-04-26 19:00:04 -04:00
|
|
|
use std::io::Read;
|
2021-10-11 09:45:02 -04:00
|
|
|
use std::io::Write;
|
2021-08-23 06:35:38 -04:00
|
|
|
use std::num::NonZeroUsize;
|
2022-11-28 17:28:54 -05:00
|
|
|
use std::path::Path;
|
2021-04-28 14:17:04 -04:00
|
|
|
use std::path::PathBuf;
|
2022-12-05 16:17:49 -05:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
2021-04-28 14:17:04 -04:00
|
|
|
use std::sync::Arc;
|
2021-07-13 18:11:58 -04:00
|
|
|
use std::time::Duration;
|
2021-04-29 07:42:35 -04:00
|
|
|
use std::time::Instant;
|
2022-03-08 19:34:31 -05:00
|
|
|
use tokio::sync::mpsc::unbounded_channel;
|
|
|
|
use tokio::sync::mpsc::UnboundedSender;
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
/// The test mode is used to determine how a specifier is to be tested.
|
2022-09-19 04:25:03 -04:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2022-03-29 18:59:27 -04:00
|
|
|
pub enum TestMode {
|
2021-08-26 15:21:58 -04:00
|
|
|
/// Test as documentation, type-checking fenced code blocks.
|
|
|
|
Documentation,
|
|
|
|
/// Test as an executable module, loading the module into the isolate and running each test it
|
|
|
|
/// defines.
|
|
|
|
Executable,
|
|
|
|
/// Test as both documentation and an executable module.
|
|
|
|
Both,
|
|
|
|
}
|
|
|
|
|
2022-05-30 13:58:44 -04:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct TestFilter {
|
|
|
|
pub substring: Option<String>,
|
|
|
|
pub regex: Option<Regex>,
|
|
|
|
pub include: Option<Vec<String>>,
|
|
|
|
pub exclude: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestFilter {
|
|
|
|
pub fn includes(&self, name: &String) -> bool {
|
|
|
|
if let Some(substring) = &self.substring {
|
|
|
|
if !name.contains(substring) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(regex) = &self.regex {
|
|
|
|
if !regex.is_match(name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(include) = &self.include {
|
|
|
|
if !include.contains(name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.exclude.contains(name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_flag(flag: &Option<String>) -> Self {
|
|
|
|
let mut substring = None;
|
|
|
|
let mut regex = None;
|
|
|
|
if let Some(flag) = flag {
|
|
|
|
if flag.starts_with('/') && flag.ends_with('/') {
|
|
|
|
let rs = flag.trim_start_matches('/').trim_end_matches('/');
|
|
|
|
regex =
|
|
|
|
Some(Regex::new(rs).unwrap_or_else(|_| Regex::new("$^").unwrap()));
|
|
|
|
} else {
|
|
|
|
substring = Some(flag.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self {
|
|
|
|
substring,
|
|
|
|
regex,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 19:15:54 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Eq, Hash)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct TestLocation {
|
|
|
|
pub file_name: String,
|
|
|
|
pub line_number: u32,
|
|
|
|
pub column_number: u32,
|
|
|
|
}
|
|
|
|
|
2021-10-11 09:45:02 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Eq, Hash)]
|
2021-07-14 15:05:16 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct TestDescription {
|
2022-07-15 13:09:22 -04:00
|
|
|
pub id: usize,
|
2021-07-14 15:05:16 -04:00
|
|
|
pub name: String,
|
2022-07-15 13:09:22 -04:00
|
|
|
pub origin: String,
|
2022-05-04 19:15:54 -04:00
|
|
|
pub location: TestLocation,
|
2021-07-14 15:05:16 -04:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
impl TestDescription {
|
|
|
|
pub fn static_id(&self) -> String {
|
|
|
|
checksum::gen(&[self.location.file_name.as_bytes(), self.name.as_bytes()])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
|
2021-09-04 09:16:35 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum TestOutput {
|
2022-04-26 14:46:49 -04:00
|
|
|
String(String),
|
|
|
|
Bytes(Vec<u8>),
|
2021-09-04 09:16:35 -04:00
|
|
|
}
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
#[allow(clippy::derive_partial_eq_without_eq)]
|
2021-04-28 14:17:04 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum TestResult {
|
|
|
|
Ok,
|
|
|
|
Ignored,
|
2022-04-16 13:51:12 -04:00
|
|
|
Failed(Box<JsError>),
|
2022-07-15 13:09:22 -04:00
|
|
|
Cancelled,
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
|
2021-10-11 09:45:02 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct TestStepDescription {
|
2022-07-15 13:09:22 -04:00
|
|
|
pub id: usize,
|
2021-10-11 09:45:02 -04:00
|
|
|
pub name: String,
|
2022-07-15 13:09:22 -04:00
|
|
|
pub origin: String,
|
|
|
|
pub location: TestLocation,
|
|
|
|
pub level: usize,
|
|
|
|
pub parent_id: usize,
|
|
|
|
pub root_id: usize,
|
|
|
|
pub root_name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStepDescription {
|
|
|
|
pub fn static_id(&self) -> String {
|
|
|
|
checksum::gen(&[
|
|
|
|
self.location.file_name.as_bytes(),
|
|
|
|
&self.level.to_be_bytes(),
|
|
|
|
self.name.as_bytes(),
|
|
|
|
])
|
|
|
|
}
|
2021-10-11 09:45:02 -04:00
|
|
|
}
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
#[allow(clippy::derive_partial_eq_without_eq)]
|
2021-10-11 09:45:02 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum TestStepResult {
|
|
|
|
Ok,
|
|
|
|
Ignored,
|
2022-04-16 13:51:12 -04:00
|
|
|
Failed(Option<Box<JsError>>),
|
|
|
|
Pending(Option<Box<JsError>>),
|
2021-10-11 09:45:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStepResult {
|
2022-04-16 13:51:12 -04:00
|
|
|
fn error(&self) -> Option<&JsError> {
|
2021-10-11 09:45:02 -04:00
|
|
|
match self {
|
2022-04-16 13:51:12 -04:00
|
|
|
TestStepResult::Failed(Some(error)) => Some(error),
|
|
|
|
TestStepResult::Pending(Some(error)) => Some(error),
|
2021-10-11 09:45:02 -04:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
|
2021-07-14 15:05:16 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct TestPlan {
|
|
|
|
pub origin: String,
|
|
|
|
pub total: usize,
|
|
|
|
pub filtered_out: usize,
|
|
|
|
pub used_only: bool,
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
2020-02-11 06:01:56 -05:00
|
|
|
|
2021-04-30 11:56:47 -04:00
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
2021-07-14 15:05:16 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum TestEvent {
|
2022-07-15 13:09:22 -04:00
|
|
|
Register(TestDescription),
|
2021-07-14 15:05:16 -04:00
|
|
|
Plan(TestPlan),
|
2022-07-15 13:09:22 -04:00
|
|
|
Wait(usize),
|
2022-05-01 14:44:55 -04:00
|
|
|
Output(Vec<u8>),
|
2022-07-15 13:09:22 -04:00
|
|
|
Result(usize, TestResult, u64),
|
2022-05-09 05:44:50 -04:00
|
|
|
UncaughtError(String, Box<JsError>),
|
2022-07-15 13:09:22 -04:00
|
|
|
StepRegister(TestStepDescription),
|
|
|
|
StepWait(usize),
|
|
|
|
StepResult(usize, TestStepResult, u64),
|
2021-04-30 11:56:47 -04:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:11:58 -04:00
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
pub struct TestSummary {
|
|
|
|
pub total: usize,
|
|
|
|
pub passed: usize,
|
|
|
|
pub failed: usize,
|
|
|
|
pub ignored: usize,
|
2021-11-15 10:20:37 -05:00
|
|
|
pub passed_steps: usize,
|
|
|
|
pub failed_steps: usize,
|
|
|
|
pub pending_steps: usize,
|
|
|
|
pub ignored_steps: usize,
|
2021-07-13 18:11:58 -04:00
|
|
|
pub filtered_out: usize,
|
|
|
|
pub measured: usize,
|
2022-04-16 13:51:12 -04:00
|
|
|
pub failures: Vec<(TestDescription, Box<JsError>)>,
|
2022-05-09 05:44:50 -04:00
|
|
|
pub uncaught_errors: Vec<(String, Box<JsError>)>,
|
2021-07-13 18:11:58 -04:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2021-12-30 11:18:30 -05:00
|
|
|
struct TestSpecifierOptions {
|
|
|
|
concurrent_jobs: NonZeroUsize,
|
|
|
|
fail_fast: Option<NonZeroUsize>,
|
2022-07-15 13:09:22 -04:00
|
|
|
filter: TestFilter,
|
2021-12-30 11:18:30 -05:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:11:58 -04:00
|
|
|
impl TestSummary {
|
2022-03-29 18:59:27 -04:00
|
|
|
pub fn new() -> TestSummary {
|
2021-07-13 18:11:58 -04:00
|
|
|
TestSummary {
|
|
|
|
total: 0,
|
|
|
|
passed: 0,
|
|
|
|
failed: 0,
|
|
|
|
ignored: 0,
|
2021-11-15 10:20:37 -05:00
|
|
|
passed_steps: 0,
|
|
|
|
failed_steps: 0,
|
|
|
|
pending_steps: 0,
|
|
|
|
ignored_steps: 0,
|
2021-07-13 18:11:58 -04:00
|
|
|
filtered_out: 0,
|
|
|
|
measured: 0,
|
|
|
|
failures: Vec::new(),
|
2022-05-09 05:44:50 -04:00
|
|
|
uncaught_errors: Vec::new(),
|
2021-07-13 18:11:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_failed(&self) -> bool {
|
|
|
|
self.failed > 0 || !self.failures.is_empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 07:42:35 -04:00
|
|
|
struct PrettyTestReporter {
|
2022-08-04 12:38:40 -04:00
|
|
|
parallel: bool,
|
2021-09-04 09:16:35 -04:00
|
|
|
echo_output: bool,
|
2022-05-09 05:44:50 -04:00
|
|
|
in_new_line: bool,
|
2022-07-15 13:09:22 -04:00
|
|
|
last_wait_id: Option<usize>,
|
2022-04-11 12:27:17 -04:00
|
|
|
cwd: Url,
|
2022-04-15 08:24:41 -04:00
|
|
|
did_have_user_output: bool,
|
2022-05-09 05:44:50 -04:00
|
|
|
started_tests: bool,
|
2021-04-29 07:42:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PrettyTestReporter {
|
2022-08-04 12:38:40 -04:00
|
|
|
fn new(parallel: bool, echo_output: bool) -> PrettyTestReporter {
|
2021-09-04 09:16:35 -04:00
|
|
|
PrettyTestReporter {
|
2022-08-04 12:38:40 -04:00
|
|
|
parallel,
|
2021-09-04 09:16:35 -04:00
|
|
|
echo_output,
|
2022-05-09 05:44:50 -04:00
|
|
|
in_new_line: true,
|
2022-07-15 13:09:22 -04:00
|
|
|
last_wait_id: None,
|
2022-04-11 12:27:17 -04:00
|
|
|
cwd: Url::from_directory_path(std::env::current_dir().unwrap()).unwrap(),
|
2022-04-15 08:24:41 -04:00
|
|
|
did_have_user_output: false,
|
2022-05-09 05:44:50 -04:00
|
|
|
started_tests: false,
|
2021-10-11 09:45:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn force_report_wait(&mut self, description: &TestDescription) {
|
2022-07-15 13:09:22 -04:00
|
|
|
if !self.in_new_line {
|
|
|
|
println!();
|
|
|
|
}
|
2022-08-04 12:38:40 -04:00
|
|
|
if self.parallel {
|
|
|
|
print!(
|
|
|
|
"{}",
|
|
|
|
colors::gray(format!(
|
|
|
|
"{} => ",
|
|
|
|
self.to_relative_path_or_remote_url(&description.origin)
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2022-04-11 12:27:17 -04:00
|
|
|
print!("{} ...", description.name);
|
2022-05-09 05:44:50 -04:00
|
|
|
self.in_new_line = false;
|
2021-10-11 09:45:02 -04:00
|
|
|
// flush for faster feedback when line buffered
|
|
|
|
std::io::stdout().flush().unwrap();
|
2022-07-15 13:09:22 -04:00
|
|
|
self.last_wait_id = Some(description.id);
|
2021-10-11 09:45:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-11 12:27:17 -04:00
|
|
|
fn to_relative_path_or_remote_url(&self, path_or_url: &str) -> String {
|
|
|
|
let url = Url::parse(path_or_url).unwrap();
|
|
|
|
if url.scheme() == "file" {
|
2022-04-16 15:51:55 -04:00
|
|
|
if let Some(mut r) = self.cwd.make_relative(&url) {
|
|
|
|
if !r.starts_with("../") {
|
|
|
|
r = format!("./{}", r);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2022-04-11 12:27:17 -04:00
|
|
|
}
|
2022-04-16 15:51:55 -04:00
|
|
|
path_or_url.to_string()
|
2022-04-11 12:27:17 -04:00
|
|
|
}
|
|
|
|
|
2021-10-11 09:45:02 -04:00
|
|
|
fn force_report_step_wait(&mut self, description: &TestStepDescription) {
|
2022-07-15 13:09:22 -04:00
|
|
|
self.write_output_end();
|
|
|
|
if !self.in_new_line {
|
2021-10-11 09:45:02 -04:00
|
|
|
println!();
|
|
|
|
}
|
2022-04-11 12:27:17 -04:00
|
|
|
print!("{}{} ...", " ".repeat(description.level), description.name);
|
2022-05-09 05:44:50 -04:00
|
|
|
self.in_new_line = false;
|
2021-10-11 09:45:02 -04:00
|
|
|
// flush for faster feedback when line buffered
|
|
|
|
std::io::stdout().flush().unwrap();
|
2022-07-15 13:09:22 -04:00
|
|
|
self.last_wait_id = Some(description.id);
|
2021-10-11 09:45:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn force_report_step_result(
|
|
|
|
&mut self,
|
|
|
|
description: &TestStepDescription,
|
|
|
|
result: &TestStepResult,
|
|
|
|
elapsed: u64,
|
|
|
|
) {
|
|
|
|
let status = match result {
|
|
|
|
TestStepResult::Ok => colors::green("ok").to_string(),
|
|
|
|
TestStepResult::Ignored => colors::yellow("ignored").to_string(),
|
|
|
|
TestStepResult::Pending(_) => colors::gray("pending").to_string(),
|
|
|
|
TestStepResult::Failed(_) => colors::red("FAILED").to_string(),
|
|
|
|
};
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
self.write_output_end();
|
|
|
|
if self.in_new_line || self.last_wait_id != Some(description.id) {
|
|
|
|
self.force_report_step_wait(description);
|
2022-05-09 07:25:04 -04:00
|
|
|
}
|
|
|
|
|
2022-03-08 18:19:02 -05:00
|
|
|
println!(
|
2022-07-15 13:09:22 -04:00
|
|
|
" {} {}",
|
2022-03-08 18:19:02 -05:00
|
|
|
status,
|
|
|
|
colors::gray(format!("({})", display::human_elapsed(elapsed.into())))
|
|
|
|
);
|
2021-10-11 09:45:02 -04:00
|
|
|
|
2022-04-16 13:51:12 -04:00
|
|
|
if let Some(js_error) = result.error() {
|
2022-04-18 09:22:23 -04:00
|
|
|
let err_string = format_test_error(js_error);
|
2022-05-04 19:15:54 -04:00
|
|
|
let err_string = format!("{}: {}", colors::red_bold("error"), err_string);
|
2022-04-16 13:51:12 -04:00
|
|
|
for line in err_string.lines() {
|
2021-10-11 09:45:02 -04:00
|
|
|
println!("{}{}", " ".repeat(description.level + 1), line);
|
|
|
|
}
|
2021-09-04 09:16:35 -04:00
|
|
|
}
|
2022-05-09 05:44:50 -04:00
|
|
|
self.in_new_line = true;
|
2021-04-29 07:42:35 -04:00
|
|
|
}
|
2022-04-15 08:24:41 -04:00
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
fn write_output_end(&mut self) {
|
2022-04-15 08:24:41 -04:00
|
|
|
if self.did_have_user_output {
|
|
|
|
println!("{}", colors::gray("----- output end -----"));
|
2022-05-09 05:44:50 -04:00
|
|
|
self.in_new_line = true;
|
2022-04-15 08:24:41 -04:00
|
|
|
self.did_have_user_output = false;
|
|
|
|
}
|
|
|
|
}
|
2021-04-29 07:42:35 -04:00
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
fn report_register(&mut self, _description: &TestDescription) {}
|
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
fn report_plan(&mut self, plan: &TestPlan) {
|
2022-08-04 12:38:40 -04:00
|
|
|
if self.parallel {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-14 15:05:16 -04:00
|
|
|
let inflection = if plan.total == 1 { "test" } else { "tests" };
|
2022-04-11 12:27:17 -04:00
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
colors::gray(format!(
|
|
|
|
"running {} {} from {}",
|
|
|
|
plan.total,
|
|
|
|
inflection,
|
|
|
|
self.to_relative_path_or_remote_url(&plan.origin)
|
|
|
|
))
|
|
|
|
);
|
2022-05-09 05:44:50 -04:00
|
|
|
self.in_new_line = true;
|
2021-07-14 15:05:16 -04:00
|
|
|
}
|
2021-04-29 07:42:35 -04:00
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
fn report_wait(&mut self, description: &TestDescription) {
|
2022-08-04 12:38:40 -04:00
|
|
|
if !self.parallel {
|
2021-10-11 09:45:02 -04:00
|
|
|
self.force_report_wait(description);
|
2021-07-14 15:05:16 -04:00
|
|
|
}
|
2022-05-09 05:44:50 -04:00
|
|
|
self.started_tests = true;
|
2021-07-14 15:05:16 -04:00
|
|
|
}
|
2021-04-29 07:42:35 -04:00
|
|
|
|
2022-05-01 14:44:55 -04:00
|
|
|
fn report_output(&mut self, output: &[u8]) {
|
2022-04-15 08:24:41 -04:00
|
|
|
if !self.echo_output {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-09 05:44:50 -04:00
|
|
|
if !self.did_have_user_output && self.started_tests {
|
2022-04-15 08:24:41 -04:00
|
|
|
self.did_have_user_output = true;
|
2022-08-04 12:38:40 -04:00
|
|
|
if !self.in_new_line {
|
|
|
|
println!();
|
|
|
|
}
|
2022-04-15 08:24:41 -04:00
|
|
|
println!("{}", colors::gray("------- output -------"));
|
2022-05-09 05:44:50 -04:00
|
|
|
self.in_new_line = true;
|
2022-04-15 08:24:41 -04:00
|
|
|
}
|
2022-05-01 14:44:55 -04:00
|
|
|
|
|
|
|
// output everything to stdout in order to prevent
|
|
|
|
// stdout and stderr racing
|
|
|
|
std::io::stdout().write_all(output).unwrap();
|
2021-09-04 09:16:35 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
fn report_result(
|
|
|
|
&mut self,
|
|
|
|
description: &TestDescription,
|
|
|
|
result: &TestResult,
|
|
|
|
elapsed: u64,
|
|
|
|
) {
|
2022-08-04 12:38:40 -04:00
|
|
|
if self.parallel {
|
2021-10-11 09:45:02 -04:00
|
|
|
self.force_report_wait(description);
|
2021-07-14 15:05:16 -04:00
|
|
|
}
|
2021-07-13 18:11:58 -04:00
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
self.write_output_end();
|
|
|
|
if self.in_new_line || self.last_wait_id != Some(description.id) {
|
|
|
|
self.force_report_wait(description);
|
2022-05-09 07:25:04 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
let status = match result {
|
|
|
|
TestResult::Ok => colors::green("ok").to_string(),
|
|
|
|
TestResult::Ignored => colors::yellow("ignored").to_string(),
|
|
|
|
TestResult::Failed(_) => colors::red("FAILED").to_string(),
|
2022-07-15 13:09:22 -04:00
|
|
|
TestResult::Cancelled => colors::gray("cancelled").to_string(),
|
2021-07-14 15:05:16 -04:00
|
|
|
};
|
2021-07-13 18:11:58 -04:00
|
|
|
|
2022-03-08 18:19:02 -05:00
|
|
|
println!(
|
2022-07-15 13:09:22 -04:00
|
|
|
" {} {}",
|
2022-03-08 18:19:02 -05:00
|
|
|
status,
|
|
|
|
colors::gray(format!("({})", display::human_elapsed(elapsed.into())))
|
|
|
|
);
|
2022-05-09 05:44:50 -04:00
|
|
|
self.in_new_line = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_uncaught_error(&mut self, origin: &str, _error: &JsError) {
|
|
|
|
if !self.in_new_line {
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
println!(
|
|
|
|
"Uncaught error from {} {}",
|
|
|
|
self.to_relative_path_or_remote_url(origin),
|
|
|
|
colors::red("FAILED")
|
|
|
|
);
|
|
|
|
self.in_new_line = true;
|
|
|
|
self.did_have_user_output = false;
|
2021-04-29 07:42:35 -04:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
fn report_step_register(&mut self, _description: &TestStepDescription) {}
|
|
|
|
|
2021-10-11 09:45:02 -04:00
|
|
|
fn report_step_wait(&mut self, description: &TestStepDescription) {
|
2022-08-04 12:38:40 -04:00
|
|
|
if !self.parallel {
|
2021-10-11 09:45:02 -04:00
|
|
|
self.force_report_step_wait(description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_step_result(
|
|
|
|
&mut self,
|
|
|
|
description: &TestStepDescription,
|
|
|
|
result: &TestStepResult,
|
|
|
|
elapsed: u64,
|
2022-08-04 12:38:40 -04:00
|
|
|
tests: &IndexMap<usize, TestDescription>,
|
|
|
|
test_steps: &IndexMap<usize, TestStepDescription>,
|
2021-10-11 09:45:02 -04:00
|
|
|
) {
|
2022-08-04 12:38:40 -04:00
|
|
|
if self.parallel {
|
|
|
|
self.write_output_end();
|
|
|
|
let root;
|
|
|
|
let mut ancestor_names = vec![];
|
|
|
|
let mut current_desc = description;
|
|
|
|
loop {
|
|
|
|
if let Some(step_desc) = test_steps.get(¤t_desc.parent_id) {
|
|
|
|
ancestor_names.push(&step_desc.name);
|
|
|
|
current_desc = step_desc;
|
|
|
|
} else {
|
|
|
|
root = tests.get(¤t_desc.parent_id).unwrap();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ancestor_names.reverse();
|
|
|
|
print!(
|
|
|
|
"{}",
|
|
|
|
colors::gray(format!(
|
|
|
|
"{} =>",
|
|
|
|
self.to_relative_path_or_remote_url(&description.origin)
|
|
|
|
))
|
|
|
|
);
|
|
|
|
print!(" {} ...", root.name);
|
|
|
|
for name in ancestor_names {
|
|
|
|
print!(" {} ...", name);
|
|
|
|
}
|
|
|
|
print!(" {} ...", description.name);
|
|
|
|
self.in_new_line = false;
|
|
|
|
self.last_wait_id = Some(description.id);
|
2021-10-11 09:45:02 -04:00
|
|
|
}
|
2022-08-04 12:38:40 -04:00
|
|
|
self.force_report_step_result(description, result, elapsed);
|
2021-10-11 09:45:02 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
fn report_summary(&mut self, summary: &TestSummary, elapsed: &Duration) {
|
2022-05-09 05:44:50 -04:00
|
|
|
if !summary.failures.is_empty() || !summary.uncaught_errors.is_empty() {
|
|
|
|
#[allow(clippy::type_complexity)] // Type alias doesn't look better here
|
|
|
|
let mut failures_by_origin: BTreeMap<
|
|
|
|
String,
|
|
|
|
(Vec<(&TestDescription, &JsError)>, Option<&JsError>),
|
|
|
|
> = BTreeMap::default();
|
2022-05-04 19:15:54 -04:00
|
|
|
let mut failure_titles = vec![];
|
2022-04-16 13:51:12 -04:00
|
|
|
for (description, js_error) in &summary.failures {
|
2022-05-09 05:44:50 -04:00
|
|
|
let (failures, _) = failures_by_origin
|
|
|
|
.entry(description.origin.clone())
|
|
|
|
.or_default();
|
|
|
|
failures.push((description, js_error.as_ref()));
|
|
|
|
}
|
|
|
|
for (origin, js_error) in &summary.uncaught_errors {
|
|
|
|
let (_, uncaught_error) =
|
|
|
|
failures_by_origin.entry(origin.clone()).or_default();
|
|
|
|
let _ = uncaught_error.insert(js_error.as_ref());
|
|
|
|
}
|
|
|
|
println!("\n{}\n", colors::white_bold_on_red(" ERRORS "));
|
|
|
|
for (origin, (failures, uncaught_error)) in failures_by_origin {
|
|
|
|
for (description, js_error) in failures {
|
|
|
|
let failure_title = format!(
|
|
|
|
"{} {}",
|
|
|
|
&description.name,
|
|
|
|
colors::gray(format!(
|
|
|
|
"=> {}:{}:{}",
|
|
|
|
self.to_relative_path_or_remote_url(
|
|
|
|
&description.location.file_name
|
|
|
|
),
|
|
|
|
description.location.line_number,
|
|
|
|
description.location.column_number
|
|
|
|
))
|
|
|
|
);
|
|
|
|
println!("{}", &failure_title);
|
|
|
|
println!(
|
|
|
|
"{}: {}",
|
|
|
|
colors::red_bold("error"),
|
|
|
|
format_test_error(js_error)
|
|
|
|
);
|
|
|
|
println!();
|
|
|
|
failure_titles.push(failure_title);
|
|
|
|
}
|
|
|
|
if let Some(js_error) = uncaught_error {
|
|
|
|
let failure_title = format!(
|
|
|
|
"{} (uncaught error)",
|
|
|
|
self.to_relative_path_or_remote_url(&origin)
|
|
|
|
);
|
|
|
|
println!("{}", &failure_title);
|
|
|
|
println!(
|
|
|
|
"{}: {}",
|
|
|
|
colors::red_bold("error"),
|
|
|
|
format_test_error(js_error)
|
|
|
|
);
|
|
|
|
println!("This error was not caught from a test and caused the test runner to fail on the referenced module.");
|
|
|
|
println!("It most likely originated from a dangling promise, event/timeout handler or top-level code.");
|
|
|
|
println!();
|
|
|
|
failure_titles.push(failure_title);
|
|
|
|
}
|
2022-04-11 12:27:17 -04:00
|
|
|
}
|
2022-05-09 04:56:13 -04:00
|
|
|
println!("{}\n", colors::white_bold_on_red(" FAILURES "));
|
2022-05-04 19:15:54 -04:00
|
|
|
for failure_title in failure_titles {
|
|
|
|
println!("{}", failure_title);
|
2021-04-29 07:42:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
let status = if summary.has_failed() {
|
2021-04-29 07:42:35 -04:00
|
|
|
colors::red("FAILED").to_string()
|
|
|
|
} else {
|
|
|
|
colors::green("ok").to_string()
|
|
|
|
};
|
|
|
|
|
2021-11-15 10:20:37 -05:00
|
|
|
let get_steps_text = |count: usize| -> String {
|
|
|
|
if count == 0 {
|
|
|
|
String::new()
|
|
|
|
} else if count == 1 {
|
|
|
|
" (1 step)".to_string()
|
|
|
|
} else {
|
|
|
|
format!(" ({} steps)", count)
|
|
|
|
}
|
|
|
|
};
|
2022-06-14 14:51:49 -04:00
|
|
|
|
|
|
|
let mut summary_result = String::new();
|
|
|
|
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(
|
|
|
|
summary_result,
|
2022-06-14 14:51:49 -04:00
|
|
|
"{} passed{} | {} failed{}",
|
2021-07-14 15:05:16 -04:00
|
|
|
summary.passed,
|
2021-11-15 10:20:37 -05:00
|
|
|
get_steps_text(summary.passed_steps),
|
2021-07-14 15:05:16 -04:00
|
|
|
summary.failed,
|
2021-11-15 10:20:37 -05:00
|
|
|
get_steps_text(summary.failed_steps + summary.pending_steps),
|
2022-07-01 09:28:06 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-06-14 14:51:49 -04:00
|
|
|
|
|
|
|
let ignored_steps = get_steps_text(summary.ignored_steps);
|
|
|
|
if summary.ignored > 0 || !ignored_steps.is_empty() {
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(
|
|
|
|
summary_result,
|
|
|
|
" | {} ignored{}",
|
|
|
|
summary.ignored, ignored_steps
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
}
|
2022-06-14 14:51:49 -04:00
|
|
|
|
|
|
|
if summary.measured > 0 {
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(summary_result, " | {} measured", summary.measured,).unwrap();
|
|
|
|
}
|
2022-06-14 14:51:49 -04:00
|
|
|
|
|
|
|
if summary.filtered_out > 0 {
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(summary_result, " | {} filtered out", summary.filtered_out)
|
|
|
|
.unwrap()
|
2022-06-14 14:51:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"\n{} | {} {}\n",
|
|
|
|
status,
|
|
|
|
summary_result,
|
|
|
|
colors::gray(format!(
|
|
|
|
"({})",
|
|
|
|
display::human_elapsed(elapsed.as_millis())
|
|
|
|
)),
|
2021-07-14 15:05:16 -04:00
|
|
|
);
|
2022-05-09 05:44:50 -04:00
|
|
|
self.in_new_line = true;
|
2021-04-29 07:42:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:22:23 -04:00
|
|
|
fn abbreviate_test_error(js_error: &JsError) -> JsError {
|
|
|
|
let mut js_error = js_error.clone();
|
|
|
|
let frames = std::mem::take(&mut js_error.frames);
|
|
|
|
|
|
|
|
// check if there are any stack frames coming from user code
|
|
|
|
let should_filter = frames.iter().any(|f| {
|
|
|
|
if let Some(file_name) = &f.file_name {
|
|
|
|
!(file_name.starts_with("[deno:") || file_name.starts_with("deno:"))
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if should_filter {
|
|
|
|
let mut frames = frames
|
|
|
|
.into_iter()
|
|
|
|
.rev()
|
|
|
|
.skip_while(|f| {
|
|
|
|
if let Some(file_name) = &f.file_name {
|
|
|
|
file_name.starts_with("[deno:") || file_name.starts_with("deno:")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
frames.reverse();
|
|
|
|
js_error.frames = frames;
|
|
|
|
} else {
|
|
|
|
js_error.frames = frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
js_error.cause = js_error
|
|
|
|
.cause
|
|
|
|
.as_ref()
|
|
|
|
.map(|e| Box::new(abbreviate_test_error(e)));
|
|
|
|
js_error.aggregated = js_error
|
|
|
|
.aggregated
|
|
|
|
.as_ref()
|
|
|
|
.map(|es| es.iter().map(abbreviate_test_error).collect());
|
|
|
|
js_error
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:06:10 -04:00
|
|
|
// This function prettifies `JsError` and applies some changes specifically for
|
|
|
|
// test runner purposes:
|
2022-04-18 09:22:23 -04:00
|
|
|
//
|
|
|
|
// - filter out stack frames:
|
|
|
|
// - if stack trace consists of mixed user and internal code, the frames
|
|
|
|
// below the first user code frame are filtered out
|
|
|
|
// - if stack trace consists only of internal code it is preserved as is
|
|
|
|
pub fn format_test_error(js_error: &JsError) -> String {
|
|
|
|
let mut js_error = abbreviate_test_error(js_error);
|
|
|
|
js_error.exception_message = js_error
|
|
|
|
.exception_message
|
|
|
|
.trim_start_matches("Uncaught ")
|
|
|
|
.to_string();
|
2022-04-26 19:06:10 -04:00
|
|
|
format_js_error(&js_error)
|
2022-04-18 09:22:23 -04:00
|
|
|
}
|
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
/// Test a single specifier as documentation containing test programs, an executable test module or
|
|
|
|
/// both.
|
|
|
|
async fn test_specifier(
|
2023-01-13 16:39:19 -05:00
|
|
|
ps: &ProcState,
|
2021-04-28 14:17:04 -04:00
|
|
|
permissions: Permissions,
|
2021-08-26 15:21:58 -04:00
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
mode: TestMode,
|
2022-12-05 16:17:49 -05:00
|
|
|
sender: TestEventSender,
|
|
|
|
fail_fast_tracker: FailFastTracker,
|
2021-12-30 11:18:30 -05:00
|
|
|
options: TestSpecifierOptions,
|
2021-04-28 14:17:04 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-12-05 16:17:49 -05:00
|
|
|
let stdout = StdioPipe::File(sender.stdout());
|
|
|
|
let stderr = StdioPipe::File(sender.stderr());
|
2022-11-21 08:36:26 -05:00
|
|
|
let mut worker = create_main_worker_for_test_or_bench(
|
2023-01-13 16:39:19 -05:00
|
|
|
ps,
|
|
|
|
specifier,
|
2023-01-07 11:25:34 -05:00
|
|
|
PermissionsContainer::new(permissions),
|
2022-12-05 16:17:49 -05:00
|
|
|
vec![ops::testing::init(
|
|
|
|
sender,
|
|
|
|
fail_fast_tracker,
|
2023-01-13 16:39:19 -05:00
|
|
|
options.filter,
|
2022-12-05 16:17:49 -05:00
|
|
|
)],
|
2022-04-26 19:00:04 -04:00
|
|
|
Stdio {
|
|
|
|
stdin: StdioPipe::Inherit,
|
2022-12-05 16:17:49 -05:00
|
|
|
stdout,
|
|
|
|
stderr,
|
2022-04-26 19:00:04 -04:00
|
|
|
},
|
2022-08-23 10:39:19 -04:00
|
|
|
)
|
|
|
|
.await?;
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
worker.run_test_specifier(mode).await
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
|
|
|
|
2021-07-29 15:03:06 -04:00
|
|
|
fn extract_files_from_regex_blocks(
|
2022-01-13 11:58:00 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2021-07-29 15:03:06 -04:00
|
|
|
source: &str,
|
2021-09-07 10:39:32 -04:00
|
|
|
media_type: MediaType,
|
2022-01-13 11:58:00 -05:00
|
|
|
file_line_index: usize,
|
2021-07-29 15:03:06 -04:00
|
|
|
blocks_regex: &Regex,
|
|
|
|
lines_regex: &Regex,
|
|
|
|
) -> Result<Vec<File>, AnyError> {
|
|
|
|
let files = blocks_regex
|
2021-07-30 09:03:41 -04:00
|
|
|
.captures_iter(source)
|
2021-07-29 15:03:06 -04:00
|
|
|
.filter_map(|block| {
|
2022-11-17 20:59:10 -05:00
|
|
|
block.get(1)?;
|
2022-03-10 20:14:32 -05:00
|
|
|
|
2021-08-14 06:33:58 -04:00
|
|
|
let maybe_attributes: Option<Vec<_>> = block
|
2021-07-29 15:03:06 -04:00
|
|
|
.get(1)
|
2021-08-14 06:33:58 -04:00
|
|
|
.map(|attributes| attributes.as_str().split(' ').collect());
|
|
|
|
|
|
|
|
let file_media_type = if let Some(attributes) = maybe_attributes {
|
|
|
|
if attributes.contains(&"ignore") {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-08-21 13:31:14 -04:00
|
|
|
match attributes.first() {
|
2021-08-14 06:33:58 -04:00
|
|
|
Some(&"js") => MediaType::JavaScript,
|
2022-03-07 20:10:40 -05:00
|
|
|
Some(&"javascript") => MediaType::JavaScript,
|
2021-11-01 16:22:27 -04:00
|
|
|
Some(&"mjs") => MediaType::Mjs,
|
|
|
|
Some(&"cjs") => MediaType::Cjs,
|
2021-08-14 06:33:58 -04:00
|
|
|
Some(&"jsx") => MediaType::Jsx,
|
|
|
|
Some(&"ts") => MediaType::TypeScript,
|
2022-03-07 20:10:40 -05:00
|
|
|
Some(&"typescript") => MediaType::TypeScript,
|
2021-11-01 16:22:27 -04:00
|
|
|
Some(&"mts") => MediaType::Mts,
|
|
|
|
Some(&"cts") => MediaType::Cts,
|
2021-08-14 06:33:58 -04:00
|
|
|
Some(&"tsx") => MediaType::Tsx,
|
2021-09-07 10:39:32 -04:00
|
|
|
Some(&"") => media_type,
|
2021-07-29 15:03:06 -04:00
|
|
|
_ => MediaType::Unknown,
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-07 10:39:32 -04:00
|
|
|
media_type
|
2021-07-29 15:03:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if file_media_type == MediaType::Unknown {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let line_offset = source[0..block.get(0).unwrap().start()]
|
|
|
|
.chars()
|
|
|
|
.filter(|c| *c == '\n')
|
|
|
|
.count();
|
|
|
|
|
|
|
|
let line_count = block.get(0).unwrap().as_str().split('\n').count();
|
|
|
|
|
|
|
|
let body = block.get(2).unwrap();
|
|
|
|
let text = body.as_str();
|
|
|
|
|
|
|
|
// TODO(caspervonb) generate an inline source map
|
|
|
|
let mut file_source = String::new();
|
2021-07-30 09:03:41 -04:00
|
|
|
for line in lines_regex.captures_iter(text) {
|
2021-07-29 15:03:06 -04:00
|
|
|
let text = line.get(1).unwrap();
|
2022-07-01 09:28:06 -04:00
|
|
|
writeln!(file_source, "{}", text.as_str()).unwrap();
|
2021-07-29 15:03:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let file_specifier = deno_core::resolve_url_or_path(&format!(
|
|
|
|
"{}${}-{}{}",
|
2022-01-13 11:58:00 -05:00
|
|
|
specifier,
|
|
|
|
file_line_index + line_offset + 1,
|
|
|
|
file_line_index + line_offset + line_count + 1,
|
2021-07-29 15:03:06 -04:00
|
|
|
file_media_type.as_ts_extension(),
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Some(File {
|
|
|
|
local: file_specifier.to_file_path().unwrap(),
|
|
|
|
maybe_types: None,
|
|
|
|
media_type: file_media_type,
|
2022-05-20 16:40:55 -04:00
|
|
|
source: file_source.into(),
|
2021-07-29 15:03:06 -04:00
|
|
|
specifier: file_specifier,
|
2021-09-02 11:38:19 -04:00
|
|
|
maybe_headers: None,
|
2021-07-29 15:03:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(files)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_files_from_source_comments(
|
|
|
|
specifier: &ModuleSpecifier,
|
2022-05-20 16:40:55 -04:00
|
|
|
source: Arc<str>,
|
2021-09-07 10:39:32 -04:00
|
|
|
media_type: MediaType,
|
2021-07-29 15:03:06 -04:00
|
|
|
) -> Result<Vec<File>, AnyError> {
|
2021-09-07 10:39:32 -04:00
|
|
|
let parsed_source = deno_ast::parse_module(deno_ast::ParseParams {
|
|
|
|
specifier: specifier.as_str().to_string(),
|
2022-05-20 16:40:55 -04:00
|
|
|
text_info: deno_ast::SourceTextInfo::new(source),
|
2021-09-07 10:39:32 -04:00
|
|
|
media_type,
|
|
|
|
capture_tokens: false,
|
|
|
|
maybe_syntax: None,
|
2021-10-12 09:58:04 -04:00
|
|
|
scope_analysis: false,
|
2021-09-07 10:39:32 -04:00
|
|
|
})?;
|
|
|
|
let comments = parsed_source.comments().get_vec();
|
2021-11-15 09:58:04 -05:00
|
|
|
let blocks_regex = Regex::new(r"```([^\r\n]*)\r?\n([\S\s]*?)```")?;
|
2021-07-29 15:03:06 -04:00
|
|
|
let lines_regex = Regex::new(r"(?:\* ?)(?:\# ?)?(.*)")?;
|
|
|
|
|
|
|
|
let files = comments
|
|
|
|
.iter()
|
|
|
|
.filter(|comment| {
|
|
|
|
if comment.kind != CommentKind::Block || !comment.text.starts_with('*') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
})
|
|
|
|
.flat_map(|comment| {
|
|
|
|
extract_files_from_regex_blocks(
|
2022-01-13 11:58:00 -05:00
|
|
|
specifier,
|
2021-07-29 15:03:06 -04:00
|
|
|
&comment.text,
|
2021-07-30 09:03:41 -04:00
|
|
|
media_type,
|
2022-05-20 16:40:55 -04:00
|
|
|
parsed_source.text_info().line_index(comment.start()),
|
2021-07-29 15:03:06 -04:00
|
|
|
&blocks_regex,
|
|
|
|
&lines_regex,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(files)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_files_from_fenced_blocks(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source: &str,
|
2021-09-07 10:39:32 -04:00
|
|
|
media_type: MediaType,
|
2021-07-29 15:03:06 -04:00
|
|
|
) -> Result<Vec<File>, AnyError> {
|
2022-03-10 20:14:32 -05:00
|
|
|
// The pattern matches code blocks as well as anything in HTML comment syntax,
|
|
|
|
// but it stores the latter without any capturing groups. This way, a simple
|
|
|
|
// check can be done to see if a block is inside a comment (and skip typechecking)
|
|
|
|
// or not by checking for the presence of capturing groups in the matches.
|
|
|
|
let blocks_regex =
|
|
|
|
Regex::new(r"(?s)<!--.*?-->|```([^\r\n]*)\r?\n([\S\s]*?)```")?;
|
2021-07-29 15:03:06 -04:00
|
|
|
let lines_regex = Regex::new(r"(?:\# ?)?(.*)")?;
|
|
|
|
|
|
|
|
extract_files_from_regex_blocks(
|
2022-01-13 11:58:00 -05:00
|
|
|
specifier,
|
2021-07-30 09:03:41 -04:00
|
|
|
source,
|
|
|
|
media_type,
|
2022-01-13 11:58:00 -05:00
|
|
|
/* file line index */ 0,
|
2021-07-29 15:03:06 -04:00
|
|
|
&blocks_regex,
|
|
|
|
&lines_regex,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn fetch_inline_files(
|
2023-01-13 16:39:19 -05:00
|
|
|
ps: &ProcState,
|
2021-07-29 15:03:06 -04:00
|
|
|
specifiers: Vec<ModuleSpecifier>,
|
|
|
|
) -> Result<Vec<File>, AnyError> {
|
|
|
|
let mut files = Vec::new();
|
|
|
|
for specifier in specifiers {
|
2023-01-07 11:25:34 -05:00
|
|
|
let fetch_permissions = PermissionsContainer::allow_all();
|
|
|
|
let file = ps.file_fetcher.fetch(&specifier, fetch_permissions).await?;
|
2021-07-29 15:03:06 -04:00
|
|
|
|
|
|
|
let inline_files = if file.media_type == MediaType::Unknown {
|
|
|
|
extract_files_from_fenced_blocks(
|
|
|
|
&file.specifier,
|
|
|
|
&file.source,
|
2021-09-07 10:39:32 -04:00
|
|
|
file.media_type,
|
2021-07-29 15:03:06 -04:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
extract_files_from_source_comments(
|
|
|
|
&file.specifier,
|
2023-01-13 16:39:19 -05:00
|
|
|
file.source,
|
2021-09-07 10:39:32 -04:00
|
|
|
file.media_type,
|
2021-07-29 15:03:06 -04:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
files.extend(inline_files?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(files)
|
|
|
|
}
|
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
/// Type check a collection of module and document specifiers.
|
2022-03-29 18:59:27 -04:00
|
|
|
pub async fn check_specifiers(
|
2022-02-11 14:04:31 -05:00
|
|
|
ps: &ProcState,
|
2021-05-10 02:06:13 -04:00
|
|
|
permissions: Permissions,
|
2021-08-26 15:21:58 -04:00
|
|
|
specifiers: Vec<(ModuleSpecifier, TestMode)>,
|
2021-07-22 07:34:29 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-06-29 11:51:11 -04:00
|
|
|
let lib = ps.options.ts_type_lib_window();
|
2021-08-26 15:21:58 -04:00
|
|
|
let inline_files = fetch_inline_files(
|
2023-01-13 16:39:19 -05:00
|
|
|
ps,
|
2021-08-26 15:21:58 -04:00
|
|
|
specifiers
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(specifier, mode)| {
|
|
|
|
if *mode != TestMode::Executable {
|
|
|
|
Some(specifier.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
.await?;
|
2021-07-05 21:20:33 -04:00
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
if !inline_files.is_empty() {
|
|
|
|
let specifiers = inline_files
|
|
|
|
.iter()
|
2022-03-10 20:33:02 -05:00
|
|
|
.map(|file| file.specifier.clone())
|
2021-08-26 15:21:58 -04:00
|
|
|
.collect();
|
2021-05-10 19:54:39 -04:00
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
for file in inline_files {
|
2021-09-24 11:10:42 -04:00
|
|
|
ps.file_fetcher.insert_cached(file);
|
2021-05-10 19:54:39 -04:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
ps.prepare_module_load(
|
2021-09-24 11:10:42 -04:00
|
|
|
specifiers,
|
2021-10-10 17:26:22 -04:00
|
|
|
false,
|
2022-06-28 16:45:55 -04:00
|
|
|
lib,
|
2023-01-07 11:25:34 -05:00
|
|
|
PermissionsContainer::new(Permissions::allow_all()),
|
|
|
|
PermissionsContainer::new(permissions.clone()),
|
2021-09-24 11:10:42 -04:00
|
|
|
)
|
|
|
|
.await?;
|
2021-05-10 19:54:39 -04:00
|
|
|
}
|
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
let module_specifiers = specifiers
|
2023-01-13 16:39:19 -05:00
|
|
|
.into_iter()
|
2021-08-26 15:21:58 -04:00
|
|
|
.filter_map(|(specifier, mode)| {
|
2023-01-13 16:39:19 -05:00
|
|
|
if mode != TestMode::Documentation {
|
|
|
|
Some(specifier)
|
2021-08-26 15:21:58 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2021-08-12 14:10:14 -04:00
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
ps.prepare_module_load(
|
2021-09-24 11:10:42 -04:00
|
|
|
module_specifiers,
|
2021-10-10 17:26:22 -04:00
|
|
|
false,
|
2021-09-24 11:10:42 -04:00
|
|
|
lib,
|
2023-01-07 11:25:34 -05:00
|
|
|
PermissionsContainer::allow_all(),
|
|
|
|
PermissionsContainer::new(permissions),
|
2021-09-24 11:10:42 -04:00
|
|
|
)
|
|
|
|
.await?;
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
/// Test a collection of specifiers with test modes concurrently.
|
|
|
|
async fn test_specifiers(
|
2023-01-16 15:27:41 -05:00
|
|
|
ps: &ProcState,
|
2023-01-13 16:39:19 -05:00
|
|
|
permissions: &Permissions,
|
2021-08-26 15:21:58 -04:00
|
|
|
specifiers_with_mode: Vec<(ModuleSpecifier, TestMode)>,
|
2021-12-30 11:18:30 -05:00
|
|
|
options: TestSpecifierOptions,
|
2021-08-26 15:21:58 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-06-29 11:51:11 -04:00
|
|
|
let log_level = ps.options.log_level();
|
2022-08-11 16:59:12 -04:00
|
|
|
let specifiers_with_mode = if let Some(seed) = ps.options.shuffle_tests() {
|
2021-08-26 15:21:58 -04:00
|
|
|
let mut rng = SmallRng::seed_from_u64(seed);
|
2023-01-13 16:39:19 -05:00
|
|
|
let mut specifiers_with_mode = specifiers_with_mode;
|
2021-08-26 15:21:58 -04:00
|
|
|
specifiers_with_mode.sort_by_key(|(specifier, _)| specifier.clone());
|
|
|
|
specifiers_with_mode.shuffle(&mut rng);
|
|
|
|
specifiers_with_mode
|
|
|
|
} else {
|
|
|
|
specifiers_with_mode
|
|
|
|
};
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2022-03-08 19:34:31 -05:00
|
|
|
let (sender, mut receiver) = unbounded_channel::<TestEvent>();
|
2022-05-01 14:44:55 -04:00
|
|
|
let sender = TestEventSender::new(sender);
|
2021-12-30 11:18:30 -05:00
|
|
|
let concurrent_jobs = options.concurrent_jobs;
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
let join_handles =
|
2023-01-13 16:39:19 -05:00
|
|
|
specifiers_with_mode
|
|
|
|
.into_iter()
|
|
|
|
.map(move |(specifier, mode)| {
|
|
|
|
let ps = ps.clone();
|
|
|
|
let permissions = permissions.clone();
|
|
|
|
let mut sender = sender.clone();
|
|
|
|
let options = options.clone();
|
|
|
|
let fail_fast_tracker = FailFastTracker::new(options.fail_fast);
|
|
|
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
if fail_fast_tracker.should_stop() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-12-05 16:17:49 -05:00
|
|
|
|
2023-01-13 16:39:19 -05:00
|
|
|
let origin = specifier.to_string();
|
|
|
|
let file_result = run_local(test_specifier(
|
|
|
|
&ps,
|
|
|
|
permissions,
|
|
|
|
specifier,
|
|
|
|
mode,
|
|
|
|
sender.clone(),
|
|
|
|
fail_fast_tracker,
|
|
|
|
options,
|
|
|
|
));
|
|
|
|
if let Err(error) = file_result {
|
|
|
|
if error.is::<JsError>() {
|
|
|
|
sender.send(TestEvent::UncaughtError(
|
|
|
|
origin,
|
|
|
|
Box::new(error.downcast::<JsError>().unwrap()),
|
|
|
|
))?;
|
|
|
|
} else {
|
|
|
|
return Err(error);
|
|
|
|
}
|
2022-05-09 05:44:50 -04:00
|
|
|
}
|
2023-01-13 16:39:19 -05:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
});
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-07-22 07:34:29 -04:00
|
|
|
let join_stream = stream::iter(join_handles)
|
2021-08-23 06:35:38 -04:00
|
|
|
.buffer_unordered(concurrent_jobs.get())
|
2021-04-28 14:17:04 -04:00
|
|
|
.collect::<Vec<Result<Result<(), AnyError>, tokio::task::JoinError>>>();
|
|
|
|
|
2022-08-04 12:38:40 -04:00
|
|
|
let mut reporter = Box::new(PrettyTestReporter::new(
|
|
|
|
concurrent_jobs.get() > 1,
|
|
|
|
log_level != Some(Level::Error),
|
|
|
|
));
|
2021-09-04 09:16:35 -04:00
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
let handler = {
|
2022-03-08 19:34:31 -05:00
|
|
|
tokio::task::spawn(async move {
|
2021-07-13 18:11:58 -04:00
|
|
|
let earlier = Instant::now();
|
2022-08-02 10:55:11 -04:00
|
|
|
let mut tests = IndexMap::new();
|
|
|
|
let mut test_steps = IndexMap::new();
|
2022-07-15 13:09:22 -04:00
|
|
|
let mut tests_with_result = HashSet::new();
|
2021-07-13 18:11:58 -04:00
|
|
|
let mut summary = TestSummary::new();
|
2021-04-28 14:17:04 -04:00
|
|
|
let mut used_only = false;
|
|
|
|
|
2022-03-08 19:34:31 -05:00
|
|
|
while let Some(event) = receiver.recv().await {
|
2021-07-14 15:05:16 -04:00
|
|
|
match event {
|
2022-07-15 13:09:22 -04:00
|
|
|
TestEvent::Register(description) => {
|
|
|
|
reporter.report_register(&description);
|
2022-08-02 10:55:11 -04:00
|
|
|
tests.insert(description.id, description);
|
2022-07-15 13:09:22 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
TestEvent::Plan(plan) => {
|
|
|
|
summary.total += plan.total;
|
|
|
|
summary.filtered_out += plan.filtered_out;
|
|
|
|
|
|
|
|
if plan.used_only {
|
2021-04-28 14:17:04 -04:00
|
|
|
used_only = true;
|
|
|
|
}
|
2021-07-14 15:05:16 -04:00
|
|
|
|
|
|
|
reporter.report_plan(&plan);
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
2021-07-13 18:11:58 -04:00
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
TestEvent::Wait(id) => {
|
2022-08-02 10:55:11 -04:00
|
|
|
reporter.report_wait(tests.get(&id).unwrap());
|
2021-07-14 15:05:16 -04:00
|
|
|
}
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-09-04 09:16:35 -04:00
|
|
|
TestEvent::Output(output) => {
|
|
|
|
reporter.report_output(&output);
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
TestEvent::Result(id, result, elapsed) => {
|
|
|
|
if tests_with_result.insert(id) {
|
2023-01-13 16:39:19 -05:00
|
|
|
let description = tests.get(&id).unwrap();
|
2022-07-15 13:09:22 -04:00
|
|
|
match &result {
|
|
|
|
TestResult::Ok => {
|
|
|
|
summary.passed += 1;
|
|
|
|
}
|
|
|
|
TestResult::Ignored => {
|
|
|
|
summary.ignored += 1;
|
|
|
|
}
|
|
|
|
TestResult::Failed(error) => {
|
|
|
|
summary.failed += 1;
|
|
|
|
summary.failures.push((description.clone(), error.clone()));
|
|
|
|
}
|
|
|
|
TestResult::Cancelled => {
|
2022-08-02 10:55:11 -04:00
|
|
|
unreachable!("should be handled in TestEvent::UncaughtError");
|
2022-07-15 13:09:22 -04:00
|
|
|
}
|
2021-07-14 15:05:16 -04:00
|
|
|
}
|
2023-01-13 16:39:19 -05:00
|
|
|
reporter.report_result(description, &result, elapsed);
|
2021-07-13 18:11:58 -04:00
|
|
|
}
|
2021-07-14 15:05:16 -04:00
|
|
|
}
|
2021-10-11 09:45:02 -04:00
|
|
|
|
2022-05-09 05:44:50 -04:00
|
|
|
TestEvent::UncaughtError(origin, error) => {
|
|
|
|
reporter.report_uncaught_error(&origin, &error);
|
|
|
|
summary.failed += 1;
|
2022-08-02 10:55:11 -04:00
|
|
|
summary.uncaught_errors.push((origin.clone(), error));
|
|
|
|
for desc in tests.values() {
|
|
|
|
if desc.origin == origin && tests_with_result.insert(desc.id) {
|
|
|
|
summary.failed += 1;
|
|
|
|
reporter.report_result(desc, &TestResult::Cancelled, 0);
|
|
|
|
}
|
|
|
|
}
|
2022-05-09 05:44:50 -04:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
TestEvent::StepRegister(description) => {
|
|
|
|
reporter.report_step_register(&description);
|
|
|
|
test_steps.insert(description.id, description);
|
|
|
|
}
|
|
|
|
|
|
|
|
TestEvent::StepWait(id) => {
|
|
|
|
reporter.report_step_wait(test_steps.get(&id).unwrap());
|
2021-10-11 09:45:02 -04:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
TestEvent::StepResult(id, result, duration) => {
|
2021-11-15 10:20:37 -05:00
|
|
|
match &result {
|
|
|
|
TestStepResult::Ok => {
|
|
|
|
summary.passed_steps += 1;
|
|
|
|
}
|
|
|
|
TestStepResult::Ignored => {
|
|
|
|
summary.ignored_steps += 1;
|
|
|
|
}
|
|
|
|
TestStepResult::Failed(_) => {
|
|
|
|
summary.failed_steps += 1;
|
|
|
|
}
|
|
|
|
TestStepResult::Pending(_) => {
|
|
|
|
summary.pending_steps += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
reporter.report_step_result(
|
|
|
|
test_steps.get(&id).unwrap(),
|
|
|
|
&result,
|
|
|
|
duration,
|
2022-08-04 12:38:40 -04:00
|
|
|
&tests,
|
|
|
|
&test_steps,
|
2022-07-15 13:09:22 -04:00
|
|
|
);
|
2021-10-11 09:45:02 -04:00
|
|
|
}
|
2021-07-14 15:05:16 -04:00
|
|
|
}
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:11:58 -04:00
|
|
|
let elapsed = Instant::now().duration_since(earlier);
|
2021-07-14 15:05:16 -04:00
|
|
|
reporter.report_summary(&summary, &elapsed);
|
2021-04-28 14:17:04 -04:00
|
|
|
|
|
|
|
if used_only {
|
2021-07-22 07:34:29 -04:00
|
|
|
return Err(generic_error(
|
|
|
|
"Test failed because the \"only\" option was used",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if summary.failed > 0 {
|
|
|
|
return Err(generic_error("Test failed"));
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:34:29 -04:00
|
|
|
Ok(())
|
2021-04-28 14:17:04 -04:00
|
|
|
})
|
2020-04-02 09:26:40 -04:00
|
|
|
};
|
|
|
|
|
2021-07-22 07:34:29 -04:00
|
|
|
let (join_results, result) = future::join(join_stream, handler).await;
|
2020-10-14 09:19:13 -04:00
|
|
|
|
2021-09-08 11:18:07 -04:00
|
|
|
// propagate any errors
|
|
|
|
for join_result in join_results {
|
|
|
|
join_result??;
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
2021-07-22 07:34:29 -04:00
|
|
|
|
2021-09-08 11:18:07 -04:00
|
|
|
result??;
|
2021-07-22 07:34:29 -04:00
|
|
|
|
|
|
|
Ok(())
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
2021-08-26 15:21:58 -04:00
|
|
|
|
2022-11-28 17:28:54 -05:00
|
|
|
/// Checks if the path has a basename and extension Deno supports for tests.
|
|
|
|
fn is_supported_test_path(path: &Path) -> bool {
|
|
|
|
if let Some(name) = path.file_stem() {
|
|
|
|
let basename = name.to_string_lossy();
|
|
|
|
(basename.ends_with("_test")
|
|
|
|
|| basename.ends_with(".test")
|
|
|
|
|| basename == "test")
|
|
|
|
&& is_supported_ext(path)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if the path has an extension Deno supports for tests.
|
|
|
|
fn is_supported_test_ext(path: &Path) -> bool {
|
|
|
|
if let Some(ext) = get_extension(path) {
|
|
|
|
matches!(
|
|
|
|
ext.as_str(),
|
|
|
|
"ts"
|
|
|
|
| "tsx"
|
|
|
|
| "js"
|
|
|
|
| "jsx"
|
|
|
|
| "mjs"
|
|
|
|
| "mts"
|
|
|
|
| "cjs"
|
|
|
|
| "cts"
|
|
|
|
| "md"
|
|
|
|
| "mkd"
|
|
|
|
| "mkdn"
|
|
|
|
| "mdwn"
|
|
|
|
| "mdown"
|
|
|
|
| "markdown"
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
/// Collects specifiers marking them with the appropriate test mode while maintaining the natural
|
|
|
|
/// input order.
|
|
|
|
///
|
|
|
|
/// - Specifiers matching the `is_supported_test_ext` predicate are marked as
|
|
|
|
/// `TestMode::Documentation`.
|
|
|
|
/// - Specifiers matching the `is_supported_test_path` are marked as `TestMode::Executable`.
|
|
|
|
/// - Specifiers matching both predicates are marked as `TestMode::Both`
|
|
|
|
fn collect_specifiers_with_test_mode(
|
2023-01-13 16:39:19 -05:00
|
|
|
files: &FilesConfig,
|
|
|
|
include_inline: &bool,
|
2021-08-26 15:21:58 -04:00
|
|
|
) -> Result<Vec<(ModuleSpecifier, TestMode)>, AnyError> {
|
2023-01-13 16:39:19 -05:00
|
|
|
let module_specifiers = collect_specifiers(files, is_supported_test_path)?;
|
2021-08-26 15:21:58 -04:00
|
|
|
|
2023-01-13 16:39:19 -05:00
|
|
|
if *include_inline {
|
|
|
|
return collect_specifiers(files, is_supported_test_ext).map(
|
2021-08-26 15:21:58 -04:00
|
|
|
|specifiers| {
|
|
|
|
specifiers
|
|
|
|
.into_iter()
|
|
|
|
.map(|specifier| {
|
|
|
|
let mode = if module_specifiers.contains(&specifier) {
|
|
|
|
TestMode::Both
|
|
|
|
} else {
|
|
|
|
TestMode::Documentation
|
|
|
|
};
|
|
|
|
|
|
|
|
(specifier, mode)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let specifiers_with_mode = module_specifiers
|
|
|
|
.into_iter()
|
|
|
|
.map(|specifier| (specifier, TestMode::Executable))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(specifiers_with_mode)
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
/// Collects module and document specifiers with test modes via
|
|
|
|
/// `collect_specifiers_with_test_mode` which are then pre-fetched and adjusted
|
|
|
|
/// based on the media type.
|
2021-08-26 15:21:58 -04:00
|
|
|
///
|
2021-10-10 17:26:22 -04:00
|
|
|
/// Specifiers that do not have a known media type that can be executed as a
|
|
|
|
/// module are marked as `TestMode::Documentation`. Type definition files
|
|
|
|
/// cannot be run, and therefore need to be marked as `TestMode::Documentation`
|
|
|
|
/// as well.
|
2021-08-26 15:21:58 -04:00
|
|
|
async fn fetch_specifiers_with_test_mode(
|
2022-02-11 14:04:31 -05:00
|
|
|
ps: &ProcState,
|
2023-01-13 16:39:19 -05:00
|
|
|
files: &FilesConfig,
|
|
|
|
doc: &bool,
|
2021-08-26 15:21:58 -04:00
|
|
|
) -> Result<Vec<(ModuleSpecifier, TestMode)>, AnyError> {
|
2023-01-07 15:22:09 -05:00
|
|
|
let mut specifiers_with_mode = collect_specifiers_with_test_mode(files, doc)?;
|
2023-01-13 16:39:19 -05:00
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
for (specifier, mode) in &mut specifiers_with_mode {
|
2021-09-24 11:10:42 -04:00
|
|
|
let file = ps
|
2021-08-26 15:21:58 -04:00
|
|
|
.file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(specifier, PermissionsContainer::allow_all())
|
2021-08-26 15:21:58 -04:00
|
|
|
.await?;
|
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
if file.media_type == MediaType::Unknown
|
|
|
|
|| file.media_type == MediaType::Dts
|
|
|
|
{
|
2021-08-26 15:21:58 -04:00
|
|
|
*mode = TestMode::Documentation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(specifiers_with_mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_tests(
|
2023-01-07 15:22:09 -05:00
|
|
|
cli_options: CliOptions,
|
|
|
|
test_options: TestOptions,
|
2021-08-26 15:21:58 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2023-01-07 15:22:09 -05:00
|
|
|
let ps = ProcState::from_options(Arc::new(cli_options)).await?;
|
2023-01-07 11:25:34 -05:00
|
|
|
// Various test files should not share the same permissions in terms of
|
|
|
|
// `PermissionsContainer` - otherwise granting/revoking permissions in one
|
|
|
|
// file would have impact on other files, which is undesirable.
|
2022-06-29 11:51:11 -04:00
|
|
|
let permissions =
|
2022-08-10 15:13:53 -04:00
|
|
|
Permissions::from_options(&ps.options.permissions_options())?;
|
2021-08-26 15:21:58 -04:00
|
|
|
|
2023-01-13 16:39:19 -05:00
|
|
|
let specifiers_with_mode = fetch_specifiers_with_test_mode(
|
|
|
|
&ps,
|
|
|
|
&test_options.files,
|
|
|
|
&test_options.doc,
|
|
|
|
)
|
|
|
|
.await?;
|
2023-01-07 15:22:09 -05:00
|
|
|
|
|
|
|
if !test_options.allow_none && specifiers_with_mode.is_empty() {
|
2021-08-26 15:21:58 -04:00
|
|
|
return Err(generic_error("No test modules found"));
|
|
|
|
}
|
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
check_specifiers(&ps, permissions.clone(), specifiers_with_mode.clone())
|
2022-02-11 14:04:31 -05:00
|
|
|
.await?;
|
2021-08-26 15:21:58 -04:00
|
|
|
|
2023-01-07 15:22:09 -05:00
|
|
|
if test_options.no_run {
|
2021-08-26 15:21:58 -04:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
test_specifiers(
|
2023-01-16 15:27:41 -05:00
|
|
|
&ps,
|
2023-01-13 16:39:19 -05:00
|
|
|
&permissions,
|
2021-08-26 15:21:58 -04:00
|
|
|
specifiers_with_mode,
|
2021-12-30 11:18:30 -05:00
|
|
|
TestSpecifierOptions {
|
2023-01-07 15:22:09 -05:00
|
|
|
concurrent_jobs: test_options.concurrent_jobs,
|
|
|
|
fail_fast: test_options.fail_fast,
|
|
|
|
filter: TestFilter::from_flag(&test_options.filter),
|
2021-12-30 11:18:30 -05:00
|
|
|
},
|
2021-08-26 15:21:58 -04:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_tests_with_watch(
|
2023-01-07 15:22:09 -05:00
|
|
|
cli_options: CliOptions,
|
|
|
|
test_options: TestOptions,
|
2021-08-26 15:21:58 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2023-01-07 15:22:09 -05:00
|
|
|
let ps = ProcState::from_options(Arc::new(cli_options)).await?;
|
2023-01-07 11:25:34 -05:00
|
|
|
// Various test files should not share the same permissions in terms of
|
|
|
|
// `PermissionsContainer` - otherwise granting/revoking permissions in one
|
|
|
|
// file would have impact on other files, which is undesirable.
|
2022-06-29 11:51:11 -04:00
|
|
|
let permissions =
|
2022-08-10 15:13:53 -04:00
|
|
|
Permissions::from_options(&ps.options.permissions_options())?;
|
2022-06-29 11:51:11 -04:00
|
|
|
let no_check = ps.options.type_check_mode() == TypeCheckMode::None;
|
2021-08-26 15:21:58 -04:00
|
|
|
|
2023-01-10 10:28:10 -05:00
|
|
|
let ps = RefCell::new(ps);
|
|
|
|
|
2021-08-26 15:21:58 -04:00
|
|
|
let resolver = |changed: Option<Vec<PathBuf>>| {
|
2023-01-13 16:39:19 -05:00
|
|
|
let paths_to_watch = test_options.files.include.clone();
|
2021-08-26 15:21:58 -04:00
|
|
|
let paths_to_watch_clone = paths_to_watch.clone();
|
|
|
|
let files_changed = changed.is_some();
|
2023-01-13 16:39:19 -05:00
|
|
|
let test_options = &test_options;
|
2023-01-10 10:28:10 -05:00
|
|
|
let ps = ps.borrow().clone();
|
2021-08-26 15:21:58 -04:00
|
|
|
|
|
|
|
async move {
|
2023-01-07 15:22:09 -05:00
|
|
|
let test_modules = if test_options.doc {
|
|
|
|
collect_specifiers(&test_options.files, is_supported_test_ext)
|
2021-08-26 15:21:58 -04:00
|
|
|
} else {
|
2023-01-07 15:22:09 -05:00
|
|
|
collect_specifiers(&test_options.files, is_supported_test_path)
|
2021-08-26 15:21:58 -04:00
|
|
|
}?;
|
|
|
|
|
|
|
|
let mut paths_to_watch = paths_to_watch_clone;
|
|
|
|
let mut modules_to_reload = if files_changed {
|
|
|
|
Vec::new()
|
|
|
|
} else {
|
2023-01-24 08:23:19 -05:00
|
|
|
test_modules.clone()
|
2021-08-26 15:21:58 -04:00
|
|
|
};
|
2023-01-24 08:23:19 -05:00
|
|
|
let graph = ps.create_graph(test_modules.clone()).await?;
|
2022-07-01 11:50:16 -04:00
|
|
|
graph_valid(&graph, !no_check, ps.options.check_js())?;
|
2021-08-26 15:21:58 -04:00
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
// TODO(@kitsonk) - This should be totally derivable from the graph.
|
2021-08-26 15:21:58 -04:00
|
|
|
for specifier in test_modules {
|
|
|
|
fn get_dependencies<'a>(
|
2021-10-10 17:26:22 -04:00
|
|
|
graph: &'a deno_graph::ModuleGraph,
|
|
|
|
maybe_module: Option<&'a deno_graph::Module>,
|
2021-08-26 15:21:58 -04:00
|
|
|
// This needs to be accessible to skip getting dependencies if they're already there,
|
|
|
|
// otherwise this will cause a stack overflow with circular dependencies
|
|
|
|
output: &mut HashSet<&'a ModuleSpecifier>,
|
2021-12-16 05:45:41 -05:00
|
|
|
no_check: bool,
|
2021-10-10 17:26:22 -04:00
|
|
|
) {
|
2022-01-31 17:33:57 -05:00
|
|
|
if let Some(module) = maybe_module {
|
2021-10-10 17:26:22 -04:00
|
|
|
for dep in module.dependencies.values() {
|
|
|
|
if let Some(specifier) = &dep.get_code() {
|
|
|
|
if !output.contains(specifier) {
|
|
|
|
output.insert(specifier);
|
2021-12-16 05:45:41 -05:00
|
|
|
get_dependencies(
|
|
|
|
graph,
|
|
|
|
graph.get(specifier),
|
|
|
|
output,
|
|
|
|
no_check,
|
|
|
|
);
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
2021-08-26 15:21:58 -04:00
|
|
|
}
|
2021-12-16 05:45:41 -05:00
|
|
|
if !no_check {
|
|
|
|
if let Some(specifier) = &dep.get_type() {
|
|
|
|
if !output.contains(specifier) {
|
|
|
|
output.insert(specifier);
|
|
|
|
get_dependencies(
|
|
|
|
graph,
|
|
|
|
graph.get(specifier),
|
|
|
|
output,
|
|
|
|
no_check,
|
|
|
|
);
|
|
|
|
}
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
2021-08-26 15:21:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test module and all it's dependencies
|
|
|
|
let mut modules = HashSet::new();
|
|
|
|
modules.insert(&specifier);
|
2021-12-16 05:45:41 -05:00
|
|
|
get_dependencies(&graph, graph.get(&specifier), &mut modules, no_check);
|
2021-08-26 15:21:58 -04:00
|
|
|
|
|
|
|
paths_to_watch.extend(
|
|
|
|
modules
|
|
|
|
.iter()
|
|
|
|
.filter_map(|specifier| specifier.to_file_path().ok()),
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(changed) = &changed {
|
|
|
|
for path in changed.iter().filter_map(|path| {
|
|
|
|
deno_core::resolve_url_or_path(&path.to_string_lossy()).ok()
|
|
|
|
}) {
|
2023-01-16 15:27:41 -05:00
|
|
|
if modules.contains(&path) {
|
2023-01-24 08:23:19 -05:00
|
|
|
modules_to_reload.push(specifier);
|
2021-08-26 15:21:58 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((paths_to_watch, modules_to_reload))
|
|
|
|
}
|
|
|
|
.map(move |result| {
|
|
|
|
if files_changed
|
|
|
|
&& matches!(result, Ok((_, ref modules)) if modules.is_empty())
|
|
|
|
{
|
|
|
|
ResolutionResult::Ignore
|
|
|
|
} else {
|
|
|
|
match result {
|
|
|
|
Ok((paths_to_watch, modules_to_reload)) => {
|
|
|
|
ResolutionResult::Restart {
|
|
|
|
paths_to_watch,
|
|
|
|
result: Ok(modules_to_reload),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => ResolutionResult::Restart {
|
|
|
|
paths_to_watch,
|
|
|
|
result: Err(e),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2023-01-24 08:23:19 -05:00
|
|
|
let operation = |modules_to_reload: Vec<ModuleSpecifier>| {
|
2023-01-13 16:39:19 -05:00
|
|
|
let permissions = &permissions;
|
|
|
|
let test_options = &test_options;
|
2023-01-10 10:28:10 -05:00
|
|
|
ps.borrow_mut().reset_for_file_watcher();
|
|
|
|
let ps = ps.borrow().clone();
|
2021-08-26 15:21:58 -04:00
|
|
|
|
|
|
|
async move {
|
|
|
|
let specifiers_with_mode = fetch_specifiers_with_test_mode(
|
2022-02-11 14:04:31 -05:00
|
|
|
&ps,
|
2023-01-13 16:39:19 -05:00
|
|
|
&test_options.files,
|
|
|
|
&test_options.doc,
|
2021-08-26 15:21:58 -04:00
|
|
|
)
|
|
|
|
.await?
|
2023-01-16 15:27:41 -05:00
|
|
|
.into_iter()
|
2023-01-24 08:23:19 -05:00
|
|
|
.filter(|(specifier, _)| modules_to_reload.contains(specifier))
|
2021-08-26 15:21:58 -04:00
|
|
|
.collect::<Vec<(ModuleSpecifier, TestMode)>>();
|
|
|
|
|
2022-06-28 16:45:55 -04:00
|
|
|
check_specifiers(&ps, permissions.clone(), specifiers_with_mode.clone())
|
|
|
|
.await?;
|
2021-08-26 15:21:58 -04:00
|
|
|
|
2023-01-07 15:22:09 -05:00
|
|
|
if test_options.no_run {
|
2021-08-26 15:21:58 -04:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
test_specifiers(
|
2023-01-16 15:27:41 -05:00
|
|
|
&ps,
|
2023-01-13 16:39:19 -05:00
|
|
|
permissions,
|
2021-08-26 15:21:58 -04:00
|
|
|
specifiers_with_mode,
|
2021-12-30 11:18:30 -05:00
|
|
|
TestSpecifierOptions {
|
2023-01-07 15:22:09 -05:00
|
|
|
concurrent_jobs: test_options.concurrent_jobs,
|
|
|
|
fail_fast: test_options.fail_fast,
|
|
|
|
filter: TestFilter::from_flag(&test_options.filter),
|
2021-12-30 11:18:30 -05:00
|
|
|
},
|
2021-08-26 15:21:58 -04:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-10 10:28:10 -05:00
|
|
|
let clear_screen = !ps.borrow().options.no_clear_screen();
|
2022-01-31 11:39:39 -05:00
|
|
|
file_watcher::watch_func(
|
|
|
|
resolver,
|
|
|
|
operation,
|
|
|
|
file_watcher::PrintConfig {
|
|
|
|
job_name: "Test".to_string(),
|
2023-01-10 10:28:10 -05:00
|
|
|
clear_screen,
|
2022-01-31 11:39:39 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2021-08-26 15:21:58 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-26 19:00:04 -04:00
|
|
|
|
2022-12-05 16:17:49 -05:00
|
|
|
/// Tracks failures for the `--fail-fast` argument in
|
|
|
|
/// order to tell when to stop running tests.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct FailFastTracker {
|
|
|
|
max_count: Option<usize>,
|
|
|
|
failure_count: Arc<AtomicUsize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FailFastTracker {
|
|
|
|
pub fn new(fail_fast: Option<NonZeroUsize>) -> Self {
|
|
|
|
Self {
|
|
|
|
max_count: fail_fast.map(|v| v.into()),
|
|
|
|
failure_count: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_failure(&self) -> bool {
|
|
|
|
if let Some(max_count) = &self.max_count {
|
|
|
|
self
|
|
|
|
.failure_count
|
|
|
|
.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
|
|
|
|
>= *max_count
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn should_stop(&self) -> bool {
|
|
|
|
if let Some(max_count) = &self.max_count {
|
|
|
|
self.failure_count.load(std::sync::atomic::Ordering::SeqCst) >= *max_count
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 17:01:51 -04:00
|
|
|
#[derive(Clone)]
|
2022-05-01 14:44:55 -04:00
|
|
|
pub struct TestEventSender {
|
2022-04-26 19:00:04 -04:00
|
|
|
sender: UnboundedSender<TestEvent>,
|
2022-05-04 17:01:51 -04:00
|
|
|
stdout_writer: TestOutputPipe,
|
|
|
|
stderr_writer: TestOutputPipe,
|
2022-05-01 14:44:55 -04:00
|
|
|
}
|
2022-04-26 19:00:04 -04:00
|
|
|
|
2022-05-01 14:44:55 -04:00
|
|
|
impl TestEventSender {
|
|
|
|
pub fn new(sender: UnboundedSender<TestEvent>) -> Self {
|
|
|
|
Self {
|
2022-05-04 17:01:51 -04:00
|
|
|
stdout_writer: TestOutputPipe::new(sender.clone()),
|
|
|
|
stderr_writer: TestOutputPipe::new(sender.clone()),
|
2022-05-01 14:44:55 -04:00
|
|
|
sender,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stdout(&self) -> std::fs::File {
|
2022-05-04 17:01:51 -04:00
|
|
|
self.stdout_writer.as_file()
|
2022-05-01 14:44:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stderr(&self) -> std::fs::File {
|
2022-05-04 17:01:51 -04:00
|
|
|
self.stderr_writer.as_file()
|
2022-05-01 14:44:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send(&mut self, message: TestEvent) -> Result<(), AnyError> {
|
|
|
|
// for any event that finishes collecting output, we need to
|
|
|
|
// ensure that the collected stdout and stderr pipes are flushed
|
|
|
|
if matches!(
|
|
|
|
message,
|
|
|
|
TestEvent::Result(_, _, _)
|
|
|
|
| TestEvent::StepWait(_)
|
|
|
|
| TestEvent::StepResult(_, _, _)
|
2022-12-05 16:17:49 -05:00
|
|
|
| TestEvent::UncaughtError(_, _)
|
2022-05-01 14:44:55 -04:00
|
|
|
) {
|
2023-01-11 20:30:23 -05:00
|
|
|
self.flush_stdout_and_stderr()?;
|
2022-05-01 14:44:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
self.sender.send(message)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-05-04 17:01:51 -04:00
|
|
|
|
2023-01-11 20:30:23 -05:00
|
|
|
fn flush_stdout_and_stderr(&mut self) -> Result<(), AnyError> {
|
|
|
|
self.stdout_writer.flush()?;
|
|
|
|
self.stderr_writer.flush()?;
|
|
|
|
|
|
|
|
Ok(())
|
2022-05-04 17:01:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// use a string that if it ends up in the output won't affect how things are displayed
|
|
|
|
const ZERO_WIDTH_SPACE: &str = "\u{200B}";
|
|
|
|
|
|
|
|
struct TestOutputPipe {
|
|
|
|
writer: os_pipe::PipeWriter,
|
|
|
|
state: Arc<Mutex<Option<std::sync::mpsc::Sender<()>>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for TestOutputPipe {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
writer: self.writer.try_clone().unwrap(),
|
|
|
|
state: self.state.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestOutputPipe {
|
|
|
|
pub fn new(sender: UnboundedSender<TestEvent>) -> Self {
|
|
|
|
let (reader, writer) = os_pipe::pipe().unwrap();
|
|
|
|
let state = Arc::new(Mutex::new(None));
|
|
|
|
|
|
|
|
start_output_redirect_thread(reader, sender, state.clone());
|
|
|
|
|
|
|
|
Self { writer, state }
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:30:23 -05:00
|
|
|
pub fn flush(&mut self) -> Result<(), AnyError> {
|
2022-05-04 17:01:51 -04:00
|
|
|
// We want to wake up the other thread and have it respond back
|
|
|
|
// that it's done clearing out its pipe before returning.
|
|
|
|
let (sender, receiver) = std::sync::mpsc::channel();
|
2022-05-10 17:59:35 -04:00
|
|
|
if let Some(sender) = self.state.lock().replace(sender) {
|
|
|
|
let _ = sender.send(()); // just in case
|
|
|
|
}
|
2022-05-10 16:24:37 -04:00
|
|
|
// Bit of a hack to send a zero width space in order to wake
|
|
|
|
// the thread up. It seems that sending zero bytes here does
|
|
|
|
// not work on windows.
|
2023-01-11 20:30:23 -05:00
|
|
|
self.writer.write_all(ZERO_WIDTH_SPACE.as_bytes())?;
|
|
|
|
self.writer.flush()?;
|
2022-05-10 17:59:35 -04:00
|
|
|
// ignore the error as it might have been picked up and closed
|
|
|
|
let _ = receiver.recv();
|
2023-01-11 20:30:23 -05:00
|
|
|
|
|
|
|
Ok(())
|
2022-05-04 17:01:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_file(&self) -> std::fs::File {
|
|
|
|
pipe_writer_to_file(self.writer.try_clone().unwrap())
|
|
|
|
}
|
2022-04-26 19:00:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn pipe_writer_to_file(writer: os_pipe::PipeWriter) -> std::fs::File {
|
|
|
|
use std::os::windows::prelude::FromRawHandle;
|
|
|
|
use std::os::windows::prelude::IntoRawHandle;
|
|
|
|
// SAFETY: Requires consuming ownership of the provided handle
|
|
|
|
unsafe { std::fs::File::from_raw_handle(writer.into_raw_handle()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn pipe_writer_to_file(writer: os_pipe::PipeWriter) -> std::fs::File {
|
|
|
|
use std::os::unix::io::FromRawFd;
|
|
|
|
use std::os::unix::io::IntoRawFd;
|
|
|
|
// SAFETY: Requires consuming ownership of the provided handle
|
|
|
|
unsafe { std::fs::File::from_raw_fd(writer.into_raw_fd()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_output_redirect_thread(
|
|
|
|
mut pipe_reader: os_pipe::PipeReader,
|
|
|
|
sender: UnboundedSender<TestEvent>,
|
2022-05-04 17:01:51 -04:00
|
|
|
flush_state: Arc<Mutex<Option<std::sync::mpsc::Sender<()>>>>,
|
2022-04-26 19:00:04 -04:00
|
|
|
) {
|
|
|
|
tokio::task::spawn_blocking(move || loop {
|
|
|
|
let mut buffer = [0; 512];
|
|
|
|
let size = match pipe_reader.read(&mut buffer) {
|
|
|
|
Ok(0) | Err(_) => break,
|
|
|
|
Ok(size) => size,
|
|
|
|
};
|
2022-05-04 17:01:51 -04:00
|
|
|
let oneshot_sender = flush_state.lock().take();
|
|
|
|
let mut data = &buffer[0..size];
|
|
|
|
if data.ends_with(ZERO_WIDTH_SPACE.as_bytes()) {
|
|
|
|
data = &data[0..data.len() - ZERO_WIDTH_SPACE.len()];
|
|
|
|
}
|
2022-05-01 14:44:55 -04:00
|
|
|
|
2022-05-04 17:01:51 -04:00
|
|
|
if !data.is_empty()
|
|
|
|
&& sender
|
|
|
|
.send(TestEvent::Output(buffer[0..size].to_vec()))
|
|
|
|
.is_err()
|
2022-04-26 19:00:04 -04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2022-05-04 17:01:51 -04:00
|
|
|
|
|
|
|
// Always respond back if this was set. Ideally we would also check to
|
|
|
|
// ensure the pipe reader is empty before sending back this response.
|
|
|
|
if let Some(sender) = oneshot_sender {
|
|
|
|
let _ignore = sender.send(());
|
|
|
|
}
|
2022-04-26 19:00:04 -04:00
|
|
|
});
|
|
|
|
}
|
2022-11-28 17:28:54 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod inner_test {
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_supported_test_ext() {
|
|
|
|
assert!(!is_supported_test_ext(Path::new("tests/subdir/redirects")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("README.md")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("readme.MD")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("lib/typescript.d.ts")));
|
|
|
|
assert!(is_supported_test_ext(Path::new(
|
|
|
|
"testdata/run/001_hello.js"
|
|
|
|
)));
|
|
|
|
assert!(is_supported_test_ext(Path::new(
|
|
|
|
"testdata/run/002_hello.ts"
|
|
|
|
)));
|
|
|
|
assert!(is_supported_test_ext(Path::new("foo.jsx")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("foo.tsx")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("foo.TS")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("foo.TSX")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("foo.JS")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("foo.JSX")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("foo.mjs")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("foo.mts")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("foo.cjs")));
|
|
|
|
assert!(is_supported_test_ext(Path::new("foo.cts")));
|
|
|
|
assert!(!is_supported_test_ext(Path::new("foo.mjsx")));
|
|
|
|
assert!(!is_supported_test_ext(Path::new("foo.jsonc")));
|
|
|
|
assert!(!is_supported_test_ext(Path::new("foo.JSONC")));
|
|
|
|
assert!(!is_supported_test_ext(Path::new("foo.json")));
|
|
|
|
assert!(!is_supported_test_ext(Path::new("foo.JsON")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_supported_test_path() {
|
|
|
|
assert!(is_supported_test_path(Path::new(
|
|
|
|
"tests/subdir/foo_test.ts"
|
|
|
|
)));
|
|
|
|
assert!(is_supported_test_path(Path::new(
|
|
|
|
"tests/subdir/foo_test.tsx"
|
|
|
|
)));
|
|
|
|
assert!(is_supported_test_path(Path::new(
|
|
|
|
"tests/subdir/foo_test.js"
|
|
|
|
)));
|
|
|
|
assert!(is_supported_test_path(Path::new(
|
|
|
|
"tests/subdir/foo_test.jsx"
|
|
|
|
)));
|
|
|
|
assert!(is_supported_test_path(Path::new("bar/foo.test.ts")));
|
|
|
|
assert!(is_supported_test_path(Path::new("bar/foo.test.tsx")));
|
|
|
|
assert!(is_supported_test_path(Path::new("bar/foo.test.js")));
|
|
|
|
assert!(is_supported_test_path(Path::new("bar/foo.test.jsx")));
|
|
|
|
assert!(is_supported_test_path(Path::new("foo/bar/test.js")));
|
|
|
|
assert!(is_supported_test_path(Path::new("foo/bar/test.jsx")));
|
|
|
|
assert!(is_supported_test_path(Path::new("foo/bar/test.ts")));
|
|
|
|
assert!(is_supported_test_path(Path::new("foo/bar/test.tsx")));
|
|
|
|
assert!(!is_supported_test_path(Path::new("README.md")));
|
|
|
|
assert!(!is_supported_test_path(Path::new("lib/typescript.d.ts")));
|
|
|
|
assert!(!is_supported_test_path(Path::new("notatest.js")));
|
|
|
|
assert!(!is_supported_test_path(Path::new("NotAtest.ts")));
|
|
|
|
}
|
|
|
|
}
|