Skip to content

Commit

Permalink
Return both original and updated SourceKind in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Jul 27, 2023
1 parent 4ce5c28 commit b122d14
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
6 changes: 3 additions & 3 deletions crates/ruff/src/jupyter/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ print("after empty cells")
#[test]
fn test_import_sorting() -> Result<()> {
let path = "isort.ipynb".to_string();
let (diagnostics, source_kind) = test_notebook_path(
let (diagnostics, source_kind, _) = test_notebook_path(
&path,
Path::new("isort_expected.ipynb"),
&settings::Settings::for_rule(Rule::UnsortedImports),
Expand All @@ -553,7 +553,7 @@ print("after empty cells")
#[test]
fn test_line_magics() -> Result<()> {
let path = "line_magics.ipynb".to_string();
let (diagnostics, source_kind) = test_notebook_path(
let (diagnostics, source_kind, _) = test_notebook_path(
&path,
Path::new("line_magics_expected.ipynb"),
&settings::Settings::for_rule(Rule::UnusedImport),
Expand All @@ -565,7 +565,7 @@ print("after empty cells")
#[test]
fn test_json_consistency() -> Result<()> {
let path = "before_fix.ipynb".to_string();
let (_, source_kind) = test_notebook_path(
let (_, _, source_kind) = test_notebook_path(
path,
Path::new("after_fix.ipynb"),
&settings::Settings::for_rule(Rule::UnusedImport),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,16 @@ isort.ipynb:cell 2:1:1: I001 [*] Import block is un-sorted or un-formatted
7 9 | def foo():
8 10 | pass

isort.ipynb:cell 2:6:1: I001 [*] Import block is un-sorted or un-formatted
|
4 | def foo():
5 | pass
6 | / from pathlib import Path
7 | | import sys
8 | |
9 | | %matplotlib \
| |_^ I001
10 | --inline
|
= help: Organize imports
isort.ipynb:cell 3:1:1: I001 [*] Import block is un-sorted or un-formatted
|
1 | / from pathlib import Path
2 | | import sys
3 | |
4 | | %matplotlib \
| |_^ I001
5 | --inline
|
= help: Organize imports

Fix
6 6 | # Newline should be added here
Expand All @@ -71,12 +69,12 @@ isort.ipynb:cell 2:6:1: I001 [*] Import block is un-sorted or un-formatted
12 12 | %matplotlib \
13 13 | --inline

isort.ipynb:cell 3:5:1: I001 [*] Import block is un-sorted or un-formatted
isort.ipynb:cell 3:7:1: I001 [*] Import block is un-sorted or un-formatted
|
3 | --inline
4 |
5 | / import math
6 | | import abc
5 | --inline
6 |
7 | / import math
8 | | import abc
|
= help: Organize imports

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ line_magics.ipynb:cell 1:5:8: F401 [*] `os` imported but unused
4 |
5 | import os
| ^^ F401
6 |
7 | _ = math.pi
|
= help: Remove unused import: `os`

Expand Down
5 changes: 3 additions & 2 deletions crates/ruff/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ pub(crate) fn test_notebook_path(
path: impl AsRef<Path>,
expected: impl AsRef<Path>,
settings: &Settings,
) -> Result<(Vec<Message>, SourceKind)> {
) -> Result<(Vec<Message>, SourceKind, SourceKind)> {
let mut source_kind = SourceKind::Jupyter(read_jupyter_notebook(path.as_ref())?);
let original_source_kind = source_kind.clone();
let messages = test_contents(&mut source_kind, path.as_ref(), settings);
let expected_notebook = read_jupyter_notebook(expected.as_ref())?;
if let SourceKind::Jupyter(notebook) = &source_kind {
assert_eq!(notebook.cell_offsets(), expected_notebook.cell_offsets());
assert_eq!(notebook.index(), expected_notebook.index());
assert_eq!(notebook.content(), expected_notebook.content());
};
Ok((messages, source_kind))
Ok((messages, original_source_kind, source_kind))
}

/// Run [`check_path`] on a snippet of Python code.
Expand Down

0 comments on commit b122d14

Please sign in to comment.