1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

cksum: added tests for --raw and fixed fmt whitespace error

This commit is contained in:
D9nni 2024-01-08 08:51:06 +02:00
parent 8c1fc8b287
commit f03ef79bc8
2 changed files with 30 additions and 9 deletions

View file

@ -6,15 +6,15 @@
// spell-checker:ignore (ToDO) fname, algo // spell-checker:ignore (ToDO) fname, algo
use clap::{crate_version, value_parser, Arg, ArgAction, Command}; use clap::{crate_version, value_parser, Arg, ArgAction, Command};
use hex::encode; use hex::encode;
use std::error::Error;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fmt::Display;
use std::fs::File; use std::fs::File;
use std::io::{self, stdin, stdout, BufReader, Read, Write}; use std::io::{self, stdin, stdout, BufReader, Read, Write};
use std::iter; use std::iter;
use std::path::Path; use std::path::Path;
use std::error::Error;
use std::fmt::Display;
use uucore::{ use uucore::{
error::{FromIo, UResult, UError}, error::{FromIo, UError, UResult},
format_usage, help_about, help_section, help_usage, format_usage, help_about, help_section, help_usage,
sum::{ sum::{
div_ceil, Blake2b, Digest, DigestWriter, Md5, Sha1, Sha224, Sha256, Sha384, Sha512, Sm3, div_ceil, Blake2b, Digest, DigestWriter, Md5, Sha1, Sha224, Sha256, Sha384, Sha512, Sm3,
@ -51,8 +51,7 @@ impl UError for CkSumError {
} }
} }
impl Error for CkSumError { impl Error for CkSumError {}
}
impl Display for CkSumError { impl Display for CkSumError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -152,7 +151,7 @@ fn cksum<'a, I>(mut options: Options, files: I) -> UResult<()>
where where
I: Iterator<Item = &'a OsStr>, I: Iterator<Item = &'a OsStr>,
{ {
let files_vec:Vec<_> = files.collect(); let files_vec: Vec<_> = files.collect();
if options.raw && files_vec.len() > 1 { if options.raw && files_vec.len() > 1 {
return Err(Box::new(CkSumError::RawMultipleFiles)); return Err(Box::new(CkSumError::RawMultipleFiles));
} }
@ -174,7 +173,7 @@ where
}); });
let (sum, sz) = digest_read(&mut options.digest, &mut file, options.output_bits) let (sum, sz) = digest_read(&mut options.digest, &mut file, options.output_bits)
.map_err_context(|| "failed to read input".to_string())?; .map_err_context(|| "failed to read input".to_string())?;
if options.raw { if options.raw {
let bytes_str = sum.parse::<u32>().unwrap().to_be_bytes(); let bytes_str = sum.parse::<u32>().unwrap().to_be_bytes();
stdout().write_all(&bytes_str)?; stdout().write_all(&bytes_str)?;
@ -331,7 +330,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
output_bits: bits, output_bits: bits,
length, length,
untagged: matches.get_flag(options::UNTAGGED), untagged: matches.get_flag(options::UNTAGGED),
raw: matches.get_flag(options::RAW), raw: matches.get_flag(options::RAW),
}; };
match matches.get_many::<String>(options::FILE) { match matches.get_many::<String>(options::FILE) {
@ -387,7 +386,7 @@ pub fn uu_app() -> Command {
.short('l') .short('l')
.help("digest length in bits; must not exceed the max for the blake2 algorithm and must be a multiple of 8") .help("digest length in bits; must not exceed the max for the blake2 algorithm and must be a multiple of 8")
.action(ArgAction::Set), .action(ArgAction::Set),
) )
.arg( .arg(
Arg::new(options::RAW) Arg::new(options::RAW)
.long(options::RAW) .long(options::RAW)

View file

@ -286,3 +286,25 @@ fn test_length_is_zero() {
.no_stderr() .no_stderr()
.stdout_is_fixture("length_is_zero.expected"); .stdout_is_fixture("length_is_zero.expected");
} }
#[test]
fn test_raw_single_file() {
new_ucmd!()
.arg("--raw")
.arg("lorem_ipsum.txt")
.succeeds()
.no_stderr()
.stdout_is_fixture("raw_single_file.expected");
}
#[test]
fn test_raw_multiple_files() {
new_ucmd!()
.arg("--raw")
.arg("lorem_ipsum.txt")
.arg("alice_in_wonderland.txt")
.fails()
.no_stdout()
.stderr_contains("cksum: the --raw option is not supported with multiple files")
.code_is(1);
}