Skip to content

Commit

Permalink
Add support for markdown output
Browse files Browse the repository at this point in the history
  • Loading branch information
dosas committed Feb 22, 2024
1 parent 8ce5d04 commit 017cbcb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
45 changes: 44 additions & 1 deletion testimony/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

SETTINGS = {
'json': False,
'markdown': False,
'nocolor': False,
'tokens': {},
}
Expand Down Expand Up @@ -268,13 +269,14 @@ def __str__(self):
return '\n'.join(output)


def main(report, paths, json_output, nocolor):
def main(report, paths, json_output, markdown_output, nocolor):
"""Entry point for the testimony project.
Expects a valid report type and valid directory paths, hopefully argparse
is taking care of validation
"""
SETTINGS['json'] = json_output
SETTINGS['markdown'] = markdown_output
SETTINGS['nocolor'] = nocolor

if report == SUMMARY_REPORT:
Expand All @@ -286,6 +288,44 @@ def main(report, paths, json_output, nocolor):
sys.exit(report_function(get_testcases(paths)))


def print_markdown(testcases):
"""Print markdown formatted list of test cases.
:param testcases: A dict where the key is a path and value is a list of
found testcases on that path.
"""
result = {}
for path, tests in testcases.items():
result[path] = [test.to_dict() for test in tests]
print('# {0}\n\n'.format(
colored(path, attrs=['bold'])))
if len(tests) == 0:
print('No test cases found.\n')
for test in tests:
title = testcase_title(test)
print('## {0}\n\n'.format(
title))
for token, data in test.to_dict().items():
if token == 'tokens':
for key, value in data.items():
print(f'### {key}\n')
print(value)
print('\n')
elif token == 'invalid-tokens':
print('### invalid tokens')
for key, value in data.items():
print(f'* {key}: {value}')
print('\n')
elif token == 'rst-parse-messages':
for val in data:
print(val)
else:
raise ValueError(f'Invalid token: {token}')
print('\n')

return 0


def print_report(testcases):
"""Print the list of test cases.
Expand All @@ -312,6 +352,9 @@ def print_report(testcases):
print(json.dumps(result))
return 0

if SETTINGS['markdown']:
return print_markdown(testcases)

result = {}
for path, tests in testcases.items():
result[path] = [test.to_dict() for test in tests]
Expand Down
8 changes: 5 additions & 3 deletions testimony/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

@click.command()
@click.option('-j', '--json', help='JSON output', is_flag=True)
@click.option('-m', '--markdown', help='mardown output', is_flag=True)
@click.option('-n', '--nocolor', default=False, help='Color output',
is_flag=True)
@click.option('--tokens', help='Comma separated list of expected tokens')
Expand All @@ -16,9 +17,10 @@
'-c', '--config', 'config_file', type=click.File(),
help='Configuration file (YAML)')
@click.argument('report', type=click.Choice(constants.REPORT_TAGS))
@click.argument('path', nargs=-1, type=click.Path(exists=True))
@click.argument('path', nargs=-1)
def testimony(
json, nocolor, tokens, minimum_tokens, config_file, report, path):
json, markdown, nocolor, tokens, minimum_tokens, config_file,
report, path):
"""Inspect and report on the Python test cases."""
if config_file:
SETTINGS['tokens'] = config.parse_config(config_file)
Expand All @@ -27,4 +29,4 @@ def testimony(
if minimum_tokens:
config.update_tokens_dict(
SETTINGS['tokens'], minimum_tokens, {'required': True})
main(report, path, json, nocolor)
main(report, path, json, markdown, nocolor)

0 comments on commit 017cbcb

Please sign in to comment.