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;
pub(crate) use qubit_io::AsyncOutput;
use crate::AsyncFileSystemFixture;
use crate::ContractReport;
use crate::FileSystemContract;
use crate::contract_context::ContractContext;
use crate::internal::catch_unwind_future;
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 owning_write;
mod write_cancellation;
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_cancellation;
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 AsyncFileSystemContractSuite<'a> {
fixture: &'a dyn AsyncFileSystemFixture,
context: ContractContext,
teardown_completed: bool,
}
impl<'a> AsyncFileSystemContractSuite<'a> {
#[inline]
pub fn new(fixture: &'a dyn AsyncFileSystemFixture) -> Self {
Self {
fixture,
context: ContractContext::new(fixture.file_system().properties()),
teardown_completed: false,
}
}
#[inline]
pub fn report(&self) -> &ContractReport {
self.context.report()
}
#[inline]
pub const fn run(&self) -> &crate::ContractRun {
&self.context.run
}
pub async fn run_all(&mut self) -> &crate::ContractRun {
self.run_selected(&FileSystemContract::ALL, None).await
}
pub async fn run_contract(&mut self, contract: FileSystemContract) -> &crate::ContractRun {
self.run_selected(&[contract], None).await
}
pub async fn run_check(&mut self, id: crate::ContractCheckId) -> &crate::ContractRun {
self.run_selected(&[id.contract()], Some(id)).await
}
async 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, true);
}
}
for contract in contracts {
let result = catch_unwind_future(async {
match selected {
Some(id) => self.dispatch_check(id).await,
None => self.dispatch_contract(*contract).await,
}
})
.await;
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_future(self.finish()).await {
self.context
.run
.failures
.push(crate::ContractFailure::panicked("cleanup failed", payload));
}
self.context.run.completed = true;
&self.context.run
}
async fn dispatch_check(&mut self, id: crate::ContractCheckId) -> Result<(), crate::ContractFailure> {
match id.contract() {
FileSystemContract::Copy => self.check_copy_item(id).await,
FileSystemContract::TempResources => self.check_temp_item(id).await,
FileSystemContract::List => self.check_list_item(id).await,
FileSystemContract::Rename => self.check_rename_item(id).await,
FileSystemContract::Delete => self.check_delete_item(id).await,
FileSystemContract::Representations => self.check_representation_item(id).await,
FileSystemContract::CreateDirectory => self.check_create_directory_item(id).await,
FileSystemContract::Properties => self.check_properties_item(id).await,
FileSystemContract::Stat => self.check_stat_item(id).await,
FileSystemContract::Read => self.check_read_item(id).await,
FileSystemContract::Write => self.check_write_item(id).await,
FileSystemContract::ErrorContext => self.check_error_context().await,
}
}
async fn dispatch_contract(&mut self, contract: FileSystemContract) -> Result<(), crate::ContractFailure> {
match contract {
FileSystemContract::Properties => self.check_properties().await?,
FileSystemContract::Stat => self.check_stat().await?,
FileSystemContract::Read => self.check_read().await?,
FileSystemContract::Write => {
self.check_write().await?;
}
FileSystemContract::List => self.check_list().await?,
FileSystemContract::CreateDirectory => self.check_create_directory().await?,
FileSystemContract::Representations => self.check_representations().await?,
FileSystemContract::Delete => {
self.check_delete().await?;
self.check_delete_tree().await?;
}
FileSystemContract::Copy => {
self.check_copy().await?;
}
FileSystemContract::Rename => {
self.check_rename().await?;
}
FileSystemContract::TempResources => self.check_temp_resources().await?,
FileSystemContract::ErrorContext => self.check_error_context().await?,
}
Ok(())
}
}