From 0a7e34120765170bcc6dff4277a8c457b72e1dc4 Mon Sep 17 00:00:00 2001 From: Jeff Olsen <572629+jeffols@users.noreply.github.com> Date: Sun, 14 Apr 2024 08:33:25 -0500 Subject: [PATCH] fix capture to file --- src/flake8_json_reporter/reporters.py | 3 + tests/flake8_json_reporter_test.py | 90 +++++++++++++++++++++++---- 2 files changed, 81 insertions(+), 12 deletions(-) diff --git a/src/flake8_json_reporter/reporters.py b/src/flake8_json_reporter/reporters.py index 3637cdb..79a0810 100644 --- a/src/flake8_json_reporter/reporters.py +++ b/src/flake8_json_reporter/reporters.py @@ -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.""" @@ -83,6 +84,7 @@ def stop(self): if self.files_reported_count > 0: self.write_line("\n") self.write_line("}\n") + super(DefaultJSON, self).stop() # DefaultJSON.stop would write and extra close brace def beginning(self, filename): """We're starting a new file.""" @@ -139,6 +141,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.""" diff --git a/tests/flake8_json_reporter_test.py b/tests/flake8_json_reporter_test.py index 0e860b1..a74488d 100644 --- a/tests/flake8_json_reporter_test.py +++ b/tests/flake8_json_reporter_test.py @@ -1,19 +1,37 @@ from argparse import Namespace +import json import pytest from flake8.violation import Violation -from flake8_json_reporter.reporters import FormattedJSON + +from flake8_json_reporter.reporters import DefaultJSON, FormattedJSON @pytest.fixture -def formatter(): +def pretty_formatter(): """Return a ``FormattedJSON`` instance.""" options = Namespace(output_file=None, color=False, tee=False) formatter = FormattedJSON(options) 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( @@ -36,14 +54,14 @@ def run(formatter, violations): formatter.stop() -def test_no_files(capsys, formatter): - run(formatter, {}) +def test_pretty_no_files(capsys, pretty_formatter): + run(pretty_formatter, {}) stdout, _ = capsys.readouterr() assert stdout == "{}\n" -def test_single_file_no_violations(capsys, formatter): - run(formatter, {"main.py": []}) +def test_pretty_single_file_no_violations(capsys, pretty_formatter): + run(pretty_formatter, {"main.py": []}) stdout, _ = capsys.readouterr() expected = """\ { @@ -53,8 +71,8 @@ def test_single_file_no_violations(capsys, formatter): assert stdout == expected -def test_multiple_files_no_violations(capsys, formatter): - run(formatter, {"main.py": [], "__init__.py": []}) +def test_pretty_multiple_files_no_violations(capsys, pretty_formatter): + run(pretty_formatter, {"main.py": [], "__init__.py": []}) stdout, _ = capsys.readouterr() expected = """\ { @@ -65,8 +83,8 @@ def test_multiple_files_no_violations(capsys, formatter): assert stdout == expected -def test_single_file_single_violation(capsys, formatter, violation): - run(formatter, {"main.py": [violation]}) +def test_pretty_single_file_single_violation(capsys, pretty_formatter, violation): + run(pretty_formatter, {"main.py": [violation]}) stdout, _ = capsys.readouterr() expected = """\ { @@ -85,8 +103,8 @@ def test_single_file_single_violation(capsys, formatter, violation): assert stdout == expected -def test_single_file_multiple_violations(capsys, formatter, violation): - run(formatter, {"main.py": [violation] * 3}) +def test_pretty_single_file_multiple_violations(capsys, pretty_formatter, violation): + run(pretty_formatter, {"main.py": [violation] * 3}) stdout, _ = capsys.readouterr() expected = """\ { @@ -119,3 +137,51 @@ 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 + +