Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix capture to file #61

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/flake8_json_reporter/reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def start(self):
def stop(self):
"""Override the default to finish printing JSON."""
self.write_line("}")
super().stop()

def beginning(self, filename):
"""We're starting a new file."""
Expand Down Expand Up @@ -83,6 +84,8 @@ def stop(self):
if self.files_reported_count > 0:
self.write_line("\n")
self.write_line("}\n")
# DefaultJSON.stop would write and extra close brace
base.BaseFormatter.stop(self)

def beginning(self, filename):
"""We're starting a new file."""
Expand Down Expand Up @@ -139,6 +142,7 @@ def start(self):
def stop(self):
"""Override the default to finish printing JSON."""
self.write_line("}")
super().stop()

def beginning(self, filename):
"""We're starting a new file."""
Expand Down
80 changes: 80 additions & 0 deletions tests/flake8_json_reporter_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
from argparse import Namespace

import pytest
from flake8.violation import Violation

from flake8_json_reporter.reporters import DefaultJSON
from flake8_json_reporter.reporters import FormattedJSON


Expand All @@ -14,6 +16,26 @@ def formatter():
return formatter


@pytest.fixture
def default_formatter_output_file(tmp_path):
"""Return a ``DefaultJSON`` instance that captures output to a file"""
options = Namespace(
output_file=tmp_path / "output.json", color=False, tee=False
)
formatter = DefaultJSON(options)
return formatter


@pytest.fixture
def pretty_formatter_output_file(tmp_path):
"""Return a ``DefaultJSON`` instance that captures output to a file"""
options = Namespace(
output_file=tmp_path / "output.json", color=False, tee=False
)
formatter = FormattedJSON(options)
return formatter


@pytest.fixture
def violation():
return Violation(
Expand Down Expand Up @@ -119,3 +141,61 @@ def test_single_file_multiple_violations(capsys, formatter, violation):
}
"""
assert stdout == expected


def test_pretty_single_file_single_file_capture(
pretty_formatter_output_file, violation
):
run(pretty_formatter_output_file, {"main.py": [violation]})
expected = """\
{
"main.py": [
{
"code": "E222",
"filename": "main.py",
"line_number": 42,
"column_number": 4,
"text": "multiple spaces after operator",
"physical_line": "x = 1"
}
]
}
"""
actual = pretty_formatter_output_file.filename.read_text()
assert actual == expected


def test_default_no_files_file_capture(default_formatter_output_file):
run(default_formatter_output_file, {})
expected = {}
actual = json.loads(default_formatter_output_file.filename.read_text())
assert actual == expected


def test_default_single_file_no_violations_file_capture(
default_formatter_output_file,
):
run(default_formatter_output_file, {"main.py": []})
expected = {"main.py": []}
actual = json.loads(default_formatter_output_file.filename.read_text())
assert actual == expected


def test_default_single_file_violations_file_capture(
default_formatter_output_file, violation
):
run(default_formatter_output_file, {"main.py": [violation]})
expected = {
"main.py": [
{
"code": "E222",
"filename": "main.py",
"line_number": 42,
"column_number": 4,
"text": "multiple spaces after operator",
"physical_line": "x = 1",
}
]
}
actual = json.loads(default_formatter_output_file.filename.read_text())
assert actual == expected
Loading