Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ poetry.lock
dist
docs/build
.vscode
__pycache__
*.egg-info
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ csv = "tidypy.reports.structured:CsvReport"
pylint = "tidypy.reports.pylint:PyLintReport"
pylint-parseable = "tidypy.reports.pylint:PyLintParseableReport"
null = "tidypy.reports.null:NullReport"
prospector-json = "tidypy.reports.prospector_json:ProspectorJsonReport"

[tool.poetry.plugins."tidypy.extenders"]
github = "tidypy.extenders.github:GithubExtender"
Expand Down
44 changes: 44 additions & 0 deletions src/tidypy/reports/prospector_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from datetime import datetime

import basicserial

from ..config import get_tools
from .base import Report


class ProspectorJsonReport(Report):
"""
Formats the report in Prospector JSON format.
"""

def execute(self, collector):
issues = collector.get_issues(sortby=("filename", "line", "character"))
result_json = {
"summary": {
"started": datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"),
"libraries": [],
"strictness": "from profile",
"profiles": "tidypy",
"tools": list(get_tools().keys()),
"message_count": collector.issue_count(),
"completed": datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"),
"time_taken": "0",
"formatter": "json",
},
"messages": [
{
"source": issue.tool,
"code": issue.code,
"location": {
"path": self.relative_filename(issue.filename),
"module": None,
"function": None,
"line": issue.line,
"character": issue.character or 0,
},
"message": issue.message,
}
for issue in issues
],
}
self.output(basicserial.to_json(result_json, pretty=True))
1 change: 1 addition & 0 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_get_reports():
'custom',
'json',
'null',
'prospector-json',
'pycodestyle',
'pylint',
'pylint-parseable',
Expand Down
130 changes: 130 additions & 0 deletions test/test_report_prospector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from tidypy import execute_reports, get_default_config, Collector, TidyPyIssue


ISSUES = [
TidyPyIssue(
'code1',
'Message 1',
u'someproject/foo.py',
5,
23,
),
TidyPyIssue(
'code2',
'Message 2',
u'someproject/foo.py',
2,
),
TidyPyIssue(
'code1',
'Message 1',
'someproject/blah/bar.py',
28,
),
TidyPyIssue(
'code3',
'Message 3',
'someproject/subdir/foobar.json',
5,
23,
),
]


EXPECTED_JSON = '''{
"summary": {
"started": "2021-08-09 16:27:54.695977",
"libraries": [],
"strictness": "from profile",
"profiles": "tidypy",
"tools": [
"bandit",
"dlint",
"eradicate",
"jsonlint",
"manifest",
"mccabe",
"polint",
"pycodestyle",
"pydiatra",
"pydocstyle",
"pyflakes",
"pylint",
"pyroma",
"rstlint",
"secrets",
"vulture",
"yamllint"
],
"message_count": 4,
"completed": "2021-08-09 16:27:54.695977",
"time_taken": "0",
"formatter": "json"
},
"messages": [
{
"source": "tidypy",
"code": "code1",
"location": {
"path": "blah/bar.py",
"module": null,
"function": null,
"line": 28,
"character": 0
},
"message": "Message 1"
},
{
"source": "tidypy",
"code": "code2",
"location": {
"path": "foo.py",
"module": null,
"function": null,
"line": 2,
"character": 0
},
"message": "Message 2"
},
{
"source": "tidypy",
"code": "code1",
"location": {
"path": "foo.py",
"module": null,
"function": null,
"line": 5,
"character": 23
},
"message": "Message 1"
},
{
"source": "tidypy",
"code": "code3",
"location": {
"path": "subdir/foobar.json",
"module": null,
"function": null,
"line": 5,
"character": 23
},
"message": "Message 3"
}
]
}
'''


def test_json_execute(capsys):
cfg = get_default_config()
cfg['requested_reports'] = [{'type': 'prospector-json'}]

collector = Collector(cfg)
collector.add_issues(ISSUES)

execute_reports(cfg, 'someproject', collector)

out, err = capsys.readouterr()

assert EXPECTED_JSON == out.replace('\r\n', '\n')
assert err == ''