Skip to content

Commit

Permalink
test(infra): test the path resoltion error variants (#2210)
Browse files Browse the repository at this point in the history
* test(infra): test the path resoltion error variants

* test(infra): simplify resolve project relative path tests
  • Loading branch information
ArniStarkware authored Nov 21, 2024
1 parent 2547bb7 commit ab6c9b6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/infra_utils/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ use std::sync::LazyLock;

use thiserror::Error;

#[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),
}
Expand Down
24 changes: 24 additions & 0 deletions crates/infra_utils/src/path_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::path::{path_of_project_root, resolve_project_relative_path, PathResolutionError};

// 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);
}
}

#[test]
fn resolve_project_relative_path_success() {
let relative_path = std::file!();
let result = resolve_project_relative_path(relative_path);

assert!(result.unwrap().ends_with(relative_path));
}

0 comments on commit ab6c9b6

Please sign in to comment.