Skip to content

Commit

Permalink
Add JSON output format for output
Browse files Browse the repository at this point in the history
  • Loading branch information
ominiet committed Aug 26, 2024
1 parent 99fb29a commit 9e6297a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
17 changes: 17 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,23 @@ def test_github_actions_detection(self):
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

def test_run_format_json(self):
path = os.path.join(self.wd, 'a.yaml')

with RunContext(self) as ctx:
cli.run((path, '--format', 'json'))
expected_out = (
'[\n '
'{"file": "' + str(path) + '", "level": "error", "line": 2, '
'"column": 4, "description": "trailing spaces", '
'"rule": "trailing-spaces"},\n '
'{"file": "' + str(path) + '", "level": "error", "line": 3, '
'"column": 4, "description": "no new line character at the end '
'of file", "rule": '
'"new-line-at-end-of-file"}\n]\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

def test_run_read_from_stdin(self):
# prepares stdin with an invalid yaml string so that we can check
# for its specific error, and be assured that stdin was read
Expand Down
37 changes: 35 additions & 2 deletions yamllint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import argparse
import json
import locale
import os
import platform
Expand Down Expand Up @@ -87,19 +88,45 @@ def github(problem, filename):
line += problem.desc
return line

@staticmethod
def json(problem, filename):
return json.dumps({
'file': filename,
'level': problem.level,
'line': problem.line,
'column': problem.column,
'description': problem.desc,
'rule': problem.rule,
})

@staticmethod
def json_pre_all():
return "["

@staticmethod
def json_post_all():
return "]"


def show_problems(problems, file, args_format, no_warn):
max_level = 0
first = True


if args_format == 'auto':
if ('GITHUB_ACTIONS' in os.environ and
'GITHUB_WORKFLOW' in os.environ):
args_format = 'github'
elif supports_color():
args_format = 'colored'

for problem in problems:
if args_format == "json":
print(Format.json_pre_all())

problems_list = list(problems)
size = len(problems_list)

for index, problem in enumerate(problems_list):
max_level = max(max_level, PROBLEM_LEVELS[problem.level])
if no_warn and (problem.level != 'error'):
continue
Expand All @@ -115,6 +142,9 @@ def show_problems(problems, file, args_format, no_warn):
print(f'\033[4m{file}\033[0m')
first = False
print(Format.standard_color(problem, file))
elif args_format == 'json':
print(' ' + Format.json(problem, file) +
("" if index == size - 1 else "," ))
else:
if first:
print(file)
Expand All @@ -124,6 +154,9 @@ def show_problems(problems, file, args_format, no_warn):
if not first and args_format == 'github':
print('::endgroup::')

if args_format == "json":
print(Format.json_post_all())

if not first and args_format != 'parsable':
print('')

Expand Down Expand Up @@ -163,7 +196,7 @@ def run(argv=None):
help='list files to lint and exit')
parser.add_argument('-f', '--format',
choices=('parsable', 'standard', 'colored', 'github',
'auto'),
'json', 'auto'),
default='auto', help='format for parsing output')
parser.add_argument('-s', '--strict',
action='store_true',
Expand Down

0 comments on commit 9e6297a

Please sign in to comment.