|
1 | 1 | use crates_index_diff::Index; |
2 | 2 |
|
| 3 | +mod changes_from_objects; |
3 | 4 | mod old; |
4 | | -mod changes_from_objects { |
5 | | - use crate::index::index_ro; |
6 | | - use crates_index_diff::{Change, CrateVersion, Index}; |
7 | | - use git_repository as git; |
8 | | - use git_repository::prelude::ObjectIdExt; |
9 | | - |
10 | | - #[test] |
11 | | - fn addition() -> crate::Result { |
12 | | - let changes = changes(&index_ro()?, ":/initial commit")?; |
13 | | - assert_eq!(changes.len(), 3228); |
14 | | - assert!(matches!( |
15 | | - changes |
16 | | - .first() |
17 | | - .and_then(|c| c.added().map(|v| v.name.as_str())), |
18 | | - Some("gi-get-artifact") |
19 | | - )); |
20 | | - assert!(matches!( |
21 | | - changes.last().expect("present"), |
22 | | - Change::Added(CrateVersion {name, ..}) if name == "gizmo" |
23 | | - )); |
24 | | - Ok(()) |
25 | | - } |
26 | | - #[test] |
27 | | - fn deletion() -> crate::Result { |
28 | | - let changes = changes(&index_ro()?, "@~326")?; |
29 | | - assert_eq!(changes.len(), 1); |
30 | | - assert_eq!(changes.first().and_then(|c| c.deleted()), Some("girl")); |
31 | | - Ok(()) |
32 | | - } |
33 | | - |
34 | | - #[test] |
35 | | - fn new_version() -> crate::Result { |
36 | | - let changes = changes(&index_ro()?, ":/Updating crate `git-repository#0.22.1`")?; |
37 | | - assert_eq!(changes.len(), 1); |
38 | | - assert_eq!( |
39 | | - changes |
40 | | - .first() |
41 | | - .and_then(|c| c.added().map(|v| v.name.as_str())), |
42 | | - Some("git-repository") |
43 | | - ); |
44 | | - Ok(()) |
45 | | - } |
46 | | - |
47 | | - #[test] |
48 | | - fn yanked() -> crate::Result { |
49 | | - let changes = changes(&index_ro()?, ":/Yanking crate `github_release_rs#0.1.0`")?; |
50 | | - assert_eq!(changes.len(), 1); |
51 | | - assert_eq!( |
52 | | - changes |
53 | | - .first() |
54 | | - .and_then(|c| c.yanked().map(|v| v.name.as_str())), |
55 | | - Some("github_release_rs") |
56 | | - ); |
57 | | - Ok(()) |
58 | | - } |
59 | | - |
60 | | - #[test] |
61 | | - fn normalization() -> crate::Result { |
62 | | - let changes = changes(&index_ro()?, ":/normalize")?; |
63 | | - assert_eq!( |
64 | | - changes.len(), |
65 | | - 2356, // should be 0 |
66 | | - "normalization changes the representation, but the data itself stays the same, BUT we can't do it yet" |
67 | | - ); |
68 | | - Ok(()) |
69 | | - } |
70 | | - |
71 | | - fn changes(index: &Index, revspec: &str) -> crate::Result<Vec<Change>> { |
72 | | - let repo = git::open(index.repository().path())?; |
73 | | - let commit = repo.rev_parse(revspec)?.single().unwrap(); |
74 | | - let ancestor_tree = commit |
75 | | - .object()? |
76 | | - .into_commit() |
77 | | - .parent_ids() |
78 | | - .next() |
79 | | - .and_then(|parent| parent.object().ok()?.into_commit().tree_id().ok()) |
80 | | - .unwrap_or_else(|| git::hash::ObjectId::empty_tree(repo.object_hash()).attach(&repo)); |
81 | | - Ok(index.changes_from_objects( |
82 | | - &index |
83 | | - .repository() |
84 | | - .find_object(object_id_to_oid(ancestor_tree), None) |
85 | | - .expect("ancestor tree is available"), |
86 | | - &index |
87 | | - .repository() |
88 | | - .find_object(object_id_to_oid(commit), None) |
89 | | - .expect("first object exists"), |
90 | | - )?) |
91 | | - } |
92 | | - |
93 | | - fn object_id_to_oid(oid: impl Into<git::ObjectId>) -> git2::Oid { |
94 | | - git2::Oid::from_bytes(oid.into().as_bytes()).expect("valid") |
95 | | - } |
96 | | -} |
97 | 5 |
|
98 | 6 | fn index_ro() -> crate::Result<Index> { |
99 | 7 | let dir = git_testtools::scripted_fixture_repo_read_only_with_args( |
|
0 commit comments