From 6d572e9cdb8ecb61028f478197b2eb902e6ea2f8 Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Thu, 22 Jun 2023 12:39:53 -0400 Subject: [PATCH] fix: Removed path slash conversion on unix (#5356) ### Description Had Windows to Unix path conversion on Unix systems, which doesn't make sense. ### Testing Instructions Added tests to confirm behavior on Unix and Windows --------- Co-authored-by: --global --- crates/turborepo-paths/src/lib.rs | 47 +++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/crates/turborepo-paths/src/lib.rs b/crates/turborepo-paths/src/lib.rs index 09f7949f74c08..02377dae83559 100644 --- a/crates/turborepo-paths/src/lib.rs +++ b/crates/turborepo-paths/src/lib.rs @@ -103,6 +103,7 @@ fn is_not_system(path: impl AsRef) -> bool { path.as_ref().contains(non_system_separator) } +#[cfg(windows)] fn convert_separator( path: impl AsRef, input_separator: char, @@ -125,18 +126,15 @@ fn convert_separator( impl> IntoSystem for T { fn into_system(self) -> Utf8PathBuf { - let output; #[cfg(windows)] { - output = convert_separator(self, '/', std::path::MAIN_SEPARATOR) + convert_separator(self, '/', std::path::MAIN_SEPARATOR) } #[cfg(not(windows))] { - output = convert_separator(self, '\\', std::path::MAIN_SEPARATOR) + Utf8PathBuf::from(self.as_ref()) } - - output } } @@ -160,3 +158,42 @@ impl> IntoUnix for T { output } } + +#[cfg(test)] +mod tests { + use crate::{IntoSystem, IntoUnix}; + + #[test] + fn test_into_system() { + #[cfg(unix)] + { + assert_eq!("foo/bar".into_system(), "foo/bar"); + assert_eq!("/foo/bar".into_system(), "/foo/bar"); + assert_eq!("foo\\bar".into_system(), "foo\\bar"); + } + + #[cfg(windows)] + { + assert_eq!("foo/bar".into_system(), "foo\\bar"); + assert_eq!("/foo/bar".into_system(), "\\foo\\bar"); + assert_eq!("foo\\bar".into_system(), "foo\\bar"); + } + } + + #[test] + fn test_into_unix() { + #[cfg(unix)] + { + assert_eq!("foo/bar".into_unix(), "foo/bar"); + assert_eq!("/foo/bar".into_unix(), "/foo/bar"); + assert_eq!("foo\\bar".into_unix(), "foo\\bar"); + } + + #[cfg(windows)] + { + assert_eq!("foo/bar".into_unix(), "foo/bar"); + assert_eq!("\\foo\\bar".into_unix(), "/foo/bar"); + assert_eq!("foo\\bar".into_unix(), "foo/bar"); + } + } +}