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

feat: Match GenericVirtualPackage with MatchSpec #1016

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Changes from 1 commit
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
79 changes: 78 additions & 1 deletion crates/rattler_conda_types/src/match_spec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{build_spec::BuildNumberSpec, PackageName, PackageRecord, RepoDataRecord, VersionSpec};
use crate::{
build_spec::BuildNumberSpec, GenericVirtualPackage, PackageName, PackageRecord, RepoDataRecord,
VersionSpec,
};
use rattler_digest::{serde::SerializableHash, Md5Hash, Sha256Hash};
use serde::{Deserialize, Deserializer, Serialize};
use serde_with::{serde_as, skip_serializing_none, DisplayFromStr};
Expand Down Expand Up @@ -227,6 +230,15 @@ impl MatchSpec {
},
)
}

/// Returns whether the package is a virtual package.
/// This is determined by the package name starting with `__`.
/// Not having a package name is considered not virtual.
pub fn is_virtual(&self) -> bool {
self.name
.as_ref()
.map_or(false, |name| name.as_normalized().starts_with("__"))
}
}

// Enable constructing a match spec from a package name.
Expand Down Expand Up @@ -481,8 +493,33 @@ impl Matches<RepoDataRecord> for NamelessMatchSpec {
}
}

impl Matches<GenericVirtualPackage> for MatchSpec {
/// Match a [`MatchSpec`] against a [`GenericVirtualPackage`]
fn matches(&self, other: &GenericVirtualPackage) -> bool {
if let Some(name) = self.name.as_ref() {
if name != &other.name {
return false;
}
}

if let Some(spec) = self.version.as_ref() {
if !spec.matches(&other.version) {
return false;
}
}

if let Some(build_string) = self.build.as_ref() {
if !build_string.matches(&other.build_string) {
return false;
}
}
true
}
}

#[cfg(test)]
mod tests {
use rstest::rstest;
use std::str::FromStr;

use rattler_digest::{parse_digest_from_hex, Md5, Sha256};
Expand Down Expand Up @@ -716,4 +753,44 @@ mod tests {
.collect::<Vec<String>>()
.join("\n"));
}

#[rstest]
#[case("foo >=1.0 py37_0", true)]
#[case("foo >=1.0 py37*", true)]
#[case("foo 1.0.* py38*", false)]
#[case("foo * py37_1", false)]
#[case("foo ==1.0", true)]
#[case("foo >=2.0", false)]
#[case("foo >=1.0", true)]
#[case("foo", true)]
#[case("bar", false)]
fn test_match_generic_virtual_package(#[case] spec_str: &str, #[case] expected: bool) {
let virtual_package = crate::GenericVirtualPackage {
name: PackageName::new_unchecked("foo"),
version: Version::from_str("1.0").unwrap(),
build_string: String::from("py37_0"),
};

let spec = MatchSpec::from_str(spec_str, Strict).unwrap();
assert_eq!(spec.matches(&virtual_package), expected);
}

#[test]
fn test_is_virtual() {
let spec = MatchSpec::from_str("non_virtual_name", Strict).unwrap();
assert!(!spec.is_virtual());

let spec = MatchSpec::from_str("__virtual_name", Strict).unwrap();
assert!(spec.is_virtual());

let spec = MatchSpec::from_str("non_virtual_name >=12", Strict).unwrap();
assert!(!spec.is_virtual());

let spec = MatchSpec::from_str("__virtual_name >=12", Strict).unwrap();
assert!(spec.is_virtual());

let spec =
MatchSpec::from_nameless(NamelessMatchSpec::from_str(">=12", Strict).unwrap(), None);
assert!(!spec.is_virtual());
}
}
Loading