use qubit_fs::directory::DeleteOptions;
use qubit_fs::error::FsErrorKind;
use qubit_fs::error::FsOperation;
use qubit_fs::metadata::FileSystemCapability;
use crate::ContractCheckId;
use crate::ContractCheckOutcome;
use crate::ContractFailure;
use crate::FileSystemContractSuite;
use crate::FixtureSupport;
use crate::internal::verify_condition;
use crate::internal::verify_fs_error;
impl FileSystemContractSuite<'_> {
pub(super) fn check_delete_tree(&mut self) -> Result<(), ContractFailure> {
let id = ContractCheckId::DeleteTree;
self.context.begin(id.as_str());
let capability = FileSystemCapability::RecursiveDelete;
let relative = self.context.relative_name("recursive-delete-root");
let options = DeleteOptions::default().with_recursive(true);
if !self.capable(capability) {
let root = self.fixture.path(&relative).map_err(|error| {
ContractFailure::with_source("recursive delete path preparation failed", error).at(id)
})?;
let error = match self.fixture.file_system().delete_directory(&root, options) {
Err(error) => error,
Ok(_) => {
return Err(ContractFailure::message_only("unavailable recursive deletion succeeded").at(id));
}
};
verify_fs_error(
error,
FsErrorKind::RequirementNotMet,
FsOperation::Delete,
&root,
self.context.properties().info().provider_id(),
Some(capability),
id,
)?;
self.context
.record_check(id, Some(capability), ContractCheckOutcome::RejectedAsExpected);
return Ok(());
}
let mut paths = Vec::new();
for directory in [relative.clone(), format!("{relative}/nested")] {
let prepared = self.fixture.seed_empty_directory(&directory).map_err(|error| {
ContractFailure::with_source("recursive-delete directory preparation failed", error).at(id)
})?;
let FixtureSupport::Supported(path) = prepared else {
self.context.record_check(
id,
Some(capability),
ContractCheckOutcome::Unverified {
reason: "recursive deletion requires independently prepared directories".to_owned(),
},
);
return Ok(());
};
self.context.record_created(path.clone());
paths.push(path);
}
for file in [format!("{relative}/child"), format!("{relative}/nested/child")] {
let prepared = self.fixture.seed_file(&file, b"child").map_err(|error| {
ContractFailure::with_source("recursive-delete child preparation failed", error).at(id)
})?;
let FixtureSupport::Supported(path) = prepared else {
self.context.record_check(
id,
Some(capability),
ContractCheckOutcome::Unverified {
reason: "recursive deletion requires independently prepared children".to_owned(),
},
);
return Ok(());
};
self.context.record_created(path.clone());
paths.push(path);
}
for path in &paths {
let before = self.fixture.exists_out_of_band(path).map_err(|error| {
ContractFailure::with_source("recursive-delete initial observation failed", error).at(id)
})?;
verify_condition(
matches!(before, FixtureSupport::Supported(true)),
id,
"recursive-delete seed must independently contain every entry",
)?;
}
let outcome = self
.fixture
.file_system()
.delete_directory(&paths[0], options)
.map_err(|error| ContractFailure::with_source("recursive removal failed", error).at(id))?;
verify_condition(
!outcome.already_missing(),
id,
"recursive deletion reported a prepared tree missing",
)?;
for path in &paths {
let after = self.fixture.exists_out_of_band(path).map_err(|error| {
ContractFailure::with_source("recursive-delete final observation failed", error).at(id)
})?;
verify_condition(
matches!(after, FixtureSupport::Supported(false)),
id,
"delete/tree: child remained after removal or root remained after removal",
)?;
}
self.context
.record_check(id, Some(capability), ContractCheckOutcome::Passed);
Ok(())
}
}