-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprepare_env.py
executable file
·111 lines (85 loc) · 3.63 KB
/
prepare_env.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
import sys
import types
import logging
import argparse
from misttests import config
from functools import reduce
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def prepare_arg_parser(parser, arg_list, arg_name, *args, **kwargs):
parser.add_argument(arg_name, *args, **kwargs)
arg_list.append(arg_name)
def arg_index(arg_list, arg):
for i in range(len(arg_list)):
if arg_list[i].startswith(arg):
return i
raise ValueError
def arg_to_snake(s):
return reduce(lambda y, z: y + '_' + z,
[x for x in s.split('-') if x != ''])
def snake_to_arg(s):
return '--' + reduce(lambda y, z: y + '-' + z,
[x for x in s.lower().split('_') if x != ''])
def strtobool(s):
if s.lower() in ['true', 'yes', 'ja']:
return True
if s.lower() in ['false', 'no', 'nein']:
return False
raise ValueError('What the hell is this supposed to be: %s?' % s)
def update_test_settings(arguments, cleanup_list):
for attr_name in cleanup_list:
argument_attr_name = arg_to_snake(attr_name)
snake_attr_name = argument_attr_name.swapcase()
if hasattr(config, snake_attr_name) and hasattr(arguments, argument_attr_name):
new_value = getattr(arguments, argument_attr_name)
if new_value:
old_value = getattr(config, snake_attr_name)
if isinstance(old_value, bool):
new_value = strtobool(new_value)
setattr(config, snake_attr_name, new_value)
print(("%s: %s -> %s" % (snake_attr_name, old_value, new_value)))
def clean_args(arg_list, args_to_be_removed):
if len(arg_list) == 0 or len(args_to_be_removed) == 0:
return
next_arg_to_clean = args_to_be_removed.pop()
try:
while True:
index = arg_index(arg_list, next_arg_to_clean)
del arg_list[index]
while index < len(arg_list) and not arg_list[index].startswith('-'):
del arg_list[index]
except ValueError:
pass
clean_args(arg_list, args_to_be_removed)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Will execute behavioral'
'(default) or api tests and '
'can also override some of the'
' test user data')
cleanup_list = []
for attr in dir(config):
if not isinstance(attr, types.FunctionType):
if isinstance(attr, str):
if attr.startswith('__') or attr.islower():
continue
arg = snake_to_arg(attr)
prepare_arg_parser(parser, cleanup_list, arg, default=None)
prepare_arg_parser(parser, cleanup_list, '--gui', action='store_true')
prepare_arg_parser(parser, cleanup_list, '--api', action='store_true')
args = parser.parse_known_args()[0]
if args.gui and args.api:
raise Exception("You must either provide the gui or the api flag but "
"not both. If you provide no flag then the behave will"
" be invoked")
update_test_settings(args, cleanup_list)
# Make sure to remove any args before handing over to the behave or the
# py.test main to prevent errors
args_to_be_cleaned = sys.argv[1:]
clean_args(args_to_be_cleaned, cleanup_list)
if args.gui or not args.api:
import behave.__main__
sys.exit(behave.__main__.main(args_to_be_cleaned))
else:
import pytest
sys.exit(pytest.main(args_to_be_cleaned))