use qubit_fs::error::FsError;
use qubit_fs::error::FsErrorKind;
use qubit_fs::error::FsOperation;
use qubit_fs::metadata::FileSystemCapability;
use qubit_fs::path::Path;
#[track_caller]
#[cfg(test)]
pub(crate) fn assert_error(
error: &FsError,
kind: FsErrorKind,
operation: FsOperation,
path: Option<&Path>,
provider: Option<&str>,
capability: Option<FileSystemCapability>,
) {
assert_eq!(kind, error.kind(), "filesystem error kind must match");
assert_eq!(operation, error.operation(), "filesystem error operation must match",);
assert_eq!(path, error.path(), "filesystem error path must match");
assert!(
error.provider().is_none() || error.provider() == provider,
"filesystem error provider must be absent or match the configured provider",
);
assert_eq!(
capability,
error.required_capability(),
"filesystem error required capability must match",
);
}
#[track_caller]
#[cfg(test)]
pub(crate) fn assert_error_with_target(
error: &FsError,
kind: FsErrorKind,
operation: FsOperation,
path: Option<&Path>,
target: Option<&Path>,
provider: Option<&str>,
capability: Option<FileSystemCapability>,
) {
assert_error(error, kind, operation, path, provider, capability);
assert_eq!(target, error.target(), "filesystem error target must match");
}
#[track_caller]
#[cfg(test)]
pub(crate) fn assert_error_with_source_or_target(
error: &FsError,
kind: FsErrorKind,
operation: FsOperation,
source: &Path,
target: &Path,
provider: Option<&str>,
capability: Option<FileSystemCapability>,
) {
assert_eq!(kind, error.kind(), "filesystem error kind must match");
assert_eq!(operation, error.operation(), "filesystem error operation must match");
assert!(
matches!(error.path(), Some(path) if path == source || path == target),
"filesystem error path must identify the source or target failure location",
);
assert_eq!(Some(target), error.target(), "filesystem error target must match");
assert!(
error.provider().is_none() || error.provider() == provider,
"filesystem error provider must be absent or match the configured provider",
);
assert_eq!(
capability,
error.required_capability(),
"filesystem error required capability must match"
);
}
#[track_caller]
#[cfg(test)]
pub(crate) fn assert_unsupported_error(
error: &FsError,
kind: FsErrorKind,
operation: FsOperation,
path: Option<&Path>,
provider: Option<&str>,
capability: Option<FileSystemCapability>,
) {
assert_eq!(kind, error.kind(), "filesystem error kind must match");
assert_eq!(operation, error.operation(), "filesystem error operation must match",);
assert_eq!(path, error.path(), "filesystem error path must match");
assert_eq!(
capability,
error.required_capability(),
"filesystem error required capability must match",
);
assert!(
error.provider().is_none() || error.provider() == provider,
"filesystem error provider must be absent or match the configured provider",
);
}
pub(crate) fn verify_missing_error(
error: FsError,
path: &Path,
provider: &str,
check: crate::ContractCheckId,
) -> Result<(), crate::ContractFailure> {
let valid = error.kind() == FsErrorKind::NotFound
&& error.operation() == FsOperation::Stat
&& error.path() == Some(path)
&& error.target().is_none()
&& error.required_capability().is_none()
&& error.provider().is_none_or(|actual| actual == provider);
if valid {
Ok(())
} else {
Err(
crate::ContractFailure::with_source("missing-resource error context differs from the contract", error)
.at(check),
)
}
}
pub(crate) fn verify_fs_error(
error: FsError,
kind: FsErrorKind,
operation: FsOperation,
path: &Path,
provider: &str,
capability: Option<FileSystemCapability>,
check: crate::ContractCheckId,
) -> Result<(), crate::ContractFailure> {
let valid = error.kind() == kind
&& error.operation() == operation
&& error.path() == Some(path)
&& error.required_capability() == capability
&& error.provider().is_none_or(|actual| actual == provider);
if valid {
Ok(())
} else {
Err(crate::ContractFailure::with_source(format!("{check}: filesystem error context mismatch"), error).at(check))
}
}
pub(crate) fn verify_condition(
condition: bool,
check: crate::ContractCheckId,
message: &str,
) -> Result<(), crate::ContractFailure> {
if condition {
Ok(())
} else {
Err(crate::ContractFailure::message_only(format!("{check}: {message}")).at(check))
}
}
#[cfg(test)]
mod tests {
use qubit_fs::error::FsError;
use qubit_fs::error::FsErrorKind;
use qubit_fs::error::FsOperation;
use qubit_fs::metadata::FileSystemCapability;
use qubit_fs::path::Path;
use super::assert_error;
use super::assert_error_with_source_or_target;
use super::assert_error_with_target;
use super::assert_unsupported_error;
use super::verify_condition;
use super::verify_fs_error;
use super::verify_missing_error;
#[test]
fn test_error_assertions_accept_matching_structured_context() {
let path = Path::parse("/source").unwrap();
let target = Path::parse("/target").unwrap();
let capability = FileSystemCapability::Read;
let error = || {
FsError::new(FsErrorKind::Io, FsOperation::Read, "read failed")
.with_path(path.clone())
.with_provider("memory")
.with_required_capability(capability)
};
assert_error(
&error(),
FsErrorKind::Io,
FsOperation::Read,
Some(&path),
Some("memory"),
Some(capability),
);
assert_error_with_target(
&error().with_target(target.clone()),
FsErrorKind::Io,
FsOperation::Read,
Some(&path),
Some(&target),
Some("memory"),
Some(capability),
);
assert_error_with_source_or_target(
&error().with_path(target.clone()).with_target(target.clone()),
FsErrorKind::Io,
FsOperation::Read,
&path,
&target,
Some("memory"),
Some(capability),
);
assert_unsupported_error(
&FsError::new(FsErrorKind::UnsupportedCapability, FsOperation::Read, "unsupported")
.with_path(path.clone())
.with_required_capability(capability),
FsErrorKind::UnsupportedCapability,
FsOperation::Read,
Some(&path),
Some("memory"),
Some(capability),
);
}
#[test]
fn test_error_verifiers_accept_and_reject_structured_context() {
let path = Path::parse("/missing").unwrap();
let check = crate::ContractCheckId::ReadBasic;
let missing = FsError::new(FsErrorKind::NotFound, FsOperation::Stat, "missing").with_path(path.clone());
assert!(verify_missing_error(missing, &path, "memory", check).is_ok());
let wrong_missing = FsError::new(FsErrorKind::Io, FsOperation::Stat, "wrong kind").with_path(path.clone());
assert!(verify_missing_error(wrong_missing, &path, "memory", check).is_err());
let matching = FsError::new(FsErrorKind::Io, FsOperation::Read, "read failed")
.with_path(path.clone())
.with_provider("memory");
assert!(
verify_fs_error(
matching,
FsErrorKind::Io,
FsOperation::Read,
&path,
"memory",
None,
check,
)
.is_ok()
);
let mismatching = FsError::new(FsErrorKind::Io, FsOperation::Read, "wrong provider")
.with_path(path.clone())
.with_provider("other");
assert!(
verify_fs_error(
mismatching,
FsErrorKind::Io,
FsOperation::Read,
&path,
"memory",
None,
check,
)
.is_err()
);
assert!(verify_condition(true, check, "condition").is_ok());
assert!(verify_condition(false, check, "condition").is_err());
}
}