From 60fe94c29280d45da868b1ff29b15ffa1b9375a4 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 5 Jun 2021 13:46:40 -0500 Subject: [PATCH] fix(cli): Ensure we can run on files --- src/bin/typos-cli/main.rs | 9 +++++---- tests/cli.rs | 7 +++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/bin/typos-cli/main.rs b/src/bin/typos-cli/main.rs index d9dc667..8667b89 100644 --- a/src/bin/typos-cli/main.rs +++ b/src/bin/typos-cli/main.rs @@ -172,13 +172,14 @@ fn run_checks( let mut errors_found = false; for path in args.path.iter() { let cwd = if path == std::path::Path::new("-") { - global_cwd.as_path() + global_cwd.clone() } else if path.is_file() { - path.parent().unwrap() + let mut cwd = path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?; + cwd.pop(); + cwd } else { - path.as_path() + path.clone() }; - let cwd = cwd.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?; engine .init_dir(&cwd) diff --git a/tests/cli.rs b/tests/cli.rs index c65b771..9fcc0cd 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -22,3 +22,10 @@ fn test_stdin_correct() { .write_stdin("Apropriate world"); cmd.assert().success().stdout("Appropriate world"); } + +#[test] +fn test_file_failure() { + let mut cmd = Command::cargo_bin("typos").unwrap(); + cmd.arg("README.md"); + cmd.assert().code(2); +}