Skip to content

Commit

Permalink
Loosen package script regexp to match spec
Browse files Browse the repository at this point in the history
Fixes #1479
  • Loading branch information
akx committed Feb 16, 2024
1 parent c474370 commit f1eb216
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion crates/install-wheel-rs/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ impl Script {
value: &str,
extras: Option<&[String]>,
) -> Result<Option<Script>, Error> {
let script_regex = Regex::new(r"^(?P<module>[\w\d_\-.]+):(?P<function>[\w\d_\-.]+)(?:\s+\[(?P<extras>(?:[^,]+,?\s*)+)\])?$").unwrap();
// "Within a value, readers must accept and ignore spaces (including multiple consecutive spaces) before or after the colon,
// between the object reference and the left square bracket, between the extra names and the square brackets and colons delimiting them,
// and after the right square bracket."
// – https://packaging.python.org/en/latest/specifications/entry-points/#file-format
let script_regex = Regex::new(r"^(?P<module>[\w\d_\-.]+)\s*:\s*(?P<function>[\w\d_\-.]+)(?:\s*\[\s*(?P<extras>(?:[^,]+,?\s*)+)\])?\s*$").unwrap();

let captures = script_regex
.captures(value)
Expand All @@ -64,3 +68,31 @@ impl Script {
}))
}
}

#[cfg(test)]
mod test {
use crate::Script;

#[test]
fn test_valid_script_names() {
for case in [
"foomod:main",
"foomod:main_bar [bar,baz]",
"pylutron_caseta.cli:lap_pair[cli]",
] {
assert!(Script::from_value("script", case, None).is_ok());
}
}
#[test]
fn test_invalid_script_names() {
for case in [
"", // Empty
":weh", // invalid function
"foomod:main_bar [bar", // extras malformed
"pylutron_caseta", // missing function
"weh:", // invalid function
] {
assert!(Script::from_value("script", case, None).is_err(), "case: {}", case);
}
}
}

0 comments on commit f1eb216

Please sign in to comment.