Skip to content

fix: Limiting pre-release match semantics to use only on OptVersionReq::Req #14412

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

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/cargo/util/semver_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,17 @@ impl OptVersionReq {
}
}

/// Since Semver does not support prerelease versions,
/// the simplest implementation is taken here without comparing the prerelease section.
/// The logic here is temporary, we'll have to consider more boundary conditions later,
/// and we're not sure if this part of the functionality should be implemented in semver or cargo.
/// An interim approach allows to update to SemVer-Compatible prerelease version.
pub fn matches_prerelease(&self, version: &Version) -> bool {
// Others Non `OptVersionReq::Req` have their own implementation.
if !matches!(self, OptVersionReq::Req(_)) {
return self.matches(version);
}

// TODO: In the future we have a prerelease semantic to be implemented.
if version.is_prerelease() {
let mut version = version.clone();
let mut version: Version = version.clone();
// Ignores the Prerelease tag to unlock the limit of non prerelease unpdate to prerelease.
version.pre = semver::Prerelease::EMPTY;
return self.matches(&version);
}
Expand Down Expand Up @@ -178,7 +182,10 @@ impl From<VersionReq> for OptVersionReq {

#[cfg(test)]
mod matches_prerelease {
use semver::VersionReq;

use super::OptVersionReq;
use super::Version;

#[test]
fn prerelease() {
Expand Down Expand Up @@ -238,4 +245,19 @@ mod matches_prerelease {
assert_eq!(expected, matched, "req: {req}; ver: {ver}");
}
}

#[test]
fn opt_version_req_matches_prerelease() {
let req_ver: VersionReq = "^1.2.3-rc.0".parse().unwrap();
let to_ver: Version = "1.2.3-rc.0".parse().unwrap();

let req = OptVersionReq::Req(req_ver.clone());
assert!(req.matches_prerelease(&to_ver));

let req = OptVersionReq::Locked(to_ver.clone(), req_ver.clone());
assert!(req.matches_prerelease(&to_ver));

let req = OptVersionReq::Precise(to_ver.clone(), req_ver.clone());
assert!(req.matches_prerelease(&to_ver));
}
}