-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_console_script.py
More file actions
210 lines (176 loc) · 6.66 KB
/
test_console_script.py
File metadata and controls
210 lines (176 loc) · 6.66 KB
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
import re
from io import StringIO
from os import environ
from pathlib import Path
from shutil import copy
from sys import modules
from tomllib import loads as toml_loads
import pytest
from fractured_json import (
__version__ as fractured_json_version, # pyright: ignore[reportAttributeAccessIssue]
)
def test_version(script_runner):
ret = script_runner.run(["fractured-json", "--version"], print_result=False)
assert ret.success
assert ret.stdout.strip() == fractured_json_version
assert ret.stderr == ""
project_toml = (Path(__file__).parent.parent / "pyproject.toml").read_text()
toml = toml_loads(project_toml)
project_version = toml["project"]["version"]
assert ret.stdout.strip() == project_version
def test_help(script_runner):
ret = script_runner.run(["fractured-json", "--help"], print_result=False)
assert ret.success
assert "Format JSON into compact, human readable form" in ret.stdout
assert "max-total-line-length N" in ret.stdout
assert ret.stderr == ""
def test_no_args(script_runner):
ret = script_runner.run(["fractured-json"], print_result=False)
assert ret.success
assert "Format JSON into compact, human readable form" in ret.stdout
assert "max-total-line-length N" in ret.stdout
assert ret.stderr == ""
def test_question_mark(script_runner):
ret = script_runner.run(["fractured-json", "-?"], print_result=False)
assert ret.success
assert "Format JSON into compact, human readable form" in ret.stdout
assert "max-total-line-length N" in ret.stdout
assert ret.stderr == ""
def test_all_args(script_runner, pytestconfig):
ret = script_runner.run(
[
"fractured-json",
"--allow-trailing-commas",
"--always-expand-depth=2",
"--colon-before-prop-name-padding",
"--colon-padding",
"--comma-padding",
"--comment-padding",
"--comment-policy=PRESERVE",
"--indent-spaces=2",
"--json-eol-style=LF",
"--max-compact-array-complexity=2",
"--max-inline-complexity=2",
"--max-prop-name-padding=2",
"--max-table-row-complexity=2",
"--max-total-line-length=100",
"--min-compact-array-row-items=2",
"--nested-bracket-padding",
"--number-list-alignment=LEFT",
"--prefix-string=::",
"--preserve-blank-lines",
"--simple-bracket-padding",
"--table-comma-placement=BEFORE_PADDING_EXCEPT_NUMBERS",
"--use-tab-to-indent",
"--east-asian-chars",
"tests/data/test-comments-0.jsonc",
],
print_result=False,
)
ref_output = Path("tests/data/test-comments-0.ref-1.jsonc").read_text()
if pytestconfig.getoption("test_verbose") and ret.stdout != ref_output:
json_string_dbg = ">" + re.sub(r"\n", "<\n>", ret.stdout) + "<"
ref_json_dbg = ">" + re.sub(r"\n", "<\n>", ref_output) + "<"
print("===== TEST")
print(json_string_dbg)
print("===== REF")
print(ref_json_dbg)
print("=====")
assert ret.stderr == ""
assert ret.success
assert ret.stdout == ref_output
def test_unicode(script_runner):
ret = script_runner.run(
[
"fractured-json",
"--east-asian-chars",
"--json-eol-style=CRLF",
"tests/data/test-wide-chars.json",
],
print_result=False,
)
assert ret.stderr == ""
assert ret.success
# Use read_bytes() to get \r
ref_output = Path("tests/data/test-wide-chars.ref-1.json").read_bytes().decode("utf-8")
assert ret.stdout == ref_output
@pytest.mark.script_launch_mode("subprocess")
def test_main(script_runner):
# We need to run as a subprocess so we force the child to read coverage setup if
# the current pytest is running with coverage
env = environ.copy()
if "coverage" in modules:
env["COVERAGE_PROCESS_START"] = "pyproject.toml"
ret = script_runner.run(
["python3", "-m", "fractured_json", "--help"],
print_result=False,
env=env,
)
assert ret.stderr == ""
assert ret.success
assert "[-h] [-V] [--in-place] [--output OUTPUT]" in ret.stdout
def test_multifile(script_runner):
ret = script_runner.run(
["fractured-json", "tests/data/test-bool.json", "tests/data/test-bool.json"],
print_result=False,
)
assert ret.stderr == ""
assert ret.success
assert ret.stdout == '{ "bools": {"true": true, "false": false} }\n' * 2
def test_output(script_runner, tmp_path):
tmp_file = tmp_path / "test.json"
ret = script_runner.run(
["fractured-json", "tests/data/test-bool.json", "--output", str(tmp_file)],
print_result=False,
)
assert ret.stderr == ""
assert ret.success
assert tmp_file.read_text() == '{ "bools": {"true": true, "false": false} }\n'
def test_in_place(script_runner, tmp_path):
tmp_file = tmp_path / "test.json"
copy(Path("tests/data/test-bool.json"), tmp_file)
ret = script_runner.run(
["fractured-json", "--in-place", tmp_file],
print_result=False,
)
assert ret.stderr == ""
assert ret.success
assert tmp_file.read_text() == '{ "bools": {"true": true, "false": false} }\n'
timestamp = tmp_file.stat().st_mtime_ns
ret = script_runner.run(
["fractured-json", "--in-place", tmp_file],
print_result=False,
)
assert ret.stderr == ""
assert ret.success
assert tmp_file.read_text() == '{ "bools": {"true": true, "false": false} }\n'
assert timestamp == tmp_file.stat().st_mtime_ns
def test_mismatched_file_count(script_runner):
ret = script_runner.run(
[
"fractured-json",
"tests/data/test-bool.json",
"--output",
"foo",
"--output",
"bar",
],
print_result=False,
)
assert ret.stderr == "fractured-json: the numbers of input and output file names do not match\n"
assert ret.returncode == 1
def test_pipe_stdin(script_runner):
json_input_fh = StringIO(Path("tests/data/test-bool.json").read_text())
ret = script_runner.run(["fractured-json", "-"], stdin=json_input_fh, print_result=False)
assert ret.success
assert ret.stderr == ""
assert ret.success
assert ret.stdout == '{ "bools": {"true": true, "false": false} }\n'
def test_missing_file(script_runner, tmp_path):
ret = script_runner.run(
["fractured-json", "file-does-not-exist.json"],
print_result=False,
)
assert not ret.success
assert "[Errno 2] No such file or directory: 'file-does-not-exist.json'" in ret.stderr
assert ret.stdout == ""