Skip to content
Merged
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
58 changes: 55 additions & 3 deletions crates/uv-requirements-txt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,22 @@ const fn is_terminal(c: char) -> bool {
matches!(c, '\n' | '\r' | '#')
}

/// Eat an option name if it is followed by a token boundary.
fn eat_option(s: &mut Scanner, option: &str) -> bool {
let Some(remainder) = s.after().strip_prefix(option) else {
return false;
};
if remainder
.chars()
.next()
.is_none_or(|char| char.is_whitespace() || matches!(char, '=' | '#'))
{
s.eat_if(option)
} else {
false
}
}

/// Parse a single entry, that is a requirement, an inclusion or a comment line.
///
/// Consumes all preceding trivia (whitespace and comments). If it returns `None`, we've reached
Expand Down Expand Up @@ -773,7 +789,7 @@ fn parse_entry(
})?
};
RequirementsTxtStatement::ExtraIndexUrl(url.with_given(given))
} else if s.eat_if("--no-index") {
} else if eat_option(s, "--no-index") {
RequirementsTxtStatement::NoIndex
} else if s.eat_if("--require-hashes") {
RequirementsTxtStatement::RequireHashes
Expand Down Expand Up @@ -882,7 +898,8 @@ fn parse_entry(
})
} else if let Some(char) = s.peek() {
// Identify an unsupported option, like `--trusted-host`.
if let Some(option) = UnsupportedOption::iter().find(|option| s.eat_if(option.name())) {
if let Some(option) = UnsupportedOption::iter().find(|option| eat_option(s, option.name()))
{
s.eat_while(|c: char| !is_terminal(c));
RequirementsTxtStatement::UnsupportedOption(option)
} else {
Expand Down Expand Up @@ -1510,7 +1527,7 @@ mod test {

use uv_fs::Simplified;

use crate::{RequirementsTxt, calculate_row_column};
use crate::{RequirementsTxt, calculate_row_column, eat_option};

fn workspace_test_data_dir() -> PathBuf {
Path::new("./test-data").simple_canonicalize().unwrap()
Expand Down Expand Up @@ -2866,6 +2883,41 @@ mod test {
Ok(())
}

#[test_case("--no-indexx"; "no index")]
#[test_case("--prefoo"; "pre")]
#[test_case("--trusted-hostile"; "trusted host")]
#[tokio::test]
async fn malformed_option(content: &str) -> Result<()> {
let temp_dir = assert_fs::TempDir::new()?;
let requirements_txt = temp_dir.child("requirements.txt");
requirements_txt.write_str(content)?;

let error = RequirementsTxt::parse(requirements_txt.path(), temp_dir.path())
.await
.unwrap_err();
let errors = anyhow::Error::new(error).chain().join("\n");

let requirement_txt = regex::escape(&requirements_txt.path().user_display().to_string());
let filters = vec![(requirement_txt.as_str(), "<REQUIREMENTS_TXT>")];
insta::with_settings!({
filters => filters
}, {
insta::assert_snapshot!(errors, @"Unexpected '-', expected '-c', '-e', '-r' or the start of a requirement: <REQUIREMENTS_TXT>:1:1");
});

Ok(())
}

#[test_case("--no-index", "--no-index", ""; "end of file")]
#[test_case("--no-index ", "--no-index", " "; "whitespace")]
#[test_case("--trusted-host=example.com", "--trusted-host", "=example.com"; "equals")]
#[test_case("--no-index# comment", "--no-index", "# comment"; "comment")]
fn option_boundary(input: &str, option: &str, remainder: &str) {
let mut scanner = Scanner::new(input);
assert!(eat_option(&mut scanner, option));
assert_eq!(scanner.after(), remainder);
}

#[test_case("numpy>=1,<2\n @-broken\ntqdm", "2:4"; "ASCII Character with LF")]
#[test_case("numpy>=1,<2\r\n #-broken\ntqdm", "2:4"; "ASCII Character with CRLF")]
#[test_case("numpy>=1,<2\n \n-broken\ntqdm", "3:1"; "ASCII Character LF then LF")]
Expand Down
Loading
⌂ 换网址