diff --git a/ruff_cli/src/commands.rs b/ruff_cli/src/commands.rs index 65e605735d..57eb447998 100644 --- a/ruff_cli/src/commands.rs +++ b/ruff_cli/src/commands.rs @@ -120,7 +120,7 @@ pub fn run( location: Location::default(), end_location: Location::default(), fix: None, - filename: path.to_string_lossy().to_string(), + filename: format!("{}", path.display()), source: None, }]) } else { diff --git a/ruff_cli/tests/integration_test.rs b/ruff_cli/tests/integration_test.rs index d2f77172c6..e82b774d84 100644 --- a/ruff_cli/tests/integration_test.rs +++ b/ruff_cli/tests/integration_test.rs @@ -1,9 +1,12 @@ #![cfg(not(target_family = "wasm"))] +#[cfg(unix)] +use std::path::Path; use std::str; use anyhow::Result; use assert_cmd::Command; +#[cfg(unix)] use path_absolutize::path_dedot; const BIN_NAME: &str = "ruff"; @@ -50,6 +53,7 @@ fn test_stdin_filename() -> Result<()> { Ok(()) } +#[cfg(unix)] #[test] fn test_stdin_json() -> Result<()> { let mut cmd = Command::cargo_bin(BIN_NAME)?; @@ -58,6 +62,11 @@ fn test_stdin_json() -> Result<()> { .write_stdin("import os\n") .assert() .failure(); + + let directory = path_dedot::CWD.to_str().unwrap(); + let binding = Path::new(directory).join("F401.py"); + let file_path = binding.display(); + assert_eq!( str::from_utf8(&output.get_output().stdout)?, format!( @@ -85,11 +94,10 @@ fn test_stdin_json() -> Result<()> { "row": 1, "column": 10 }}, - "filename": "{}/F401.py" + "filename": "{file_path}" }} ] -"#, - path_dedot::CWD.to_str().unwrap() +"# ) ); Ok(()) diff --git a/src/fs.rs b/src/fs.rs index a50ba8e10a..2e5b347af6 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -1,4 +1,3 @@ -use std::borrow::Cow; use std::fs::File; use std::io::{BufReader, Read}; use std::path::{Path, PathBuf}; @@ -62,11 +61,11 @@ pub fn normalize_path_to, R: AsRef>(path: P, project_root: } /// Convert an absolute path to be relative to the current working directory. -pub fn relativize_path(path: &Path) -> Cow { +pub fn relativize_path(path: &Path) -> String { if let Ok(path) = path.strip_prefix(&*path_dedot::CWD) { - return path.to_string_lossy(); + return format!("{}", path.display()); } - path.to_string_lossy() + format!("{}", path.display()) } /// Read a file's contents from disk. diff --git a/src/rules/flake8_executable/mod.rs b/src/rules/flake8_executable/mod.rs index 6fcc956c93..f27d82ea9b 100644 --- a/src/rules/flake8_executable/mod.rs +++ b/src/rules/flake8_executable/mod.rs @@ -2,6 +2,7 @@ pub(crate) mod helpers; pub(crate) mod rules; +#[cfg(unix)] #[cfg(test)] mod tests { use std::path::Path; diff --git a/src/rules/flake8_no_pep420/mod.rs b/src/rules/flake8_no_pep420/mod.rs index 88d8422d11..04d38aeff3 100644 --- a/src/rules/flake8_no_pep420/mod.rs +++ b/src/rules/flake8_no_pep420/mod.rs @@ -21,11 +21,13 @@ mod tests { #[test_case(Path::new("test_pass_namespace_package"); "INP001_5")] fn test_flake8_no_pep420(path: &Path) -> Result<()> { let snapshot = format!("{}", path.to_string_lossy()); + // Platform-independent paths + let p = PathBuf::from(format!( + "./resources/test/fixtures/flake8_no_pep420/{}/example.py", + path.display() + )); let diagnostics = test_path( - Path::new("./resources/test/fixtures/flake8_no_pep420") - .join(path) - .join("example.py") - .as_path(), + p.as_path(), &Settings { namespace_packages: vec![PathBuf::from( "./resources/test/fixtures/flake8_no_pep420/test_pass_namespace_package", diff --git a/src/rules/flake8_no_pep420/rules.rs b/src/rules/flake8_no_pep420/rules.rs index 932fabe8f8..fdf6d29546 100644 --- a/src/rules/flake8_no_pep420/rules.rs +++ b/src/rules/flake8_no_pep420/rules.rs @@ -8,7 +8,7 @@ use crate::{fs, violations}; pub fn implicit_namespace_package(path: &Path, package: Option<&Path>) -> Option { if package.is_none() { Some(Diagnostic::new( - violations::ImplicitNamespacePackage(fs::relativize_path(path).to_string()), + violations::ImplicitNamespacePackage(fs::relativize_path(path)), Range::default(), )) } else {