use std::panic::AssertUnwindSafe;
use std::panic::catch_unwind;
pub(crate) use qubit_fs::error::FsErrorKind;
pub(crate) use qubit_fs::error::FsOperation;
pub(crate) use qubit_fs::metadata::FileSystemCapability;
pub(crate) use qubit_fs::path::Path;
pub(crate) use qubit_fs::write::WriteDisposition;
use crate::ContractReport;
use crate::FileSystemContract;
use crate::FileSystemFixture;
use crate::contract_context::ContractContext;
mod properties;
mod read;
mod random_access;
mod write;
mod write_limit;
mod write_if_absent;
mod write_if_match;
mod write_guarantee;
mod append;
mod create_conflict;
mod replace;
mod abort;
mod stat;
mod create_directory;
mod representations;
mod delete;
mod delete_tree;
mod rename;
mod list;
mod namespace;
mod basic_copy;
mod copy;
mod copy_conflict;
mod native_copy;
mod server_side_copy;
mod strong_file_copy;
mod strong_tree_copy;
mod temp;
mod temp_directory_check;
mod temp_file_check;
mod support;
#[must_use = "the suite must run at least one contract assertion"]
pub struct FileSystemContractSuite<'a> {
fixture: &'a dyn FileSystemFixture,
context: ContractContext,
teardown_completed: bool,
}
impl<'a> FileSystemContractSuite<'a> {
#[inline]
pub fn new(fixture: &'a dyn FileSystemFixture) -> Self {
Self {
fixture,
context: ContractContext::new(fixture.file_system().properties()),
teardown_completed: false,
}
}
#[must_use = "inspect the retained contract run"]
#[inline]
pub const fn run(&self) -> &crate::ContractRun {
&self.context.run
}
#[must_use = "inspect the accumulated contract report"]
#[inline]
pub fn report(&self) -> &ContractReport {
self.context.report()
}
#[must_use = "inspect the retained contract run"]
pub fn run_all(&mut self) -> &crate::ContractRun {
self.run_selected(&FileSystemContract::ALL, None)
}
#[must_use = "inspect the retained contract run"]
pub fn run_contract(&mut self, contract: FileSystemContract) -> &crate::ContractRun {
self.run_selected(&[contract], None)
}
#[must_use = "inspect the retained contract run"]
pub fn run_check(&mut self, id: crate::ContractCheckId) -> &crate::ContractRun {
self.run_selected(&[id.contract()], Some(id))
}
fn run_selected(
&mut self,
contracts: &[FileSystemContract],
selected: Option<crate::ContractCheckId>,
) -> &crate::ContractRun {
if self.context.run.started {
self.context.run.failures.push(crate::ContractFailure::message_only(
"session already started; create a fresh fixture for another run",
));
return &self.context.run;
}
if self.context.run.cleanup.attempts > 0 {
self.context.run.failures.push(crate::ContractFailure::message_only(
"session cleanup already started; create a fresh fixture for another run",
));
return &self.context.run;
}
self.context.run.started = true;
if let Some(id) = selected {
let spec = crate::internal::check_catalog::specification(id);
self.context.run.report.register(id, spec.capability);
self.context.begin_check(id);
} else {
for contract in contracts {
self.context.prepare_phase(*contract, false);
}
}
for contract in contracts {
let result = catch_unwind(AssertUnwindSafe(|| match selected {
Some(id) => self.dispatch_check(id),
None => self.dispatch_contract(*contract),
}));
match result {
Ok(Ok(())) => {}
Ok(Err(failure)) => {
self.context.fail(failure);
break;
}
Err(payload) => {
let mut failure = crate::ContractFailure::panicked("contract execution panicked", payload);
if let Some(id) = selected.or(self.context.active_check()) {
failure = failure.at(id);
}
self.context.fail(failure);
break;
}
}
}
self.context.begin("cleanup");
if let Err(payload) = catch_unwind(AssertUnwindSafe(|| self.finish())) {
self.context
.run
.failures
.push(crate::ContractFailure::panicked("cleanup failed", payload));
}
self.context.run.completed = true;
&self.context.run
}
fn dispatch_check(&mut self, id: crate::ContractCheckId) -> Result<(), crate::ContractFailure> {
if !id.supports_synchronous() {
return Err(crate::ContractFailure::message_only("selected check requires asynchronous execution").at(id));
}
match id.contract() {
FileSystemContract::Copy => self.check_copy_item(id),
FileSystemContract::TempResources => self.check_temp_item(id),
FileSystemContract::List => self.check_list_item(id),
FileSystemContract::Rename => self.check_rename_item(id),
FileSystemContract::Delete => self.check_delete_item(id),
FileSystemContract::Representations => self.check_representation_item(id),
FileSystemContract::CreateDirectory => self.check_create_directory_item(id),
FileSystemContract::Properties => self.check_properties_item(id),
FileSystemContract::Stat => self.check_stat_item(id),
FileSystemContract::Read => self.check_read_item(id),
FileSystemContract::Write => self.check_write_item(id),
FileSystemContract::ErrorContext => self.check_error_context(),
}
}
fn dispatch_contract(&mut self, contract: FileSystemContract) -> Result<(), crate::ContractFailure> {
match contract {
FileSystemContract::Properties => self.check_properties()?,
FileSystemContract::Stat => self.check_stat()?,
FileSystemContract::Read => self.check_read()?,
FileSystemContract::Write => {
self.check_write()?;
}
FileSystemContract::List => self.check_list()?,
FileSystemContract::CreateDirectory => self.check_create_directory()?,
FileSystemContract::Representations => self.check_representations()?,
FileSystemContract::Delete => {
self.check_delete()?;
self.check_delete_tree()?;
}
FileSystemContract::Copy => {
self.check_copy()?;
}
FileSystemContract::Rename => {
self.check_rename()?;
}
FileSystemContract::TempResources => self.check_temp_resources()?,
FileSystemContract::ErrorContext => self.check_error_context()?,
}
Ok(())
}
}