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

fix: match date -I error messages with GNU date #4499

Merged
merged 4 commits into from
Mar 16, 2023

Conversation

shanmukhateja
Copy link
Contributor

Fixes #4494

@sylvestre
Copy link
Contributor

panic isn't the right approach to manage a user facing error.

With your change:

$ ./target/debug/coreutils date -I@
thread 'main' panicked at 'date:` invalid argument `@` for '--iso-8601'', src/uu/date/src/date.rs:118:18
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

it should be like GNU:

date: invalid argument '@' for '--iso-8601'
Valid arguments are:
  - 'hours'
  - 'minutes'
  - 'date'
  - 'seconds'
  - 'ns'
Try 'date --help' for more information.

See https:/uutils/coreutils/blob/main/src/uu/date/src/date.rs#L249-L253 as example

Also, it will need a test to make sure we don't regress

@sylvestre
Copy link
Contributor

@shanmukhateja are you still working on it ? :)

@shanmukhateja
Copy link
Contributor Author

@sylvestre Yes I plan on working on it except I'm not sure how to implement SimpleError struct inside FromStr which returns Self.

In the case of panic, it halts the execution but now when I try using the struct, it complaints of trying to return Result.

Thoughts?

@sylvestre
Copy link
Contributor

You should try a bit more :)
and look how it is done elsewere

@tertsdiepraam
Copy link
Member

Have you seen my comment in the issue? You probably don't need to change this function, just make sure that clap gives an error before we even get here.

@shanmukhateja
Copy link
Contributor Author

@sylvestre @tertsdiepraam I used value_parser to declare valid options. Please let me know if this is correct.

Note: unit test is missing.

Success case:

$ cargo run date -Idate
    Finished dev [unoptimized + debuginfo] target(s) in 0.33s
     Running `target/debug/coreutils date -Idate`
2023-03-15
$ cargo run date -Ihour
    Finished dev [unoptimized + debuginfo] target(s) in 0.31s
     Running `target/debug/coreutils date -Ihour`
2023-03-15T21+05:30

Error case:

$ cargo run date -I@
    Finished dev [unoptimized + debuginfo] target(s) in 0.31s
     Running `target/debug/coreutils date '-I@'`
error: '@' isn't a valid value for '--iso-8601 <FMT>'
  [possible values: date, hour, hours, minute, minutes, ns]

For more information try '--help'

@sylvestre
Copy link
Contributor

look great :)
just a missing test

@@ -115,7 +115,7 @@ impl<'a> From<&'a str> for Iso8601Format {
NS => Self::Ns,
DATE => Self::Date,
// Should be caught by clap
_ => panic!("Invalid format: {s}"),
_ => panic!("date:` invalid argument `{s}` for '--iso-8601'"),
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe

Suggested change
_ => panic!("date:` invalid argument `{s}` for '--iso-8601'"),
_ => unreachable!(),

if we won't go there then

Copy link
Contributor

Choose a reason for hiding this comment

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

ping ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, this commit 5e42b58 (#4499) fixes it.

Still missing the unit test. Will ping when I have it working.

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks

@shanmukhateja
Copy link
Contributor Author

@sylvestre unit tests added.

@sylvestre
Copy link
Contributor

please add the tests here:
https:/uutils/coreutils/blob/main/tests/by-util/test_date.rs
and following their behavior :)

@sylvestre sylvestre closed this Mar 16, 2023
@sylvestre sylvestre reopened this Mar 16, 2023
- move unit test to test_date.rs

- include `second(s)` as valid values for iso8601 argument
@shanmukhateja
Copy link
Contributor Author

@sylvestre ..third time's the charm?

@sylvestre sylvestre merged commit bd6905c into uutils:main Mar 16, 2023
@sylvestre
Copy link
Contributor

thanks :)

@shanmukhateja
Copy link
Contributor Author

Thank you for being patient. @sylvestre

@sylvestre
Copy link
Contributor

oh, don't mention it :)

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.

fuzzing on date: date -I @ triggers a panic
3 participants