|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use crates_index_diff::Change::*; |
| 4 | + |
| 5 | +#[test] |
| 6 | +#[ignore] |
| 7 | +fn all_aggregrated_diffs_equal_latest_version( |
| 8 | +) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { |
| 9 | + let ((expected, baseline_duration), (actual, diff_duration)) = std::thread::scope( |
| 10 | + |scope| -> Result<_, Box<dyn std::error::Error + Send + Sync>> { |
| 11 | + let baseline = scope.spawn(|| -> Result<_, crates_index::Error> { |
| 12 | + let index = crates_index::Index::new_cargo_default()?; |
| 13 | + let start = std::time::Instant::now(); |
| 14 | + let mut versions = HashSet::new(); |
| 15 | + for krate in index.crates() { |
| 16 | + for version in krate.versions() { |
| 17 | + versions.insert(version.checksum().to_owned()); |
| 18 | + } |
| 19 | + } |
| 20 | + Ok((versions, start.elapsed())) |
| 21 | + }); |
| 22 | + let actual = scope.spawn(|| -> Result<_, Box<dyn std::error::Error + Send + Sync>> { |
| 23 | + use crates_index_diff::git; |
| 24 | + |
| 25 | + let start = std::time::Instant::now(); |
| 26 | + let repo_path = crates_index::Index::new_cargo_default()?.path().to_owned(); |
| 27 | + let index = crates_index_diff::Index::from_path_or_cloned(repo_path)?; |
| 28 | + let repo = index.repository(); |
| 29 | + let head = repo |
| 30 | + .find_reference("FETCH_HEAD") |
| 31 | + .or_else(|_| repo.find_reference("HEAD"))? |
| 32 | + .id(); |
| 33 | + let mut commits = head |
| 34 | + .ancestors() |
| 35 | + .first_parent_only() |
| 36 | + .all()? |
| 37 | + .map(|id| id.map(|id| id.detach())) |
| 38 | + .collect::<Result<Vec<_>, _>>()?; |
| 39 | + commits.push(head.detach()); |
| 40 | + |
| 41 | + // This could be more complex, like jumping to landmarks like 'Delete crate(s)' and so forth. |
| 42 | + |
| 43 | + let mut versions = HashSet::default(); |
| 44 | + let mut previous = None; |
| 45 | + for current in commits.into_iter() { |
| 46 | + let old = previous |
| 47 | + .unwrap_or_else(|| git::hash::ObjectId::empty_tree(git::hash::Kind::Sha1)); |
| 48 | + previous = Some(current); |
| 49 | + |
| 50 | + let changes = index.changes_between_commits(old, current)?; |
| 51 | + for change in changes { |
| 52 | + match change { |
| 53 | + Added(v) | AddedAndYanked(v) => { |
| 54 | + // found a new crate, add it to the index |
| 55 | + versions.insert(v.checksum.to_owned()); |
| 56 | + } |
| 57 | + Unyanked(v) | Yanked(v) => { |
| 58 | + // yanked/unyanked crates must be part of the index |
| 59 | + assert!(versions.contains(&v.checksum)) |
| 60 | + } |
| 61 | + Deleted { |
| 62 | + versions: deleted, .. |
| 63 | + } => { |
| 64 | + // delete a yanked crate |
| 65 | + for deleted_version in deleted { |
| 66 | + versions.remove(&deleted_version.checksum); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + Ok((versions, start.elapsed())) |
| 73 | + }); |
| 74 | + |
| 75 | + Ok(( |
| 76 | + baseline.join().expect("no panic")?, |
| 77 | + actual.join().expect("no panic")?, |
| 78 | + )) |
| 79 | + }, |
| 80 | + )?; |
| 81 | + |
| 82 | + dbg!(baseline_duration, expected.len()); |
| 83 | + dbg!(diff_duration, actual.len()); |
| 84 | + assert_eq!( |
| 85 | + actual.len(), |
| 86 | + expected.len(), |
| 87 | + "aggregated of all changes produces the final result" |
| 88 | + ); |
| 89 | + assert!(actual.eq(&expected), "actual should be exactly the same"); |
| 90 | + |
| 91 | + Ok(()) |
| 92 | +} |
0 commit comments