diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/Cargo.toml | 1 | ||||
-rw-r--r-- | cli/fmt.rs | 30 | ||||
-rw-r--r-- | cli/fs.rs | 15 | ||||
-rw-r--r-- | cli/lib.rs | 2 | ||||
-rw-r--r-- | cli/test_runner.rs | 71 |
5 files changed, 89 insertions, 30 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml index a8f2e1dd3..a9b79dbbc 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -62,6 +62,7 @@ url = "2.1.0" utime = "0.2.1" webpki = "0.21.0" webpki-roots = "0.17.0" +walkdir = "2.3.1" [target.'cfg(windows)'.dependencies] winapi = "0.3.8" diff --git a/cli/fmt.rs b/cli/fmt.rs index c9b0ef826..a71c41102 100644 --- a/cli/fmt.rs +++ b/cli/fmt.rs @@ -7,9 +7,9 @@ //! the future it can be easily extended to provide //! the same functions as ops available in JS runtime. +use crate::fs::files_in_subtree; use deno_core::ErrBox; use dprint_plugin_typescript as dprint; -use glob; use std::fs; use std::io::stdin; use std::io::stdout; @@ -135,26 +135,6 @@ fn format_source_files( ); } -pub fn source_files_in_subtree(root: PathBuf) -> Vec<PathBuf> { - assert!(root.is_dir()); - // TODO(ry) Use WalkDir instead of globs. - let g = root.join("**/*"); - glob::glob(&g.into_os_string().into_string().unwrap()) - .expect("Failed to execute glob.") - .filter_map(|result| { - if let Ok(p) = result { - if is_supported(&p) { - Some(p) - } else { - None - } - } else { - None - } - }) - .collect() -} - /// Format JavaScript/TypeScript files. /// /// First argument supports globs, and if it is `None` @@ -168,13 +148,15 @@ pub fn format_files(args: Vec<String>, check: bool) -> Result<(), ErrBox> { let mut target_files: Vec<PathBuf> = vec![]; if args.is_empty() { - target_files - .extend(source_files_in_subtree(std::env::current_dir().unwrap())); + target_files.extend(files_in_subtree( + std::env::current_dir().unwrap(), + is_supported, + )); } else { for arg in args { let p = PathBuf::from(arg); if p.is_dir() { - target_files.extend(source_files_in_subtree(p)); + target_files.extend(files_in_subtree(p, is_supported)); } else { target_files.push(p); }; @@ -9,6 +9,7 @@ use deno_core::ErrBox; use rand; use rand::Rng; use url::Url; +use walkdir::WalkDir; #[cfg(unix)] use nix::unistd::{chown as unix_chown, Gid, Uid}; @@ -188,3 +189,17 @@ mod tests { assert_eq!(resolve_from_cwd(expected).unwrap(), expected); } } + +pub fn files_in_subtree<F>(root: PathBuf, filter: F) -> Vec<PathBuf> +where + F: Fn(&Path) -> bool, +{ + assert!(root.is_dir()); + + WalkDir::new(root) + .into_iter() + .filter_map(|e| e.ok()) + .map(|e| e.path().to_owned()) + .filter(|p| if p.is_dir() { false } else { filter(&p) }) + .collect() +} diff --git a/cli/lib.rs b/cli/lib.rs index e646c4199..81fd8810e 100644 --- a/cli/lib.rs +++ b/cli/lib.rs @@ -429,7 +429,7 @@ async fn test_command( let global_state = create_global_state(flags.clone()); let cwd = std::env::current_dir().expect("No current directory"); let include = include.unwrap_or_else(|| vec![".".to_string()]); - let res = test_runner::prepare_test_modules_urls(include, cwd.clone()); + let res = test_runner::prepare_test_modules_urls(include, &cwd); let test_modules = match res { Ok(modules) => modules, diff --git a/cli/test_runner.rs b/cli/test_runner.rs index e05013889..3d177f60e 100644 --- a/cli/test_runner.rs +++ b/cli/test_runner.rs @@ -3,12 +3,28 @@ use crate::installer::is_remote_url; use deno_core::ErrBox; use std; +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.js") + || basename.ends_with(".test.ts") + || basename.ends_with(".test.js") + || basename == "test.ts" + || basename == "test.js" + } else { + false + } +} + pub fn prepare_test_modules_urls( include: Vec<String>, - root_path: PathBuf, + 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)); @@ -16,9 +32,19 @@ pub fn prepare_test_modules_urls( let mut prepared = vec![]; for path in include_paths { - let p = root_path.join(path).canonicalize()?; - let url = Url::from_file_path(p).unwrap(); - prepared.push(url); + let q = root_path.join(path); + let p = q.canonicalize()?; + 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 { @@ -59,7 +85,7 @@ mod tests { "subdir2/mod2.ts".to_string(), "http://example.com/printf_test.ts".to_string(), ], - test_data_path.clone(), + &test_data_path, ) .unwrap(); let test_data_url = @@ -78,4 +104,39 @@ mod tests { 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.js"))); + assert!(is_supported(Path::new("bar/foo.test.ts"))); + assert!(is_supported(Path::new("bar/foo.test.js"))); + assert!(is_supported(Path::new("foo/bar/test.js"))); + assert!(is_supported(Path::new("foo/bar/test.ts"))); + 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!("{}/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); + } } |