forked from kyclark/new.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_test.py
executable file
·87 lines (64 loc) · 2.14 KB
/
new_test.py
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
#!/usr/bin/env python3
""" tests for new.py """
import io
import os
import random
import string
from subprocess import getstatusoutput
from shutil import rmtree
from new import get_defaults
PRG = './new.py'
# --------------------------------------------------
def test_exists():
""" Program exists """
assert os.path.isfile(PRG)
# --------------------------------------------------
def test_get_defaults():
""" Test get_defaults() """
expected = {
random_string(): random_string()
for _ in range(random.randint(3, 7))
}
text = io.StringIO('\n'.join(f'{k}={v}' for k, v in expected.items()))
assert get_defaults(text) == expected
assert get_defaults(None) == {}
# --------------------------------------------------
def test_usage():
""" Prints usage """
for flag in ['-h', '--help']:
retval, out = getstatusoutput(f'{PRG} {flag}')
assert retval == 0
assert out.lower().startswith('usage')
# --------------------------------------------------
def test_ok():
""" Runs ok """
cwd = os.path.abspath(os.getcwd())
dirname = os.path.join(cwd, random_string())
abs_new = os.path.abspath(PRG)
if os.path.isdir(dirname):
rmtree(dirname)
os.makedirs(dirname)
os.chdir(dirname)
try:
basename = random_string()
name = basename + '.py'
retval, out = getstatusoutput(f'{abs_new} -t {name}')
assert retval == 0
assert os.path.isfile(name)
assert out == f'Done, see new script "{name}".'
retval2, usage = getstatusoutput(f'./{name} --help')
assert retval2 == 0
assert usage.lower().startswith('usage:')
tests_dir = 'tests'
assert os.path.isdir(tests_dir)
test_file = os.path.join(tests_dir, f'{basename}_test.py')
assert os.path.isfile(test_file)
finally:
if os.path.isdir(dirname):
rmtree(dirname)
os.chdir(cwd)
# --------------------------------------------------
def random_string():
""" generate a random string """
k = random.randint(5, 10)
return ''.join(random.choices(string.ascii_letters + string.digits, k=k))