From dced4d5e19046ce14bed0ac73d172bd42932660e Mon Sep 17 00:00:00 2001 From: Roj Date: Mon, 4 Jul 2022 17:46:10 +0300 Subject: fix(cli): handle collecting a directory with file:// (#15002) --- cli/fs_util.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 10 deletions(-) (limited to 'cli') diff --git a/cli/fs_util.rs b/cli/fs_util.rs index 578a2ec37..fd7ecbea6 100644 --- a/cli/fs_util.rs +++ b/cli/fs_util.rs @@ -254,14 +254,18 @@ where let lowercase_path = path.to_lowercase(); if lowercase_path.starts_with("http://") || lowercase_path.starts_with("https://") - || lowercase_path.starts_with("file://") { let url = ModuleSpecifier::parse(&path)?; prepared.push(url); continue; } - let p = normalize_path(&root_path.join(path)); + let p = if lowercase_path.starts_with("file://") { + specifier_to_file_path(&ModuleSpecifier::parse(&path)?)? + } else { + root_path.join(path) + }; + let p = normalize_path(&p); if p.is_dir() { let test_files = collect_files(&[p], ignore, &predicate).unwrap(); let mut test_files_as_urls = test_files @@ -663,6 +667,14 @@ mod tests { let ignore_dir_files = ["g.d.ts", ".gitignore"]; create_files(&ignore_dir_path, &ignore_dir_files); + let predicate = |path: &Path| { + // exclude dotfiles + path + .file_name() + .and_then(|f| f.to_str()) + .map_or(false, |f| !f.starts_with('.')) + }; + let result = collect_specifiers( vec![ "http://localhost:8080".to_string(), @@ -670,13 +682,7 @@ mod tests { "https://localhost:8080".to_string(), ], &[ignore_dir_path], - |path| { - // exclude dotfiles - path - .file_name() - .and_then(|f| f.to_str()) - .map_or(false, |f| !f.starts_with('.')) - }, + predicate, ) .unwrap(); @@ -698,7 +704,38 @@ mod tests { ] .iter() .map(|f| ModuleSpecifier::parse(f).unwrap()) - .collect::>(); + .collect::>(); + + assert_eq!(result, expected); + + let scheme = if cfg!(target_os = "windows") { + "file:///" + } else { + "file://" + }; + let result = collect_specifiers( + vec![format!( + "{}{}", + scheme, + root_dir_path + .join("child") + .to_str() + .unwrap() + .replace('/', "\\") + )], + &[], + predicate, + ) + .unwrap(); + + let expected: Vec = [ + &format!("{}/child/README.md", root_dir_url), + &format!("{}/child/e.mjs", root_dir_url), + &format!("{}/child/f.mjsx", root_dir_url), + ] + .iter() + .map(|f| ModuleSpecifier::parse(f).unwrap()) + .collect::>(); assert_eq!(result, expected); } -- cgit v1.2.3