Merge pull request #106 from nymtech/hotfix/semver_compatibility

Hotfix/semver compatibility
This commit is contained in:
Dave Hrycyszyn
2020-01-28 12:28:22 +00:00
committed by GitHub
+8 -6
View File
@@ -1,5 +1,4 @@
use semver::Version;
use semver::VersionReq;
/// Checks whether given `version` is compatible with a given semantic version requirement `req`
/// according to major-minor semver rules. The semantic version requirement can be passed as a full,
@@ -7,18 +6,16 @@ use semver::VersionReq;
/// The patch number in the requirement gets dropped and replaced with a wildcard (0.3.*) as all
/// minor versions should be compatible with each other.
pub fn is_minor_version_compatible(version: &str, req: &str) -> bool {
let version = match Version::parse(version) {
let expected_version = match Version::parse(version) {
Ok(v) => v,
Err(_) => return false,
};
let tmp = match Version::parse(req) {
let req_version = match Version::parse(req) {
Ok(v) => v,
Err(_) => return false,
};
let wildcard = format!("{}.{}.*", tmp.major, tmp.minor).to_string();
let semver_requirement = VersionReq::parse(&wildcard).expect("panicked on semver requirement parsing. This should never happen as inputs should already have been sanitized.");
semver_requirement.matches(&version)
expected_version.major == req_version.major && expected_version.minor == req_version.minor
}
#[cfg(test)]
@@ -55,6 +52,11 @@ mod tests {
assert!(!is_minor_version_compatible("1.3.2", "0.3.2"));
}
#[test]
fn version_0_4_0_rc_1_is_compatible_with_version_0_4_0_rc_1() {
assert!(is_minor_version_compatible("0.4.0-rc.1", "0.4.0-rc.1"));
}
#[test]
fn returns_false_on_foo_version() {
assert!(!is_minor_version_compatible("foo", "0.3.2"));