tests: replace many .unwrap()s with ?s using eyre#9111
tests: replace many .unwrap()s with ?s using eyre#9111glehmann wants to merge 7 commits intojj-vcs:mainfrom
Conversation
|
Thanks for looking at this! Have you tested what the backtraces look like? My understanding is that colored-eyre's backtraces will look like the version of panic backtraces you get with That's the issue that turned that pr into a bit of a headache. |
|
Another reason I was doubtful about this approach is the need to either "install" colored-eyre in every test or use the ctor crate. ctor's docs make it seem a bit questionable. Also I'm not sure whether this installing affects just the panic handler, or whether it also allows backtraces when you use |
There was a problem hiding this comment.
The following commits do not follow our format for subject lines:
Commits should have a subject line following the format <topic>: <description>. Please review the commit guidelines for more information.
3771490 to
0e3b5ad
Compare
All commits are now correctly formatted. Thank you for your contribution!
|
|
I agree that it's okay to use some linker magic like |
Filter backtrace frames to show only project code (jj_lib and jj_cli), removing noise from dependencies and standard library to make error reports easier to read during testing.
0e3b5ad to
395ea53
Compare
|
I had one new thought: perhaps the backtraces for the If that's correct, and if What color-eyre is good for is for formatting panics that occur in other locations. Whether that's worth using the |
| frame | ||
| .name | ||
| .as_ref() | ||
| .map(|name| name.starts_with("jj_")) |
There was a problem hiding this comment.
It's nice that color-eyre has filtering functionality! That could be very helpful.
I don't know if this filter is correct for all use-cases, though. Ideally, there's be a message letting you know that you can set RUST_BACKTRACE=full to get the full backtrace in the cases where this filters too much. (I haven't checked how easy that is to do).
As per my other message, I'm thinking of panic-s, not TestResult use-cases.
Aside: My PR has an example of a more complicated-looking filter that's similar to default panic handler frame filtering logic, for comparison. I'm not sure that's necessarily better; this stricter filter could be good in 90% of the cases if there was a way to disable it in the other 10%.
There was a problem hiding this comment.
There is an env var to set to disable the filtering, and it's displayed with the backtrace—see the screenshots in #9111 (comment)
The filtering is especially useful with #[tokio::test]: the backtraces are much longer in that case—29 frames instead of just 7 with the #[test] for the same test failure. #[pollster::test] is a bit more at 9 frames.
I like the way the error chains are displayed, but eyre alone already displays them well.
It may not be perfect every time, but it should work in most cases, produce much more compact and readable backtraces, also work with panics, and the filtering is easily deactivable when needed. I like it :)
There was a problem hiding this comment.
Have you tested that it works, that your code doesn't override it?
It's fine if the filtering is not completely perfect, as long as it can be disabled; we can always fine-tune or even change our mind about using color_eyre later. So, I think that as long as COLORBT_SHOW_HIDDEN works, that's sufficient.
…tests So backtraces are shown without requiring the environment variable.
…Result from parent commit This commit should be comparable with jj-vcs#9050 As of this writing, this replaced all but out of 1053 instances of `.block().unwrap()`. The remaining ones (as counted by AI): ``` ┌──────────────────┬───────┬────────────────────────────────────────────────────┐ │ Category │ Count │ Could convert? │ ├──────────────────┼───────┼────────────────────────────────────────────────────┤ │ testutils │ 20 │ No — return concrete types, would cascade to │ │ helpers │ │ hundreds of callers across all test files │ ├──────────────────┼───────┼────────────────────────────────────────────────────┤ │ Closures │ 53 │ No — closures return () or concrete values │ ├──────────────────┼───────┼────────────────────────────────────────────────────┤ │ Test helpers │ 22 │ Technically yes, but adds ~70 ? at call sites to │ │ │ │ save 22 unwraps │ ├──────────────────┼───────┼────────────────────────────────────────────────────┤ │ Non-convertible │ 4 │ No — CommandError (2), Pin<Box<dyn Future>> (1), │ │ │ │ closure in test_matrix (1) │ └──────────────────┴───────┴────────────────────────────────────────────────────┘ ```
Mostly done with OpenCode and Claude Sonnet 4.6 with the following prompt, and reviewed manually: * Replace the Result.unwrap with the ? operator in test functions * Add TestResult as return type in the test functions where you replace unwrap by the ? operator, and Ok(()) at the end. * Do the replacement only in method with the `#[test]` decorator. You can't exclude files that are not in tests dirs, because some files have inline tests. * Don't replace unwraps in closures. * Don't replace Option.unwrap. * Be careful to not change the insta snapshot references. * You can identify all the Result.unwrap locations with this command: cargo clippy --tests --message-format=short -- -W clippy::unwrap_used 2>&1 | grep / | grep Result | grep <filename> * Run the tests each time you're done with a file. * Begin with the file with the most Result.unwrap in them as shown by this command: cargo clippy --tests --message-format=short -- -W clippy::unwrap_used 2>&1 | grep Result | cut -d: -f1 | sort | uniq -c | sort -nr Before this PR: ❯ cargo clippy --tests --message-format=short -- -W clippy::unwrap_used 2>&1 | grep Result | wc -l 3973 After this PR: ❯ cargo clippy --tests --message-format=short -- -W clippy::unwrap_used 2>&1 | grep Result | wc -l 1174
395ea53 to
3b5a9a7
Compare
| #[ctor::ctor] | ||
| fn init_color_eyre() { | ||
| // Enable backtrace capture by default in tests | ||
| if std::env::var("RUST_BACKTRACE").is_err() { |
There was a problem hiding this comment.
(Probably no action needed) I have two worries about this:
- Can
ctorsupport this on all systems? AFAIU, it's only supposed to use libc. OTOH, if this passes CI, it probably works. - I'm not 100% sure we want this. But it's probably better? I'm up for trying it and seeing if it makes anyone unhappy.
|
I'm pretty happy with this. I'm not approving yet because
One minor concern I have is #9111 (comment), but unless somebody has thoughts, I don't think changes are needed. If either of my concerns are realized, I think it will be neither subtle nor confusing. |
I think you have more experience with linkers than I, @yuja, so thanks for looking. Perhaps you're also the person who can tell whether #9111 (comment) is worrying or fine. |




Checklist
If applicable:
CHANGELOG.mdREADME.md,docs/,demos/)cli/src/config-schema.json)how it works, how it's organized), including any code drafted by an LLM.
an eye towards deleting anything that is irrelevant, clarifying anything
that is confusing, and adding details that are relevant. This includes,
for example, commit descriptions, PR descriptions, and code comments.