-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
test_ruff_works.py
129 lines (104 loc) · 4.02 KB
/
test_ruff_works.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""Check :code:`ruff` works as intended."""
import os
from pathlib import Path
from typing import TYPE_CHECKING
import pytest
from nbqa.__main__ import main
if TYPE_CHECKING:
from _pytest.capture import CaptureFixture
@pytest.mark.parametrize(
"path_0, path_1, path_2",
(
(
os.path.abspath(
os.path.join("tests", "data", "notebook_for_testing.ipynb")
),
os.path.abspath(
os.path.join("tests", "data", "notebook_for_testing_copy.ipynb")
),
os.path.abspath(
os.path.join("tests", "data", "notebook_starting_with_md.ipynb")
),
),
(
os.path.join("tests", "data", "notebook_for_testing.ipynb"),
os.path.join("tests", "data", "notebook_for_testing_copy.ipynb"),
os.path.join("tests", "data", "notebook_starting_with_md.ipynb"),
),
),
)
def test_ruff_works(
path_0: str, path_1: str, path_2: str, capsys: "CaptureFixture"
) -> None:
"""
Check flake8 works. Shouldn't alter the notebook content.
Parameters
----------
capsys
Pytest fixture to capture stdout and stderr.
"""
# check passing both absolute and relative paths
main(["ruff", path_0, path_1, path_2])
expected_path_0 = os.path.join("tests", "data", "notebook_for_testing.ipynb")
expected_path_1 = os.path.join("tests", "data", "notebook_for_testing_copy.ipynb")
expected_path_2 = os.path.join("tests", "data", "notebook_starting_with_md.ipynb")
out, err = capsys.readouterr()
expected_out = (
"Found 13 errors.\n"
"[*] 10 fixable with the `--fix` option.\n"
f"{expected_path_0}:cell_1:1:8: F401 [*] `os` imported but unused\n"
f"{expected_path_0}:cell_1:3:8: F401 [*] `glob` imported but unused\n"
f"{expected_path_0}:cell_1:5:8: F401 [*] `nbqa` imported but unused\n"
f"{expected_path_0}:cell_4:1:1: E402 Module level import not at top of file\n"
f"{expected_path_0}:cell_4:1:20: F401 [*] `random.randint` imported but unused\n"
f"{expected_path_0}:cell_5:1:1: E402 Module level import not at top of file\n"
f"{expected_path_0}:cell_5:2:1: E402 Module level import not at top of file\n"
f"{expected_path_1}:cell_1:1:8: F401 [*] `os` imported but unused\n"
f"{expected_path_1}:cell_1:3:8: F401 [*] `glob` imported but unused\n"
f"{expected_path_1}:cell_1:5:8: F401 [*] `nbqa` imported but unused\n"
f"{expected_path_2}:cell_1:1:8: F401 [*] `os` imported but unused\n"
f"{expected_path_2}:cell_1:3:8: F401 [*] `glob` imported but unused\n"
f"{expected_path_2}:cell_1:5:8: F401 [*] `nbqa` imported but unused"
)
assert "\n".join(sorted(out.replace("\r\n", "\n").splitlines())) == "\n".join(
sorted(expected_out.splitlines())
)
assert err == ""
def test_cell_with_all_magics(capsys: "CaptureFixture") -> None:
"""
Should ignore cell with all magics.
Parameters
----------
capsys
Pytest fixture to capture stdout and stderr.
"""
path = os.path.join("tests", "data", "all_magic_cell.ipynb")
main(["ruff", "--quiet", path])
out, err = capsys.readouterr()
assert out == ""
assert err == ""
def test_ruff_isort(capsys: "CaptureFixture") -> None:
"""
Should ignore cell with all magics.
Parameters
----------
capsys
Pytest fixture to capture stdout and stderr.
"""
path = os.path.join("tests", "data", "simple_imports.ipynb")
main(["ruff", "--quiet", path, "--select=I"])
out, err = capsys.readouterr()
assert out == ""
assert err == ""
def test_ruff_format(capsys: "CaptureFixture", tmp_notebook_for_testing: Path) -> None:
"""
Should ignore cell with all magics.
Parameters
----------
capsys
Pytest fixture to capture stdout and stderr.
"""
main(["ruff format", str(tmp_notebook_for_testing)])
out, err = capsys.readouterr()
assert out == "1 file reformatted\n"
assert err == ""