-
Notifications
You must be signed in to change notification settings - Fork 61
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
Simplify test code by using Results instead of unwraps #39
Conversation
Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
Oh, I'm not sure it's a good idea - maybe if they've changed something in how Rust is implemented, but if you use |
Indeed that seems to be the case. Running it like that gives the following info:
But when passing
Filtering our stackframes (that is frames with sources in
This points to this line and just for tests I've inserted the following code into here: if true {
return Err(Error::NotSupported);
} Unfortunately the line it points to is just the name of the test not the exact line when the error was returned - not really useful. So, thanks for your time! I'm closing this PR. |
It would be so much nicer if errors somehow got some diagnostic information attached so you could print the backtrace for where it actually originated, that would be useful for debugging "production" code as well. 😞 |
Err... having 15 years of professional Java experience I can say that this is definitely a good point! :) Maybe this is just toothing problems for Rust as in nightly there are some interesting APIs like https://doc.rust-lang.org/std/backtrace/index.html I just hacked together a small proof-of-concept and it seems it's possible to "have a cake and eat it too" with a bit of boilerplate code: #[derive(Debug)]
struct WError; // this error struct is used only in function signatures as a result type
impl From<cryptoki::Error> for WError {
fn from(p: cryptoki::Error) -> Self {
panic!("WError: {:#?}", p);
}
}
#[test]
#[serial]
fn encrypt_decrypt() -> std::result::Result<(), WError> { // <-- WError used here, the rest of the code remains unchanged And since each
Well, that's it, an interesting behavior 🤷 See you later! 👋 |
Nice! Could do something even more generic and make that type in |
Indeed. This looks simple enough. Naming suggestions welcome as I'm really bad at that ;) |
1662920
to
1147c15
Compare
This error type is used only for tests and makes stacktraces generated by the Rust test runner point to the exact place when an error was encountered. Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
I'm not particularly good at it either, nor too fussy, so we can just leave as is, we don't even need to use the type anywhere. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, good idea!
This is a follow-up to an old PR parallaxsecond#39 and uses packaged-as-a-library `TestResult` type instead of an ad-hoc struct. The library provides better diagnostics due to small tweaks on how the error is captured but is otherwise identical with what the code does currently. Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
As it says on the tin. I thought it would be a little bit more readable if we had
?
s instead ofunwrap
s in test code too. :)