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
173 changes: 147 additions & 26 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,49 @@ fn create_rustdoc_manifest_for_crate_version(
}
}

fn choose_baseline_version(
crate_: &Crate,
version_current: Option<&semver::Version>,
) -> anyhow::Result<String> {
// Try to avoid pre-releases
// - Breaking changes are allowed between them
// - Most likely the user cares about the last official release
if let Some(current) = version_current {
let mut instances = crate_
.versions()
.iter()
.filter_map(|i| {
semver::Version::parse(i.version())
.map(|v| (v, i.is_yanked()))
.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, yanked)| v.pre.is_empty() && !yanked)
.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 @@ -254,35 +298,11 @@ impl BaselineLoader for RegistryBaseline {
.index
.crate_(name)
.with_context(|| anyhow::format_err!("{} not found in registry", name))?;
// Try to avoid pre-releases
// - Breaking changes are allowed between them
// - Most likely the user cares about the last official release

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 +360,104 @@ 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),
}))
.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: 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 }))
.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
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() {
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() {
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
test_choose_baseline_version(
vec![("1.2.0", false), ("1.2.1-rc1", false), ("1.3.1", true)],
None,
"1.2.0",
);
}

#[test]
fn choose_baseline_version_no_normal_largest_yanked() {
test_choose_baseline_version(
vec![("1.2.0", true), ("1.2.1-rc1", false)],
Some("1.2.1"),
"1.2.1-rc1",
);
}

#[test]
fn choose_baseline_version_no_normal_largest_pre_release() {
test_choose_baseline_version(
vec![("1.2.1-rc1", false), ("1.2.1", true)],
Some("1.2.1"),
"1.2.1",
);
}
}