Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hashsum: add --no-names option from official b3sum tool #3361

Merged
merged 3 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/uu/hashsum/src/hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code.

// spell-checker:ignore (ToDO) algo, algoname, regexes, nread
// spell-checker:ignore (ToDO) algo, algoname, regexes, nread, nonames

#[macro_use]
extern crate clap;
Expand Down Expand Up @@ -46,6 +46,7 @@ struct Options {
binary: bool,
check: bool,
tag: bool,
nonames: bool,
status: bool,
quiet: bool,
strict: bool,
Expand Down Expand Up @@ -316,6 +317,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
};
let check = matches.is_present("check");
let tag = matches.is_present("tag");
let nonames = matches.is_present("no-names");
let status = matches.is_present("status");
let quiet = matches.is_present("quiet") || status;
let strict = matches.is_present("strict");
Expand All @@ -328,6 +330,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
binary,
check,
tag,
nonames,
status,
quiet,
strict,
Expand Down Expand Up @@ -370,6 +373,11 @@ pub fn uu_app_common<'a>() -> Command<'a> {
.long("tag")
.help("create a BSD-style checksum"),
)
.arg(
Arg::new("no-names")
.long("no-names")
.help("Omits filenames in the output (option not present in GNU/Coreutils)"),
)
.arg(
Arg::new("text")
.short('t')
Expand Down Expand Up @@ -602,6 +610,8 @@ where
.map_err_context(|| "failed to read input".to_string())?;
if options.tag {
println!("{} ({}) = {}", options.algoname, filename.display(), sum);
} else if options.nonames {
println!("{}", sum);
} else {
println!("{} {}{}", sum, binary_marker, filename.display());
}
Expand Down
12 changes: 11 additions & 1 deletion tests/by-util/test_hashsum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// spell-checker:ignore checkfile
// spell-checker:ignore checkfile, nonames
macro_rules! get_hash(
($str:expr) => (
$str.split(' ').collect::<Vec<&str>>()[0]
Expand Down Expand Up @@ -29,6 +29,16 @@ macro_rules! test_digest {
get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).pipe_in_fixture("input.txt").succeeds().no_stderr().stdout_str()));
}

#[test]
fn test_nonames() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test with different files at the same time ?

ex
hashsum --no-names input1.txt input2.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, how about covering the special - at the same time, i.e., hashsum --no-names input.txt - with input.txt also piped through stdin? I've pushed this now.

let ts = TestScenario::new("hashsum");
// EXPECTED_FILE has no newline character at the end
assert_eq!(format!("{0}\n{0}\n", ts.fixtures.read(EXPECTED_FILE)),
ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("--no-names").arg("input.txt").arg("-").pipe_in_fixture("input.txt")
.succeeds().no_stderr().stdout_str()
);
}

#[test]
fn test_check() {
let ts = TestScenario::new("hashsum");
Expand Down