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

chore(infra): canonicalize path resolution #2254

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions crates/infra_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ description = "Infrastructure utility."

[lints]
workspace = true

[dependencies]
thiserror.workspace = true
27 changes: 6 additions & 21 deletions crates/infra_utils/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
use std::env;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;

use thiserror::Error;
use std::{env, fs};

#[cfg(test)]
#[path = "path_test.rs"]
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.
#[error(transparent)]
IoError(#[from] std::io::Error),
}

// TODO(tsabary): wrap path-related env::* invocations in the repo as utility functions
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,16 +22,13 @@ 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.
pub fn resolve_project_relative_path(relative_path: &str) -> Result<PathBuf, PathResolutionError> {
/// * A `PathBuf` representing the resolved path starting from the project root.
pub fn resolve_project_relative_path(relative_path: &str) -> Result<PathBuf, std::io::Error> {
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(path)
Ok(absolute_path)
}

fn path_of_project_root() -> PathBuf {
Expand Down
9 changes: 2 additions & 7 deletions crates/infra_utils/src/path_test.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
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]
fn resolve_project_relative_path_on_non_existent_path() {
let relative_path = "does_not_exist.txt";
let expected_path = path_of_project_root().join(relative_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
3 changes: 0 additions & 3 deletions crates/papyrus_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@

use clap::parser::MatchesError;
use dumping::REQUIRED_PARAM_DESCRIPTION_PREFIX;
use infra_utils::path::PathResolutionError;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use validator::ValidationError;
Expand Down Expand Up @@ -180,8 +179,6 @@ pub enum ConfigError {
#[error(transparent)]
CommandMatches(#[from] MatchesError),
#[error(transparent)]
GetPathError(#[from] PathResolutionError),
#[error(transparent)]
IOError(#[from] std::io::Error),
// TODO(Eitan): Improve error message
#[error("Insert a new param is not allowed: {param_path}.")]
Expand Down
Loading