1
+ import json
1
2
from argparse import Namespace
2
3
3
4
import pytest
4
5
from flake8 .violation import Violation
5
6
7
+ from flake8_json_reporter .reporters import DefaultJSON
6
8
from flake8_json_reporter .reporters import FormattedJSON
7
9
8
10
@@ -14,6 +16,26 @@ def formatter():
14
16
return formatter
15
17
16
18
19
+ @pytest .fixture
20
+ def default_formatter_output_file (tmp_path ):
21
+ """Return a ``DefaultJSON`` instance that captures output to a file"""
22
+ options = Namespace (
23
+ output_file = tmp_path / "output.json" , color = False , tee = False
24
+ )
25
+ formatter = DefaultJSON (options )
26
+ return formatter
27
+
28
+
29
+ @pytest .fixture
30
+ def pretty_formatter_output_file (tmp_path ):
31
+ """Return a ``DefaultJSON`` instance that captures output to a file"""
32
+ options = Namespace (
33
+ output_file = tmp_path / "output.json" , color = False , tee = False
34
+ )
35
+ formatter = FormattedJSON (options )
36
+ return formatter
37
+
38
+
17
39
@pytest .fixture
18
40
def violation ():
19
41
return Violation (
@@ -119,3 +141,61 @@ def test_single_file_multiple_violations(capsys, formatter, violation):
119
141
}
120
142
"""
121
143
assert stdout == expected
144
+
145
+
146
+ def test_pretty_single_file_single_file_capture (
147
+ pretty_formatter_output_file , violation
148
+ ):
149
+ run (pretty_formatter_output_file , {"main.py" : [violation ]})
150
+ expected = """\
151
+ {
152
+ "main.py": [
153
+ {
154
+ "code": "E222",
155
+ "filename": "main.py",
156
+ "line_number": 42,
157
+ "column_number": 4,
158
+ "text": "multiple spaces after operator",
159
+ "physical_line": "x = 1"
160
+ }
161
+ ]
162
+ }
163
+ """
164
+ actual = pretty_formatter_output_file .filename .read_text ()
165
+ assert actual == expected
166
+
167
+
168
+ def test_default_no_files_file_capture (default_formatter_output_file ):
169
+ run (default_formatter_output_file , {})
170
+ expected = {}
171
+ actual = json .loads (default_formatter_output_file .filename .read_text ())
172
+ assert actual == expected
173
+
174
+
175
+ def test_default_single_file_no_violations_file_capture (
176
+ default_formatter_output_file ,
177
+ ):
178
+ run (default_formatter_output_file , {"main.py" : []})
179
+ expected = {"main.py" : []}
180
+ actual = json .loads (default_formatter_output_file .filename .read_text ())
181
+ assert actual == expected
182
+
183
+
184
+ def test_default_single_file_violations_file_capture (
185
+ default_formatter_output_file , violation
186
+ ):
187
+ run (default_formatter_output_file , {"main.py" : [violation ]})
188
+ expected = {
189
+ "main.py" : [
190
+ {
191
+ "code" : "E222" ,
192
+ "filename" : "main.py" ,
193
+ "line_number" : 42 ,
194
+ "column_number" : 4 ,
195
+ "text" : "multiple spaces after operator" ,
196
+ "physical_line" : "x = 1" ,
197
+ }
198
+ ]
199
+ }
200
+ actual = json .loads (default_formatter_output_file .filename .read_text ())
201
+ assert actual == expected
0 commit comments