forked from PyCQA/isort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_main.py
227 lines (192 loc) · 6.3 KB
/
test_main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import json
import os
import sys
from datetime import datetime
from io import BytesIO, TextIOWrapper
import pytest
from hypothesis_auto import auto_pytest_magic
from isort import main
from isort._version import __version__
from isort.settings import DEFAULT_CONFIG, Config
from isort.wrap_modes import WrapModes
auto_pytest_magic(main.sort_imports)
def test_iter_source_code(tmpdir):
tmp_file = tmpdir.join("file.py")
tmp_file.write("import os, sys\n")
assert tuple(main.iter_source_code((tmp_file,), DEFAULT_CONFIG, [])) == (tmp_file,)
def test_sort_imports(tmpdir):
tmp_file = tmpdir.join("file.py")
tmp_file.write("import os, sys\n")
assert main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted
main.sort_imports(str(tmp_file), DEFAULT_CONFIG)
assert not main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted
skip_config = Config(skip=["file.py"])
assert main.sort_imports(
str(tmp_file), config=skip_config, check=True, disregard_skip=False
).skipped
assert main.sort_imports(str(tmp_file), config=skip_config, disregard_skip=False).skipped
def test_is_python_file():
assert main.is_python_file("file.py")
assert main.is_python_file("file.pyi")
assert main.is_python_file("file.pyx")
assert not main.is_python_file("file.pyc")
assert not main.is_python_file("file.txt")
assert not main.is_python_file("file.pex")
def test_parse_args():
assert main.parse_args([]) == {}
assert main.parse_args(["--multi-line", "1"]) == {"multi_line_output": WrapModes.VERTICAL}
assert main.parse_args(["--multi-line", "GRID"]) == {"multi_line_output": WrapModes.GRID}
def test_ascii_art(capsys):
main.main(["--version"])
out, error = capsys.readouterr()
assert (
out
== f"""
_ _
(_) ___ ___ _ __| |_
| |/ _/ / _ \\/ '__ _/
| |\\__ \\/\\_\\/| | | |_
|_|\\___/\\___/\\_/ \\_/
isort your imports, so you don't have to.
VERSION {__version__}
"""
)
assert error == ""
def test_preconvert():
assert main._preconvert(frozenset([1, 1, 2])) == [1, 2]
assert main._preconvert(WrapModes.GRID) == "GRID"
with pytest.raises(TypeError):
main._preconvert(datetime.now())
@pytest.mark.skipif(sys.platform == "win32", reason="cannot create fifo file on Windows platform")
def test_is_python_file_fifo(tmpdir):
fifo_file = os.path.join(tmpdir, "fifo_file")
os.mkfifo(fifo_file)
assert not main.is_python_file(fifo_file)
def test_main(capsys, tmpdir):
base_args = [
"--settings-path",
str(tmpdir),
"--virtual-env",
str(tmpdir),
"--src-path",
str(tmpdir),
]
tmpdir.mkdir(".git")
# If no files are passed in the quick guide is returned
main.main(base_args)
out, error = capsys.readouterr()
assert main.QUICK_GUIDE in out
assert not error
# Unless the config is requested, in which case it will be returned alone as JSON
main.main(base_args + ["--show-config"])
out, error = capsys.readouterr()
returned_config = json.loads(out)
assert returned_config
assert returned_config["virtual_env"] == str(tmpdir)
# This should work even if settings path is non-existent or not provided
main.main(base_args[2:] + ["--show-config"])
out, error = capsys.readouterr()
assert json.loads(out)["virtual_env"] == str(tmpdir)
main.main(
base_args[2:]
+ ["--show-config"]
+ ["--settings-path", "/random-root-folder-that-cant-exist-right?"]
)
out, error = capsys.readouterr()
assert json.loads(out)["virtual_env"] == str(tmpdir)
# Should be able to set settings path to a file
config_file = tmpdir.join(".isort.cfg")
config_file.write(
"""
[settings]
profile=hug
verbose=true
"""
)
config_args = ["--settings-path", str(config_file)]
main.main(
config_args
+ ["--virtual-env", "/random-root-folder-that-cant-exist-right?"]
+ ["--show-config"]
)
out, error = capsys.readouterr()
assert json.loads(out)["profile"] == "hug"
# Should be able to stream in content to sort
input_content = TextIOWrapper(
BytesIO(
b"""
import b
import a
"""
)
)
main.main(config_args + ["-"], stdin=input_content)
out, error = capsys.readouterr()
assert (
out
== f"""else-type place_module for b returned {DEFAULT_CONFIG.default_section}
else-type place_module for a returned {DEFAULT_CONFIG.default_section}
import a
import b
"""
)
# Should be able to run with just a file
python_file = tmpdir.join("has_imports.py")
python_file.write(
"""
import b
import a
"""
)
main.main([str(python_file), "--filter-files", "--verbose"])
assert python_file.read().lstrip() == "import a\nimport b\n"
# Add a file to skip
should_skip = tmpdir.join("should_skip.py")
should_skip.write("import nothing")
main.main(
[
str(python_file),
str(should_skip),
"--filter-files",
"--verbose",
"--skip",
str(should_skip),
]
)
# Should raise a system exit if check only, with broken file
python_file.write(
"""
import b
import a
"""
)
with pytest.raises(SystemExit):
main.main(
[
str(python_file),
str(should_skip),
"--filter-files",
"--verbose",
"--check-only",
"--skip",
str(should_skip),
]
)
# Should have same behavior if full directory is skipped
with pytest.raises(SystemExit):
main.main(
[str(tmpdir), "--filter-files", "--verbose", "--check-only", "--skip", str(should_skip)]
)
# Nested files should be skipped without needing --filter-files
nested_file = tmpdir.mkdir("nested_dir").join("skip.py")
nested_file.write("import b;import a")
python_file.write(
"""
import a
import b
"""
)
main.main([str(tmpdir), "--skip", "skip.py", "--check"])
def test_isort_command():
"""Ensure ISortCommand got registered, otherwise setuptools error must have occured"""
assert main.ISortCommand