fix(Files): Do not panic if unable to make path relative
This avoids a panic that was occuring on MacOS when using the `--dry-run` option. e.g.
`stencila sources add github:stencila/test data --dry-run`.
(i.e. when a temp dir is created and paths are made relative to it)
@@ -240,17 +240,16 @@ impl Files {
if absolute_paths {
(path, file)
} else {
- file.path = file
- .path
- .strip_prefix(&root_path)
- .expect("Should always have path as prefix")
- .to_path_buf();
+ file.path = path.strip_prefix(&root_path).unwrap_or(&path).to_path_buf();
file.parent = match file.parent {
- Some(path) => match path == root_path {
+ Some(parent_path) => match parent_path == root_path {
true => None,
false => Some(
- path.strip_prefix(&root_path).unwrap_or(&path).to_path_buf(),
+ parent_path
+ .strip_prefix(&root_path)
+ .unwrap_or(&parent_path)
+ .to_path_buf(),
),
},
