cksum: implement --untagged

This commit is contained in:
Daniel Hofstetter
2023-05-14 16:10:46 +02:00
parent 24e0bef781
commit 690fff2fc9
35 changed files with 135 additions and 9 deletions
+26 -9
View File
@@ -6,7 +6,7 @@
// file that was distributed with this source code.
// spell-checker:ignore (ToDO) fname, algo
use clap::{crate_version, Arg, Command};
use clap::{crate_version, Arg, ArgAction, Command};
use hex::encode;
use std::ffi::OsStr;
use std::fs::File;
@@ -103,6 +103,7 @@ struct Options {
algo_name: &'static str,
digest: Box<dyn Digest + 'static>,
output_bits: usize,
untagged: bool,
}
/// Calculate checksum
@@ -161,12 +162,20 @@ where
),
(ALGORITHM_OPTIONS_CRC, true) => println!("{sum} {sz}"),
(ALGORITHM_OPTIONS_CRC, false) => println!("{sum} {sz} {}", filename.display()),
(ALGORITHM_OPTIONS_BLAKE2B, _) => println!("BLAKE2b ({}) = {sum}", filename.display()),
_ => println!(
"{} ({}) = {sum}",
options.algo_name.to_ascii_uppercase(),
filename.display()
),
(ALGORITHM_OPTIONS_BLAKE2B, _) if !options.untagged => {
println!("BLAKE2b ({}) = {sum}", filename.display());
}
_ => {
if options.untagged {
println!("{sum} {}", filename.display());
} else {
println!(
"{} ({}) = {sum}",
options.algo_name.to_ascii_uppercase(),
filename.display()
);
}
}
}
}
@@ -208,8 +217,9 @@ fn digest_read<T: Read>(
}
mod options {
pub static FILE: &str = "file";
pub static ALGORITHM: &str = "algorithm";
pub const ALGORITHM: &str = "algorithm";
pub const FILE: &str = "file";
pub const UNTAGGED: &str = "untagged";
}
#[uucore::main]
@@ -228,6 +238,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
algo_name: name,
digest: algo,
output_bits: bits,
untagged: matches.get_flag(options::UNTAGGED),
};
match matches.get_many::<String>(options::FILE) {
@@ -270,5 +281,11 @@ pub fn uu_app() -> Command {
ALGORITHM_OPTIONS_SM3,
]),
)
.arg(
Arg::new(options::UNTAGGED)
.long(options::UNTAGGED)
.help("create a reversed style checksum, without digest type")
.action(ArgAction::SetTrue),
)
.after_help(AFTER_HELP)
}
+65
View File
@@ -159,3 +159,68 @@ fn test_algorithm_stdin() {
}
}
}
#[test]
fn test_untagged_single_file() {
new_ucmd!()
.arg("--untagged")
.arg("lorem_ipsum.txt")
.succeeds()
.stdout_is_fixture("untagged/crc_single_file.expected");
}
#[test]
fn test_untagged_multiple_files() {
new_ucmd!()
.arg("--untagged")
.arg("lorem_ipsum.txt")
.arg("alice_in_wonderland.txt")
.succeeds()
.stdout_is_fixture("untagged/crc_multiple_files.expected");
}
#[test]
fn test_untagged_stdin() {
new_ucmd!()
.arg("--untagged")
.pipe_in_fixture("lorem_ipsum.txt")
.succeeds()
.stdout_is_fixture("untagged/crc_stdin.expected");
}
#[test]
fn test_untagged_algorithm_single_file() {
for algo in ALGOS {
new_ucmd!()
.arg("--untagged")
.arg(format!("--algorithm={algo}"))
.arg("lorem_ipsum.txt")
.succeeds()
.stdout_is_fixture(format!("untagged/{algo}_single_file.expected"));
}
}
#[test]
fn test_untagged_algorithm_multiple_files() {
for algo in ALGOS {
new_ucmd!()
.arg("--untagged")
.arg(format!("--algorithm={algo}"))
.arg("lorem_ipsum.txt")
.arg("alice_in_wonderland.txt")
.succeeds()
.stdout_is_fixture(format!("untagged/{algo}_multiple_files.expected"));
}
}
#[test]
fn test_untagged_algorithm_stdin() {
for algo in ALGOS {
new_ucmd!()
.arg("--untagged")
.arg(format!("--algorithm={algo}"))
.pipe_in_fixture("lorem_ipsum.txt")
.succeeds()
.stdout_is_fixture(format!("untagged/{algo}_stdin.expected"));
}
}
@@ -0,0 +1,2 @@
0e97a09189e560c3789c0bff1f020166861ef857d1fbfe4574de1842e3c06cabb9575e4af6309a166158c2b408d3c038c1b49d828b35158142cdc0396d1195c3 lorem_ipsum.txt
91b8b0f0868e905ad18b8ac35e4a1dacd289857b19258ab5d1e071761af758b0134ec152d4f011fe1825ca889c80c2e072ca70eb50548c25fc49a98937515af4 alice_in_wonderland.txt
@@ -0,0 +1 @@
0e97a09189e560c3789c0bff1f020166861ef857d1fbfe4574de1842e3c06cabb9575e4af6309a166158c2b408d3c038c1b49d828b35158142cdc0396d1195c3 lorem_ipsum.txt
+1
View File
@@ -0,0 +1 @@
0e97a09189e560c3789c0bff1f020166861ef857d1fbfe4574de1842e3c06cabb9575e4af6309a166158c2b408d3c038c1b49d828b35158142cdc0396d1195c3 -
@@ -0,0 +1,2 @@
08109 1 lorem_ipsum.txt
01814 1 alice_in_wonderland.txt
@@ -0,0 +1 @@
08109 1 lorem_ipsum.txt
+1
View File
@@ -0,0 +1 @@
08109 1
@@ -0,0 +1,2 @@
378294376 772 lorem_ipsum.txt
3805907707 302 alice_in_wonderland.txt
@@ -0,0 +1 @@
378294376 772 lorem_ipsum.txt
+1
View File
@@ -0,0 +1 @@
378294376 772
@@ -0,0 +1,2 @@
cd724690f7dc61775dfac400a71f2caa lorem_ipsum.txt
f6fa7033e16166a9589aa1c0388ffd58 alice_in_wonderland.txt
@@ -0,0 +1 @@
cd724690f7dc61775dfac400a71f2caa lorem_ipsum.txt
+1
View File
@@ -0,0 +1 @@
cd724690f7dc61775dfac400a71f2caa -
@@ -0,0 +1,2 @@
ab1dd0bae1d8883a3d18a66de6afbd28252cfbef lorem_ipsum.txt
22b54b2520e8b4fa59eb10719028a4e587c12d1e alice_in_wonderland.txt
@@ -0,0 +1 @@
ab1dd0bae1d8883a3d18a66de6afbd28252cfbef lorem_ipsum.txt
+1
View File
@@ -0,0 +1 @@
ab1dd0bae1d8883a3d18a66de6afbd28252cfbef -
@@ -0,0 +1,2 @@
3de66fbcad106e1b40ab391be56c51d2007eb1f9c655d0f4e29bfc01 lorem_ipsum.txt
54c9c7d78458886418ce0845111fc49fe1c628ffd4bf3da14226ffd9 alice_in_wonderland.txt
@@ -0,0 +1 @@
3de66fbcad106e1b40ab391be56c51d2007eb1f9c655d0f4e29bfc01 lorem_ipsum.txt
+1
View File
@@ -0,0 +1 @@
3de66fbcad106e1b40ab391be56c51d2007eb1f9c655d0f4e29bfc01 -

Some files were not shown because too many files have changed in this diff Show More