Skip to content

Commit

Permalink
fix: Auto-ignore dest in more cases
Browse files Browse the repository at this point in the history
In manual testing, I found the current setup could not handle
```
source: "."
dest: "./build"
```

Ideally, we'd canonicalize these paths but at least dest might not exist
yet, so it'd fail.  I hand-implemented some path normalization.  I now
realize a real solution could possibly be made with `Path::components`.
That can be an exercise for if we run into more cases of this breaking.
  • Loading branch information
Ed Page authored and Ed Page committed Oct 26, 2017
1 parent 33c7d0d commit 8676a3a
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/config.rs
Expand Up @@ -257,6 +257,9 @@ impl ConfigBuilder {
let mut posts = posts;
posts.default = posts.default.merge(default);

let source = cleanup_path(source);
let destination = cleanup_path(destination);

let mut ignore = ignore;
if let Ok(rel_dest) = path::Path::new(&destination).strip_prefix(&source) {
let rel_dest = rel_dest.to_str().expect("started as a utf-8 string");
Expand Down Expand Up @@ -334,6 +337,15 @@ fn find_project_file_internal(dir: path::PathBuf, name: &str) -> Option<path::Pa
Some(file_path)
}

fn cleanup_path(path: String) -> String {
let stripped = path.trim_left_matches("./");
if stripped == "." {
String::new()
} else {
stripped.to_owned()
}
}

#[test]
fn find_project_file_same_dir() {
let actual = find_project_file("tests/fixtures/config", ".cobalt.yml").unwrap();
Expand All @@ -356,6 +368,32 @@ fn find_project_file_doesnt_exist() {
assert_eq!(actual, expected);
}

#[test]
fn cleanup_path_empty() {
assert_eq!(cleanup_path("".to_owned()), "".to_owned());
}

#[test]
fn cleanup_path_dot() {
assert_eq!(cleanup_path(".".to_owned()), "".to_owned());
}

#[test]
fn cleanup_path_current_dir() {
assert_eq!(cleanup_path("./".to_owned()), "".to_owned());
}

#[test]
fn cleanup_path_current_dir_extreme() {
assert_eq!(cleanup_path("././././.".to_owned()), "".to_owned());
}

#[test]
fn cleanup_path_current_dir_child() {
assert_eq!(cleanup_path("./build/file.txt".to_owned()),
"build/file.txt".to_owned());
}

#[test]
fn test_from_file_ok() {
let result = ConfigBuilder::from_file("tests/fixtures/config/.cobalt.yml").unwrap();
Expand Down

0 comments on commit 8676a3a

Please sign in to comment.