Debian Codegraph @

lib/tests/test_bisect.rs

raw
1 // Copyright 2025 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use std::sync::Arc;
16
17 use assert_matches::assert_matches;
18 use jj_lib::backend::CommitId;
19 use jj_lib::bisect::BisectionResult;
20 use jj_lib::bisect::Bisector;
21 use jj_lib::bisect::Evaluation;
22 use jj_lib::bisect::NextStep;
23 use jj_lib::repo::Repo;
24 use jj_lib::revset::ResolvedRevsetExpression;
25 use pollster::FutureExt as _;
26 use testutils::TestRepo;
27 use testutils::write_random_commit;
28 use testutils::write_random_commit_with_parents;
29
30 fn test_bisection<'a>(
31 repo: &dyn Repo,
32 input_range: &Arc<ResolvedRevsetExpression>,
33 results: impl IntoIterator<Item = (&'a CommitId, Evaluation)>,
34 ) -> BisectionResult {
35 let mut bisector = Bisector::new(repo, input_range.clone()).block_on().unwrap();
36 let mut iter = results.into_iter().enumerate();
37 loop {
38 match bisector.next_step().block_on().unwrap() {
39 NextStep::Evaluate(commit) => {
40 let (i, (expected_id, result)) =
41 iter.next().expect("More commits than expected were tested");
42 assert_eq!(
43 commit.id(),
44 expected_id,
45 "Attempt to test unexpected commit at iteration {i}"
46 );
47 bisector.mark(commit.id().clone(), result);
48 }
49 NextStep::Done(bisection_result) => {
50 assert!(iter.next().is_none(), "Finished earlier than expected");
51 return bisection_result;
52 }
53 }
54 }
55 }
56
57 #[test]
58 fn test_bisect_empty_input() {
59 let test_repo = TestRepo::init();
60 let repo = &test_repo.repo;
61
62 let input_range = ResolvedRevsetExpression::none();
63 let expected_tests = [];
64 let result = test_bisection(repo.as_ref(), &input_range, expected_tests);
65 assert_matches!(result, BisectionResult::Indeterminate);
66 }
67
68 #[test]
69 fn test_bisect_linear() {
70 let test_repo = TestRepo::init();
71 let repo = &test_repo.repo;
72
73 let mut tx = repo.start_transaction();
74 let root_commit = repo.store().root_commit();
75 let commit1 = write_random_commit(tx.repo_mut());
76 let commit2 = write_random_commit_with_parents(tx.repo_mut(), &[&commit1]);
77 let commit3 = write_random_commit_with_parents(tx.repo_mut(), &[&commit2]);
78 let commit4 = write_random_commit_with_parents(tx.repo_mut(), &[&commit3]);
79 let commit5 = write_random_commit_with_parents(tx.repo_mut(), &[&commit4]);
80 let commit6 = write_random_commit_with_parents(tx.repo_mut(), &[&commit5]);
81 let commit7 = write_random_commit_with_parents(tx.repo_mut(), &[&commit6]);
82
83 let input_range = ResolvedRevsetExpression::commit(commit7.id().clone()).ancestors();
84
85 // Root commit is the first bad commit
86 let expected_tests = [
87 (commit3.id(), Evaluation::Bad),
88 (commit1.id(), Evaluation::Bad),
89 (root_commit.id(), Evaluation::Bad),
90 ];
91 let result = test_bisection(tx.repo(), &input_range, expected_tests);
92 assert_eq!(result, BisectionResult::Found(vec![root_commit.clone()]));
93
94 // Commit 1 is the first bad commit
95 let expected_tests = [
96 (commit3.id(), Evaluation::Bad),
97 (commit1.id(), Evaluation::Bad),
98 (root_commit.id(), Evaluation::Good),
99 ];
100 let result = test_bisection(tx.repo(), &input_range, expected_tests);
101 assert_eq!(result, BisectionResult::Found(vec![commit1.clone()]));
102
103 // Commit 3 is the first bad commit
104 let expected_tests = [
105 (commit3.id(), Evaluation::Bad),
106 (commit1.id(), Evaluation::Good),
107 (commit2.id(), Evaluation::Good),
108 ];
109 let result = test_bisection(tx.repo(), &input_range, expected_tests);
110 assert_eq!(result, BisectionResult::Found(vec![commit3.clone()]));
111
112 // Commit 5 is the first bad commit
113 let expected_tests = [
114 (commit3.id(), Evaluation::Good),
115 (commit5.id(), Evaluation::Bad),
116 (commit4.id(), Evaluation::Good),
117 ];
118 let result = test_bisection(tx.repo(), &input_range, expected_tests);
119 assert_eq!(result, BisectionResult::Found(vec![commit5.clone()]));
120
121 // Commit 7 is the first bad commit
122 let expected_tests = [
123 (commit3.id(), Evaluation::Good),
124 (commit5.id(), Evaluation::Good),
125 (commit6.id(), Evaluation::Good),
126 ];
127 let result = test_bisection(tx.repo(), &input_range, expected_tests);
128 assert_eq!(result, BisectionResult::Found(vec![commit7.clone()]));
129
130 // Commit 2 is the first bad commit but commit 3 is skipped
131 let expected_tests = [
132 (commit3.id(), Evaluation::Skip),
133 (commit2.id(), Evaluation::Bad),
134 (root_commit.id(), Evaluation::Good),
135 (commit1.id(), Evaluation::Good),
136 ];
137 let result = test_bisection(tx.repo(), &input_range, expected_tests);
138 assert_eq!(result, BisectionResult::Found(vec![commit2.clone()]));
139
140 // Commit 4 is the first bad commit but commit 3 is skipped
141 let expected_tests = [
142 (commit3.id(), Evaluation::Skip),
143 (commit2.id(), Evaluation::Good),
144 (commit5.id(), Evaluation::Bad),
145 (commit4.id(), Evaluation::Bad),
146 ];
147 let result = test_bisection(tx.repo(), &input_range, expected_tests);
148 assert_eq!(
149 result,
150 BisectionResult::FoundDespiteSkips {
151 bad_commits: vec![commit4.clone()],
152 possibly_bad: vec![commit3.clone()]
153 }
154 );
155
156 // commit2 is the first bad commit; its parent and child commits were skipped
157 // * commit1 is a possibly_bad commit: it's between a bad and a good commit
158 // * commit3 is NOT a possibly_bad commit: it's implicitly bad due to commit2
159 let expected_tests = [
160 (commit3.id(), Evaluation::Skip),
161 (commit2.id(), Evaluation::Bad),
162 (root_commit.id(), Evaluation::Good),
163 (commit1.id(), Evaluation::Skip),
164 ];
165 let result = test_bisection(tx.repo(), &input_range, expected_tests);
166 assert_eq!(
167 result,
168 BisectionResult::FoundDespiteSkips {
169 bad_commits: vec![commit2.clone()],
170 possibly_bad: vec![commit1.clone()]
171 }
172 );
173
174 // Commit 7 is the first bad commit but commits before 6 were skipped
175 // TODO: Avoid testing every commit near first skipped commit. Test e.g. commit
176 // 1 and commit 5 once we see that commit 3 was indeterminate.
177 let expected_tests = [
178 (commit3.id(), Evaluation::Skip),
179 (commit2.id(), Evaluation::Skip),
180 (commit4.id(), Evaluation::Skip),
181 (commit1.id(), Evaluation::Skip),
182 (commit5.id(), Evaluation::Skip),
183 (root_commit.id(), Evaluation::Skip),
184 (commit6.id(), Evaluation::Good),
185 ];
186 let result = test_bisection(tx.repo(), &input_range, expected_tests);
187 assert_eq!(result, BisectionResult::Found(vec![commit7.clone()]));
188
189 // Gaps in the input range are allowed
190 let input_range = ResolvedRevsetExpression::commits(vec![
191 commit7.id().clone(),
192 commit4.id().clone(),
193 commit2.id().clone(),
194 commit1.id().clone(),
195 ]);
196 // Commit 4 is the first bad commit
197 let expected_tests = [
198 (commit2.id(), Evaluation::Good),
199 (commit4.id(), Evaluation::Bad),
200 ];
201 let result = test_bisection(tx.repo(), &input_range, expected_tests);
202 assert_eq!(result, BisectionResult::Found(vec![commit4.clone()]));
203 }
204
205 #[test]
206 fn test_bisect_nonlinear() {
207 let test_repo = TestRepo::init();
208 let repo = &test_repo.repo;
209
210 // 7
211 // |\
212 // 5 6
213 // | |
214 // 3 4
215 // | |
216 // 1 2
217 // |/
218 // 0
219 let mut tx = repo.start_transaction();
220 let root_commit = repo.store().root_commit();
221 let commit1 = write_random_commit(tx.repo_mut());
222 let commit2 = write_random_commit(tx.repo_mut());
223 let commit3 = write_random_commit_with_parents(tx.repo_mut(), &[&commit1]);
224 let commit4 = write_random_commit_with_parents(tx.repo_mut(), &[&commit2]);
225 let commit5 = write_random_commit_with_parents(tx.repo_mut(), &[&commit3]);
226 let commit6 = write_random_commit_with_parents(tx.repo_mut(), &[&commit4]);
227 let commit7 = write_random_commit_with_parents(tx.repo_mut(), &[&commit5, &commit6]);
228
229 let input_range = ResolvedRevsetExpression::commit(commit7.id().clone()).ancestors();
230
231 // Root commit is the first bad commit
232 let expected_tests = [
233 (commit3.id(), Evaluation::Bad),
234 (root_commit.id(), Evaluation::Bad),
235 ];
236 let result = test_bisection(tx.repo(), &input_range, expected_tests);
237 assert_eq!(result, BisectionResult::Found(vec![root_commit.clone()]));
238
239 // Commit 3 is the first bad commit
240 let expected_tests = [
241 (commit3.id(), Evaluation::Bad),
242 (root_commit.id(), Evaluation::Good),
243 (commit1.id(), Evaluation::Good),
244 ];
245 let result = test_bisection(tx.repo(), &input_range, expected_tests);
246 assert_eq!(result, BisectionResult::Found(vec![commit3.clone()]));
247
248 // Commit 4 is the first bad commit
249 let expected_tests = [
250 (commit3.id(), Evaluation::Good),
251 (commit4.id(), Evaluation::Bad),
252 (commit2.id(), Evaluation::Good),
253 ];
254 let result = test_bisection(tx.repo(), &input_range, expected_tests);
255 assert_eq!(result, BisectionResult::Found(vec![commit4.clone()]));
256
257 // Commit 6 is the first bad commit
258 let expected_tests = [
259 (commit3.id(), Evaluation::Good),
260 (commit4.id(), Evaluation::Good),
261 (commit5.id(), Evaluation::Good),
262 (commit6.id(), Evaluation::Bad),
263 ];
264 let result = test_bisection(tx.repo(), &input_range, expected_tests);
265 assert_eq!(result, BisectionResult::Found(vec![commit6.clone()]));
266 }
267
268 #[test]
269 fn test_bisect_disjoint_sets() {
270 let test_repo = TestRepo::init();
271 let repo = &test_repo.repo;
272
273 // 1 2
274 // |/
275 // 0
276 let mut tx = repo.start_transaction();
277 let commit1 = write_random_commit(tx.repo_mut());
278 let commit2 = write_random_commit(tx.repo_mut());
279
280 let input_range =
281 ResolvedRevsetExpression::commits(vec![commit1.id().clone(), commit2.id().clone()]);
282
283 // Both commit 1 and commit 2 are (implicitly) the first bad commits
284 let expected_tests = [];
285 let result = test_bisection(tx.repo(), &input_range, expected_tests);
286 assert_eq!(
287 result,
288 BisectionResult::Found(vec![commit2.clone(), commit1.clone()])
289 );
290 }
291
292 #[test]
293 fn test_bisect_abort() {
294 let test_repo = TestRepo::init();
295 let repo = &test_repo.repo;
296
297 let mut tx = repo.start_transaction();
298 let commit1 = write_random_commit(tx.repo_mut());
299 let commit2 = write_random_commit_with_parents(tx.repo_mut(), &[&commit1]);
300 let commit3 = write_random_commit_with_parents(tx.repo_mut(), &[&commit2]);
301 let commit4 = write_random_commit_with_parents(tx.repo_mut(), &[&commit3]);
302 let commit5 = write_random_commit_with_parents(tx.repo_mut(), &[&commit4]);
303 let commit6 = write_random_commit_with_parents(tx.repo_mut(), &[&commit5]);
304 let commit7 = write_random_commit_with_parents(tx.repo_mut(), &[&commit6]);
305
306 let input_range = ResolvedRevsetExpression::commit(commit7.id().clone()).ancestors();
307
308 // Commit 5 is an abort commit
309 let expected_tests = [
310 (commit3.id(), Evaluation::Good),
311 (commit5.id(), Evaluation::Abort),
312 ];
313 let result = test_bisection(tx.repo(), &input_range, expected_tests);
314 assert_eq!(result, BisectionResult::Abort);
315 }