summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZheyu Zhang <zheyuzhang03@gmail.com>2021-10-31 01:20:56 +0800
committerGitHub <noreply@github.com>2021-10-30 19:20:56 +0200
commit3fb23ab772e87b9314cd8608eb589c72a53efaee (patch)
tree702262c57f3ac3c32046e6443983f58d6c962686
parent94a81e5e9bda5a7da51bc864e544bf35ef26910e (diff)
fix(cli): linter/formater watches current directory without args (#12550)
-rw-r--r--cli/fs_util.rs3
-rw-r--r--cli/tests/integration/watcher_tests.rs106
-rw-r--r--cli/tools/fmt.rs4
-rw-r--r--cli/tools/lint.rs4
4 files changed, 114 insertions, 3 deletions
diff --git a/cli/fs_util.rs b/cli/fs_util.rs
index abab720f2..88855b0e8 100644
--- a/cli/fs_util.rs
+++ b/cli/fs_util.rs
@@ -236,9 +236,6 @@ where
.filter_map(|i| i.canonicalize().ok())
.collect();
- let cur_dir = [std::env::current_dir()?];
- let files = if files.is_empty() { &cur_dir } else { files };
-
for file in files {
for entry in WalkDir::new(file)
.into_iter()
diff --git a/cli/tests/integration/watcher_tests.rs b/cli/tests/integration/watcher_tests.rs
index ce7848527..a71d6bd51 100644
--- a/cli/tests/integration/watcher_tests.rs
+++ b/cli/tests/integration/watcher_tests.rs
@@ -144,6 +144,72 @@ fn lint_watch_test() {
}
#[test]
+fn lint_watch_without_args_test() {
+ let t = TempDir::new().expect("tempdir fail");
+ let badly_linted_original =
+ util::testdata_path().join("lint/watch/badly_linted.js");
+ let badly_linted_output =
+ util::testdata_path().join("lint/watch/badly_linted.js.out");
+ let badly_linted_fixed1 =
+ util::testdata_path().join("lint/watch/badly_linted_fixed1.js");
+ let badly_linted_fixed1_output =
+ util::testdata_path().join("lint/watch/badly_linted_fixed1.js.out");
+ let badly_linted_fixed2 =
+ util::testdata_path().join("lint/watch/badly_linted_fixed2.js");
+ let badly_linted_fixed2_output =
+ util::testdata_path().join("lint/watch/badly_linted_fixed2.js.out");
+ let badly_linted = t.path().join("badly_linted.js");
+
+ std::fs::copy(&badly_linted_original, &badly_linted)
+ .expect("Failed to copy file");
+
+ let mut child = util::deno_cmd()
+ .current_dir(t.path())
+ .arg("lint")
+ .arg("--watch")
+ .arg("--unstable")
+ .stdout(std::process::Stdio::piped())
+ .stderr(std::process::Stdio::piped())
+ .spawn()
+ .expect("Failed to spawn script");
+ let mut stderr = child.stderr.as_mut().unwrap();
+ let mut stderr_lines = std::io::BufReader::new(&mut stderr)
+ .lines()
+ .map(|r| r.unwrap());
+
+ // TODO(lucacasonato): remove this timeout. It seems to be needed on Linux.
+ std::thread::sleep(std::time::Duration::from_secs(1));
+
+ let mut output = read_all_lints(&mut stderr_lines);
+ let expected = std::fs::read_to_string(badly_linted_output).unwrap();
+ assert_eq!(expected, output);
+
+ // Change content of the file again to be badly-linted1
+ std::fs::copy(&badly_linted_fixed1, &badly_linted)
+ .expect("Failed to copy file");
+ std::thread::sleep(std::time::Duration::from_secs(1));
+
+ output = read_all_lints(&mut stderr_lines);
+ let expected = std::fs::read_to_string(badly_linted_fixed1_output).unwrap();
+ assert_eq!(expected, output);
+
+ // Change content of the file again to be badly-linted1
+ std::fs::copy(&badly_linted_fixed2, &badly_linted)
+ .expect("Failed to copy file");
+ std::thread::sleep(std::time::Duration::from_secs(1));
+
+ output = read_all_lints(&mut stderr_lines);
+ let expected = std::fs::read_to_string(badly_linted_fixed2_output).unwrap();
+ assert_eq!(expected, output);
+
+ // the watcher process is still alive
+ assert!(child.try_wait().unwrap().is_none());
+
+ child.kill().unwrap();
+ drop(t);
+}
+
+#[test]
fn lint_all_files_on_each_change_test() {
let t = TempDir::new().expect("tempdir fail");
let badly_linted_fixed0 =
@@ -233,6 +299,46 @@ fn fmt_watch_test() {
}
#[test]
+fn fmt_watch_without_args_test() {
+ let t = TempDir::new().unwrap();
+ let fixed = util::testdata_path().join("badly_formatted_fixed.js");
+ let badly_formatted_original =
+ util::testdata_path().join("badly_formatted.mjs");
+ let badly_formatted = t.path().join("badly_formatted.js");
+ std::fs::copy(&badly_formatted_original, &badly_formatted).unwrap();
+
+ let mut child = util::deno_cmd()
+ .current_dir(t.path())
+ .arg("fmt")
+ .arg("--watch")
+ .arg("--unstable")
+ .stdout(std::process::Stdio::piped())
+ .stderr(std::process::Stdio::piped())
+ .spawn()
+ .unwrap();
+ let (_stdout_lines, stderr_lines) = child_lines(&mut child);
+
+ // TODO(lucacasonato): remove this timeout. It seems to be needed on Linux.
+ std::thread::sleep(std::time::Duration::from_secs(1));
+
+ assert!(skip_restarting_line(stderr_lines).contains("badly_formatted.js"));
+
+ let expected = std::fs::read_to_string(fixed.clone()).unwrap();
+ let actual = std::fs::read_to_string(badly_formatted.clone()).unwrap();
+ assert_eq!(expected, actual);
+
+ // Change content of the file again to be badly formatted
+ std::fs::copy(&badly_formatted_original, &badly_formatted).unwrap();
+ std::thread::sleep(std::time::Duration::from_secs(1));
+
+ // Check if file has been automatically formatted by watcher
+ let expected = std::fs::read_to_string(fixed).unwrap();
+ let actual = std::fs::read_to_string(badly_formatted).unwrap();
+ assert_eq!(expected, actual);
+ check_alive_then_kill(child);
+}
+
+#[test]
fn fmt_check_all_files_on_each_change_test() {
let t = TempDir::new().unwrap();
let badly_formatted_original =
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index 2cc276c5b..d70c95d29 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -73,6 +73,10 @@ pub async fn format(
}
}
+ if include_files.is_empty() {
+ include_files = [std::env::current_dir()?].to_vec();
+ }
+
// Now do the same for options
let fmt_options = resolve_fmt_options(
&fmt_flags,
diff --git a/cli/tools/lint.rs b/cli/tools/lint.rs
index 1f85e8b02..706ecbbd8 100644
--- a/cli/tools/lint.rs
+++ b/cli/tools/lint.rs
@@ -86,6 +86,10 @@ pub async fn lint(
}
}
+ if include_files.is_empty() {
+ include_files = [std::env::current_dir()?].to_vec();
+ }
+
let reporter_kind = if json {
LintReporterKind::Json
} else {