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
141 changes: 118 additions & 23 deletions src/baseline.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Context as _;
use crates_index::Crate;

use crate::dump::RustDocCommand;
use crate::util::slugify;
Expand Down Expand Up @@ -242,6 +243,43 @@ fn create_rustdoc_manifest_for_crate_version(
}
}

fn choose_baseline_version(
crate_: &Crate,
version_current: Option<&semver::Version>,
) -> anyhow::Result<String> {
if let Some(current) = version_current {
let mut instances = crate_
.versions()
.iter()
.filter(|i| !i.is_yanked())
.filter_map(|i| semver::Version::parse(i.version()).ok())
// For unpublished changes when the user doesn't increment the version
// post-release, allow using the current version as a baseline.
.filter(|v| v <= current)
.collect::<Vec<_>>();
instances.sort();
instances
.iter()
.rev()
.find(|v| v.pre.is_empty())
.or_else(|| instances.last())
.map(|v| v.to_string())
.with_context(|| {
anyhow::format_err!(
"No available baseline versions for {}@{}",
crate_.name(),
current
)
})
} else {
let instance = crate_
.highest_normal_version()
.unwrap_or_else(|| crate_.highest_version())
tonowak marked this conversation as resolved.
Show resolved Hide resolved
.version();
Ok(instance.to_owned())
}
}

impl BaselineLoader for RegistryBaseline {
fn load_rustdoc(
&self,
Expand All @@ -259,30 +297,8 @@ impl BaselineLoader for RegistryBaseline {
// - Most likely the user cares about the last official release
mgr0dzicki marked this conversation as resolved.
Show resolved Hide resolved
let base_version = if let Some(base) = self.version.as_ref() {
base.to_string()
} else if let Some(current) = version_current {
let mut instances = crate_
.versions()
.iter()
.filter_map(|i| semver::Version::parse(i.version()).ok())
// For unpublished changes when the user doesn't increment the version
// post-release, allow using the current version as a baseline.
.filter(|v| v <= current)
.collect::<Vec<_>>();
instances.sort();
let instance = instances
.iter()
.rev()
.find(|v| v.pre.is_empty())
.or_else(|| instances.last())
.with_context(|| {
anyhow::format_err!("No available baseline versions for {}@{}", name, current)
})?;
instance.to_string()
} else {
let instance = crate_
.highest_normal_version()
.unwrap_or_else(|| crate_.highest_version());
instance.version().to_owned()
choose_baseline_version(&crate_, version_current)?
};

let base_root = self.target_root.join(format!(
Expand Down Expand Up @@ -340,3 +356,82 @@ fn need_retry(res: Result<(), crates_index::Error>) -> anyhow::Result<bool> {
Err(err) => Err(err.into()),
}
}

#[cfg(test)]
mod tests {
use crates_index::{Crate, Version};

use super::choose_baseline_version;

fn new_mock_version(version_name: &str, yanked: bool) -> Version {
// `crates_index::Version` cannot be created explicitly, as all its fields
// are private, so we use the fact that it can be deserialized.
serde_json::from_value(serde_json::json!({
"name": "test-crate",
"vers": version_name,
"deps": [],
"features": {},
"yanked": yanked,
"cksum": "00".repeat(32),
}))
.unwrap()
}

fn new_crate(versions: &[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()
tonowak marked this conversation as resolved.
Show resolved Hide resolved
}

#[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),
tonowak marked this conversation as resolved.
Show resolved Hide resolved
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]
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]
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]
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()
);
}
}