feat(cli/fmt): ignore .git folder when formatting files (#14138)

This commit is contained in:
TrickyPi 2022-03-30 02:57:42 +08:00 committed by GitHub
parent 03c71a8b4a
commit 85e16a08c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 29 deletions

View file

@ -125,6 +125,39 @@ fn fmt_ignore_unexplicit_files() {
);
}
#[test]
fn fmt_auto_ignore_git() {
use std::fs::{create_dir_all, File};
use std::io::Write;
use std::path::PathBuf;
fn create_bad_json(t: PathBuf) {
let bad_json_path = t.join("bad.json");
let mut bad_json_file = File::create(bad_json_path).unwrap();
writeln!(bad_json_file, "bad json").unwrap();
}
let t = TempDir::new().unwrap().path().join("target");
let nest_git = t.join("nest").join(".git");
let git_dir = t.join(".git");
create_dir_all(&nest_git).unwrap();
create_dir_all(&git_dir).unwrap();
create_bad_json(nest_git);
create_bad_json(git_dir);
let output = util::deno_cmd()
.current_dir(t)
.env("NO_COLOR", "1")
.arg("fmt")
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(!output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stderr),
"error: No target files found.\n"
);
}
itest!(fmt_quiet_check_fmt_dir {
args: "fmt --check --quiet fmt/regular/",
output_str: Some(""),

View file

@ -90,31 +90,32 @@ pub async fn format(
maybe_fmt_config.map(|c| c.options).unwrap_or_default(),
);
let fmt_predicate =
|path: &Path| is_supported_ext_fmt(path) && !is_contain_git(path);
let resolver = |changed: Option<Vec<PathBuf>>| {
let files_changed = changed.is_some();
let result =
collect_files(&include_files, &exclude_files, is_supported_ext_fmt).map(
|files| {
let refmt_files = if let Some(paths) = changed {
if check {
files
.iter()
.any(|path| paths.contains(path))
.then(|| files)
.unwrap_or_else(|| [].to_vec())
} else {
files
.into_iter()
.filter(|path| paths.contains(path))
.collect::<Vec<_>>()
}
let result = collect_files(&include_files, &exclude_files, fmt_predicate)
.map(|files| {
let refmt_files = if let Some(paths) = changed {
if check {
files
.iter()
.any(|path| paths.contains(path))
.then(|| files)
.unwrap_or_else(|| [].to_vec())
} else {
files
};
(refmt_files, fmt_options.clone())
},
);
.into_iter()
.filter(|path| paths.contains(path))
.collect::<Vec<_>>()
}
} else {
files
};
(refmt_files, fmt_options.clone())
});
let paths_to_watch = include_files.clone();
async move {
@ -150,15 +151,14 @@ pub async fn format(
)
.await?;
} else {
let files =
collect_files(&include_files, &exclude_files, is_supported_ext_fmt)
.and_then(|files| {
if files.is_empty() {
Err(generic_error("No target files found."))
} else {
Ok(files)
}
})?;
let files = collect_files(&include_files, &exclude_files, fmt_predicate)
.and_then(|files| {
if files.is_empty() {
Err(generic_error("No target files found."))
} else {
Ok(files)
}
})?;
operation((files, fmt_options.clone())).await?;
}
@ -624,6 +624,10 @@ fn is_supported_ext_fmt(path: &Path) -> bool {
}
}
fn is_contain_git(path: &Path) -> bool {
path.components().any(|c| c.as_os_str() == ".git")
}
#[test]
fn test_is_supported_ext_fmt() {
assert!(!is_supported_ext_fmt(Path::new("tests/subdir/redirects")));
@ -650,3 +654,11 @@ fn test_is_supported_ext_fmt() {
assert!(is_supported_ext_fmt(Path::new("foo.json")));
assert!(is_supported_ext_fmt(Path::new("foo.JsON")));
}
#[test]
fn test_is_located_in_git() {
assert!(is_contain_git(Path::new("test/.git")));
assert!(is_contain_git(Path::new(".git/bad.json")));
assert!(is_contain_git(Path::new("test/.git/bad.json")));
assert!(!is_contain_git(Path::new("test/bad.git/bad.json")));
}