use std::io::Cursor;
use zesven::ArchivePath;
use zesven::codec::CodecMethod;
use zesven::write::{WriteOptions, Writer};
const METHODS: &[CodecMethod] = &[
CodecMethod::Copy,
CodecMethod::Lzma,
CodecMethod::Lzma2,
CodecMethod::Deflate,
CodecMethod::BZip2,
CodecMethod::PPMd,
CodecMethod::Lz4,
CodecMethod::Zstd,
CodecMethod::Brotli,
];
fn can_write_with(method: CodecMethod) -> Result<(), String> {
let options = WriteOptions::new().method(method);
let mut writer = Writer::create(Cursor::new(Vec::new()))
.map_err(|e| e.to_string())?
.options(options);
writer
.add_bytes(
ArchivePath::new("payload.bin").map_err(|e| e.to_string())?,
&b"something worth compressing, at least a little\n".repeat(64),
)
.map_err(|e| e.to_string())?;
let (result, _sink) = writer.finish_into_inner().map_err(|e| e.to_string())?;
if result.entries_written != 1 {
return Err(format!(
"wrote {} entries, expected 1",
result.entries_written
));
}
Ok(())
}
#[test]
fn test_availability_matches_what_the_build_can_write() {
for &method in METHODS {
let claimed = method.is_available();
let actual = can_write_with(method);
match (claimed, &actual) {
(true, Err(e)) => panic!(
"{method:?} reports itself available and writing failed: {e}\n\
the writer accepts entries on the strength of that claim",
),
(false, Ok(())) => panic!(
"{method:?} reports itself unavailable and wrote an archive; \
callers are being refused a codec this build has",
),
_ => {}
}
}
}
#[test]
fn test_an_unavailable_method_is_refused_before_the_first_entry() {
for &method in METHODS {
if method.is_available() {
continue;
}
let mut writer = Writer::create(Cursor::new(Vec::new()))
.unwrap()
.options(WriteOptions::new().method(method));
let refused = writer.add_bytes(ArchivePath::new("a.bin").unwrap(), b"DATA");
assert!(
refused.is_err(),
"{method:?} is not in this build and the entry was accepted anyway",
);
}
}