Skip to content

Commit

Permalink
Implement a new way to parse args
Browse files Browse the repository at this point in the history
  • Loading branch information
mgielda committed Jan 19, 2021
1 parent 5dbe584 commit 340b445
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 41 deletions.
32 changes: 16 additions & 16 deletions .github/workflows/test.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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!"

61 changes: 36 additions & 25 deletions bin/tuttest
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,56 @@
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 <filename:snippet1,snippet2> 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')

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)]
Expand Down

0 comments on commit 340b445

Please sign in to comment.