Skip to content

Commit de76154

Browse files
authored
Merge pull request #61 from jeffols/fix-json-output-to-file
fix capture to file
2 parents 768cdd2 + 081bb17 commit de76154

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/flake8_json_reporter/reporters.py

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def start(self):
3232
def stop(self):
3333
"""Override the default to finish printing JSON."""
3434
self.write_line("}")
35+
super().stop()
3536

3637
def beginning(self, filename):
3738
"""We're starting a new file."""
@@ -83,6 +84,8 @@ def stop(self):
8384
if self.files_reported_count > 0:
8485
self.write_line("\n")
8586
self.write_line("}\n")
87+
# DefaultJSON.stop would write and extra close brace
88+
base.BaseFormatter.stop(self)
8689

8790
def beginning(self, filename):
8891
"""We're starting a new file."""
@@ -139,6 +142,7 @@ def start(self):
139142
def stop(self):
140143
"""Override the default to finish printing JSON."""
141144
self.write_line("}")
145+
super().stop()
142146

143147
def beginning(self, filename):
144148
"""We're starting a new file."""

tests/flake8_json_reporter_test.py

+80
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import json
12
from argparse import Namespace
23

34
import pytest
45
from flake8.violation import Violation
56

7+
from flake8_json_reporter.reporters import DefaultJSON
68
from flake8_json_reporter.reporters import FormattedJSON
79

810

@@ -14,6 +16,26 @@ def formatter():
1416
return formatter
1517

1618

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+
1739
@pytest.fixture
1840
def violation():
1941
return Violation(
@@ -119,3 +141,61 @@ def test_single_file_multiple_violations(capsys, formatter, violation):
119141
}
120142
"""
121143
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

Comments
 (0)