2020-02-11 06:01:56 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-11-16 14:48:50 -05:00
|
|
|
use crate::fs_util;
|
2020-11-19 13:19:34 -05:00
|
|
|
use crate::tools::installer::is_remote_url;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
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;
|
2020-02-17 13:11:45 -05:00
|
|
|
use std::path::Path;
|
2020-02-11 06:01:56 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-02-17 13:11:45 -05:00
|
|
|
fn is_supported(p: &Path) -> bool {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 06:01:56 -05:00
|
|
|
pub fn prepare_test_modules_urls(
|
|
|
|
include: Vec<String>,
|
2020-02-17 13:11:45 -05:00
|
|
|
root_path: &PathBuf,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Vec<Url>, AnyError> {
|
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 {
|
2020-11-16 14:48:50 -05:00
|
|
|
let p = fs_util::normalize_path(&root_path.join(path));
|
2020-02-17 13:11:45 -05:00
|
|
|
if p.is_dir() {
|
2020-11-22 15:45:44 -05:00
|
|
|
let test_files = fs_util::collect_files(&[p], &[], is_supported).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)
|
|
|
|
}
|
|
|
|
|
2020-04-02 09:26:40 -04:00
|
|
|
pub fn render_test_file(
|
|
|
|
modules: Vec<Url>,
|
|
|
|
fail_fast: bool,
|
2020-04-27 07:05:26 -04:00
|
|
|
quiet: bool,
|
2020-04-02 09:26:40 -04:00
|
|
|
filter: Option<String>,
|
|
|
|
) -> String {
|
2020-02-11 06:01:56 -05:00
|
|
|
let mut test_file = "".to_string();
|
|
|
|
|
|
|
|
for module in modules {
|
|
|
|
test_file.push_str(&format!("import \"{}\";\n", module.to_string()));
|
|
|
|
}
|
|
|
|
|
2020-04-02 09:26:40 -04:00
|
|
|
let options = if let Some(filter) = filter {
|
2020-04-27 07:05:26 -04:00
|
|
|
json!({ "failFast": fail_fast, "reportToConsole": !quiet, "disableLog": quiet, "filter": filter })
|
2020-04-02 09:26:40 -04:00
|
|
|
} else {
|
2020-04-27 07:05:26 -04:00
|
|
|
json!({ "failFast": fail_fast, "reportToConsole": !quiet, "disableLog": quiet })
|
2020-04-02 09:26:40 -04:00
|
|
|
};
|
|
|
|
|
2020-10-14 09:19:13 -04:00
|
|
|
test_file.push_str("// @ts-ignore\n");
|
|
|
|
|
|
|
|
test_file.push_str(&format!(
|
|
|
|
"await Deno[Deno.internal].runTests({});\n",
|
2020-04-27 08:51:22 -04:00
|
|
|
options
|
2020-10-14 09:19:13 -04:00
|
|
|
));
|
2020-02-11 06:01:56 -05:00
|
|
|
|
|
|
|
test_file
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_prepare_test_modules_urls() {
|
|
|
|
let test_data_path = test_util::root_path().join("cli/tests/subdir");
|
|
|
|
let mut matched_urls = prepare_test_modules_urls(
|
|
|
|
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,
|
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() {
|
|
|
|
let root = test_util::root_path().join("std").join("http");
|
|
|
|
println!("root {:?}", root);
|
|
|
|
let mut matched_urls =
|
|
|
|
prepare_test_modules_urls(vec![".".to_string()], &root).unwrap();
|
|
|
|
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);
|
|
|
|
}
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|