mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-21 01:29:56 +00:00
Prevent invalid utf8 indexing in cell magic detection (#9146)
The example below used to panic because we tried to split at 2 bytes in the 4-bytes character `转`. ```python def sample_func(xx): """ 转置 (transpose) """ return xx.T ``` Fixes #9145 Fixes https://github.com/astral-sh/ruff-vscode/issues/362 The second commit is a small test refactoring.
This commit is contained in:
parent
3ce145c476
commit
cd3c2f773f
3 changed files with 72 additions and 54 deletions
|
@ -421,17 +421,18 @@ mod tests {
|
|||
));
|
||||
}
|
||||
|
||||
#[test_case(Path::new("markdown.json"), false; "markdown")]
|
||||
#[test_case(Path::new("only_magic.json"), true; "only_magic")]
|
||||
#[test_case(Path::new("code_and_magic.json"), true; "code_and_magic")]
|
||||
#[test_case(Path::new("only_code.json"), true; "only_code")]
|
||||
#[test_case(Path::new("cell_magic.json"), false; "cell_magic")]
|
||||
#[test_case(Path::new("valid_cell_magic.json"), true; "valid_cell_magic")]
|
||||
#[test_case(Path::new("automagic.json"), false; "automagic")]
|
||||
#[test_case(Path::new("automagics.json"), false; "automagics")]
|
||||
#[test_case(Path::new("automagic_before_code.json"), false; "automagic_before_code")]
|
||||
#[test_case(Path::new("automagic_after_code.json"), true; "automagic_after_code")]
|
||||
fn test_is_valid_code_cell(path: &Path, expected: bool) -> Result<()> {
|
||||
#[test_case("markdown", false)]
|
||||
#[test_case("only_magic", true)]
|
||||
#[test_case("code_and_magic", true)]
|
||||
#[test_case("only_code", true)]
|
||||
#[test_case("cell_magic", false)]
|
||||
#[test_case("valid_cell_magic", true)]
|
||||
#[test_case("automagic", false)]
|
||||
#[test_case("automagics", false)]
|
||||
#[test_case("automagic_before_code", false)]
|
||||
#[test_case("automagic_after_code", true)]
|
||||
#[test_case("unicode_magic_gh9145", true)]
|
||||
fn test_is_valid_code_cell(cell: &str, expected: bool) -> Result<()> {
|
||||
/// Read a Jupyter cell from the `resources/test/fixtures/jupyter/cell` directory.
|
||||
fn read_jupyter_cell(path: impl AsRef<Path>) -> Result<Cell> {
|
||||
let path = notebook_path("cell").join(path);
|
||||
|
@ -439,7 +440,10 @@ mod tests {
|
|||
Ok(serde_json::from_str(&source_code)?)
|
||||
}
|
||||
|
||||
assert_eq!(read_jupyter_cell(path)?.is_valid_code_cell(), expected);
|
||||
assert_eq!(
|
||||
read_jupyter_cell(format!("{cell}.json"))?.is_valid_code_cell(),
|
||||
expected
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue