From 1cdac0f9024605e94c6ba63cb57f313d385a77ce Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 15 May 2021 09:32:35 -0700 Subject: [PATCH] working copy: use system path separator when creating file system path Hopefully this fixes the failing Windows CI. --- lib/src/repo_path.rs | 45 ++++++++++++++++++++++++++++++++--------- lib/src/working_copy.rs | 4 +--- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/lib/src/repo_path.rs b/lib/src/repo_path.rs index d19e6fc25..7b99fed63 100644 --- a/lib/src/repo_path.rs +++ b/lib/src/repo_path.rs @@ -13,6 +13,7 @@ // limitations under the License. use std::fmt::{Debug, Error, Formatter}; +use std::path::{Path, PathBuf}; #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] pub struct RepoPathComponent { @@ -284,6 +285,14 @@ impl FileRepoPath { }, } } + + pub fn to_fs_path(&self, base: &Path) -> PathBuf { + let mut result = base.to_owned(); + for dir in self.dir.components() { + result = result.join(&dir.value); + } + result.join(&self.basename.value) + } } impl From<&str> for FileRepoPath { @@ -345,7 +354,7 @@ mod tests { use super::*; #[test] - fn is_root() { + fn test_is_root() { assert!(RepoPath::root().is_root()); assert!(RepoPath::from("").is_root()); assert!(!RepoPath::from("foo").is_root()); @@ -355,7 +364,7 @@ mod tests { } #[test] - fn value() { + fn test_value() { assert_eq!(RepoPath::root().to_internal_string(), ""); assert_eq!(RepoPath::from("dir").to_internal_string(), "dir"); assert_eq!(RepoPath::from("file").to_internal_string(), "file"); @@ -374,7 +383,7 @@ mod tests { } #[test] - fn order() { + fn test_order() { assert!(DirRepoPath::root() < DirRepoPath::from("dir/")); assert!(DirRepoPath::from("dir/") < DirRepoPath::from("dirx/")); // '#' < '/' @@ -389,7 +398,7 @@ mod tests { } #[test] - fn join() { + fn test_join() { let root = DirRepoPath::root(); let dir_component = DirRepoPathComponent::from("dir"); let subdir_component = DirRepoPathComponent::from("subdir"); @@ -407,7 +416,7 @@ mod tests { } #[test] - fn parent() { + fn test_parent() { let root = DirRepoPath::root(); let dir_component = DirRepoPathComponent::from("dir"); let subdir_component = DirRepoPathComponent::from("subdir"); @@ -421,7 +430,7 @@ mod tests { } #[test] - fn split_dir() { + fn test_split_dir() { let root = DirRepoPath::root(); let dir_component = DirRepoPathComponent::from("dir"); let subdir_component = DirRepoPathComponent::from("subdir"); @@ -435,7 +444,7 @@ mod tests { } #[test] - fn split_file() { + fn test_split_file() { let root = DirRepoPath::root(); let dir_component = DirRepoPathComponent::from("dir"); let file_component = FileRepoPathComponent::from("file"); @@ -450,7 +459,7 @@ mod tests { } #[test] - fn dir() { + fn test_dir() { let root = DirRepoPath::root(); let dir_component = DirRepoPathComponent::from("dir"); let file_component = FileRepoPathComponent::from("file"); @@ -462,7 +471,7 @@ mod tests { } #[test] - fn components() { + fn test_components() { assert_eq!(DirRepoPath::root().components(), &vec![]); assert_eq!( DirRepoPath::from("dir/").components(), @@ -478,7 +487,23 @@ mod tests { } #[test] - fn convert() { + fn test_to_fs_path() { + assert_eq!( + FileRepoPath::from("file").to_fs_path(&Path::new("base/dir")), + Path::new("base/dir/file") + ); + assert_eq!( + FileRepoPath::from("some/deep/dir/file").to_fs_path(&Path::new("base/dir")), + Path::new("base/dir/some/deep/dir/file") + ); + assert_eq!( + FileRepoPath::from("dir/file").to_fs_path(&Path::new("")), + Path::new("dir/file") + ); + } + + #[test] + fn test_convert() { assert_eq!(RepoPath::root().to_dir_repo_path(), DirRepoPath::root()); assert_eq!( RepoPath::from("dir").to_dir_repo_path(), diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index 48f96e290..b84d4134b 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -512,9 +512,7 @@ impl TreeState { }; for (path, diff) in old_tree.diff(&new_tree) { - let disk_path = self - .working_copy_path - .join(PathBuf::from(path.to_internal_string())); + let disk_path = path.to_fs_path(&self.working_copy_path); // TODO: Check that the file has not changed before overwriting/removing it. match diff {