summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrickyPi <33021497+TrickyPi@users.noreply.github.com>2022-03-30 02:57:42 +0800
committerGitHub <noreply@github.com>2022-03-29 14:57:42 -0400
commit85e16a08c7559f3d0b9a15d5c1de6aa93d30cdd9 (patch)
treeda52d387e36bc14f977c4f2cd4e5b35e6664106e
parent03c71a8b4a5e4c451799144eb973402aafe22ea8 (diff)
feat(cli/fmt): ignore .git folder when formatting files (#14138)
-rw-r--r--cli/tests/integration/fmt_tests.rs33
-rw-r--r--cli/tools/fmt.rs70
2 files changed, 74 insertions, 29 deletions
diff --git a/cli/tests/integration/fmt_tests.rs b/cli/tests/integration/fmt_tests.rs
index aa807b727..28a1dda7d 100644
--- a/cli/tests/integration/fmt_tests.rs
+++ b/cli/tests/integration/fmt_tests.rs
@@ -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(""),
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index 3c5ab523e..401fe836e 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -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")));
+}