@@ -41,6 +41,57 @@ all_environments = pv_environments + hvm_environments
41
41
class RunnerError (Exception ):
42
42
""" Errors relating to xtf-runner itself """
43
43
44
+ class TestInfo (object ):
45
+ """ Object representing a tests info.json, in a more convenient form. """
46
+
47
+ def __init__ (self , test_json ):
48
+ """Parse and verify 'test_json'.
49
+
50
+ May raise KeyError, TypeError or ValueError.
51
+ """
52
+
53
+ name = test_json ["name" ]
54
+ if not isinstance (name , basestring ):
55
+ raise TypeError ("Expected string for 'name', got '%s'"
56
+ % (type (name ), ))
57
+ self .name = name
58
+
59
+ cat = test_json ["category" ]
60
+ if not isinstance (cat , basestring ):
61
+ raise TypeError ("Expected string for 'category', got '%s'"
62
+ % (type (cat ), ))
63
+ if not cat in all_categories :
64
+ raise ValueError ("Unknown category '%s'" % (cat , ))
65
+ self .cat = cat
66
+
67
+ envs = test_json ["environments" ]
68
+ if not isinstance (envs , list ):
69
+ raise TypeError ("Expected list for 'environments', got '%s'"
70
+ % (type (envs ), ))
71
+ if not envs :
72
+ raise ValueError ("Expected at least one environment" )
73
+ for env in envs :
74
+ if not env in all_environments :
75
+ raise ValueError ("Unknown environments '%s'" % (env , ))
76
+ self .envs = envs
77
+
78
+ def all_instances (self , env_filter = None ):
79
+ """Return a list of test instances, for each supported environment.
80
+ Optionally filtered by env_filter. May return an empty list if
81
+ the filter doesn't match any supported environment.
82
+ """
83
+
84
+ if env_filter :
85
+ envs = set (env_filter ).intersection (self .envs )
86
+ else :
87
+ envs = self .envs
88
+
89
+ return [ "test-%s-%s" % (env , self .name ) for env in envs ]
90
+
91
+ def __repr__ (self ):
92
+ return "TestInfo(%s)" % (self .name , )
93
+
94
+
44
95
# Cached test json from disk
45
96
_all_test_info = {}
46
97
@@ -54,7 +105,6 @@ def get_all_test_info():
54
105
for test in os .listdir ("tests" ):
55
106
56
107
info_file = None
57
- test_json = {}
58
108
try :
59
109
60
110
# Ignore directories which don't have a info.json inside them
@@ -65,26 +115,15 @@ def get_all_test_info():
65
115
66
116
# Ignore tests which have bad JSON
67
117
try :
68
- test_json = json .load (info_file )
69
- except ValueError :
70
- continue
118
+ test_info = TestInfo (json .load (info_file ))
71
119
72
- # Sanity check JSON fields and types
73
- if (not isinstance (test_json .get ("name" , None ), basestring ) or
74
- not isinstance (test_json .get ("category" , None ), basestring ) or
75
- not isinstance (test_json .get ("environments" , None ), list )):
76
- continue
120
+ if test_info .name != test :
121
+ continue
77
122
78
- # Sanity check JSON values
79
- if test_json ["name" ] != test :
80
- continue
81
- if test_json ["category" ] not in all_categories :
123
+ except (ValueError , KeyError , TypeError ):
82
124
continue
83
- for test_env in test_json ["environments" ]:
84
- if test_env not in all_environments :
85
- continue
86
125
87
- _all_test_info [test ] = test_json
126
+ _all_test_info [test ] = test_info
88
127
89
128
finally :
90
129
if info_file :
@@ -126,11 +165,11 @@ def list_tests(opts):
126
165
127
166
info = all_test_info [name ]
128
167
129
- if cat and info [ "category" ] not in cat :
168
+ if cat and info . cat not in cat :
130
169
continue
131
170
132
171
if env :
133
- for test_env in info [ "environments" ] :
172
+ for test_env in info . envs :
134
173
if test_env in env :
135
174
break
136
175
else :
@@ -201,31 +240,20 @@ def run_tests(opts):
201
240
202
241
# If arg is a recognised test name, run every environment
203
242
if arg in all_test_names :
204
-
205
- info = all_test_info [arg ]
206
-
207
- for env in info ["environments" ]:
208
- tests .append ("test-%s-%s" % (env , arg ))
243
+ tests .extend (all_test_info [arg ].all_instances ())
209
244
continue
210
245
211
246
# If arg is a recognised category, run every included test
212
247
if arg in all_categories :
213
-
214
- for name , info in all_test_info .iteritems ():
215
-
216
- if info ["category" ] == arg :
217
-
218
- for env in info ["environments" ]:
219
- tests .append ("test-%s-%s" % (env , name ))
248
+ for info in all_test_info .values ():
249
+ if info .cat == arg :
250
+ tests .extend (info .all_instances ())
220
251
continue
221
252
222
253
# If arg is a recognised environment, run every included test
223
254
if arg in all_environments :
224
-
225
- for name , info in all_test_info .iteritems ():
226
-
227
- if arg in info ["environments" ]:
228
- tests .append ("test-%s-%s" % (arg , name ))
255
+ for info in all_test_info .values ():
256
+ tests .extend (info .all_instances (env_filter = [arg ]))
229
257
continue
230
258
231
259
parts = arg .split ('-' , 2 )
0 commit comments