Skip to content

Commit 9080dd2

Browse files
committed
Add on unit tests for other formatting tools
1 parent 530a713 commit 9080dd2

File tree

1 file changed

+143
-2
lines changed

1 file changed

+143
-2
lines changed

test/unit/nox/_format_test.py

Lines changed: 143 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
from inspect import cleandoc
2+
from unittest.mock import patch
3+
14
import pytest
25
from nox.command import CommandFailed
36

4-
from exasol.toolbox.nox._format import _ruff
7+
from exasol.toolbox.nox._format import (
8+
_code_format,
9+
_pyupgrade,
10+
_ruff,
11+
fix,
12+
fmt_check,
13+
)
514
from exasol.toolbox.nox._shared import Mode
15+
from noxconfig import Config
616

717

818
@pytest.fixture
@@ -12,6 +22,77 @@ def file_with_unneeded_import(tmp_path):
1222
return file_path
1323

1424

25+
@pytest.fixture
26+
def file_with_not_ordered_import(tmp_path):
27+
file_path = tmp_path / "dummy_file.py"
28+
file_path.write_text("import isort\nimport black")
29+
return file_path
30+
31+
32+
@pytest.fixture
33+
def file_without_blank_line(tmp_path):
34+
file_path = tmp_path / "dummy_file.py"
35+
file_path.write_text("import black\nimport isort")
36+
return file_path
37+
38+
39+
class TestCodeFormat:
40+
@staticmethod
41+
def test_isort_mode_fix(nox_session, file_with_not_ordered_import):
42+
_code_format(
43+
session=nox_session,
44+
mode=Mode.Fix,
45+
files=[str(file_with_not_ordered_import)],
46+
)
47+
assert (
48+
file_with_not_ordered_import.read_text() == "import black\nimport isort\n"
49+
)
50+
51+
@staticmethod
52+
def test_isort_mode_check(nox_session, file_with_not_ordered_import, caplog):
53+
with pytest.raises(CommandFailed):
54+
_code_format(
55+
session=nox_session,
56+
mode=Mode.Check,
57+
files=[str(file_with_not_ordered_import)],
58+
)
59+
assert (
60+
caplog.messages[1]
61+
== f"Command isort --check {file_with_not_ordered_import} failed with exit code 1"
62+
)
63+
assert file_with_not_ordered_import.read_text() == "import isort\nimport black"
64+
65+
@staticmethod
66+
def test_black_mode_fix(nox_session, file_without_blank_line):
67+
_code_format(
68+
session=nox_session,
69+
mode=Mode.Fix,
70+
files=[str(file_without_blank_line)],
71+
)
72+
assert file_without_blank_line.read_text() == "import black\nimport isort\n"
73+
74+
@staticmethod
75+
def test_black_mode_check(nox_session, file_without_blank_line, caplog):
76+
with pytest.raises(CommandFailed):
77+
_code_format(
78+
session=nox_session,
79+
mode=Mode.Check,
80+
files=[str(file_without_blank_line)],
81+
)
82+
assert (
83+
caplog.messages[2]
84+
== f"Command black --check {file_without_blank_line} failed with exit code 1"
85+
)
86+
assert file_without_blank_line.read_text() == "import black\nimport isort"
87+
88+
89+
def test_pyupgrade(nox_session, tmp_path):
90+
file_path = tmp_path / "dummy_file.py"
91+
file_path.write_text("from typing import Union\nx:Union[int, str]=2")
92+
_pyupgrade(session=nox_session, config=Config(), files=[str(file_path)])
93+
assert file_path.read_text() == "from typing import Union\nx:int | str=2"
94+
95+
1596
class TestRuff:
1697
@staticmethod
1798
def test_mode_fix(nox_session, file_with_unneeded_import):
@@ -21,11 +102,71 @@ def test_mode_fix(nox_session, file_with_unneeded_import):
21102
assert file_with_unneeded_import.read_text() == ""
22103

23104
@staticmethod
24-
def test_mode_check(nox_session, file_with_unneeded_import):
105+
def test_mode_check(nox_session, file_with_unneeded_import, caplog):
25106
with pytest.raises(CommandFailed):
26107
_ruff(
27108
session=nox_session,
28109
mode=Mode.Check,
29110
files=[str(file_with_unneeded_import)],
30111
)
112+
assert (
113+
caplog.messages[1]
114+
== f"Command ruff check {file_with_unneeded_import} failed with exit code 1"
115+
)
31116
assert file_with_unneeded_import.read_text() == "import black"
117+
118+
119+
@pytest.fixture
120+
def file_with_multiple_problems(tmp_path):
121+
file_path = tmp_path / "dummy_file.py"
122+
text = """
123+
import numpy as np
124+
import isort # unused import
125+
from typing import Union # outdated typing from < Python 3.10
126+
x: Union[int, str]=2 # outdated typing from < Python 3.10
127+
y: np.ndarray = np.array([1, 2, 3])
128+
"""
129+
file_path.write_text(cleandoc(text))
130+
return file_path
131+
132+
133+
def test_project_fix(nox_session, tmp_path, file_with_multiple_problems):
134+
with patch("exasol.toolbox.nox._format.PROJECT_CONFIG") as config:
135+
with patch("exasol.toolbox.nox._format._version") as version:
136+
config.root = tmp_path
137+
config.pyupgrade_argument = ("--py310-plus",)
138+
# skip version check as modifies version.py file only
139+
version.return_value = True
140+
fix(nox_session)
141+
142+
assert (
143+
file_with_multiple_problems.read_text()
144+
== cleandoc(
145+
"""
146+
import numpy as np
147+
148+
x: int | str = 2 # outdated typing from < Python 3.10
149+
y: np.ndarray = np.array([1, 2, 3])
150+
"""
151+
)
152+
+ "\n"
153+
)
154+
155+
156+
def test_project_format(
157+
nox_session, tmp_path, file_with_multiple_problems, caplog, capsys
158+
):
159+
expected_text = file_with_multiple_problems.read_text()
160+
161+
with patch("exasol.toolbox.nox._format.PROJECT_CONFIG") as config:
162+
config.root = tmp_path
163+
with pytest.raises(CommandFailed):
164+
fmt_check(nox_session)
165+
166+
# The failed message should always be for the first called checking function.
167+
assert (
168+
caplog.messages[1]
169+
== f"Command ruff check {file_with_multiple_problems} failed with exit code 1"
170+
)
171+
# The file should not be changed.
172+
assert file_with_multiple_problems.read_text() == expected_text

0 commit comments

Comments
 (0)