diff --git a/cli/fs_util.rs b/cli/fs_util.rs index abab720f26..88855b0e83 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 ce78485276..a71d6bd512 100644 --- a/cli/tests/integration/watcher_tests.rs +++ b/cli/tests/integration/watcher_tests.rs @@ -143,6 +143,72 @@ fn lint_watch_test() { drop(t); } +#[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"); @@ -232,6 +298,46 @@ fn fmt_watch_test() { check_alive_then_kill(child); } +#[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(); diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 2cc276c5b4..d70c95d29f 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 1f85e8b02a..706ecbbd8e 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 {