Skip to content

Commit

Permalink
Remove all quotes from (lenient) version specifiers (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin authored Dec 28, 2023
1 parent 0ebff94 commit 7bf2790
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions crates/pypi-types/src/lenient_requirement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ static INVALID_TRAILING_DOT_STAR: Lazy<Regex> =
static MISSING_DOT: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\d\.\d)+\*").unwrap());
/// Ex) `>=3.6,`
static TRAILING_COMMA: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\d\.(\d|\*))+,$").unwrap());
/// Ex) `>= '2.7'`
static INVALID_QUOTES: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"((?:~=|==|!=|<=|>=|<|>|===) )*['"](\d(?:\.\d)*)['"]"#).unwrap());
/// Ex) `>= '2.7'`, `>=3.6'`
static STRAY_QUOTES: Lazy<Regex> = Lazy::new(|| Regex::new(r#"['"]"#).unwrap());

/// Regex to match the invalid specifier, replacement to fix it and message about was wrong and
/// fixed
Expand All @@ -45,7 +44,7 @@ static FIXUPS: &[(&Lazy<Regex>, &str, &str)] = &[
// Given `>=3.6,`, rewrite to `>=3.6`
(&TRAILING_COMMA, r"${1}", "removing trailing comma"),
// Given `>= '2.7'`, rewrite to `>= 2.7`
(&INVALID_QUOTES, r"${1}${2}", "removing invalid quotes"),
(&STRAY_QUOTES, r"", "removing stray quotes"),
];

fn parse_with_fixups<Err, T: FromStr<Err = Err>>(input: &str, type_name: &str) -> Result<T, Err> {
Expand Down Expand Up @@ -283,4 +282,21 @@ mod tests {
VersionSpecifiers::from_str(">=2.7,!=3.0.*,!=3.1.*,<3.4").unwrap();
assert_eq!(actual, expected);
}

/// <https://pypi.org/simple/algoliasearch/?format=application/vnd.pypi.simple.v1+json>
/// <https://pypi.org/simple/okta/?format=application/vnd.pypi.simple.v1+json>
#[test]
fn stray_quote() {
let actual: VersionSpecifiers =
LenientVersionSpecifiers::from_str(">=2.7, !=3.0.*, !=3.1.*', !=3.2.*, !=3.3.*'")
.unwrap()
.into();
let expected: VersionSpecifiers =
VersionSpecifiers::from_str(">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*").unwrap();
assert_eq!(actual, expected);
let actual: VersionSpecifiers =
LenientVersionSpecifiers::from_str(">=3.6'").unwrap().into();
let expected: VersionSpecifiers = VersionSpecifiers::from_str(">=3.6").unwrap();
assert_eq!(actual, expected);
}
}

0 comments on commit 7bf2790

Please sign in to comment.