|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""tests for howler.py""" |
| 3 | + |
| 4 | +import os |
| 5 | +import re |
| 6 | +import random |
| 7 | +import string |
| 8 | +from subprocess import getstatusoutput, getoutput |
| 9 | + |
| 10 | +prg = './howler.py' |
| 11 | + |
| 12 | + |
| 13 | +# -------------------------------------------------- |
| 14 | +def random_string(): |
| 15 | + """generate a random string""" |
| 16 | + |
| 17 | + k = random.randint(5, 10) |
| 18 | + return ''.join(random.choices(string.ascii_letters + string.digits, k=k)) |
| 19 | + |
| 20 | + |
| 21 | +# -------------------------------------------------- |
| 22 | +def out_flag(): |
| 23 | + """Either -o or --outfile""" |
| 24 | + |
| 25 | + return '-o' if random.randint(0, 1) else '--outfile' |
| 26 | + |
| 27 | + |
| 28 | +# -------------------------------------------------- |
| 29 | +def test_usage(): |
| 30 | + """usage""" |
| 31 | + |
| 32 | + for flag in ['-h', '--help']: |
| 33 | + rv, out = getstatusoutput('{} {}'.format(prg, flag)) |
| 34 | + assert rv == 0 |
| 35 | + assert re.match("usage", out, re.IGNORECASE) |
| 36 | + |
| 37 | + |
| 38 | +# -------------------------------------------------- |
| 39 | +def test_text_stdout(): |
| 40 | + """Test STDIN/STDOUT""" |
| 41 | + |
| 42 | + out = getoutput('{} "foo bar baz"'.format(prg)) |
| 43 | + expected = 'FOO BAR BAZ' |
| 44 | + assert out.strip() == expected |
| 45 | + |
| 46 | + |
| 47 | +# -------------------------------------------------- |
| 48 | +def test_text_outfile(): |
| 49 | + """Test STDIN/outfile""" |
| 50 | + |
| 51 | + out_file = random_string() |
| 52 | + |
| 53 | + if os.path.isfile(out_file): |
| 54 | + os.remove(out_file) |
| 55 | + |
| 56 | + try: |
| 57 | + out = getoutput('{} {} {} "foo bar baz"'.format( |
| 58 | + prg, out_flag(), out_file)) |
| 59 | + assert out.strip() == '' |
| 60 | + assert os.path.isfile(out_file) |
| 61 | + text = open(out_file).read().rstrip() |
| 62 | + assert text == 'FOO BAR BAZ' |
| 63 | + finally: |
| 64 | + if os.path.isfile(out_file): |
| 65 | + os.remove(out_file) |
| 66 | + |
| 67 | + |
| 68 | +# -------------------------------------------------- |
| 69 | +def test_file(): |
| 70 | + """Test file in/out""" |
| 71 | + |
| 72 | + for expected_file in os.listdir('test-outs'): |
| 73 | + try: |
| 74 | + out_file = random_string() |
| 75 | + if os.path.isfile(out_file): |
| 76 | + os.remove(out_file) |
| 77 | + |
| 78 | + basename = os.path.basename(expected_file) |
| 79 | + in_file = os.path.join('../inputs', basename) |
| 80 | + out = getoutput('{} {} {} {}'.format(prg, out_flag(), out_file, |
| 81 | + in_file)) |
| 82 | + assert out.strip() == '' |
| 83 | + produced = open(out_file).read().rstrip() |
| 84 | + expected = open(os.path.join('test-outs', |
| 85 | + expected_file)).read().strip() |
| 86 | + assert expected == produced |
| 87 | + finally: |
| 88 | + if os.path.isfile(out_file): |
| 89 | + os.remove(out_file) |
0 commit comments