Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude yanked releases when choosing baseline version #255

Merged
merged 14 commits into from
Jan 3, 2023
74 changes: 39 additions & 35 deletions src/baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,64 +374,68 @@ mod tests {
"yanked": yanked,
"cksum": "00".repeat(32),
}))
.unwrap()
.expect("Parsing JSON as crates_index::Version should not result in any errors.")
mgr0dzicki marked this conversation as resolved.
Show resolved Hide resolved
}

fn new_crate(versions: &[Version]) -> Crate {
fn new_crate(versions: Vec<Version>) -> Crate {
// `crates_index::Crate` cannot be created explicitly, as its field
// is private, so we use the fact that it can be deserialized.
serde_json::from_value(serde_json::json!({ "versions": versions })).unwrap()
serde_json::from_value(serde_json::json!({ "versions": versions }))
.expect("Parsing JSON as crates_index::Crate should not result in any errors.")
}

fn test_choose_baseline_version(
mgr0dzicki marked this conversation as resolved.
Show resolved Hide resolved
versions: Vec<(&str, bool)>,
current_version_name: Option<&str>,
expected: &str,
) {
let crate_ = new_crate(
versions
.into_iter()
.map(|(version, yanked)| new_mock_version(version, yanked))
.collect(),
);
let current_version = current_version_name.map(|version_name| {
semver::Version::parse(version_name).expect("current_version_name should be valid")
tonowak marked this conversation as resolved.
Show resolved Hide resolved
});
let choosen_baseline = choose_baseline_version(&crate_, current_version.as_ref())
.expect("choose_baseline_version should not return any error");
mgr0dzicki marked this conversation as resolved.
Show resolved Hide resolved
mgr0dzicki marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(choosen_baseline, expected.to_owned());
}

#[test]
fn choose_baseline_version_yanked() {
mgr0dzicki marked this conversation as resolved.
Show resolved Hide resolved
let crate_ = new_crate(&[
new_mock_version("1.2.0", false),
new_mock_version("1.2.1", true),
]);
let current_version = semver::Version::new(1, 2, 2);
assert_eq!(
choose_baseline_version(&crate_, Some(&current_version)).unwrap(),
"1.2.0".to_string()
test_choose_baseline_version(
vec![("1.2.0", false), ("1.2.1", true)],
Some("1.2.2"),
"1.2.0",
);
}

#[test]
fn choose_baseline_version_not_latest() {
let crate_ = new_crate(&[
new_mock_version("1.2.0", false),
new_mock_version("1.2.1", false),
]);
let current_version = semver::Version::new(1, 2, 0);
assert_eq!(
choose_baseline_version(&crate_, Some(&current_version)).unwrap(),
"1.2.0".to_string()
test_choose_baseline_version(
vec![("1.2.0", false), ("1.2.1", false)],
Some("1.2.0"),
"1.2.0",
);
}

#[test]
fn choose_baseline_version_pre_release() {
let crate_ = new_crate(&[
new_mock_version("1.2.0", false),
new_mock_version("1.2.1-rc1", false),
]);
let current_version = semver::Version::parse("1.2.1-rc2").unwrap();
assert_eq!(
choose_baseline_version(&crate_, Some(&current_version)).unwrap(),
"1.2.0".to_string()
test_choose_baseline_version(
vec![("1.2.0", false), ("1.2.1-rc1", false)],
Some("1.2.1-rc2"),
"1.2.0",
);
}

#[test]
fn choose_baseline_version_no_current() {
mgr0dzicki marked this conversation as resolved.
Show resolved Hide resolved
let crate_ = new_crate(&[
new_mock_version("1.2.0", false),
new_mock_version("1.2.1-rc1", false),
new_mock_version("1.3.1", true),
]);
assert_eq!(
choose_baseline_version(&crate_, None).unwrap(),
"1.2.0".to_string()
test_choose_baseline_version(
vec![("1.2.0", false), ("1.2.1-rc1", false), ("1.3.1", true)],
None,
"1.2.0",
);
}
}