Skip to content

Commit

Permalink
fix: Removed path slash conversion on unix (#5356)
Browse files Browse the repository at this point in the history
### 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 <Nicholas Yang>
  • Loading branch information
NicholasLYang committed Jun 22, 2023
1 parent 2f2fd78 commit 6d572e9
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions crates/turborepo-paths/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn is_not_system(path: impl AsRef<str>) -> bool {
path.as_ref().contains(non_system_separator)
}

#[cfg(windows)]
fn convert_separator(
path: impl AsRef<str>,
input_separator: char,
Expand All @@ -125,18 +126,15 @@ fn convert_separator(

impl<T: AsRef<str>> 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
}
}

Expand All @@ -160,3 +158,42 @@ impl<T: AsRef<str>> 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");
}
}
}

0 comments on commit 6d572e9

Please sign in to comment.