Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed path strip panic on Windows #36

Merged
merged 5 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading