fix: platform-independent paths

This commit is contained in:
Simon Brugman
2023-01-25 23:43:54 +01:00
committed by Charlie Marsh
parent 5835d719c4
commit d7fe1eeba0
6 changed files with 23 additions and 13 deletions

View File

@@ -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 {

View File

@@ -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(())

View File

@@ -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<P: AsRef<Path>, R: AsRef<Path>>(path: P, project_root:
}
/// Convert an absolute path to be relative to the current working directory.
pub fn relativize_path(path: &Path) -> Cow<str> {
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.

View File

@@ -2,6 +2,7 @@
pub(crate) mod helpers;
pub(crate) mod rules;
#[cfg(unix)]
#[cfg(test)]
mod tests {
use std::path::Path;

View File

@@ -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",

View File

@@ -8,7 +8,7 @@ use crate::{fs, violations};
pub fn implicit_namespace_package(path: &Path, package: Option<&Path>) -> Option<Diagnostic> {
if package.is_none() {
Some(Diagnostic::new(
violations::ImplicitNamespacePackage(fs::relativize_path(path).to_string()),
violations::ImplicitNamespacePackage(fs::relativize_path(path)),
Range::default(),
))
} else {