Skip to content

Commit

Permalink
chore(infra): canonicalize path resolution
Browse files Browse the repository at this point in the history
commit-id:c2547652
  • Loading branch information
Itay-Tsabary-Starkware committed Nov 24, 2024
1 parent 389ae7d commit 2463080
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
20 changes: 9 additions & 11 deletions crates/infra_utils/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use std::{env, fs};

use thiserror::Error;

Expand All @@ -10,11 +10,8 @@ mod path_test;

#[derive(Debug, Error)]
pub enum PathResolutionError {
// TODO(Arni): Handle manifest dir not exist here?
#[error("No file exists at '{path}'")]
PathDoesNotExist { path: PathBuf },
/// This error is raised when file existence can be neither confirmed nor denied. See
/// [`std::path::Path::try_exists`] for more information.
/// This error is raised when the file path does not exist, or when a non-final component in a
/// path is not a directory. See [`std::fs::canonicalize`] for more information.
#[error(transparent)]
IoError(#[from] std::io::Error),
}
Expand All @@ -23,6 +20,7 @@ pub enum PathResolutionError {
static PATH_TO_CARGO_MANIFEST_DIR: LazyLock<Option<PathBuf>> =
LazyLock::new(|| env::var("CARGO_MANIFEST_DIR").ok().map(|dir| Path::new(&dir).into()));

// TODO(Tsabary): should not be public. Use a getter instead.
pub fn cargo_manifest_dir() -> Option<PathBuf> {
PATH_TO_CARGO_MANIFEST_DIR.clone()
}
Expand All @@ -34,14 +32,14 @@ pub fn cargo_manifest_dir() -> Option<PathBuf> {
/// * `relative_path` - A string slice representing the relative path from the project root.
///
/// # Returns
/// * An absolute `PathBuf` representing the resolved path starting from the project root.
/// * A `PathBuf` representing the resolved path starting from the project root.
pub fn resolve_project_relative_path(relative_path: &str) -> Result<PathBuf, PathResolutionError> {
let base_dir = path_of_project_root();

let path = base_dir.join(relative_path);
if !path.try_exists()? {
return Err(PathResolutionError::PathDoesNotExist { path });
}
let absolute_path = fs::canonicalize(path)?;

Ok(absolute_path)
}

Ok(path)
}
Expand Down
8 changes: 2 additions & 6 deletions crates/infra_utils/src/path_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::path::{path_of_project_root, resolve_project_relative_path, PathResolutionError};
use crate::path::{path_of_project_root, resolve_project_relative_path};

// TODO: Add a test for PathResolutionError::IoError.
#[test]
Expand All @@ -8,11 +8,7 @@ fn resolve_project_relative_path_on_non_existent_path() {
assert!(!expected_path.exists());
let result = resolve_project_relative_path(relative_path);

if let Err(PathResolutionError::PathDoesNotExist { path }) = result {
assert_eq!(path, expected_path);
} else {
panic!("Expected PathDoesNotExist error, got {:?}", result);
}
assert!(result.is_err(), "Expected an non-existent path error, got {:?}", result);
}

#[test]
Expand Down

0 comments on commit 2463080

Please sign in to comment.