Skip to content

Commit 56bfad7

Browse files
committed
add more tests for typical operations (#16)
1 parent 365bcf0 commit 56bfad7

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

src/types.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22

3-
use std::fmt;
43
use git2::Repository;
4+
use std::fmt;
55

66
/// A wrapper for a repository of the crates.io index.
77
pub struct Index {
@@ -23,6 +23,32 @@ pub enum Change {
2323
Deleted(String),
2424
}
2525

26+
impl Change {
27+
/// Return the added crate, if this is this kind of change.
28+
pub fn added(&self) -> Option<&CrateVersion> {
29+
match self {
30+
Change::Added(v) => Some(v),
31+
_ => None,
32+
}
33+
}
34+
35+
/// Return the yanked crate, if this is this kind of change.
36+
pub fn yanked(&self) -> Option<&CrateVersion> {
37+
match self {
38+
Change::Yanked(v) => Some(v),
39+
_ => None,
40+
}
41+
}
42+
43+
/// Return the deleted crate, if this is this kind of change.
44+
pub fn deleted(&self) -> Option<&str> {
45+
match self {
46+
Change::Deleted(v) => Some(v.as_str()),
47+
_ => None,
48+
}
49+
}
50+
}
51+
2652
impl fmt::Display for Change {
2753
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2854
write!(
@@ -38,7 +64,7 @@ impl fmt::Display for Change {
3864
}
3965

4066
/// Pack all information we know about a change made to a version of a crate.
41-
#[derive(Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug)]
67+
#[derive(Default, Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug)]
4268
pub struct CrateVersion {
4369
/// The crate name, i.e. `clap`.
4470
pub name: String,

tests/index/mod.rs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,57 @@ mod changes_from_objects {
99

1010
#[test]
1111
fn addition() -> crate::Result {
12-
let _index = index_ro()?;
13-
let changes = changes(&_index, "initial commit")?;
12+
let changes = changes(&index_ro()?, ":/initial commit")?;
1413
assert_eq!(changes.len(), 3228);
1514
assert!(matches!(
16-
changes.first().expect("present"),
17-
Change::Added(CrateVersion {name, ..}) if name == "gi-get-artifact"
15+
changes
16+
.first()
17+
.and_then(|c| c.added().map(|v| v.name.as_str())),
18+
Some("gi-get-artifact")
1819
));
1920
assert!(matches!(
2021
changes.last().expect("present"),
2122
Change::Added(CrateVersion {name, ..}) if name == "gizmo"
2223
));
2324
Ok(())
2425
}
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+
}
2559

26-
fn changes(index: &Index, commit_message: &str) -> crate::Result<Vec<Change>> {
60+
fn changes(index: &Index, revspec: &str) -> crate::Result<Vec<Change>> {
2761
let repo = git::open(index.repository().path())?;
28-
let commit = repo
29-
.rev_parse(format!(":/{commit_message}").as_str())?
30-
.single()
31-
.unwrap();
62+
let commit = repo.rev_parse(revspec)?.single().unwrap();
3263
let ancestor_tree = commit
3364
.object()?
3465
.into_commit()

0 commit comments

Comments
 (0)