From 06f3006cd86b5b7a26f191d57540e58eed19bab1 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 4 Mar 2024 12:22:09 -0500 Subject: [PATCH] Review feedback --- crates/ruff/src/args.rs | 17 ++++++++++++----- crates/ruff/tests/format.rs | 2 +- crates/ruff/tests/lint.rs | 6 +----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/crates/ruff/src/args.rs b/crates/ruff/src/args.rs index f56d75ecda6fb4..8aea65056394ad 100644 --- a/crates/ruff/src/args.rs +++ b/crates/ruff/src/args.rs @@ -812,12 +812,19 @@ impl TypedValueParser for ConfigArgumentParser { arg: Option<&clap::Arg>, value: &std::ffi::OsStr, ) -> Result { - let value = value - .to_str() - .ok_or_else(|| clap::Error::new(clap::error::ErrorKind::InvalidUtf8))?; + // Convert to UTF-8. + let Some(value) = value.to_str() else { + // But respect non-UTF-8 paths. + let path_to_config_file = PathBuf::from(value); + if path_to_config_file.is_file() { + return Ok(SingleConfigArgument::FilePath(path_to_config_file)); + } + return Err(clap::Error::new(clap::error::ErrorKind::InvalidUtf8)); + }; + // Expand environment variables and tildes. if let Ok(path_to_config_file) = - shellexpand::full(value).map(|config| PathBuf::from(config.as_ref())) + shellexpand::full(value).map(|config| PathBuf::from(&*config)) { if path_to_config_file.is_file() { return Ok(SingleConfigArgument::FilePath(path_to_config_file)); @@ -890,7 +897,7 @@ A `--config` flag must either be a path to a `.toml` configuration file " It looks like you were trying to pass a path to a configuration file. -The path `{value}` does not exist" +The path `{value}` does not point to a configuration file" )); } } else if value.contains('=') { diff --git a/crates/ruff/tests/format.rs b/crates/ruff/tests/format.rs index 0a63670cf676c9..8301e789abcd2a 100644 --- a/crates/ruff/tests/format.rs +++ b/crates/ruff/tests/format.rs @@ -111,7 +111,7 @@ fn nonexistent_config_file() { option It looks like you were trying to pass a path to a configuration file. - The path `foo.toml` does not exist + The path `foo.toml` does not point to a configuration file For more information, try '--help'. "###); diff --git a/crates/ruff/tests/lint.rs b/crates/ruff/tests/lint.rs index 23aa96ea4cca53..1dc89484a907a1 100644 --- a/crates/ruff/tests/lint.rs +++ b/crates/ruff/tests/lint.rs @@ -522,7 +522,7 @@ fn nonexistent_config_file() { option It looks like you were trying to pass a path to a configuration file. - The path `foo.toml` does not exist + The path `foo.toml` does not point to a configuration file For more information, try '--help'. "###); @@ -1141,9 +1141,6 @@ ignore = ["F841"] "#, )?; - insta::with_settings!({ - filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")] - }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .args(STDIN_BASE_OPTIONS) .arg("--config") @@ -1166,7 +1163,6 @@ def func(): ----- stderr ----- "###); - }); Ok(()) }