mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
✅ add test, move
This commit is contained in:
parent
df5f846581
commit
ffa16c7d6e
@ -44,10 +44,6 @@ pub struct CliArgs {
|
|||||||
/// Ouch and claps subcommands
|
/// Ouch and claps subcommands
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
pub cmd: Subcommand,
|
pub cmd: Subcommand,
|
||||||
|
|
||||||
/// Compression raw level as each algo has
|
|
||||||
#[arg(short = 'l', long)]
|
|
||||||
pub level: Option<i16>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Parser, PartialEq, Eq, Debug)]
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
||||||
@ -63,6 +59,10 @@ pub enum Subcommand {
|
|||||||
/// The resulting file. Its extensions can be used to specify the compression formats
|
/// The resulting file. Its extensions can be used to specify the compression formats
|
||||||
#[arg(required = true, value_hint = ValueHint::FilePath)]
|
#[arg(required = true, value_hint = ValueHint::FilePath)]
|
||||||
output: PathBuf,
|
output: PathBuf,
|
||||||
|
|
||||||
|
/// Compression raw level as each algo has
|
||||||
|
#[arg(short, long)]
|
||||||
|
level: Option<i16>,
|
||||||
},
|
},
|
||||||
/// Decompresses one or more files, optionally into another folder
|
/// Decompresses one or more files, optionally into another folder
|
||||||
#[command(visible_alias = "d")]
|
#[command(visible_alias = "d")]
|
||||||
|
@ -45,40 +45,41 @@ pub fn compress_files(
|
|||||||
// by default, ParCompress uses a default compression level of 3
|
// by default, ParCompress uses a default compression level of 3
|
||||||
// instead of the regular default that flate2 uses
|
// instead of the regular default that flate2 uses
|
||||||
gzp::par::compress::ParCompress::<gzp::deflate::Gzip>::builder()
|
gzp::par::compress::ParCompress::<gzp::deflate::Gzip>::builder()
|
||||||
.compression_level(level.map_or_else(|| Default::default(), |l| gzp::Compression::new(l as u32)))
|
.compression_level(level.map_or_else(Default::default, |l| gzp::Compression::new(l as u32)))
|
||||||
.from_writer(encoder),
|
.from_writer(encoder),
|
||||||
),
|
),
|
||||||
Bzip => Box::new(bzip2::write::BzEncoder::new(
|
Bzip => Box::new(bzip2::write::BzEncoder::new(
|
||||||
encoder,
|
encoder,
|
||||||
level.map_or_else(|| Default::default(), |l| bzip2::Compression::new(l as u32)),
|
level.map_or_else(Default::default, |l| {
|
||||||
|
if l < 1 || l > 9 {
|
||||||
|
bzip2::Compression::new(1)
|
||||||
|
} else {
|
||||||
|
bzip2::Compression::new(l as u32)
|
||||||
|
}
|
||||||
|
}),
|
||||||
)),
|
)),
|
||||||
Lz4 => Box::new(lzzzz::lz4f::WriteCompressor::new(
|
Lz4 => Box::new(lzzzz::lz4f::WriteCompressor::new(
|
||||||
encoder,
|
encoder,
|
||||||
lzzzz::lz4f::PreferencesBuilder::new()
|
lzzzz::lz4f::PreferencesBuilder::new()
|
||||||
.compression_level(level.map_or_else(|| Default::default(), |l| l as i32))
|
.compression_level(level.map_or(0, |l| l as i32))
|
||||||
.build(),
|
.build(),
|
||||||
)?),
|
)?),
|
||||||
Lzma => Box::new(xz2::write::XzEncoder::new(
|
Lzma => Box::new(xz2::write::XzEncoder::new(
|
||||||
encoder,
|
encoder,
|
||||||
level.map_or_else(|| Default::default(), |l| l as u32),
|
level.map_or(6, |l| if l < 0 || l > 9 { 6 } else { l as u32 }),
|
||||||
)),
|
)),
|
||||||
Snappy => Box::new(
|
Snappy => Box::new(
|
||||||
gzp::par::compress::ParCompress::<gzp::snap::Snap>::builder()
|
gzp::par::compress::ParCompress::<gzp::snap::Snap>::builder()
|
||||||
.compression_level(gzp::par::compress::Compression::new(
|
.compression_level(gzp::par::compress::Compression::new(
|
||||||
level.map_or_else(|| Default::default(), |l| l as u32),
|
level.map_or_else(Default::default, |l| l as u32),
|
||||||
))
|
))
|
||||||
.from_writer(encoder),
|
.from_writer(encoder),
|
||||||
),
|
),
|
||||||
Zstd => {
|
Zstd => Box::new(
|
||||||
Box::new(
|
zstd::stream::write::Encoder::new(encoder, level.map_or(0, |l| l as i32))
|
||||||
zstd::stream::write::Encoder::new(encoder, level.map_or_else(|| Default::default(), |l| l as i32))
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.auto_finish(),
|
.auto_finish(),
|
||||||
)
|
),
|
||||||
// Safety:
|
|
||||||
// Encoder::new() can only fail if `level` is invalid, but Default::default()
|
|
||||||
// is guaranteed to be valid
|
|
||||||
}
|
|
||||||
Tar | Zip => unreachable!(),
|
Tar | Zip => unreachable!(),
|
||||||
};
|
};
|
||||||
Ok(encoder)
|
Ok(encoder)
|
||||||
|
@ -44,6 +44,7 @@ pub fn run(
|
|||||||
Subcommand::Compress {
|
Subcommand::Compress {
|
||||||
files,
|
files,
|
||||||
output: output_path,
|
output: output_path,
|
||||||
|
level,
|
||||||
} => {
|
} => {
|
||||||
// After cleaning, if there are no input files left, exit
|
// After cleaning, if there are no input files left, exit
|
||||||
if files.is_empty() {
|
if files.is_empty() {
|
||||||
@ -80,7 +81,7 @@ pub fn run(
|
|||||||
args.quiet,
|
args.quiet,
|
||||||
question_policy,
|
question_policy,
|
||||||
file_visibility_policy,
|
file_visibility_policy,
|
||||||
args.level,
|
level,
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Ok(true) = compress_result {
|
if let Ok(true) = compress_result {
|
||||||
|
@ -133,3 +133,23 @@ fn multiple_files(
|
|||||||
ouch!("-A", "d", archive, "-d", after);
|
ouch!("-A", "d", archive, "-d", after);
|
||||||
assert_same_directory(before, after, !matches!(ext, DirectoryExtension::Zip));
|
assert_same_directory(before, after, !matches!(ext, DirectoryExtension::Zip));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// compress and decompress a single file with comp levels
|
||||||
|
#[proptest(cases = 512)]
|
||||||
|
fn single_file_level(
|
||||||
|
ext: Extension,
|
||||||
|
#[any(size_range(0..8).lift())] exts: Vec<FileExtension>,
|
||||||
|
#[strategy(-3i16..30)] level: i16,
|
||||||
|
) {
|
||||||
|
let dir = tempdir().unwrap();
|
||||||
|
let dir = dir.path();
|
||||||
|
let before = &dir.join("before");
|
||||||
|
fs::create_dir(before).unwrap();
|
||||||
|
let before_file = &before.join("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);
|
||||||
|
ouch!("-A", "d", archive, "-d", after);
|
||||||
|
assert_same_directory(before, after, false);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user