Skip to content

Commit

Permalink
fix: fix path strip panic on Windows (#36)
Browse files Browse the repository at this point in the history
* fix: Fixed path strip panic on Windows

* chore(vscode): update rust analyzer settings

* fix(codspeed): fix handling of symbolic links in get_git_relative_path

* fixup! fix(codspeed): fix handling of symbolic links in get_git_relative_path

---------

Co-authored-by: Adrien Cacciaguerra <adrien.caccia@gmail.com>
  • Loading branch information
marc2332 and adriencaccia authored Feb 26, 2024
1 parent 69c8309 commit db3d58c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
6 changes: 4 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"rust-analyzer.cargo.extraEnv": {
"RUSTFLAGS": "--cfg codspeed"
},
"rust-analyzer.cargo.allFeatures": true,
"rust-analyzer.checkOnSave.command": "clippy"
"rust-analyzer.cargo.features": "all",
"editor.formatOnSave": true,
"rust-analyzer.checkOnSave": true,
"rust-analyzer.check.command": "clippy"
}
31 changes: 24 additions & 7 deletions crates/codspeed/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ pub fn get_git_relative_path<P>(abs_path: P) -> PathBuf
where
P: AsRef<Path>,
{
let abs_path = abs_path.as_ref();
match abs_path
.canonicalize()
.and_then(|p| get_parent_git_repo_path(&p))
{
Ok(repo_path) => abs_path.strip_prefix(repo_path).unwrap().to_path_buf(),
Err(_) => abs_path.to_path_buf(),
if let Ok(canonicalized_abs_path) = abs_path.as_ref().canonicalize() {
// `repo_path` is still canonicalized as it is a subpath of `canonicalized_abs_path`
if let Ok(repo_path) = get_parent_git_repo_path(&canonicalized_abs_path) {
canonicalized_abs_path
.strip_prefix(repo_path)
.expect("Repository path is malformed.")
.to_path_buf()
} else {
canonicalized_abs_path
}
} else {
abs_path.as_ref().to_path_buf()
}
}

Expand Down Expand Up @@ -62,6 +67,18 @@ mod tests {
assert_eq!(relative_path, path_dir.canonicalize().unwrap());
}

#[test]
fn test_get_git_relative_path_not_found_with_symlink() {
let dir = tempdir().unwrap();
let path_dir = dir.path().join("folder");
fs::create_dir_all(&path_dir).unwrap();
let symlink = dir.path().join("symlink");
std::os::unix::fs::symlink(&path_dir, &symlink).unwrap();

let relative_path = get_git_relative_path(&symlink);
assert_eq!(relative_path, symlink.canonicalize().unwrap());
}

#[test]
fn test_get_formated_function_path() {
let input = "std :: vec :: Vec :: new";
Expand Down

0 comments on commit db3d58c

Please sign in to comment.