From 340b4459657a5c3b9e7d2cf09b945ecca4fc7310 Mon Sep 17 00:00:00 2001 From: Michael Gielda Date: Tue, 19 Jan 2021 22:20:22 +0100 Subject: [PATCH] Implement a new way to parse args --- .github/workflows/test.sh | 32 ++++++++++---------- bin/tuttest | 61 +++++++++++++++++++++++---------------- 2 files changed, 52 insertions(+), 41 deletions(-) mode change 100644 => 100755 .github/workflows/test.sh diff --git a/.github/workflows/test.sh b/.github/workflows/test.sh old mode 100644 new mode 100755 index 13fb4af..9e5dcea --- a/.github/workflows/test.sh +++ b/.github/workflows/test.sh @@ -1,24 +1,24 @@ set -e -diff <(tuttest test/test.rst) <(tuttest README.md test-wholefile) -diff <(tuttest test/test.rst bash-tutorial) <(tuttest README.md test-named) -diff <(tuttest test/test.rst unnamed2) <(tuttest README.md test-unnamed2) -diff <(tuttest test/test.rst unnamed2 --prefix-lines-with "docker exec -t test bash -c") <(tuttest README.md test-prefix) -diff <(tuttest test/test.rst bash-tutorial,unnamed2 --prefix-lines-with "docker exec -t test bash -c" --single-command) <(tuttest README.md single-command) +diff <(tuttest test/test.rst) <(tuttest README.md:test-wholefile) +diff <(tuttest test/test.rst:bash-tutorial) <(tuttest README.md:test-named) +diff <(tuttest test/test.rst:unnamed2) <(tuttest README.md:test-unnamed2) +diff <(tuttest test/test.rst:unnamed2 --prefix-lines-with "docker exec -t test bash -c") <(tuttest README.md:test-prefix) +diff <(tuttest test/test.rst:bash-tutorial,unnamed2 --prefix-lines-with "docker exec -t test bash -c" --single-command) <(tuttest README.md:single-command) -diff <(tuttest test/test.md) <(tuttest README.md test-wholefile) -diff <(tuttest test/test.md bash-tutorial) <(tuttest README.md test-named) -diff <(tuttest test/test.md unnamed2) <(tuttest README.md test-unnamed2) -diff <(tuttest test/test.md unnamed2 --prefix-lines-with "docker exec -t test bash -c") <(tuttest README.md test-prefix) -diff <(tuttest test/test.md bash-tutorial,unnamed2 --prefix-lines-with "docker exec -t test bash -c" --single-command) <(tuttest README.md single-command) +diff <(tuttest test/test.md) <(tuttest README.md:test-wholefile) +diff <(tuttest test/test.md:bash-tutorial) <(tuttest README.md:test-named) +diff <(tuttest test/test.md:unnamed2) <(tuttest README.md:test-unnamed2) +diff <(tuttest test/test.md:unnamed2 --prefix-lines-with "docker exec -t test bash -c") <(tuttest README.md:test-prefix) +diff <(tuttest test/test.md:bash-tutorial,unnamed2 --prefix-lines-with "docker exec -t test bash -c" --single-command) <(tuttest README.md:single-command) # wildcard testing -diff <(tuttest test/test.md 'unnamed?') <(tuttest test/test.md unnamed0,unnamed2) -diff <(tuttest test/test.rst 'unnamed?') <(tuttest test/test.rst unnamed0,unnamed2) -diff <(tuttest test/test-wildcard.md '[bd]ash*') <(tuttest test/test-wildcard.md bash-tutorial,dash-tutorial-duplicate) -diff <(tuttest test/test-wildcard.rst '[bd]ash*') <(tuttest test/test-wildcard.rst bash-tutorial,dash-tutorial-duplicate) -diff <(tuttest test/test-wildcard.md '[!d]ash*') <(tuttest test/test-wildcard.md bash-tutorial) -diff <(tuttest test/test-wildcard.rst '[!d]ash*') <(tuttest test/test-wildcard.rst bash-tutorial) +diff <(tuttest test/test.md:'unnamed?') <(tuttest test/test.md:unnamed0,unnamed2) +diff <(tuttest test/test.rst:'unnamed?') <(tuttest test/test.rst:unnamed0,unnamed2) +diff <(tuttest test/test-wildcard.md:'[bd]ash*') <(tuttest test/test-wildcard.md:bash-tutorial,dash-tutorial-duplicate) +diff <(tuttest test/test-wildcard.rst:'[bd]ash*') <(tuttest test/test-wildcard.rst:bash-tutorial,dash-tutorial-duplicate) +diff <(tuttest test/test-wildcard.md:'[!d]ash*') <(tuttest test/test-wildcard.md:bash-tutorial) +diff <(tuttest test/test-wildcard.rst:'[!d]ash*') <(tuttest test/test-wildcard.rst:bash-tutorial) echo "All tests successfully passed!" diff --git a/bin/tuttest b/bin/tuttest index 2233bd8..65e014a 100755 --- a/bin/tuttest +++ b/bin/tuttest @@ -3,14 +3,24 @@ from tuttest import get_snippets import fnmatch +def snipdef(arg): + # TODO add http/https capability + # TODO take into account the "+" functionality + filename = '' + snippetlist = [] + split = arg.split(':') + filename = split[0] + if len(split) > 1: + snippetlist = split[1].split(',') + return {'filename': filename, "snippetlist": snippetlist} + if __name__ == "__main__": import sys import argparse parser = argparse.ArgumentParser(description='A tutorial tester script. Extract the code blocks from tutorial and see if, when followed, the tutorial actually works.') - parser.add_argument('filename', metavar='filename', type=str, help='filename with tutorial') - parser.add_argument('commands', metavar='commands', nargs='?', type=str, help='optional comma-separated list of names of snippets to extract if you want a subset') + parser.add_argument('snipdefs', metavar='snipdefs', type=snipdef, nargs="+", help='Filename and snippet list, in the form of etc. Skip the snippet list to extract all snippets') parser.add_argument('--prefix-lines-with', metavar='prefix', type=str, help='string to prefix each command with') parser.add_argument('--single-command', action='store_true', help='executes all snippets in single command') @@ -18,30 +28,31 @@ if __name__ == "__main__": args = parser.parse_args() code = [] - snippets = get_snippets(args.filename) - if not args.commands: - for s in snippets: - code.append(snippets[s].text.strip()) - else: - commands = args.commands.split(',') - commands_filter = [] - for c in commands: - commands_filter.extend(fnmatch.filter(snippets, c)) - - for c in commands_filter: - if c in snippets: - # name matches, add snippet code - code.append(snippets[c].text.strip()) - else: - if c[0] == "+": - # no match, + sign, add ad hoc code to previous line; don't strip! - code[len(code)-1] += c[1:] - elif c[0] == "#": - # select snippet by number - code.append(list(snippets.values())[int(c[1:])].text.strip()) + for snipdef in args.snipdefs: + snippets = get_snippets(snipdef['filename']) + if not snipdef['snippetlist']: + for s in snippets: + code.append(snippets[s].text.strip()) + else: + commands = snipdef['snippetlist'] + commands_filter = [] + for c in commands: + commands_filter.extend(fnmatch.filter(snippets, c)) + + for c in commands_filter: + if c in snippets: + # name matches, add snippet code + code.append(snippets[c].text.strip()) else: - # no match, exit with error code - exit(1) + if c[0] == "+": + # no match, + sign, add ad hoc code to previous line; don't strip! + code[len(code)-1] += c[1:] + elif c[0] == "#": + # select snippet by number + code.append(list(snippets.values())[int(c[1:])].text.strip()) + else: + # no match, exit with error code + exit(1) if args.single_command: code = [';'.join(code)]