Skip to content

Commit d03e204

Browse files
committed
tests: .block_on().unwrap() with .block_on()? in tests using TestResult from parent commit
This commit should be comparable with #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) │ └──────────────────┴───────┴────────────────────────────────────────────────────┘ ```
1 parent 92ef5c8 commit d03e204

38 files changed

Lines changed: 1704 additions & 1793 deletions

cli/src/commands/arrange.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ mod tests {
608608
use pollster::FutureExt as _;
609609
use testutils::CommitBuilderExt as _;
610610
use testutils::TestRepo;
611+
use testutils::TestResult;
611612

612613
use super::*;
613614

@@ -629,15 +630,16 @@ mod tests {
629630
}
630631

631632
#[test]
632-
fn test_update_commit_order_empty() {
633-
let mut state = State::new(vec![], vec![]).block_on().unwrap();
633+
fn test_update_commit_order_empty() -> TestResult {
634+
let mut state = State::new(vec![], vec![]).block_on()?;
634635
assert_eq!(state.head_order, vec![]);
635636
state.update_commit_order();
636637
assert_eq!(state.current_order, vec![]);
638+
Ok(())
637639
}
638640

639641
#[test]
640-
fn test_update_commit_order_reorder() {
642+
fn test_update_commit_order_reorder() -> TestResult {
641643
let test_repo = TestRepo::init();
642644
let store = test_repo.repo.store();
643645
let empty_tree = store.empty_merged_tree();
@@ -668,8 +670,7 @@ mod tests {
668670
],
669671
vec![],
670672
)
671-
.block_on()
672-
.unwrap();
673+
.block_on()?;
673674

674675
// The initial head order is determined by the input order
675676
assert_eq!(
@@ -703,10 +704,11 @@ mod tests {
703704
commit_b.id().clone(),
704705
]
705706
);
707+
Ok(())
706708
}
707709

708710
#[test]
709-
fn test_swap_commits() {
711+
fn test_swap_commits() -> TestResult {
710712
let test_repo = TestRepo::init();
711713
let store = test_repo.repo.store();
712714
let empty_tree = store.empty_merged_tree();
@@ -745,8 +747,7 @@ mod tests {
745747
],
746748
vec![commit_e.clone(), commit_f.clone()],
747749
)
748-
.block_on()
749-
.unwrap();
750+
.block_on()?;
750751
assert_eq!(state.head_order, vec![commit_d.id().clone()]);
751752
assert_eq!(
752753
state.current_order,
@@ -787,10 +788,11 @@ mod tests {
787788
*state.commits.get(commit_f.id()).unwrap().parents,
788789
vec![commit_c.id().clone()],
789790
);
791+
Ok(())
790792
}
791793

792794
#[test]
793-
fn test_execute_plan_reorder() {
795+
fn test_execute_plan_reorder() -> TestResult {
794796
let test_repo = TestRepo::init();
795797
let store = test_repo.repo.store();
796798
let empty_tree = store.empty_merged_tree();
@@ -828,7 +830,7 @@ mod tests {
828830
plan.rewrites.get_mut(commit_f.id()).unwrap().new_parents = vec![commit_a.id().clone()];
829831

830832
let rewritten = plan.execute(tx.repo_mut()).block_on().unwrap();
831-
tx.repo_mut().rebase_descendants().block_on().unwrap();
833+
tx.repo_mut().rebase_descendants().block_on()?;
832834
assert_eq!(
833835
rewritten.keys().collect::<HashSet<_>>(),
834836
hashset![
@@ -852,10 +854,11 @@ mod tests {
852854
assert_eq!(new_commit_d.parent_ids(), &[new_commit_b.id().clone()]);
853855
assert_eq!(new_commit_e.parent_ids(), &[new_commit_a.id().clone()]);
854856
assert_eq!(new_commit_f.parent_ids(), &[new_commit_a.id().clone()]);
857+
Ok(())
855858
}
856859

857860
#[test]
858-
fn test_execute_plan_abandon() {
861+
fn test_execute_plan_abandon() -> TestResult {
859862
let test_repo = TestRepo::init();
860863
let store = test_repo.repo.store();
861864
let empty_tree = store.empty_merged_tree();
@@ -890,13 +893,14 @@ mod tests {
890893
};
891894

892895
let rewritten = plan.execute(tx.repo_mut()).block_on().unwrap();
893-
tx.repo_mut().rebase_descendants().block_on().unwrap();
896+
tx.repo_mut().rebase_descendants().block_on()?;
894897
assert_eq!(rewritten.keys().sorted().collect_vec(), vec![commit_d.id()]);
895898
let new_commit_d = rewritten.get(commit_d.id()).unwrap();
896899
assert_eq!(new_commit_d.parent_ids(), &[commit_a.id().clone()]);
897900
assert_eq!(
898901
*tx.repo_mut().view().heads(),
899902
hashset![commit_b.id().clone(), new_commit_d.id().clone()]
900903
);
904+
Ok(())
901905
}
902906
}

cli/src/merge_tools/builtin.rs

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ mod tests {
778778
use proptest_state_machine::prop_state_machine;
779779
use test_case::test_matrix;
780780
use testutils::TestRepo;
781+
use testutils::TestResult;
781782
use testutils::assert_tree_eq;
782783
use testutils::dump_tree;
783784
use testutils::proptest::Transition;
@@ -1135,7 +1136,7 @@ mod tests {
11351136
fn test_edit_diff_builtin_apply_diff_should_preserve_copy_id(
11361137
create_left_tree: impl FnOnce(&Arc<Store>, &RepoPath, &CopyId) -> MergedTree,
11371138
create_right_tree: impl FnOnce(&Arc<Store>, &RepoPath, &CopyId) -> MergedTree,
1138-
) {
1139+
) -> TestResult {
11391140
let test_repo = TestRepo::init();
11401141
let store = test_repo.repo.store();
11411142

@@ -1159,26 +1160,23 @@ mod tests {
11591160
}
11601161
}
11611162
let tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files);
1162-
let actual_copy_ids = tree
1163-
.path_value(file_path)
1164-
.block_on()
1165-
.unwrap()
1166-
.map(|tree_value| {
1167-
let Some(TreeValue::File { copy_id, .. }) = tree_value else {
1168-
panic!("The path should point to an existing file.");
1169-
};
1170-
copy_id.clone()
1171-
});
1163+
let actual_copy_ids = tree.path_value(file_path).block_on()?.map(|tree_value| {
1164+
let Some(TreeValue::File { copy_id, .. }) = tree_value else {
1165+
panic!("The path should point to an existing file.");
1166+
};
1167+
copy_id.clone()
1168+
});
11721169
assert_eq!(
11731170
actual_copy_ids.resolve_trivial(SameChange::Accept),
11741171
Some(&copy_id),
11751172
"Expect the copy id of the file to be resolved to {copy_id:?}, but got \
11761173
{actual_copy_ids:?}."
11771174
);
1175+
Ok(())
11781176
}
11791177

11801178
#[test]
1181-
fn test_edit_merge_builtin_should_preserve_copy_id() {
1179+
fn test_edit_merge_builtin_should_preserve_copy_id() -> TestResult {
11821180
let test_repo = TestRepo::init();
11831181
let store = test_repo.repo.store();
11841182

@@ -1203,30 +1201,24 @@ mod tests {
12031201
.copy_id(new_copy_id.clone());
12041202
let tree = tree_builder.write_merged_tree();
12051203

1206-
let merge_tool_file = MergeToolFile::from_tree_and_path(&tree, file_path)
1207-
.block_on()
1208-
.unwrap();
1204+
let merge_tool_file = MergeToolFile::from_tree_and_path(&tree, file_path).block_on()?;
12091205
let merge_file = make_merge_file(&merge_tool_file, store.merge_options()).unwrap();
12101206
let tree = apply_merge_builtin(store, &tree, vec![file_path.to_owned()], &[merge_file])
1211-
.block_on()
1212-
.unwrap();
1207+
.block_on()?;
12131208

1214-
let actual_copy_ids = tree
1215-
.path_value(file_path)
1216-
.block_on()
1217-
.unwrap()
1218-
.map(|tree_value| {
1219-
let Some(TreeValue::File { copy_id, .. }) = tree_value else {
1220-
panic!("The path should point to an existing file.");
1221-
};
1222-
copy_id.clone()
1223-
});
1209+
let actual_copy_ids = tree.path_value(file_path).block_on()?.map(|tree_value| {
1210+
let Some(TreeValue::File { copy_id, .. }) = tree_value else {
1211+
panic!("The path should point to an existing file.");
1212+
};
1213+
copy_id.clone()
1214+
});
12241215
assert_eq!(
12251216
actual_copy_ids.resolve_trivial(SameChange::Accept),
12261217
Some(&new_copy_id),
12271218
"Expect the copy id of the file to be resolved to {new_copy_id:?}, but got \
12281219
{actual_copy_ids:?}."
12291220
);
1221+
Ok(())
12301222
}
12311223

12321224
#[test]
@@ -2022,7 +2014,7 @@ mod tests {
20222014
}
20232015

20242016
#[test]
2025-
fn test_edit_diff_builtin_with_matcher() {
2017+
fn test_edit_diff_builtin_with_matcher() -> TestResult {
20262018
let test_repo = TestRepo::init();
20272019
let store = test_repo.repo.store();
20282020

@@ -2050,22 +2042,22 @@ mod tests {
20502042

20512043
assert_eq!(changed_files, vec![matched_path.to_owned()]);
20522044

2053-
let result_tree = apply_diff_builtin(store, &left_tree, &right_tree, changed_files, &files)
2054-
.block_on()
2055-
.unwrap();
2045+
let result_tree =
2046+
apply_diff_builtin(store, &left_tree, &right_tree, changed_files, &files).block_on()?;
20562047

20572048
assert_eq!(
2058-
result_tree.path_value(matched_path).block_on().unwrap(),
2059-
left_tree.path_value(matched_path).block_on().unwrap()
2049+
result_tree.path_value(matched_path).block_on()?,
2050+
left_tree.path_value(matched_path).block_on()?
20602051
);
20612052
assert_eq!(
2062-
result_tree.path_value(unmatched_path).block_on().unwrap(),
2063-
right_tree.path_value(unmatched_path).block_on().unwrap()
2053+
result_tree.path_value(unmatched_path).block_on()?,
2054+
right_tree.path_value(unmatched_path).block_on()?
20642055
);
2056+
Ok(())
20652057
}
20662058

20672059
#[test]
2068-
fn test_make_merge_sections() {
2060+
fn test_make_merge_sections() -> TestResult {
20692061
let test_repo = TestRepo::init();
20702062
let store = test_repo.repo.store();
20712063

@@ -2097,13 +2089,11 @@ mod tests {
20972089
}
20982090

20992091
let merge = Merge::from_vec(vec![
2100-
to_file_id(left_tree.path_value(path).block_on().unwrap()),
2101-
to_file_id(base_tree.path_value(path).block_on().unwrap()),
2102-
to_file_id(right_tree.path_value(path).block_on().unwrap()),
2092+
to_file_id(left_tree.path_value(path).block_on()?),
2093+
to_file_id(base_tree.path_value(path).block_on()?),
2094+
to_file_id(right_tree.path_value(path).block_on()?),
21032095
]);
2104-
let content = extract_as_single_hunk(&merge, store, path)
2105-
.block_on()
2106-
.unwrap();
2096+
let content = extract_as_single_hunk(&merge, store, path).block_on()?;
21072097
let merge_result = files::merge_hunks(&content, store.merge_options());
21082098
let sections = make_merge_sections(merge_result).unwrap();
21092099
insta::assert_debug_snapshot!(sections, @r#"
@@ -2155,6 +2145,7 @@ mod tests {
21552145
},
21562146
]
21572147
"#);
2148+
Ok(())
21582149
}
21592150

21602151
prop_state_machine! {

lib/src/file_util.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ mod tests {
487487
use itertools::Itertools as _;
488488
use pollster::FutureExt as _;
489489
use test_case::test_case;
490+
use testutils::TestResult;
490491

491492
use super::*;
492493
use crate::tests::new_temp_dir;
@@ -691,30 +692,32 @@ mod tests {
691692
}
692693

693694
#[test]
694-
fn test_blocking_async_reader() {
695+
fn test_blocking_async_reader() -> TestResult {
695696
let input = b"hello";
696697
let sync_reader = Cursor::new(&input);
697698
let mut async_reader = BlockingAsyncReader::new(sync_reader);
698699

699700
let mut buf = [0u8; 3];
700-
let num_bytes_read = async_reader.read(&mut buf).block_on().unwrap();
701+
let num_bytes_read = async_reader.read(&mut buf).block_on()?;
701702
assert_eq!(num_bytes_read, 3);
702703
assert_eq!(&buf, &input[0..3]);
703704

704-
let num_bytes_read = async_reader.read(&mut buf).block_on().unwrap();
705+
let num_bytes_read = async_reader.read(&mut buf).block_on()?;
705706
assert_eq!(num_bytes_read, 2);
706707
assert_eq!(&buf[0..2], &input[3..5]);
708+
Ok(())
707709
}
708710

709711
#[test]
710-
fn test_blocking_async_reader_read_to_end() {
712+
fn test_blocking_async_reader_read_to_end() -> TestResult {
711713
let input = b"hello";
712714
let sync_reader = Cursor::new(&input);
713715
let mut async_reader = BlockingAsyncReader::new(sync_reader);
714716

715717
let mut buf = vec![];
716-
let num_bytes_read = async_reader.read_to_end(&mut buf).block_on().unwrap();
718+
let num_bytes_read = async_reader.read_to_end(&mut buf).block_on()?;
717719
assert_eq!(num_bytes_read, input.len());
718720
assert_eq!(&buf, &input);
721+
Ok(())
719722
}
720723
}

0 commit comments

Comments
 (0)