From 76a74da6d6df0a85acc21624c3c614995ab7903a Mon Sep 17 00:00:00 2001 From: dosas Date: Thu, 22 Feb 2024 13:20:44 +0100 Subject: [PATCH] Add support for markdown output --- testimony/__init__.py | 47 ++++++++++++++++++++++++++++++++++++++++++- testimony/cli.py | 8 +++++--- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/testimony/__init__.py b/testimony/__init__.py index a76cacb..940ead3 100755 --- a/testimony/__init__.py +++ b/testimony/__init__.py @@ -36,6 +36,7 @@ SETTINGS = { 'json': False, + 'markdown': False, 'nocolor': False, 'tokens': {}, } @@ -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: @@ -286,6 +288,46 @@ 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 = {} + total_cases = sum([len(cases) for cases in testcases.values()]) + print(f'Total number of Tests: {total_cases}\n') + 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. @@ -312,6 +354,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] diff --git a/testimony/cli.py b/testimony/cli.py index 2f09f82..f693fb6 100644 --- a/testimony/cli.py +++ b/testimony/cli.py @@ -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') @@ -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) @@ -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)