From ab6c9b64d8b13af0ceed66a68f499dc04f5fee63 Mon Sep 17 00:00:00 2001 From: Arnon Hod Date: Thu, 21 Nov 2024 15:20:27 +0200 Subject: [PATCH] test(infra): test the path resoltion error variants (#2210) * test(infra): test the path resoltion error variants * test(infra): simplify resolve project relative path tests --- crates/infra_utils/src/path.rs | 6 ++++++ crates/infra_utils/src/path_test.rs | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 crates/infra_utils/src/path_test.rs diff --git a/crates/infra_utils/src/path.rs b/crates/infra_utils/src/path.rs index 9f2ce3cd15..cfa2444a24 100644 --- a/crates/infra_utils/src/path.rs +++ b/crates/infra_utils/src/path.rs @@ -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), } diff --git a/crates/infra_utils/src/path_test.rs b/crates/infra_utils/src/path_test.rs new file mode 100644 index 0000000000..88d7f5ff23 --- /dev/null +++ b/crates/infra_utils/src/path_test.rs @@ -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)); +}