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

ignore trailing .0 in version comparison #196

Merged
merged 5 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
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
85 changes: 78 additions & 7 deletions crates/rattler_conda_types/src/version/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,13 @@ mod parse;
/// this problem by appending an underscore to plain version numbers:
///
/// 1.0.1_ < 1.0.1a => True # ensure correct ordering for openssl
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq)]
pub struct Version {
/// The epoch of this version. This is an optional number that can be used to indicate a change
/// in the versioning scheme.
/// A normed copy of the original version string trimmed and converted to lower case.
/// Also dashes are replaced with underscores if the version string does not contain
/// any underscores.
norm: String,
/// The version of this version. This is the actual version string.
/// The version of this version. This is the actual version string, including the epoch.
version: VersionComponent,
/// The local version of this version (everything following a `+`).
/// This is an optional string that can be used to indicate a local version of a package.
Expand Down Expand Up @@ -175,8 +176,6 @@ impl PartialEq<Self> for Version {
}
}

impl Eq for Version {}

impl Hash for Version {
fn hash<H: Hasher>(&self, state: &mut H) {
self.version.hash(state);
Expand Down Expand Up @@ -262,7 +261,7 @@ impl Display for NumeralOrOther {
}
}

#[derive(Default, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[derive(Default, Clone, Eq, Serialize, Deserialize)]
struct VersionComponent {
components: SmallVec<[NumeralOrOther; 4]>,
ranges: SmallVec<[Range<usize>; 4]>,
Expand Down Expand Up @@ -313,6 +312,20 @@ impl VersionComponent {
}
}

impl Hash for VersionComponent {
fn hash<H: Hasher>(&self, state: &mut H) {
let default = NumeralOrOther::default();
for range in self.ranges.iter().cloned() {
// skip trailing default components
self.components[range]
.iter()
.rev()
.skip_while(|c| **c == default)
.for_each(|c| c.hash(state));
}
}
}

impl Debug for VersionComponent {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "[")?;
Expand All @@ -333,6 +346,34 @@ impl Debug for VersionComponent {
}
}

impl PartialEq for VersionComponent {
fn eq(&self, other: &Self) -> bool {
for ranges in self
.ranges
.iter()
.cloned()
.zip_longest(other.ranges.iter().cloned())
{
let (a_range, b_range) = ranges.or_default();
let default = NumeralOrOther::default();
for components in self.components[a_range]
.iter()
.zip_longest(other.components[b_range].iter())
{
let (a_component, b_component) = match components {
EitherOrBoth::Left(l) => (l, &default),
EitherOrBoth::Right(r) => (&default, r),
EitherOrBoth::Both(l, r) => (l, r),
};
if a_component != b_component {
return false;
}
}
}
true
}
}

impl Ord for VersionComponent {
fn cmp(&self, other: &Self) -> Ordering {
for ranges in self
Expand Down Expand Up @@ -403,6 +444,9 @@ mod test {
use std::cmp::Ordering;
use std::str::FromStr;

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use rand::seq::SliceRandom;

use super::Version;
Expand Down Expand Up @@ -608,4 +652,31 @@ mod test {
.unwrap()
.starts_with(&Version::from_str("1.2").unwrap()));
}

fn get_hash(spec: &Version) -> u64 {
let mut s = DefaultHasher::new();
spec.hash(&mut s);
s.finish()
}

#[test]
fn hash() {
let v1 = Version::from_str("1.2.0").unwrap();

let vx2 = Version::from_str("1.2.0").unwrap();
assert_eq!(get_hash(&v1), get_hash(&vx2));
let vx2 = Version::from_str("1.2.0.0.0").unwrap();
assert_eq!(get_hash(&v1), get_hash(&vx2));
let vx2 = Version::from_str("1!1.2.0").unwrap();
assert_ne!(get_hash(&v1), get_hash(&vx2));

let vx2 = Version::from_str("1.2.0+post1").unwrap();
assert_ne!(get_hash(&v1), get_hash(&vx2));

let vx1 = Version::from_str("1.2+post1").unwrap();
assert_eq!(get_hash(&vx1), get_hash(&vx2));

let v2 = Version::from_str("1.2.3").unwrap();
assert_ne!(get_hash(&v1), get_hash(&v2));
}
}
24 changes: 24 additions & 0 deletions crates/rattler_conda_types/src/version_spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ mod tests {
))
);
}

#[test]
fn test_group() {
assert_eq!(
Expand Down Expand Up @@ -300,4 +301,27 @@ mod tests {
))
);
}

#[test]
fn test_matches() {
let v1 = Version::from_str("1.2.0").unwrap();
let vs1 = VersionSpec::from_str(">=1.2.3,<2.0.0").unwrap();
assert!(!vs1.matches(&v1));

let vs2 = VersionSpec::from_str("1.2").unwrap();
assert!(vs2.matches(&v1));

let v2 = Version::from_str("1.2.3").unwrap();
assert!(vs1.matches(&v2));
assert!(!vs2.matches(&v2));

let v3 = Version::from_str("1!1.2.3").unwrap();
println!("{:?}", v3);

assert!(!vs1.matches(&v3));
assert!(!vs2.matches(&v3));

let vs3 = VersionSpec::from_str(">=1!1.2,<1!2").unwrap();
assert!(vs3.matches(&v3));
}
}