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