forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_runner.rs
167 lines (150 loc) · 5.1 KB
/
test_runner.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::fs as deno_fs;
use crate::installer::is_remote_url;
use deno_core::ErrBox;
use std::path::Path;
use std::path::PathBuf;
use url::Url;
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")
|| basename.ends_with("_test.tsx")
|| basename.ends_with("_test.js")
|| basename.ends_with("_test.jsx")
|| basename.ends_with(".test.ts")
|| basename.ends_with(".test.tsx")
|| basename.ends_with(".test.js")
|| basename.ends_with(".test.jsx")
|| basename == "test.ts"
|| basename == "test.tsx"
|| basename == "test.js"
|| basename == "test.jsx"
} else {
false
}
}
pub fn prepare_test_modules_urls(
include: Vec<String>,
root_path: &PathBuf,
) -> Result<Vec<Url>, ErrBox> {
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 {
let p = deno_fs::normalize_path(&root_path.join(path));
if p.is_dir() {
let test_files = crate::fs::files_in_subtree(p, is_supported);
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);
}
}
for remote_url in include_urls {
let url = Url::parse(&remote_url)?;
prepared.push(url);
}
Ok(prepared)
}
pub fn render_test_file(
modules: Vec<Url>,
fail_fast: bool,
quiet: bool,
filter: Option<String>,
) -> String {
let mut test_file = "".to_string();
for module in modules {
test_file.push_str(&format!("import \"{}\";\n", module.to_string()));
}
let options = if let Some(filter) = filter {
json!({ "failFast": fail_fast, "reportToConsole": !quiet, "disableLog": quiet, "filter": filter })
} else {
json!({ "failFast": fail_fast, "reportToConsole": !quiet, "disableLog": quiet })
};
let run_tests_cmd = format!(
"// @ts-ignore\nDeno[Deno.internal].runTests({});\n",
options
);
test_file.push_str(&run_tests_cmd);
test_file
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util;
#[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:https://example.com/printf_test.ts".to_string(),
],
&test_data_path,
)
.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:https://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);
}
#[test]
fn test_is_supported() {
assert!(is_supported(Path::new("tests/subdir/foo_test.ts")));
assert!(is_supported(Path::new("tests/subdir/foo_test.tsx")));
assert!(is_supported(Path::new("tests/subdir/foo_test.js")));
assert!(is_supported(Path::new("tests/subdir/foo_test.jsx")));
assert!(is_supported(Path::new("bar/foo.test.ts")));
assert!(is_supported(Path::new("bar/foo.test.tsx")));
assert!(is_supported(Path::new("bar/foo.test.js")));
assert!(is_supported(Path::new("bar/foo.test.jsx")));
assert!(is_supported(Path::new("foo/bar/test.js")));
assert!(is_supported(Path::new("foo/bar/test.jsx")));
assert!(is_supported(Path::new("foo/bar/test.ts")));
assert!(is_supported(Path::new("foo/bar/test.tsx")));
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![
format!("{}/_io_test.ts", root_url),
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),
]
.into_iter()
.map(|f| Url::parse(&f).unwrap())
.collect();
assert_eq!(matched_urls, expected);
}
}