|
1 | 1 | """Check :code:`flake8` works as intended.""" |
2 | 2 |
|
3 | 3 | import os |
| 4 | +from pathlib import Path |
4 | 5 | from textwrap import dedent |
5 | 6 | from typing import TYPE_CHECKING |
6 | 7 |
|
@@ -91,3 +92,49 @@ def test_cell_with_all_magics(capsys: "CaptureFixture") -> None: |
91 | 92 | out, err = capsys.readouterr() |
92 | 93 | assert out == "" |
93 | 94 | assert err == "" |
| 95 | + |
| 96 | + |
| 97 | +def test_per_file_ignores( |
| 98 | + tmp_notebook_for_testing: Path, capsys: "CaptureFixture" |
| 99 | +) -> None: |
| 100 | + """ |
| 101 | + Check flake8 per-file-ignore patterns work. |
| 102 | +
|
| 103 | + Parameters |
| 104 | + ---------- |
| 105 | + tmp_notebook_for_testing |
| 106 | + notebook Path to test |
| 107 | + capsys |
| 108 | + Pytest fixture to capture stdout and stderr. |
| 109 | + """ |
| 110 | + # enable per-file ignores with nbqa glob |
| 111 | + flake8_ini = Path(".flake8") |
| 112 | + flake8_ini.write_text( |
| 113 | + dedent( |
| 114 | + """ |
| 115 | + [flake8] |
| 116 | + per-file-ignores = |
| 117 | + **/*.ipynb: E402 |
| 118 | + **/*nbqa_ipynb.py: E402 |
| 119 | + """ |
| 120 | + ), |
| 121 | + encoding="utf-8", |
| 122 | + ) |
| 123 | + |
| 124 | + main(["flake8", str(tmp_notebook_for_testing)]) |
| 125 | + flake8_ini.unlink() |
| 126 | + |
| 127 | + expected_path_0 = os.path.join("tests", "data", "notebook_for_testing.ipynb") |
| 128 | + |
| 129 | + out, err = capsys.readouterr() |
| 130 | + expected_out = dedent( |
| 131 | + f"""\ |
| 132 | + {expected_path_0}:cell_1:1:1: F401 'os' imported but unused |
| 133 | + {expected_path_0}:cell_1:3:1: F401 'glob' imported but unused |
| 134 | + {expected_path_0}:cell_1:5:1: F401 'nbqa' imported but unused |
| 135 | + {expected_path_0}:cell_2:19:9: W291 trailing whitespace |
| 136 | + {expected_path_0}:cell_4:1:1: F401 'random.randint' imported but unused |
| 137 | + """ |
| 138 | + ) |
| 139 | + assert err == "" |
| 140 | + assert sorted(out.splitlines()) == sorted(expected_out.splitlines()) |
0 commit comments