From fe7b2c24b19f6358aa844b39c730ede7dbdeba7e Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Tue, 18 Jul 2023 23:28:53 +0530 Subject: [PATCH] Initial test :) --- .../test/fixtures/jupyter/line_magics.ipynb | 41 +++++++++++++++++++ .../jupyter/line_magics_expected.ipynb | 40 ++++++++++++++++++ crates/ruff/src/jupyter/notebook.rs | 12 ++++++ ...jupyter__notebook__tests__line_magics.snap | 21 ++++++++++ 4 files changed, 114 insertions(+) create mode 100644 crates/ruff/resources/test/fixtures/jupyter/line_magics.ipynb create mode 100644 crates/ruff/resources/test/fixtures/jupyter/line_magics_expected.ipynb create mode 100644 crates/ruff/src/jupyter/snapshots/ruff__jupyter__notebook__tests__line_magics.snap diff --git a/crates/ruff/resources/test/fixtures/jupyter/line_magics.ipynb b/crates/ruff/resources/test/fixtures/jupyter/line_magics.ipynb new file mode 100644 index 0000000000000..785a73102ad73 --- /dev/null +++ b/crates/ruff/resources/test/fixtures/jupyter/line_magics.ipynb @@ -0,0 +1,41 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "eab4754a-d6df-4b41-8ee8-7e23aef440f9", + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "\n", + "%matplotlib inline\n", + "\n", + "import os\n", + "\n", + "_ = math.pi" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python (ruff)", + "language": "python", + "name": "ruff" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/crates/ruff/resources/test/fixtures/jupyter/line_magics_expected.ipynb b/crates/ruff/resources/test/fixtures/jupyter/line_magics_expected.ipynb new file mode 100644 index 0000000000000..cdf69fa719600 --- /dev/null +++ b/crates/ruff/resources/test/fixtures/jupyter/line_magics_expected.ipynb @@ -0,0 +1,40 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "cad32845-44f9-4a53-8b8c-a6b1bb3f3378", + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "\n", + "%matplotlib inline\n", + "\n", + "\n", + "_ = math.pi" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python (ruff)", + "language": "python", + "name": "ruff" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/crates/ruff/src/jupyter/notebook.rs b/crates/ruff/src/jupyter/notebook.rs index 0bee249c95ff5..227e42871e1f0 100644 --- a/crates/ruff/src/jupyter/notebook.rs +++ b/crates/ruff/src/jupyter/notebook.rs @@ -552,6 +552,18 @@ print("after empty cells") Ok(()) } + #[test] + fn test_line_magics() -> Result<()> { + let path = "line_magics.ipynb".to_string(); + let (diagnostics, source_kind) = test_notebook_path( + &path, + Path::new("line_magics_expected.ipynb"), + &settings::Settings::for_rule(Rule::UnusedImport), + )?; + assert_messages!(diagnostics, path, source_kind); + Ok(()) + } + #[test] fn test_json_consistency() -> Result<()> { let path = "before_fix.ipynb".to_string(); diff --git a/crates/ruff/src/jupyter/snapshots/ruff__jupyter__notebook__tests__line_magics.snap b/crates/ruff/src/jupyter/snapshots/ruff__jupyter__notebook__tests__line_magics.snap new file mode 100644 index 0000000000000..576b938b6b2e0 --- /dev/null +++ b/crates/ruff/src/jupyter/snapshots/ruff__jupyter__notebook__tests__line_magics.snap @@ -0,0 +1,21 @@ +--- +source: crates/ruff/src/jupyter/notebook.rs +--- +line_magics.ipynb:cell 1:5:8: F401 [*] `os` imported but unused + | +3 | %matplotlib inline +4 | +5 | import os + | ^^ F401 + | + = help: Remove unused import: `os` + +ℹ Fix +2 2 | +3 3 | %matplotlib inline +4 4 | +5 |-import os +6 5 | +7 6 | _ = math.pi + +