2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-02-11 06:01:56 -05:00
|
|
|
|
2021-05-10 19:54:39 -04:00
|
|
|
use crate::ast;
|
2021-07-29 15:03:06 -04:00
|
|
|
use crate::ast::Location;
|
2021-04-28 14:17:04 -04:00
|
|
|
use crate::colors;
|
|
|
|
use crate::create_main_worker;
|
|
|
|
use crate::file_fetcher::File;
|
2021-05-10 19:54:39 -04:00
|
|
|
use crate::fs_util::collect_files;
|
|
|
|
use crate::fs_util::normalize_path;
|
2021-04-28 14:17:04 -04:00
|
|
|
use crate::media_type::MediaType;
|
|
|
|
use crate::module_graph;
|
|
|
|
use crate::program_state::ProgramState;
|
|
|
|
use crate::tokio_util;
|
|
|
|
use crate::tools::coverage::CoverageCollector;
|
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;
|
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;
|
2021-06-21 19:45:41 -04:00
|
|
|
use deno_core::located_script_name;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json::json;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2021-04-28 14:17:04 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_runtime::permissions::Permissions;
|
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;
|
2020-02-17 13:11:45 -05:00
|
|
|
use std::path::Path;
|
2021-04-28 14:17:04 -04:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
use std::sync::mpsc::Sender;
|
|
|
|
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;
|
2021-05-10 19:54:39 -04:00
|
|
|
use swc_common::comments::CommentKind;
|
2021-07-26 08:05:44 -04:00
|
|
|
use uuid::Uuid;
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct TestDescription {
|
|
|
|
pub origin: String,
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum TestResult {
|
|
|
|
Ok,
|
|
|
|
Ignored,
|
|
|
|
Failed(String),
|
|
|
|
}
|
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
#[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 {
|
|
|
|
Plan(TestPlan),
|
|
|
|
Wait(TestDescription),
|
|
|
|
Result(TestDescription, TestResult, 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,
|
|
|
|
pub filtered_out: usize,
|
|
|
|
pub measured: usize,
|
2021-07-14 15:05:16 -04:00
|
|
|
pub failures: Vec<(TestDescription, String)>,
|
2021-07-13 18:11:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestSummary {
|
|
|
|
fn new() -> TestSummary {
|
|
|
|
TestSummary {
|
|
|
|
total: 0,
|
|
|
|
passed: 0,
|
|
|
|
failed: 0,
|
|
|
|
ignored: 0,
|
|
|
|
filtered_out: 0,
|
|
|
|
measured: 0,
|
|
|
|
failures: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_failed(&self) -> bool {
|
|
|
|
self.failed > 0 || !self.failures.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_pending(&self) -> bool {
|
|
|
|
self.total - self.passed - self.failed - self.ignored > 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 07:42:35 -04:00
|
|
|
trait TestReporter {
|
2021-07-14 15:05:16 -04:00
|
|
|
fn report_plan(&mut self, plan: &TestPlan);
|
|
|
|
fn report_wait(&mut self, description: &TestDescription);
|
|
|
|
fn report_result(
|
|
|
|
&mut self,
|
|
|
|
description: &TestDescription,
|
|
|
|
result: &TestResult,
|
|
|
|
elapsed: u64,
|
|
|
|
);
|
|
|
|
fn report_summary(&mut self, summary: &TestSummary, elapsed: &Duration);
|
2021-04-29 07:42:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PrettyTestReporter {
|
|
|
|
concurrent: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PrettyTestReporter {
|
|
|
|
fn new(concurrent: bool) -> PrettyTestReporter {
|
2021-07-13 18:11:58 -04:00
|
|
|
PrettyTestReporter { concurrent }
|
2021-04-29 07:42:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestReporter for PrettyTestReporter {
|
2021-07-14 15:05:16 -04:00
|
|
|
fn report_plan(&mut self, plan: &TestPlan) {
|
|
|
|
let inflection = if plan.total == 1 { "test" } else { "tests" };
|
|
|
|
println!("running {} {} from {}", plan.total, inflection, plan.origin);
|
|
|
|
}
|
2021-04-29 07:42:35 -04:00
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
fn report_wait(&mut self, description: &TestDescription) {
|
|
|
|
if !self.concurrent {
|
|
|
|
print!("test {} ...", description.name);
|
|
|
|
}
|
|
|
|
}
|
2021-04-29 07:42:35 -04:00
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
fn report_result(
|
|
|
|
&mut self,
|
|
|
|
description: &TestDescription,
|
|
|
|
result: &TestResult,
|
|
|
|
elapsed: u64,
|
|
|
|
) {
|
|
|
|
if self.concurrent {
|
|
|
|
print!("test {} ...", description.name);
|
|
|
|
}
|
2021-07-13 18:11:58 -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(),
|
|
|
|
};
|
2021-07-13 18:11:58 -04:00
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
println!(
|
|
|
|
" {} {}",
|
|
|
|
status,
|
|
|
|
colors::gray(format!("({}ms)", elapsed)).to_string()
|
|
|
|
);
|
2021-04-29 07:42:35 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
fn report_summary(&mut self, summary: &TestSummary, elapsed: &Duration) {
|
2021-07-13 18:11:58 -04:00
|
|
|
if !summary.failures.is_empty() {
|
2021-04-29 07:42:35 -04:00
|
|
|
println!("\nfailures:\n");
|
2021-07-14 15:05:16 -04:00
|
|
|
for (description, error) in &summary.failures {
|
|
|
|
println!("{}", description.name);
|
2021-04-29 07:42:35 -04:00
|
|
|
println!("{}", error);
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("failures:\n");
|
2021-07-14 15:05:16 -04:00
|
|
|
for (description, _) in &summary.failures {
|
|
|
|
println!("\t{}", description.name);
|
2021-04-29 07:42:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 18:11:58 -04:00
|
|
|
let status = if summary.has_failed() || summary.has_pending() {
|
2021-04-29 07:42:35 -04:00
|
|
|
colors::red("FAILED").to_string()
|
|
|
|
} else {
|
|
|
|
colors::green("ok").to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
println!(
|
2021-07-14 15:05:16 -04:00
|
|
|
"\ntest result: {}. {} passed; {} failed; {} ignored; {} measured; {} filtered out {}\n",
|
|
|
|
status,
|
|
|
|
summary.passed,
|
|
|
|
summary.failed,
|
|
|
|
summary.ignored,
|
|
|
|
summary.measured,
|
|
|
|
summary.filtered_out,
|
|
|
|
colors::gray(format!("({}ms)", elapsed.as_millis())),
|
|
|
|
);
|
2021-04-29 07:42:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-10 10:51:30 -04:00
|
|
|
fn create_reporter(concurrent: bool) -> Box<dyn TestReporter + Send> {
|
|
|
|
Box::new(PrettyTestReporter::new(concurrent))
|
2021-04-29 07:42:35 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 19:54:39 -04:00
|
|
|
pub(crate) fn is_supported(p: &Path) -> bool {
|
2020-02-17 13:11:45 -05:00
|
|
|
use std::path::Component;
|
|
|
|
if let Some(Component::Normal(basename_os_str)) = p.components().next_back() {
|
|
|
|
let basename = basename_os_str.to_string_lossy();
|
|
|
|
basename.ends_with("_test.ts")
|
2020-03-14 08:23:53 -04:00
|
|
|
|| basename.ends_with("_test.tsx")
|
2020-02-17 13:11:45 -05:00
|
|
|
|| basename.ends_with("_test.js")
|
2020-06-05 17:01:44 -04:00
|
|
|
|| basename.ends_with("_test.mjs")
|
2020-03-14 08:23:53 -04:00
|
|
|
|| basename.ends_with("_test.jsx")
|
2020-02-17 13:11:45 -05:00
|
|
|
|| basename.ends_with(".test.ts")
|
2020-03-14 08:23:53 -04:00
|
|
|
|| basename.ends_with(".test.tsx")
|
2020-02-17 13:11:45 -05:00
|
|
|
|| basename.ends_with(".test.js")
|
2020-06-05 17:01:44 -04:00
|
|
|
|| basename.ends_with(".test.mjs")
|
2020-03-14 08:23:53 -04:00
|
|
|
|| basename.ends_with(".test.jsx")
|
2020-02-17 13:11:45 -05:00
|
|
|
|| basename == "test.ts"
|
2020-03-14 08:23:53 -04:00
|
|
|
|| basename == "test.tsx"
|
2020-02-17 13:11:45 -05:00
|
|
|
|| basename == "test.js"
|
2020-06-05 17:01:44 -04:00
|
|
|
|| basename == "test.mjs"
|
2020-03-14 08:23:53 -04:00
|
|
|
|| basename == "test.jsx"
|
2020-02-17 13:11:45 -05:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 07:40:23 -04:00
|
|
|
pub fn is_remote_url(module_url: &str) -> bool {
|
|
|
|
let lower = module_url.to_lowercase();
|
|
|
|
lower.starts_with("http://") || lower.starts_with("https://")
|
|
|
|
}
|
|
|
|
|
2021-05-10 19:54:39 -04:00
|
|
|
pub fn collect_test_module_specifiers<P>(
|
2020-02-11 06:01:56 -05:00
|
|
|
include: Vec<String>,
|
2021-03-25 14:17:37 -04:00
|
|
|
root_path: &Path,
|
2021-05-10 19:54:39 -04:00
|
|
|
predicate: P,
|
|
|
|
) -> Result<Vec<Url>, AnyError>
|
|
|
|
where
|
|
|
|
P: Fn(&Path) -> bool,
|
|
|
|
{
|
2020-02-11 06:01:56 -05:00
|
|
|
let (include_paths, include_urls): (Vec<String>, Vec<String>) =
|
|
|
|
include.into_iter().partition(|n| !is_remote_url(n));
|
|
|
|
let mut prepared = vec![];
|
|
|
|
|
|
|
|
for path in include_paths {
|
2021-05-10 19:54:39 -04:00
|
|
|
let p = normalize_path(&root_path.join(path));
|
2020-02-17 13:11:45 -05:00
|
|
|
if p.is_dir() {
|
2021-05-10 19:54:39 -04:00
|
|
|
let test_files = collect_files(&[p], &[], &predicate).unwrap();
|
2020-02-17 13:11:45 -05:00
|
|
|
let test_files_as_urls = test_files
|
|
|
|
.iter()
|
|
|
|
.map(|f| Url::from_file_path(f).unwrap())
|
|
|
|
.collect::<Vec<Url>>();
|
|
|
|
prepared.extend(test_files_as_urls);
|
|
|
|
} else {
|
|
|
|
let url = Url::from_file_path(p).unwrap();
|
|
|
|
prepared.push(url);
|
|
|
|
}
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for remote_url in include_urls {
|
|
|
|
let url = Url::parse(&remote_url)?;
|
|
|
|
prepared.push(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(prepared)
|
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
pub async fn run_test_file(
|
|
|
|
program_state: Arc<ProgramState>,
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: Permissions,
|
2021-07-26 08:05:44 -04:00
|
|
|
quiet: bool,
|
|
|
|
filter: Option<String>,
|
|
|
|
shuffle: Option<u64>,
|
2021-04-30 11:56:47 -04:00
|
|
|
channel: Sender<TestEvent>,
|
2021-04-28 14:17:04 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-07-26 08:05:44 -04:00
|
|
|
let test_module =
|
|
|
|
deno_core::resolve_path(&format!("{}$deno$test.js", Uuid::new_v4()))?;
|
|
|
|
let test_source = format!(
|
|
|
|
r#"
|
|
|
|
import "{}";
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
|
|
await Deno[Deno.internal].runTests({});
|
|
|
|
"#,
|
|
|
|
main_module,
|
|
|
|
json!({
|
|
|
|
"disableLog": quiet,
|
|
|
|
"filter": filter,
|
|
|
|
"shuffle": shuffle,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
let test_file = File {
|
|
|
|
local: test_module.to_file_path().unwrap(),
|
|
|
|
maybe_types: None,
|
|
|
|
media_type: MediaType::JavaScript,
|
|
|
|
source: test_source.clone(),
|
|
|
|
specifier: test_module.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
program_state.file_fetcher.insert_cached(test_file);
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
let mut worker =
|
|
|
|
create_main_worker(&program_state, main_module.clone(), permissions, true);
|
|
|
|
|
|
|
|
{
|
|
|
|
let js_runtime = &mut worker.js_runtime;
|
|
|
|
js_runtime
|
|
|
|
.op_state()
|
|
|
|
.borrow_mut()
|
2021-04-30 11:56:47 -04:00
|
|
|
.put::<Sender<TestEvent>>(channel.clone());
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut maybe_coverage_collector = if let Some(ref coverage_dir) =
|
|
|
|
program_state.coverage_dir
|
|
|
|
{
|
2021-05-26 11:47:33 -04:00
|
|
|
let session = worker.create_inspector_session().await;
|
2021-04-28 14:17:04 -04:00
|
|
|
let coverage_dir = PathBuf::from(coverage_dir);
|
|
|
|
let mut coverage_collector = CoverageCollector::new(coverage_dir, session);
|
2021-05-26 11:47:33 -04:00
|
|
|
worker
|
|
|
|
.with_event_loop(coverage_collector.start_collecting().boxed_local())
|
|
|
|
.await?;
|
2021-04-28 14:17:04 -04:00
|
|
|
|
|
|
|
Some(coverage_collector)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-06-21 19:45:41 -04:00
|
|
|
worker.execute_script(
|
|
|
|
&located_script_name!(),
|
|
|
|
"window.dispatchEvent(new Event('load'))",
|
|
|
|
)?;
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-07-12 06:46:32 -04:00
|
|
|
worker.execute_module(&test_module).await?;
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-05-26 15:07:12 -04:00
|
|
|
worker
|
|
|
|
.run_event_loop(maybe_coverage_collector.is_none())
|
|
|
|
.await?;
|
2021-06-21 19:45:41 -04:00
|
|
|
worker.execute_script(
|
|
|
|
&located_script_name!(),
|
|
|
|
"window.dispatchEvent(new Event('unload'))",
|
|
|
|
)?;
|
2021-04-28 14:17:04 -04:00
|
|
|
|
|
|
|
if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
|
2021-05-26 11:47:33 -04:00
|
|
|
worker
|
|
|
|
.with_event_loop(coverage_collector.stop_collecting().boxed_local())
|
|
|
|
.await?;
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-29 15:03:06 -04:00
|
|
|
fn extract_files_from_regex_blocks(
|
|
|
|
location: &Location,
|
|
|
|
source: &str,
|
|
|
|
media_type: &MediaType,
|
|
|
|
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| {
|
|
|
|
let maybe_attributes = block
|
|
|
|
.get(1)
|
|
|
|
.map(|attributes| attributes.as_str().split(' '));
|
|
|
|
|
|
|
|
let file_media_type = if let Some(mut attributes) = maybe_attributes {
|
|
|
|
match attributes.next() {
|
|
|
|
Some("js") => MediaType::JavaScript,
|
|
|
|
Some("jsx") => MediaType::Jsx,
|
|
|
|
Some("ts") => MediaType::TypeScript,
|
|
|
|
Some("tsx") => MediaType::Tsx,
|
|
|
|
Some("") => *media_type,
|
|
|
|
_ => MediaType::Unknown,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*media_type
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
file_source.push_str(&format!("{}\n", text.as_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
file_source.push_str("export {};");
|
|
|
|
|
|
|
|
let file_specifier = deno_core::resolve_url_or_path(&format!(
|
|
|
|
"{}${}-{}{}",
|
|
|
|
location.filename,
|
|
|
|
location.line + line_offset,
|
|
|
|
location.line + line_offset + line_count,
|
|
|
|
file_media_type.as_ts_extension(),
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Some(File {
|
|
|
|
local: file_specifier.to_file_path().unwrap(),
|
|
|
|
maybe_types: None,
|
|
|
|
media_type: file_media_type,
|
|
|
|
source: file_source,
|
|
|
|
specifier: file_specifier,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(files)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_files_from_source_comments(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source: &str,
|
|
|
|
media_type: &MediaType,
|
|
|
|
) -> Result<Vec<File>, AnyError> {
|
2021-07-30 09:03:41 -04:00
|
|
|
let parsed_module = ast::parse(specifier.as_str(), source, media_type)?;
|
2021-07-29 15:03:06 -04:00
|
|
|
let mut comments = parsed_module.get_comments();
|
|
|
|
comments
|
|
|
|
.sort_by_key(|comment| parsed_module.get_location(&comment.span).line);
|
|
|
|
|
|
|
|
let blocks_regex = Regex::new(r"```([^\n]*)\n([\S\s]*?)```")?;
|
|
|
|
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| {
|
|
|
|
let location = parsed_module.get_location(&comment.span);
|
|
|
|
|
|
|
|
extract_files_from_regex_blocks(
|
|
|
|
&location,
|
|
|
|
&comment.text,
|
2021-07-30 09:03:41 -04:00
|
|
|
media_type,
|
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,
|
|
|
|
media_type: &MediaType,
|
|
|
|
) -> Result<Vec<File>, AnyError> {
|
|
|
|
let location = Location {
|
|
|
|
filename: specifier.to_string(),
|
|
|
|
line: 1,
|
|
|
|
col: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let blocks_regex = Regex::new(r"```([^\n]*)\n([\S\s]*?)```")?;
|
|
|
|
let lines_regex = Regex::new(r"(?:\# ?)?(.*)")?;
|
|
|
|
|
|
|
|
extract_files_from_regex_blocks(
|
|
|
|
&location,
|
2021-07-30 09:03:41 -04:00
|
|
|
source,
|
|
|
|
media_type,
|
2021-07-29 15:03:06 -04:00
|
|
|
&blocks_regex,
|
|
|
|
&lines_regex,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn fetch_inline_files(
|
|
|
|
program_state: Arc<ProgramState>,
|
|
|
|
specifiers: Vec<ModuleSpecifier>,
|
|
|
|
) -> Result<Vec<File>, AnyError> {
|
|
|
|
let mut files = Vec::new();
|
|
|
|
for specifier in specifiers {
|
|
|
|
let mut fetch_permissions = Permissions::allow_all();
|
|
|
|
let file = program_state
|
|
|
|
.file_fetcher
|
|
|
|
.fetch(&specifier, &mut fetch_permissions)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let inline_files = if file.media_type == MediaType::Unknown {
|
|
|
|
extract_files_from_fenced_blocks(
|
|
|
|
&file.specifier,
|
|
|
|
&file.source,
|
|
|
|
&file.media_type,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
extract_files_from_source_comments(
|
|
|
|
&file.specifier,
|
|
|
|
&file.source,
|
|
|
|
&file.media_type,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
files.extend(inline_files?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(files)
|
|
|
|
}
|
|
|
|
|
2021-05-10 02:06:13 -04:00
|
|
|
/// Runs tests.
|
|
|
|
///
|
2021-04-28 14:17:04 -04:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
pub async fn run_tests(
|
2021-05-10 02:06:13 -04:00
|
|
|
program_state: Arc<ProgramState>,
|
|
|
|
permissions: Permissions,
|
|
|
|
lib: module_graph::TypeLib,
|
2021-05-10 19:54:39 -04:00
|
|
|
doc_modules: Vec<ModuleSpecifier>,
|
2021-05-10 02:06:13 -04:00
|
|
|
test_modules: Vec<ModuleSpecifier>,
|
2021-04-28 14:17:04 -04:00
|
|
|
no_run: bool,
|
2021-07-12 06:55:42 -04:00
|
|
|
fail_fast: Option<usize>,
|
2020-04-27 07:05:26 -04:00
|
|
|
quiet: bool,
|
2021-04-28 14:17:04 -04:00
|
|
|
allow_none: bool,
|
2020-04-02 09:26:40 -04:00
|
|
|
filter: Option<String>,
|
2021-07-05 21:20:33 -04:00
|
|
|
shuffle: Option<u64>,
|
2021-04-28 14:17:04 -04:00
|
|
|
concurrent_jobs: usize,
|
2021-07-22 07:34:29 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if !allow_none && doc_modules.is_empty() && test_modules.is_empty() {
|
|
|
|
return Err(generic_error("No test modules found"));
|
|
|
|
}
|
|
|
|
|
2021-07-05 21:20:33 -04:00
|
|
|
let test_modules = if let Some(seed) = shuffle {
|
|
|
|
let mut rng = SmallRng::seed_from_u64(seed);
|
|
|
|
let mut test_modules = test_modules.clone();
|
|
|
|
test_modules.sort();
|
|
|
|
test_modules.shuffle(&mut rng);
|
|
|
|
test_modules
|
|
|
|
} else {
|
|
|
|
test_modules
|
|
|
|
};
|
|
|
|
|
2021-05-10 19:54:39 -04:00
|
|
|
if !doc_modules.is_empty() {
|
2021-07-29 15:03:06 -04:00
|
|
|
let files = fetch_inline_files(program_state.clone(), doc_modules).await?;
|
|
|
|
let specifiers = files.iter().map(|file| file.specifier.clone()).collect();
|
2021-05-10 19:54:39 -04:00
|
|
|
|
2021-07-29 15:03:06 -04:00
|
|
|
for file in files {
|
|
|
|
program_state.file_fetcher.insert_cached(file);
|
2021-05-10 19:54:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
program_state
|
|
|
|
.prepare_module_graph(
|
2021-07-29 15:03:06 -04:00
|
|
|
specifiers,
|
2021-05-10 19:54:39 -04:00
|
|
|
lib.clone(),
|
2021-05-17 03:44:38 -04:00
|
|
|
Permissions::allow_all(),
|
2021-05-10 19:54:39 -04:00
|
|
|
permissions.clone(),
|
|
|
|
program_state.maybe_import_map.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
program_state
|
|
|
|
.prepare_module_graph(
|
|
|
|
test_modules.clone(),
|
|
|
|
lib.clone(),
|
2021-05-17 03:44:38 -04:00
|
|
|
Permissions::allow_all(),
|
2021-04-28 14:17:04 -04:00
|
|
|
permissions.clone(),
|
|
|
|
program_state.maybe_import_map.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
if no_run {
|
2021-07-22 07:34:29 -04:00
|
|
|
return Ok(());
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
|
|
|
|
2021-04-30 11:56:47 -04:00
|
|
|
let (sender, receiver) = channel::<TestEvent>();
|
2021-04-28 14:17:04 -04:00
|
|
|
|
|
|
|
let join_handles = test_modules.iter().map(move |main_module| {
|
|
|
|
let program_state = program_state.clone();
|
|
|
|
let main_module = main_module.clone();
|
|
|
|
let permissions = permissions.clone();
|
2021-07-26 08:05:44 -04:00
|
|
|
let filter = filter.clone();
|
2021-04-28 14:17:04 -04:00
|
|
|
let sender = sender.clone();
|
|
|
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
let join_handle = std::thread::spawn(move || {
|
|
|
|
let future = run_test_file(
|
|
|
|
program_state,
|
|
|
|
main_module,
|
|
|
|
permissions,
|
2021-07-26 08:05:44 -04:00
|
|
|
quiet,
|
|
|
|
filter,
|
|
|
|
shuffle,
|
2021-04-28 14:17:04 -04:00
|
|
|
sender,
|
|
|
|
);
|
|
|
|
|
|
|
|
tokio_util::run_basic(future)
|
|
|
|
});
|
|
|
|
|
|
|
|
join_handle.join().unwrap()
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-07-22 07:34:29 -04:00
|
|
|
let join_stream = stream::iter(join_handles)
|
2021-04-28 14:17:04 -04:00
|
|
|
.buffer_unordered(concurrent_jobs)
|
|
|
|
.collect::<Vec<Result<Result<(), AnyError>, tokio::task::JoinError>>>();
|
|
|
|
|
2021-07-10 10:51:30 -04:00
|
|
|
let mut reporter = create_reporter(concurrent_jobs > 1);
|
2021-04-28 14:17:04 -04:00
|
|
|
let handler = {
|
|
|
|
tokio::task::spawn_blocking(move || {
|
2021-07-13 18:11:58 -04:00
|
|
|
let earlier = Instant::now();
|
|
|
|
let mut summary = TestSummary::new();
|
2021-04-28 14:17:04 -04:00
|
|
|
let mut used_only = false;
|
|
|
|
|
2021-04-30 11:56:47 -04:00
|
|
|
for event in receiver.iter() {
|
2021-07-14 15:05:16 -04:00
|
|
|
match event {
|
|
|
|
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
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
TestEvent::Wait(description) => {
|
|
|
|
reporter.report_wait(&description);
|
|
|
|
}
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
TestEvent::Result(description, result, elapsed) => {
|
|
|
|
match &result {
|
|
|
|
TestResult::Ok => {
|
|
|
|
summary.passed += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestResult::Ignored => {
|
|
|
|
summary.ignored += 1;
|
|
|
|
}
|
2021-07-13 18:11:58 -04:00
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
TestResult::Failed(error) => {
|
|
|
|
summary.failed += 1;
|
|
|
|
summary.failures.push((description.clone(), error.clone()));
|
|
|
|
}
|
2021-07-13 18:11:58 -04:00
|
|
|
}
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-07-14 15:05:16 -04:00
|
|
|
reporter.report_result(&description, &result, elapsed);
|
|
|
|
}
|
|
|
|
}
|
2021-04-29 07:42:35 -04:00
|
|
|
|
2021-07-12 06:55:42 -04:00
|
|
|
if let Some(x) = fail_fast {
|
2021-07-13 18:11:58 -04:00
|
|
|
if summary.failed >= x {
|
2021-07-12 06:55:42 -04:00
|
|
|
break;
|
|
|
|
}
|
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-04-28 14:17:04 -04:00
|
|
|
let mut join_errors = join_results.into_iter().filter_map(|join_result| {
|
|
|
|
join_result
|
|
|
|
.ok()
|
|
|
|
.map(|handle_result| handle_result.err())
|
|
|
|
.flatten()
|
|
|
|
});
|
2020-02-11 06:01:56 -05:00
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
if let Some(e) = join_errors.next() {
|
2021-07-22 07:34:29 -04:00
|
|
|
return Err(e);
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
2021-07-22 07:34:29 -04:00
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(result) => {
|
|
|
|
if let Some(err) = result.err() {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(err) => {
|
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2021-04-28 14:17:04 -04:00
|
|
|
fn test_collect_test_module_specifiers() {
|
2020-02-11 06:01:56 -05:00
|
|
|
let test_data_path = test_util::root_path().join("cli/tests/subdir");
|
2021-04-28 14:17:04 -04:00
|
|
|
let mut matched_urls = collect_test_module_specifiers(
|
2020-02-11 06:01:56 -05:00
|
|
|
vec![
|
|
|
|
"https://example.com/colors_test.ts".to_string(),
|
|
|
|
"./mod1.ts".to_string(),
|
|
|
|
"./mod3.js".to_string(),
|
|
|
|
"subdir2/mod2.ts".to_string(),
|
|
|
|
"http://example.com/printf_test.ts".to_string(),
|
|
|
|
],
|
2020-02-17 13:11:45 -05:00
|
|
|
&test_data_path,
|
2021-05-10 19:54:39 -04:00
|
|
|
is_supported,
|
2020-02-11 06:01:56 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let test_data_url =
|
|
|
|
Url::from_file_path(test_data_path).unwrap().to_string();
|
|
|
|
|
|
|
|
let expected: Vec<Url> = vec![
|
|
|
|
format!("{}/mod1.ts", test_data_url),
|
|
|
|
format!("{}/mod3.js", test_data_url),
|
|
|
|
format!("{}/subdir2/mod2.ts", test_data_url),
|
|
|
|
"http://example.com/printf_test.ts".to_string(),
|
|
|
|
"https://example.com/colors_test.ts".to_string(),
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.map(|f| Url::parse(&f).unwrap())
|
|
|
|
.collect();
|
|
|
|
matched_urls.sort();
|
|
|
|
assert_eq!(matched_urls, expected);
|
|
|
|
}
|
2020-02-17 13:11:45 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_supported() {
|
|
|
|
assert!(is_supported(Path::new("tests/subdir/foo_test.ts")));
|
2020-03-14 08:23:53 -04:00
|
|
|
assert!(is_supported(Path::new("tests/subdir/foo_test.tsx")));
|
2020-02-17 13:11:45 -05:00
|
|
|
assert!(is_supported(Path::new("tests/subdir/foo_test.js")));
|
2020-03-14 08:23:53 -04:00
|
|
|
assert!(is_supported(Path::new("tests/subdir/foo_test.jsx")));
|
2020-02-17 13:11:45 -05:00
|
|
|
assert!(is_supported(Path::new("bar/foo.test.ts")));
|
2020-03-14 08:23:53 -04:00
|
|
|
assert!(is_supported(Path::new("bar/foo.test.tsx")));
|
2020-02-17 13:11:45 -05:00
|
|
|
assert!(is_supported(Path::new("bar/foo.test.js")));
|
2020-03-14 08:23:53 -04:00
|
|
|
assert!(is_supported(Path::new("bar/foo.test.jsx")));
|
2020-02-17 13:11:45 -05:00
|
|
|
assert!(is_supported(Path::new("foo/bar/test.js")));
|
2020-03-14 08:23:53 -04:00
|
|
|
assert!(is_supported(Path::new("foo/bar/test.jsx")));
|
2020-02-17 13:11:45 -05:00
|
|
|
assert!(is_supported(Path::new("foo/bar/test.ts")));
|
2020-03-14 08:23:53 -04:00
|
|
|
assert!(is_supported(Path::new("foo/bar/test.tsx")));
|
2020-02-17 13:11:45 -05:00
|
|
|
assert!(!is_supported(Path::new("README.md")));
|
|
|
|
assert!(!is_supported(Path::new("lib/typescript.d.ts")));
|
|
|
|
assert!(!is_supported(Path::new("notatest.js")));
|
|
|
|
assert!(!is_supported(Path::new("NotAtest.ts")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn supports_dirs() {
|
2021-02-02 06:05:46 -05:00
|
|
|
// TODO(caspervonb) generate some fixtures in a temporary directory instead, there's no need
|
|
|
|
// for this to rely on external fixtures.
|
|
|
|
let root = test_util::root_path()
|
|
|
|
.join("test_util")
|
|
|
|
.join("std")
|
|
|
|
.join("http");
|
2020-02-17 13:11:45 -05:00
|
|
|
println!("root {:?}", root);
|
2021-05-10 19:54:39 -04:00
|
|
|
let mut matched_urls = collect_test_module_specifiers(
|
|
|
|
vec![".".to_string()],
|
|
|
|
&root,
|
|
|
|
is_supported,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-02-17 13:11:45 -05:00
|
|
|
matched_urls.sort();
|
|
|
|
let root_url = Url::from_file_path(root).unwrap().to_string();
|
|
|
|
println!("root_url {}", root_url);
|
|
|
|
let expected: Vec<Url> = vec![
|
2020-05-09 08:34:47 -04:00
|
|
|
format!("{}/_io_test.ts", root_url),
|
2020-02-17 13:11:45 -05:00
|
|
|
format!("{}/cookie_test.ts", root_url),
|
|
|
|
format!("{}/file_server_test.ts", root_url),
|
|
|
|
format!("{}/racing_server_test.ts", root_url),
|
|
|
|
format!("{}/server_test.ts", root_url),
|
2020-08-31 22:26:55 -04:00
|
|
|
format!("{}/test.ts", root_url),
|
2020-02-17 13:11:45 -05:00
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.map(|f| Url::parse(&f).unwrap())
|
|
|
|
.collect();
|
|
|
|
assert_eq!(matched_urls, expected);
|
|
|
|
}
|
2021-05-19 07:40:23 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_remote_url() {
|
|
|
|
assert!(is_remote_url("https://deno.land/std/http/file_server.ts"));
|
|
|
|
assert!(is_remote_url("http://deno.land/std/http/file_server.ts"));
|
|
|
|
assert!(is_remote_url("HTTP://deno.land/std/http/file_server.ts"));
|
|
|
|
assert!(is_remote_url("HTTp://deno.land/std/http/file_server.ts"));
|
|
|
|
assert!(!is_remote_url("file:///dev/deno_std/http/file_server.ts"));
|
|
|
|
assert!(!is_remote_url("./dev/deno_std/http/file_server.ts"));
|
|
|
|
}
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|