| 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 |
//! Test that optimized revset evaluates to the same result as the original |
| 16 |
//! expression. |
| 17 |
//! |
| 18 |
//! Use `PROPTEST_CASES=10000` to increase the number of test cases to run. |
| 19 |
//! The default is `256`, which might be too small to catch edge-case bugs. |
| 20 |
//! <https://proptest-rs.github.io/proptest/proptest/tutorial/config.html> |
| 21 |
|
| 22 |
use std::sync::Arc; |
| 23 |
|
| 24 |
use futures::TryStreamExt as _; |
| 25 |
use itertools::Itertools as _; |
| 26 |
use jj_lib::backend::CommitId; |
| 27 |
use jj_lib::commit::Commit; |
| 28 |
use jj_lib::config::ConfigLayer; |
| 29 |
use jj_lib::config::ConfigSource; |
| 30 |
use jj_lib::repo::MutableRepo; |
| 31 |
use jj_lib::repo::Repo; |
| 32 |
use jj_lib::revset::ResolvedRevsetExpression; |
| 33 |
use jj_lib::revset::RevsetExpression; |
| 34 |
use jj_lib::revset::RevsetFilterPredicate; |
| 35 |
use jj_lib::rewrite::RebaseOptions; |
| 36 |
use jj_lib::rewrite::RebasedCommit; |
| 37 |
use jj_lib::settings::UserSettings; |
| 38 |
use pollster::FutureExt as _; |
| 39 |
use proptest::prelude::*; |
| 40 |
use testutils::CommitBuilderExt as _; |
| 41 |
use testutils::TestRepo; |
| 42 |
use testutils::TestResult; |
| 43 |
|
| 44 |
fn stable_settings() -> UserSettings { |
| 45 |
let mut config = testutils::base_user_config(); |
| 46 |
let mut layer = ConfigLayer::empty(ConfigSource::User); |
| 47 |
layer |
| 48 |
.set_value("debug.commit-timestamp", "2001-02-03T04:05:06+07:00") |
| 49 |
.unwrap(); |
| 50 |
config.add_layer(layer); |
| 51 |
UserSettings::from_config(config).unwrap() |
| 52 |
} |
| 53 |
|
| 54 |
fn write_new_commit<'a>( |
| 55 |
repo: &mut MutableRepo, |
| 56 |
desc: &str, |
| 57 |
parents: impl IntoIterator<Item = &'a Commit>, |
| 58 |
) -> Commit { |
| 59 |
let parents = parents.into_iter().map(|c| c.id().clone()).collect(); |
| 60 |
let tree = repo.store().empty_merged_tree(); |
| 61 |
repo.new_commit(parents, tree) |
| 62 |
.set_description(desc) |
| 63 |
.write_unwrap() |
| 64 |
} |
| 65 |
|
| 66 |
fn rebase_descendants(repo: &mut MutableRepo) -> Vec<Commit> { |
| 67 |
let immutable = RevsetExpression::none(); |
| 68 |
let options = RebaseOptions::default(); |
| 69 |
let mut commits = Vec::new(); |
| 70 |
repo.rebase_descendants_with_options(&immutable, &options, |_, rebased| match rebased { |
| 71 |
RebasedCommit::Rewritten(commit) => commits.push(commit), |
| 72 |
RebasedCommit::Abandoned { .. } => {} |
| 73 |
}) |
| 74 |
.block_on() |
| 75 |
.unwrap(); |
| 76 |
commits |
| 77 |
} |
| 78 |
|
| 79 |
/// Strategy to generate arbitrary revset expressions. |
| 80 |
fn arb_expression( |
| 81 |
known_commits: Vec<CommitId>, |
| 82 |
visible_heads: Vec<Vec<CommitId>>, |
| 83 |
) -> impl Strategy<Value = Arc<ResolvedRevsetExpression>> { |
| 84 |
// https://proptest-rs.github.io/proptest/proptest/tutorial/recursive.html |
| 85 |
let max_commits = known_commits.len(); |
| 86 |
let leaf_expr = prop_oneof![ |
| 87 |
Just(RevsetExpression::none()), |
| 88 |
Just(RevsetExpression::all()), |
| 89 |
Just(RevsetExpression::visible_heads()), |
| 90 |
Just(RevsetExpression::root()), |
| 91 |
Just(RevsetExpression::forks()), |
| 92 |
proptest::sample::subsequence(known_commits, 1..=5.min(max_commits)) |
| 93 |
.prop_map(RevsetExpression::commits), |
| 94 |
// Use merges() as a filter that isn't constant. Since we don't have an |
| 95 |
// optimization rule that rewrites filter predicates, we wouldn't have |
| 96 |
// to add various filter predicates. |
| 97 |
Just(RevsetExpression::filter( |
| 98 |
RevsetFilterPredicate::ParentCount(2..u32::MAX) |
| 99 |
)), |
| 100 |
]; |
| 101 |
leaf_expr.prop_recursive( |
| 102 |
10, // depth |
| 103 |
100, // total nodes |
| 104 |
2, // unary or binary |
| 105 |
move |expr| { |
| 106 |
// This table includes redundant expressions (e.g. parents() and |
| 107 |
// ancestors()) if they are common, which will probably make them be |
| 108 |
// more weighted? |
| 109 |
prop_oneof![ |
| 110 |
// Ancestors |
| 111 |
expr.clone().prop_map(|x| x.parents()), |
| 112 |
expr.clone().prop_map(|x| x.ancestors()), |
| 113 |
(expr.clone(), 0..5_u64).prop_map(|(x, d)| x.ancestors_range(0..d)), |
| 114 |
// Descendants |
| 115 |
expr.clone().prop_map(|x| x.children()), |
| 116 |
expr.clone().prop_map(|x| x.descendants()), |
| 117 |
(expr.clone(), 0..5_u64).prop_map(|(x, d)| x.descendants_range(0..d)), |
| 118 |
// First ancestors |
| 119 |
expr.clone().prop_map(|x| x.first_ancestors_at(1)), |
| 120 |
expr.clone().prop_map(|x| x.first_ancestors()), |
| 121 |
(expr.clone(), 0..5_u64).prop_map(|(x, d)| x.first_ancestors_range(0..d)), |
| 122 |
// Range |
| 123 |
(expr.clone(), expr.clone()).prop_map(|(x, y)| x.range(&y)), |
| 124 |
// DagRange |
| 125 |
(expr.clone(), expr.clone()).prop_map(|(x, y)| x.dag_range_to(&y)), |
| 126 |
expr.clone().prop_map(|x| x.connected()), |
| 127 |
// Reachable |
| 128 |
(expr.clone(), expr.clone()).prop_map(|(x, y)| x.reachable(&y)), |
| 129 |
// Heads |
| 130 |
expr.clone().prop_map(|x| x.heads()), |
| 131 |
// Roots |
| 132 |
expr.clone().prop_map(|x| x.roots()), |
| 133 |
// ForkPoint |
| 134 |
expr.clone().prop_map(|x| x.fork_point()), |
| 135 |
// MergePoint |
| 136 |
expr.clone().prop_map(|x| x.merge_point()), |
| 137 |
// Latest |
| 138 |
(expr.clone(), 0..5_usize).prop_map(|(x, n)| x.latest(n)), |
| 139 |
// AtOperation (or WithinVisibility) |
| 140 |
( |
| 141 |
expr.clone(), |
| 142 |
proptest::sample::select(visible_heads.clone()) |
| 143 |
) |
| 144 |
.prop_map(|(candidates, visible_heads)| Arc::new( |
| 145 |
RevsetExpression::WithinVisibility { |
| 146 |
candidates, |
| 147 |
visible_heads |
| 148 |
} |
| 149 |
)), |
| 150 |
// Coalesce (in binary form) |
| 151 |
[expr.clone(), expr.clone()].prop_map(|xs| RevsetExpression::coalesce(&xs)), |
| 152 |
// General set operations |
| 153 |
expr.clone().prop_map(|x| x.negated()), |
| 154 |
(expr.clone(), expr.clone()).prop_map(|(x, y)| x.union(&y)), |
| 155 |
(expr.clone(), expr.clone()).prop_map(|(x, y)| x.intersection(&y)), |
| 156 |
(expr.clone(), expr.clone()).prop_map(|(x, y)| x.minus(&y)), |
| 157 |
] |
| 158 |
}, |
| 159 |
) |
| 160 |
} |
| 161 |
|
| 162 |
fn verify_optimized( |
| 163 |
repo: &dyn Repo, |
| 164 |
expression: &Arc<ResolvedRevsetExpression>, |
| 165 |
) -> Result<(), TestCaseError> { |
| 166 |
let optimized_revset = expression.clone().evaluate(repo).unwrap(); |
| 167 |
let unoptimized_revset = expression.clone().evaluate_unoptimized(repo).unwrap(); |
| 168 |
let optimized_ids: Vec<_> = optimized_revset.stream().try_collect().block_on().unwrap(); |
| 169 |
let unoptimized_ids: Vec<_> = unoptimized_revset |
| 170 |
.stream() |
| 171 |
.try_collect() |
| 172 |
.block_on() |
| 173 |
.unwrap(); |
| 174 |
prop_assert_eq!(optimized_ids, unoptimized_ids); |
| 175 |
Ok(()) |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn test_mostly_linear() -> TestResult { |
| 180 |
let settings = stable_settings(); |
| 181 |
let test_repo = TestRepo::init_with_settings(&settings); |
| 182 |
let repo = test_repo.repo; |
| 183 |
|
| 184 |
// 8 9 |
| 185 |
// 6 7 |
| 186 |
// |/| |
| 187 |
// 4 5 |
| 188 |
// 3 | |
| 189 |
// 1 2 |
| 190 |
// |/ |
| 191 |
// 0 |
| 192 |
let mut tx = repo.start_transaction(); |
| 193 |
let commit0 = repo.store().root_commit(); |
| 194 |
let commit1 = write_new_commit(tx.repo_mut(), "1", [&commit0]); |
| 195 |
let commit2 = write_new_commit(tx.repo_mut(), "2", [&commit0]); |
| 196 |
let commit3 = write_new_commit(tx.repo_mut(), "3", [&commit1]); |
| 197 |
let commit4 = write_new_commit(tx.repo_mut(), "4", [&commit3]); |
| 198 |
let commit5 = write_new_commit(tx.repo_mut(), "5", [&commit2]); |
| 199 |
let commit6 = write_new_commit(tx.repo_mut(), "6", [&commit4]); |
| 200 |
let commit7 = write_new_commit(tx.repo_mut(), "7", [&commit4, &commit5]); |
| 201 |
let commit8 = write_new_commit(tx.repo_mut(), "8", [&commit6]); |
| 202 |
let commit9 = write_new_commit(tx.repo_mut(), "9", [&commit7]); |
| 203 |
let commits = vec![ |
| 204 |
commit0, commit1, commit2, commit3, commit4, commit5, commit6, commit7, commit8, commit9, |
| 205 |
]; |
| 206 |
let repo = tx.commit("a").block_on()?; |
| 207 |
|
| 208 |
// Commit ids for reference |
| 209 |
insta::assert_snapshot!( |
| 210 |
commits.iter().map(|c| format!("{:<2} {}\n", c.description(), c.id())).join(""), @" |
| 211 |
00000000000000000000 |
| 212 |
1 78f823b31f2c4a77030b |
| 213 |
2 1ba216c17ef680561823 |
| 214 |
3 c2c719328d78654d9f8e |
| 215 |
4 d6b40f7dfac149c7181c |
| 216 |
5 c682b87d91a8940f71d5 |
| 217 |
6 456fe15ac6ebfdf56219 |
| 218 |
7 d2bba8ce1ce80751aab5 |
| 219 |
8 536f4a045e558c9927a5 |
| 220 |
9 6ab43bd6d94bdaff491f |
| 221 |
"); |
| 222 |
|
| 223 |
let commit_ids = commits.iter().map(|c| c.id().clone()).collect_vec(); |
| 224 |
let visible_heads = vec![ |
| 225 |
vec![commit_ids[0].clone()], |
| 226 |
vec![commit_ids[8].clone(), commit_ids[9].clone()], |
| 227 |
]; |
| 228 |
|
| 229 |
proptest!(|(expression in arb_expression(commit_ids, visible_heads))| { |
| 230 |
verify_optimized(repo.as_ref(), &expression)?; |
| 231 |
}); |
| 232 |
Ok(()) |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn test_weird_merges() -> TestResult { |
| 237 |
let settings = stable_settings(); |
| 238 |
let test_repo = TestRepo::init_with_settings(&settings); |
| 239 |
let repo = test_repo.repo; |
| 240 |
|
| 241 |
// 8 |
| 242 |
// /|\ |
| 243 |
// 4 5 6 7 |
| 244 |
// |X| |/ |
| 245 |
// 1 2 3 |
| 246 |
// \|/ |
| 247 |
// 0 |
| 248 |
let mut tx = repo.start_transaction(); |
| 249 |
let commit0 = repo.store().root_commit(); |
| 250 |
let commit1 = write_new_commit(tx.repo_mut(), "1", [&commit0]); |
| 251 |
let commit2 = write_new_commit(tx.repo_mut(), "2", [&commit0]); |
| 252 |
let commit3 = write_new_commit(tx.repo_mut(), "3", [&commit0]); |
| 253 |
let commit4 = write_new_commit(tx.repo_mut(), "4", [&commit1, &commit2]); |
| 254 |
let commit5 = write_new_commit(tx.repo_mut(), "5", [&commit1, &commit2]); |
| 255 |
let commit6 = write_new_commit(tx.repo_mut(), "6", [&commit3]); |
| 256 |
let commit7 = write_new_commit(tx.repo_mut(), "7", [&commit3]); |
| 257 |
let commit8 = write_new_commit(tx.repo_mut(), "8", [&commit5, &commit6, &commit7]); |
| 258 |
let commits = vec![ |
| 259 |
commit0, commit1, commit2, commit3, commit4, commit5, commit6, commit7, commit8, |
| 260 |
]; |
| 261 |
let repo = tx.commit("a").block_on()?; |
| 262 |
|
| 263 |
// Commit ids for reference |
| 264 |
insta::assert_snapshot!( |
| 265 |
commits.iter().map(|c| format!("{:<2} {}\n", c.description(), c.id())).join(""), @" |
| 266 |
00000000000000000000 |
| 267 |
1 78f823b31f2c4a77030b |
| 268 |
2 1ba216c17ef680561823 |
| 269 |
3 83a7b5b8138c9428d837 |
| 270 |
4 43a3ed8115915cb0ebe0 |
| 271 |
5 aec384ff4d34c039e4db |
| 272 |
6 d80cec48faa50bf2ac56 |
| 273 |
7 2667f762c099ffcda2f0 |
| 274 |
8 54feb3e8186bc4450be4 |
| 275 |
"); |
| 276 |
|
| 277 |
let commit_ids = commits.iter().map(|c| c.id().clone()).collect_vec(); |
| 278 |
let visible_heads = vec![ |
| 279 |
vec![commit_ids[0].clone()], |
| 280 |
vec![commit_ids[4].clone(), commit_ids[8].clone()], |
| 281 |
]; |
| 282 |
|
| 283 |
proptest!(|(expression in arb_expression(commit_ids, visible_heads))| { |
| 284 |
verify_optimized(repo.as_ref(), &expression)?; |
| 285 |
}); |
| 286 |
Ok(()) |
| 287 |
} |
| 288 |
|
| 289 |
#[test] |
| 290 |
fn test_feature_branches() -> TestResult { |
| 291 |
let settings = stable_settings(); |
| 292 |
let test_repo = TestRepo::init_with_settings(&settings); |
| 293 |
let repo = test_repo.repo; |
| 294 |
|
| 295 |
// 9 |
| 296 |
// |\ |
| 297 |
// 8 \ |
| 298 |
// |\ \ |
| 299 |
// | 7 | |
| 300 |
// |/ | |
| 301 |
// 6 | |
| 302 |
// |\ 5 |
| 303 |
// | | 4 |
| 304 |
// | | 3 |
| 305 |
// | 2 | |
| 306 |
// | 1 | |
| 307 |
// | |/ |
| 308 |
// |/ |
| 309 |
// 0 |
| 310 |
|
| 311 |
// Fetch branch 2 and 5 |
| 312 |
let mut tx = repo.start_transaction(); |
| 313 |
let commit0 = repo.store().root_commit(); |
| 314 |
let commit1 = write_new_commit(tx.repo_mut(), "1", [&commit0]); |
| 315 |
let commit2 = write_new_commit(tx.repo_mut(), "2", [&commit1]); |
| 316 |
let commit3 = write_new_commit(tx.repo_mut(), "3", [&commit0]); |
| 317 |
let commit4 = write_new_commit(tx.repo_mut(), "4", [&commit3]); |
| 318 |
let commit5 = write_new_commit(tx.repo_mut(), "5", [&commit4]); |
| 319 |
let repo = tx.commit("a").block_on()?; |
| 320 |
|
| 321 |
// Merge branch 2 |
| 322 |
let mut tx = repo.start_transaction(); |
| 323 |
let commit6 = write_new_commit(tx.repo_mut(), "6", [&commit0, &commit2]); |
| 324 |
let repo = tx.commit("a").block_on()?; |
| 325 |
|
| 326 |
// Fetch merged branch 7 |
| 327 |
let mut tx = repo.start_transaction(); |
| 328 |
let commit7 = write_new_commit(tx.repo_mut(), "7", [&commit6]); |
| 329 |
let commit8 = write_new_commit(tx.repo_mut(), "8", [&commit6, &commit7]); |
| 330 |
let repo = tx.commit("a").block_on()?; |
| 331 |
|
| 332 |
// Merge branch 5 |
| 333 |
let mut tx = repo.start_transaction(); |
| 334 |
let commit9 = write_new_commit(tx.repo_mut(), "9", [&commit8, &commit5]); |
| 335 |
let commits = vec![ |
| 336 |
commit0, commit1, commit2, commit3, commit4, commit5, commit6, commit7, commit8, commit9, |
| 337 |
]; |
| 338 |
let repo = tx.commit("a").block_on()?; |
| 339 |
|
| 340 |
// Commit ids for reference |
| 341 |
insta::assert_snapshot!( |
| 342 |
commits.iter().map(|c| format!("{:<2} {}\n", c.description(), c.id())).join(""), @" |
| 343 |
00000000000000000000 |
| 344 |
1 78f823b31f2c4a77030b |
| 345 |
2 6323cf55a45bcc85315d |
| 346 |
3 83a7b5b8138c9428d837 |
| 347 |
4 93731ec1a14276206ba7 |
| 348 |
5 c388b47bd72fcfee9e3c |
| 349 |
6 da751caa45bda2e3d526 |
| 350 |
7 45a86fae2b51ec68f8c0 |
| 351 |
8 9f83496b963cbaf8cb7a |
| 352 |
9 998a24ecd56446732f55 |
| 353 |
"); |
| 354 |
|
| 355 |
let commit_ids = commits.iter().map(|c| c.id().clone()).collect_vec(); |
| 356 |
let visible_heads = vec![ |
| 357 |
vec![commit_ids[0].clone()], |
| 358 |
vec![commit_ids[2].clone(), commit_ids[5].clone()], |
| 359 |
vec![commit_ids[5].clone(), commit_ids[6].clone()], |
| 360 |
vec![commit_ids[5].clone(), commit_ids[8].clone()], |
| 361 |
vec![commit_ids[9].clone()], |
| 362 |
]; |
| 363 |
|
| 364 |
proptest!(|(expression in arb_expression(commit_ids, visible_heads))| { |
| 365 |
verify_optimized(repo.as_ref(), &expression)?; |
| 366 |
}); |
| 367 |
Ok(()) |
| 368 |
} |
| 369 |
|
| 370 |
#[test] |
| 371 |
fn test_rewritten() -> TestResult { |
| 372 |
let settings = stable_settings(); |
| 373 |
let test_repo = TestRepo::init_with_settings(&settings); |
| 374 |
let repo = test_repo.repo; |
| 375 |
|
| 376 |
// 5 |
| 377 |
// |\ |
| 378 |
// 4 | 3 |
| 379 |
// | |/ |
| 380 |
// 1 2 |
| 381 |
// |/ |
| 382 |
// 0 |
| 383 |
let mut tx = repo.start_transaction(); |
| 384 |
let commit0 = repo.store().root_commit(); |
| 385 |
let commit1 = write_new_commit(tx.repo_mut(), "1", [&commit0]); |
| 386 |
let commit2 = write_new_commit(tx.repo_mut(), "2", [&commit0]); |
| 387 |
let commit3 = write_new_commit(tx.repo_mut(), "3", [&commit2]); |
| 388 |
let commit4 = write_new_commit(tx.repo_mut(), "4", [&commit1]); |
| 389 |
let commit5 = write_new_commit(tx.repo_mut(), "5", [&commit4, &commit2]); |
| 390 |
let mut commits = vec![commit0, commit1, commit2, commit3, commit4, commit5]; |
| 391 |
let repo = tx.commit("a").block_on()?; |
| 392 |
|
| 393 |
// Rewrite 2, rebase 3 and 5 |
| 394 |
let mut tx = repo.start_transaction(); |
| 395 |
let commit2b = tx |
| 396 |
.repo_mut() |
| 397 |
.rewrite_commit(&commits[2]) |
| 398 |
.set_description("2b") |
| 399 |
.write_unwrap(); |
| 400 |
commits.push(commit2b); |
| 401 |
commits.extend(rebase_descendants(tx.repo_mut())); |
| 402 |
let repo = tx.commit("b").block_on()?; |
| 403 |
|
| 404 |
// Abandon 4, rebase 5 |
| 405 |
let mut tx = repo.start_transaction(); |
| 406 |
tx.repo_mut().record_abandoned_commit(&commits[4]); |
| 407 |
commits.extend(rebase_descendants(tx.repo_mut())); |
| 408 |
let repo = tx.commit("c").block_on()?; |
| 409 |
|
| 410 |
// Commit ids for reference |
| 411 |
insta::assert_snapshot!( |
| 412 |
commits.iter().map(|c| format!("{:<2} {}\n", c.description(), c.id())).join(""), @" |
| 413 |
00000000000000000000 |
| 414 |
1 78f823b31f2c4a77030b |
| 415 |
2 1ba216c17ef680561823 |
| 416 |
3 068410d7a4a5b7052c18 |
| 417 |
4 8ee10ec699f52df8c624 |
| 418 |
5 32bd65d7134884955150 |
| 419 |
2b a7d217f53df0908d3f7a |
| 420 |
3 c199d1c8c617cf15893f |
| 421 |
5 5fc1da61558a03f69a8d |
| 422 |
5 cb81e73e341e59553ff6 |
| 423 |
"); |
| 424 |
|
| 425 |
let commit_ids = commits.iter().map(|c| c.id().clone()).collect_vec(); |
| 426 |
let visible_heads = vec![ |
| 427 |
vec![commit_ids[0].clone()], |
| 428 |
vec![commit_ids[3].clone(), commit_ids[5].clone()], |
| 429 |
vec![commit_ids[7].clone(), commit_ids[8].clone()], |
| 430 |
vec![commit_ids[7].clone(), commit_ids[9].clone()], |
| 431 |
]; |
| 432 |
|
| 433 |
proptest!(|(expression in arb_expression(commit_ids, visible_heads))| { |
| 434 |
verify_optimized(repo.as_ref(), &expression)?; |
| 435 |
}); |
| 436 |
Ok(()) |
| 437 |
} |