Skip to content

Commit

Permalink
fix: parsing of >=2.*.* (#1006)
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra authored Jan 4, 2025
1 parent 1cbf7a2 commit d74147d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions crates/rattler_conda_types/src/match_spec/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,8 @@ mod tests {
"./relative/channel::package",
"python[channel=https://conda.anaconda.org/python/conda,version=3.9]",
"channel/win-64::foobar[channel=conda-forge, subdir=linux-64]",
// Issue #1004
"numpy>=2.*.*",
];

let evaluated: IndexMap<_, _> = specs
Expand Down Expand Up @@ -1128,6 +1130,8 @@ mod tests {
// subdir in brackets take precedence
"[version=3.9, subdir=linux-64]",
"==3.9[subdir=linux-64, build_number=\"0\"]",
// Issue #1004
">=2.*.*",
];

let evaluated: IndexMap<_, _> = specs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/rattler_conda_types/src/match_spec/parse.rs
expression: evaluated
snapshot_kind: text
---
blas *.* mkl:
name: blas
Expand Down Expand Up @@ -138,3 +139,6 @@ rust ~=1.2.3:
base_url: "https://conda.anaconda.org/conda-forge"
name: conda-forge
subdir: linux-64
numpy>=2.*.*:
name: numpy
version: ">=2"
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,5 @@ rust ~=1.2.3:
base_url: "https://conda.anaconda.org/conda-forge"
name: conda-forge
subdir: linux-64
numpy>=2.*.*:
error: "invalid version constraint: regex constraints are not supported"
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/rattler_conda_types/src/match_spec/parse.rs
expression: evaluated
snapshot_kind: text
---
2.7|>=3.6:
version: "==2.7|>=3.6"
Expand Down Expand Up @@ -57,3 +58,5 @@ expression: evaluated
op: Eq
rhs: 0
subdir: linux-64
">=2.*.*":
version: ">=2"
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ snapshot_kind: text
op: Eq
rhs: 0
subdir: linux-64
">=2.*.*":
error: "invalid version constraint: regex constraints are not supported"
10 changes: 10 additions & 0 deletions crates/rattler_conda_types/src/version_spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,16 @@ mod tests {
assert!(VersionSpec::from_str("0.2.18.*.", ParseStrictness::Strict).is_err());
}

#[test]
fn issue_1004() {
assert_eq!(
VersionSpec::from_str(">=2.*.*", ParseStrictness::Lenient).unwrap(),
VersionSpec::from_str(">=2", ParseStrictness::Lenient).unwrap()
);

assert!(VersionSpec::from_str("0.2.18.*.*", ParseStrictness::Strict).is_err());
}

#[test]
fn issue_bracket_printing() {
let v = VersionSpec::from_str("(>=1,<2)|>3", ParseStrictness::Lenient).unwrap();
Expand Down
22 changes: 19 additions & 3 deletions crates/rattler_conda_types/src/version_spec/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,14 @@ fn logical_constraint_parser(
}
// The version ends in a wildcard pattern
(
"*" | ".*",
version_remainder,
Some(VersionOperators::Range(
RangeOperator::GreaterEquals | RangeOperator::Greater,
)),
Lenient,
) => VersionOperators::Range(RangeOperator::GreaterEquals),
) if is_star_or_star_dot_star(version_remainder) => {
VersionOperators::Range(RangeOperator::GreaterEquals)
}
(
"*" | ".*",
Some(VersionOperators::Exact(EqualityOperator::NotEquals)),
Expand All @@ -236,7 +238,9 @@ fn logical_constraint_parser(
// even in strict mode we allow this.
VersionOperators::StrictRange(StrictRangeOperator::NotStartsWith)
}
("*" | ".*", Some(op), Lenient) => {
(version_remainder, Some(op), Lenient)
if is_star_or_star_dot_star(version_remainder) =>
{
// In lenient mode we simply ignore the glob.
op
}
Expand Down Expand Up @@ -307,6 +311,18 @@ pub fn looks_like_infinite_starts_with(input: &str) -> bool {
false
}

/// Returns true if the input is `*` or a sequence of `.*`.
pub fn is_star_or_star_dot_star(input: &str) -> bool {
if input == "*" {
return true;
}
let mut input = input;
while let Some(rest) = input.strip_suffix(".*") {
input = rest;
}
input.is_empty()
}

/// Parses a version constraint.
pub fn constraint_parser(
strictness: ParseStrictness,
Expand Down

0 comments on commit d74147d

Please sign in to comment.