From 20f4e5de756ef667baac7e87730320270e7d20b6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 7 Jun 2021 06:28:53 -0500 Subject: [PATCH] fix(cli): Don't panic Fixes #277 --- CHANGELOG.md | 5 +++++ src/bin/typos-cli/main.rs | 18 +++++++++++------- tests/cli.rs | 13 +++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9156b7d..c024c70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] - ReleaseDate +#### Bug Fixes + +- Fix the prior `typos ` fix that broke all other forms +- Extend the fix to other modes (`--dump-config`, etc) + ## [1.0.5] - 2021-06-05 #### Bug Fixes diff --git a/src/bin/typos-cli/main.rs b/src/bin/typos-cli/main.rs index 8667b89..ecbdfdd 100644 --- a/src/bin/typos-cli/main.rs +++ b/src/bin/typos-cli/main.rs @@ -66,11 +66,13 @@ fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exi let path = &args.path[0]; let cwd = if path == std::path::Path::new("-") { - global_cwd.as_path() + global_cwd } 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.canonicalize().with_code(proc_exit::Code::USAGE_ERR)? }; let cwd = cwd.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?; @@ -108,11 +110,13 @@ fn run_type_list(args: &args::Args) -> proc_exit::ExitResult { let path = &args.path[0]; let cwd = if path == std::path::Path::new("-") { - global_cwd.as_path() + global_cwd } 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.canonicalize().with_code(proc_exit::Code::USAGE_ERR)? }; let cwd = cwd.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?; @@ -178,7 +182,7 @@ fn run_checks( cwd.pop(); cwd } else { - path.clone() + path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)? }; engine diff --git a/tests/cli.rs b/tests/cli.rs index 9fcc0cd..e0fec87 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -29,3 +29,16 @@ fn test_file_failure() { cmd.arg("README.md"); cmd.assert().code(2); } + +#[test] +fn test_relative_dir_failure() { + let mut cmd = Command::cargo_bin("typos").unwrap(); + cmd.arg("."); + cmd.assert().code(2); +} + +#[test] +fn test_assumed_dir_failure() { + let mut cmd = Command::cargo_bin("typos").unwrap(); + cmd.assert().code(2); +}