Skip to content

Commit

Permalink
Merge pull request #115 from mgeisler/check-more-comparators
Browse files Browse the repository at this point in the history
Check version numbers in `=1.2.3` and `>1.2.3`, and `>=1.2.3`
  • Loading branch information
mgeisler authored Nov 28, 2021
2 parents 4e73b72 + 5d1e368 commit c55e8c2
Showing 1 changed file with 79 additions and 30 deletions.
109 changes: 79 additions & 30 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,41 @@ pub fn version_matches_request(
request: &semver::VersionReq,
) -> Result<()> {
use semver::Op;
if request.comparators.len() != 1 {
// Can only handle simple dependencies
return Ok(());
}

let comparator = &request.comparators[0];
match comparator.op {
Op::Tilde | Op::Caret => {
if comparator.major != version.major {
return Err(format!(
"expected major version {}, found {}",
version.major, comparator.major,
));
}
if let Some(minor) = comparator.minor {
if minor != version.minor {
for comparator in &request.comparators {
match comparator.op {
Op::Tilde | Op::Caret | Op::Exact | Op::Greater | Op::GreaterEq | Op::Wildcard => {
if comparator.major != version.major {
return Err(format!(
"expected minor version {}, found {}",
version.minor, minor
"expected major version {}, found {}",
version.major, comparator.major,
));
}
}
if let Some(patch) = comparator.patch {
if patch != version.patch {
if let Some(minor) = comparator.minor {
if minor != version.minor {
return Err(format!(
"expected minor version {}, found {}",
version.minor, minor
));
}
}
if let Some(patch) = comparator.patch {
if patch != version.patch {
return Err(format!(
"expected patch version {}, found {}",
version.patch, patch
));
}
}
if comparator.pre != version.pre {
return Err(format!(
"expected patch version {}, found {}",
version.patch, patch
"expected pre-release \"{}\", found \"{}\"",
version.pre, comparator.pre
));
}
}
if comparator.pre != version.pre {
return Err(format!(
"expected pre-release \"{}\", found \"{}\"",
version.pre, comparator.pre
));
}
_ => {} // We cannot check other operators.
}
_ => return Ok(()), // We cannot check other operators.
}

Ok(())
Expand All @@ -109,20 +106,69 @@ mod tests {
let version = Version::parse("1.2.3").unwrap();
let request = VersionReq::parse("1.2.3").unwrap();
assert_eq!(version_matches_request(&version, &request), Ok(()));

let request = VersionReq::parse("1.2.0").unwrap();
assert!(version_matches_request(&version, &request).is_err());
}

#[test]
fn compatible() {
let version = Version::parse("1.2.3").unwrap();
let request = VersionReq::parse("^1.2.3").unwrap();
assert_eq!(version_matches_request(&version, &request), Ok(()));

let request = VersionReq::parse("^1.2.0").unwrap();
assert!(version_matches_request(&version, &request).is_err());
}

#[test]
fn tilde() {
let version = Version::parse("1.2.3").unwrap();
let request = VersionReq::parse("~1.2.3").unwrap();
assert_eq!(version_matches_request(&version, &request), Ok(()));

let request = VersionReq::parse("~1.2.0").unwrap();
assert!(version_matches_request(&version, &request).is_err());
}

#[test]
fn exact() {
let version = Version::parse("1.2.3").unwrap();
let request = VersionReq::parse("=1.2.3").unwrap();
assert_eq!(version_matches_request(&version, &request), Ok(()));

let request = VersionReq::parse("=1.2.0").unwrap();
assert!(version_matches_request(&version, &request).is_err());
}

#[test]
fn greater_or_equal() {
let version = Version::parse("1.2.3").unwrap();
let request = VersionReq::parse(">=1.2.3").unwrap();
assert_eq!(version_matches_request(&version, &request), Ok(()));

let request = VersionReq::parse(">=1.2.0").unwrap();
assert!(version_matches_request(&version, &request).is_err());
}

#[test]
fn wildcard() {
let version = Version::parse("1.2.3").unwrap();
let request = VersionReq::parse("1.2.*").unwrap();
assert_eq!(version_matches_request(&version, &request), Ok(()));

let request = VersionReq::parse("1.3.*").unwrap();
assert!(version_matches_request(&version, &request).is_err());
}

#[test]
fn greater() {
let version = Version::parse("1.2.3").unwrap();
let request = VersionReq::parse(">1.2.3").unwrap();
assert_eq!(version_matches_request(&version, &request), Ok(()));

let request = VersionReq::parse(">1.2.0").unwrap();
assert!(version_matches_request(&version, &request).is_err());
}

#[test]
Expand All @@ -140,10 +186,13 @@ mod tests {
}

#[test]
fn multiple_predicates() {
fn multiple_comparators() {
let version = Version::parse("1.2.3").unwrap();
let request = VersionReq::parse(">= 1.2.3, < 2.0").unwrap();
assert_eq!(version_matches_request(&version, &request), Ok(()));

let request = VersionReq::parse(">= 1.2.0, < 2.0").unwrap();
assert!(version_matches_request(&version, &request).is_err());
}

#[test]
Expand Down

0 comments on commit c55e8c2

Please sign in to comment.