Skip to content

Commit

Permalink
Clean path from find-up
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Soltis authored and Greg Soltis committed May 16, 2023
1 parent 642fac8 commit 16d0bfa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
14 changes: 11 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/turbopath/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ edition = "2021"

[dependencies]
bstr = "1.4.0"
dunce = { workspace = true }
path-clean = "1.0.1"
path-slash = "0.2.1"
# TODO: Make this a crate feature
serde = { workspace = true }
Expand Down
23 changes: 21 additions & 2 deletions crates/turbopath/src/absolute_system_path_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
path::{Components, Path, PathBuf},
};

use path_clean::PathClean;
use serde::Serialize;

use crate::{
Expand Down Expand Up @@ -237,10 +238,14 @@ impl AbsoluteSystemPathBuf {
}

pub fn to_realpath(&self) -> Result<Self, PathError> {
let realpath = fs::canonicalize(&self.0)?;
let realpath = dunce::canonicalize(&self.0)?;
Ok(Self(realpath))
}

pub fn clean(&self) -> Self {
Self(self.0.clean())
}

pub fn symlink_to_file(&self, target: impl AsRef<Path>) -> Result<(), PathError> {
self.as_absolute_path().symlink_to_file(target)
}
Expand Down Expand Up @@ -291,6 +296,13 @@ mod tests {
PathValidationError::NotAbsolute(_)
))
);

assert_eq!(
AbsoluteSystemPathBuf::new("/some/dir/../other")
.unwrap()
.clean(),
AbsoluteSystemPathBuf::new("/some/other").unwrap(),
);
}

#[cfg(windows)]
Expand All @@ -314,6 +326,13 @@ mod tests {
Err(PathError::PathValidationError(
PathValidationError::NotAbsolute(_)
))
)
);

assert_eq!(
AbsoluteSystemPathBuf::new("C:\\some\\dir\\..\\other")
.unwrap()
.clean(),
AbsoluteSystemPathBuf::new("C:\\some\\other").unwrap(),
);
}
}
16 changes: 15 additions & 1 deletion crates/turborepo-scm/src/package_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub(crate) fn find_git_root(
.current_dir(turbo_root)
.output()?;
let root = String::from_utf8(rev_parse.stdout)?;
Ok(turbo_root.join_literal(root.trim_end()).to_realpath()?)
Ok(turbo_root.join_literal(root.trim_end()).clean())
}

#[cfg(test)]
Expand Down Expand Up @@ -72,6 +72,20 @@ mod tests {
}
}

#[test]
fn test_symlinked_git_root() {
let (_, tmp_root) = tmp_dir();
let git_root = tmp_root.join_literal("actual_repo");
git_root.create_dir_all().unwrap();
setup_repository(&git_root);
git_root.join_literal("inside").create_dir_all().unwrap();
let link = tmp_root.join_literal("link");
link.symlink_to_dir("actual_repo").unwrap();
let turbo_root = link.join_literal("inside");
let result = find_git_root(&turbo_root).unwrap();
assert_eq!(result, link);
}

#[test]
fn test_get_package_deps() -> Result<(), Error> {
// Directory structure:
Expand Down

0 comments on commit 16d0bfa

Please sign in to comment.