Skip to content

Commit

Permalink
feat(path): pushing an absolute path replaces destination pathbuf
Browse files Browse the repository at this point in the history
  • Loading branch information
Oakchris1955 committed Jul 26, 2024
1 parent 22af530 commit 278e60f
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ fn is_forbidden(pathbuf: &PathBuf) -> bool {
false
}

// TODO: pathbuf, if it is a file, what happens when pushing.
// TODO: pushing an absolute path should replace a pathbuf

/// Represents an owned, mutable path
Expand All @@ -82,6 +81,11 @@ impl PathBuf {
{
let subpath = subpath.to_string();

// if this is an absolute path, clear `self`
if ['\\', '/'].contains(&subpath.chars().next().unwrap_or_default()) {
self.clear()
}

let mut split_subpath: VecDeque<&str> =
subpath.split(|c| ['\\', '/'].contains(&c)).collect();

Expand Down Expand Up @@ -251,18 +255,6 @@ fn catch_non_control_forbidden_chars() {
}
}

#[test]
fn ignore_multiple_separators() {
let mut pathbuf = PathBuf::new();

pathbuf.push("first/");
pathbuf.push("second\\");
pathbuf.push("/third\\");
pathbuf.push("\\last/");

assert_eq!(pathbuf.to_string(), "/first/second/third/last/")
}

#[test]
fn push_to_pathbuf() {
let mut pathbuf = PathBuf::new();
Expand All @@ -271,7 +263,7 @@ fn push_to_pathbuf() {
pathbuf.push("bar/test");
pathbuf.push("bar2\\test2");
pathbuf.push("ignored\\../.");
pathbuf.push("\\fintest1");
pathbuf.push("fintest1");
pathbuf.push("fintest2/");
pathbuf.push("last");

Expand All @@ -280,3 +272,12 @@ fn push_to_pathbuf() {
"/foo/bar/test/bar2/test2/fintest1/fintest2/last"
)
}

#[test]
fn push_absolute_path() {
let mut pathbuf = PathBuf::from("/foo/bar.txt");

pathbuf.push("\\test");

assert_eq!(pathbuf.to_string(), "/test")
}

0 comments on commit 278e60f

Please sign in to comment.