option level tests

This commit is contained in:
xgdgsc 2023-03-18 12:23:59 +08:00
parent b8b9c5042f
commit e92b9ff723
3 changed files with 17 additions and 11 deletions

View File

@ -60,7 +60,7 @@ pub enum Subcommand {
#[arg(required = true, value_hint = ValueHint::FilePath)]
output: PathBuf,
/// Compression raw level as each algo has
/// Compression level, applied to all formats
#[arg(short, long)]
level: Option<i16>,
},

View File

@ -57,7 +57,7 @@ pub fn compress_files(
Lz4 => Box::new(lzzzz::lz4f::WriteCompressor::new(
encoder,
lzzzz::lz4f::PreferencesBuilder::new()
.compression_level(level.map_or(0, |l| (l as i32).clamp(1, lzzzz::lz4f::CLEVEL_MAX)))
.compression_level(level.map_or(1, |l| (l as i32).clamp(1, lzzzz::lz4f::CLEVEL_MAX)))
.build(),
)?),
Lzma => Box::new(xz2::write::XzEncoder::new(
@ -71,16 +71,18 @@ pub fn compress_files(
))
.from_writer(encoder),
),
Zstd => Box::new(
zstd::stream::write::Encoder::new(
Zstd => {
let zstd_encoder = zstd::stream::write::Encoder::new(
encoder,
level.map_or(0, |l| {
level.map_or(zstd::DEFAULT_COMPRESSION_LEVEL, |l| {
(l as i32).clamp(zstd::zstd_safe::min_c_level(), zstd::zstd_safe::max_c_level())
}),
)
.unwrap()
.auto_finish(),
),
);
// Safety:
// Encoder::new() can only fail if `level` is invalid, but Default::default()
// is guaranteed to be valid
Box::new(zstd_encoder.unwrap().auto_finish())
}
Tar | Zip => unreachable!(),
};
Ok(encoder)

View File

@ -103,7 +103,7 @@ fn single_empty_file(ext: Extension, #[any(size_range(0..8).lift())] exts: Vec<F
fn single_file(
ext: Extension,
#[any(size_range(0..8).lift())] exts: Vec<FileExtension>,
#[strategy(0i16..30)] level: i16,
#[strategy(proptest::option::of(0i16..30))] level: Option<i16>,
) {
let dir = tempdir().unwrap();
let dir = dir.path();
@ -113,7 +113,11 @@ fn single_file(
let archive = &dir.join(format!("file.{}", merge_extensions(ext, exts)));
let after = &dir.join("after");
fs::write(before_file, []).unwrap();
ouch!("-A", "c", "-l", level.to_string(), before_file, archive);
if let Some(level) = level {
ouch!("-A", "c", "-l", level.to_string(), before_file, archive);
} else {
ouch!("-A", "c", before_file, archive);
}
ouch!("-A", "d", archive, "-d", after);
assert_same_directory(before, after, false);
}