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: test b3sum::test_nonames for real #4027

Merged
merged 2 commits into from
Oct 22, 2022

Conversation

huijeong-kim
Copy link
Contributor

Resolves #3818

Signed-off-by: Huijeong Kim [email protected]

@@ -297,7 +297,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
};
let check = matches.contains_id("check");
let tag = matches.contains_id("tag");
let nonames = if binary_name == "b3sum" {
let nonames = if binary_name == "b3sum" || matches.try_contains_id("b3sum").is_ok() {
Copy link
Member

Choose a reason for hiding this comment

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

I think checking whether it is Ok is not enough. try_contains_id returns Ok when the id is valid (i.e. there is an Arg in Command with that id). We also need it to be present, so Ok(true), otherwise nonames can be true when an algorithm other than b3sum is used (I put a dbg!(nonames) on line 305):

❯ cargo run -- hashsum --b2sum --no-names
[src/uu/hashsum/src/hashsum.rs:305] nonames = true

Note that we can assume that b3sum is a valid id, so matches.contains_id("b3sum) should be good enough.

Copy link
Contributor Author

@huijeong-kim huijeong-kim Oct 11, 2022

Choose a reason for hiding this comment

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

Oh, thanks! I didn't tested that part.

I used try_contains_id because it panics in test_hashsum::test_check_sha1 due to

thread 'test_hashsum::test_check_sha1' panicked at 'Command was expected to succeed.
stdout =
 stderr = thread 'main' panicked at 'Mismatch between definition and access of `b3sum`. Unknown argument or group id.  Make sure you are using the argument id and not the short or long flags

I thought about using try_contains_id("b3sum").unwrap_or(false), but when I run cargo run -- hashsum --b2sum --no-names nonames is false and it's unexpected.

If nonames should be set for all other algorithms according to the no-names argument, how about this?

let nonames = matches.try_contains_id("no-names").

Or could you suggest any other options that I can take?

Copy link
Member

@tertsdiepraam tertsdiepraam Oct 12, 2022

Choose a reason for hiding this comment

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

Oh I see! No names should be false for all other algorithms, so try_contains_id is correct then but we need matches.try_contains_id("b3sum") == Some(true).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay! Thanks! I'll work on it 😃👍🏻

@uutils uutils deleted a comment from github-actions bot Oct 12, 2022
@@ -297,7 +297,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
};
let check = matches.contains_id("check");
let tag = matches.contains_id("tag");
let nonames = if binary_name == "b3sum" || matches.try_contains_id("b3sum").is_ok() {
let nonames = if binary_name == "b3sum" || matches.try_contains_id("b3sum").unwrap_or(false) {
Copy link
Contributor Author

@huijeong-kim huijeong-kim Oct 12, 2022

Choose a reason for hiding this comment

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

I used unwrap_or(false) as the return type of try_contains_id is Result<bool, MatchesError>

@tertsdiepraam
Copy link
Member

tertsdiepraam commented Oct 15, 2022

Okay, so a funny thing happened because we merged clap 4. Now, --b3sum is marked with ArgAction::SetTrue and clap has the (questionable) behaviour contains_id is always true for a flag with SetTrue. So this happens again:

❯ cargo run -- hashsum --b2sum --no-names
[src/uu/hashsum/src/hashsum.rs:305] nonames = true

But, that might not be a problem, because we don't actually need nonames to be false for the other algorithms, we just don't care (although we might want to show an error that it's useless with other algorithms). What we actually about is that we can only check the flag if --no-names is registered as an argument (otherwise clap panics). Your solution does this correctly, however, it is not quite obvious what it actually checks. Therefore, I propose the following:

let nonames = *matches.get_one("no-names").unwrap_or(&false);

Sorry for introducing the weird behaviour with clap and changing my mind about this :)

@huijeong-kim
Copy link
Contributor Author

Oh.. that's weird. No problem, I will change as you suggested!

} else {
false
};
let nonames = *matches.try_get_one("no-names").unwrap_or(Some(&false)).unwrap();
Copy link
Contributor Author

@huijeong-kim huijeong-kim Oct 16, 2022

Choose a reason for hiding this comment

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

While I change my code, I found another weird behavior of clap.
get_one("no-names") panics inside the function call if the flag not exist (in test_check_sha1 test), so that it can't handle unwrap_or().

So I used try_get_one() and it returns Result<Option<T>, MatchesError>. That's why I have two unwraps here..

Copy link
Member

Choose a reason for hiding this comment

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

Oh yeah it should have been try_get_one! I made a mistake indeed :)

@sylvestre
Copy link
Contributor

@tertsdiepraam are we good here? thanks

@tertsdiepraam tertsdiepraam merged commit 02f6fa7 into uutils:main Oct 22, 2022
@tertsdiepraam
Copy link
Member

Yeah, looks good! Thanks @huijeong-kim!

@huijeong-kim
Copy link
Contributor Author

Thank you for your kind review @tertsdiepraam 😄👍

@sylvestre
Copy link
Contributor

Terts is the best! ;)

@tertsdiepraam
Copy link
Member

That's very kind 😄 Some GNU tests seem to be failing when after merged this: https://github.com/uutils/coreutils/actions/runs/3302752256. I can't reproduce the failure locally though. I'll check whether the issue persists in other runs.

@huijeong-kim
Copy link
Contributor Author

huijeong-kim commented Oct 22, 2022

Oh it fails 🥲 I'll try to find out something from logs..

How can I run the test locally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test test_hashsum::b3sum::test_nonames for real
3 participants