-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyeature.py
executable file
·492 lines (382 loc) · 16.5 KB
/
pyeature.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/python
# coding: utf-8
import re, types, sys, traceback, os
sys.path.append(os.getcwd())
import optparse
import pyeature
##
## Keyword definitions
##
lang = {
'en': {
'feature': 'Feature', 'scenario': 'Scenario',
'given': 'Given', 'when': 'When', 'then': 'Then', 'and': 'And',
},
'ko': {
'feature': '기능', 'scenario': '시나리오',
'given': '처음에',
'when': '만약',
'then': '그러면',
'and': '그리고',
#'and': ['그리고', '또'],
},
}
lang[None] = lang['en'] # default
##
##
class Helper:
@staticmethod
def directory_name(filename):
""" returns directory of the filename(or directory, if that's what you give me) """
full_filename = os.path.abspath(filename)
if os.path.isdir(full_filename):
return full_filename
else:
return os.path.dirname(full_filename)
@staticmethod
def error(msg):
sys.stderr.write(msg+"\n")
class World: pass
FEATURE, SCENARIO = 'feature', 'scenario'
GIVEN, WHEN, THEN, AND = 'given', 'when', 'then', 'and'
class Patterns:
""" patterns used to parse sentence of feature file """
def __init__(self, keyword_dict={}):
default_dict = lang['en']
default_dict.update(keyword_dict)
self.keyword_dict = default_dict
def set_pattern(self, contents, template=r'^\s*(%s)'):
content = "|".join(self.keyword_dict[k] for k in contents)
return re.compile(template % content, re.IGNORECASE)
def match(self, line, *target):
return self.set_pattern(target).match(line)
def remove_clause_name_prefix(self, clause):
""" "Given I did ..." => "I did ..." """
starts_with_clause_name = self.set_pattern([GIVEN,WHEN,THEN], "^(%s)\s+")
return starts_with_clause_name.sub('', clause)
@staticmethod
def starts_with_clause_name_and_underscore(string):
return re.compile("^(%s)_" % "|".join([GIVEN,WHEN,THEN]), re.IGNORECASE).match(string)
def change_and_clause_name(self, clause, new_clause_name):
""" "Given ...\nAnd something" => "Given something" """
and_clause = self.set_pattern(["and"])
return and_clause.sub(new_clause_name, clause)
def match_feature(self, line): return self.match(line, FEATURE)
def match_scenario(self, line): return self.match(line, SCENARIO)
def match_and_clause(self, line): return self.match(line, AND)
def match_any_keyword(self, line): return self.match(line, FEATURE,SCENARIO, GIVEN,WHEN,THEN,AND)
def match_clause(self, line): return self.match(line, GIVEN,WHEN,THEN)
# step decorator functions
given = lambda clause: Loader.step_decoration(clause)
when=given
then=given
class Loader:
""" loads methods from step definitions """
global_world = World()
loaded_clauses = {}
def load_steps(self, filename):
""" load methods from step definition file (or directory)
it first finds module names from file or directory,
imports the modules,
returning the methods from it
"""
sys.path.append(Helper.directory_name(filename))
# find module names
full_filename = os.path.abspath(filename)
filename_parts = self.find_module_names(full_filename)
# import modules and methods from it
modules = self.import_modules(filename_parts)
clause_methods = Matcher.clause_methods_of(modules)
for method in clause_methods:
pyeature.Loader.loaded_clauses[method.__name__] = method
sys.path.pop()
return pyeature.Loader.loaded_clauses
def import_modules(self, module_names):
""" import every modules possible given their names (not files) """
imported_modules = [self.try_importing_module(name) for name in module_names]
imported_modules = filter(None, imported_modules)
return imported_modules
def try_importing_module(self, module_name):
""" try importing a module, catching all exceptions """
try:
if module_name in sys.modules:
new_module = __import__(module_name)
new_module = reload(new_module)
else:
new_module = __import__(module_name)
new_module.self = self.global_world
return new_module
except ImportError, e:
Helper.error("%s: failed to load %s" % (str(e), module_name) )
except ValueError, e:
pass
def find_module_names(self, full_filename):
""" find module names, either from filename or directory """
filename_part = lambda x: os.path.basename(x).rsplit('.', 1)[0]
if os.path.isdir(full_filename):
files = os.listdir(full_filename)
files = filter(lambda x: x.endswith('.py') or x.endswith('.pyc'), files)
names = map(filename_part, files)
else:
names = [filename_part(full_filename)]
# uniq
names = list(set(names))
return names
@staticmethod
def step_decoration(clause):
def working_method(method):
pyeature.Loader.loaded_clauses[clause] = method
return method
return working_method
class Matcher:
re_type = type(re.compile(''))
""" match each sentence with clauses """
def __init__(self, clause_methods=[]):
self.previous_clause_name = None
self.clause_methods = clause_methods
self.patterns = Patterns()
def clause2methodname(self, clause):
""" convert a clause sentence into a method name
- ' Given some pre-condition' => 'given_some_pre_condition'
- 'And another given' => 'given_another_given' (checking previous)
- 'Then I have then some sp3c!al,, characters?!'
=> 'then_i_have_then_some_sp3c_al_characters_'
"""
def clause_name_of(sentence):
""" return clause name of sentence, if it is a clause (None if not)
Given|When|Then => itself
And => previous clause name
anything else => None
"""
matched = self.patterns.match_clause(sentence)
# store as previous clause name when Given|When|Then
if matched:
self.previous_clause_name = matched.group().lstrip()
# None if it isn't And clause
elif not self.patterns.match_and_clause(sentence):
return
return self.previous_clause_name
clause_name = clause_name_of(clause)
if not clause_name: return
# change prefix of clause
clause = self.patterns.change_and_clause_name(clause, clause_name)
convert_space = lambda c: re.sub(r'\W+', '_', c.lstrip()).lower()
return convert_space(clause)
def find_method_by_name(self, name, default=None):
return self.clause_methods.get(name, default)
def find_method_by_clause(self, clause):
''' find and return appropriate method for the given clause
for every keys registered in loaded_clauses, try matching
1. the clause itself as string
2. the clause converted into method name
3. the clause without clause prefix
4. the clause as regex
#5. the clause without clause prefix as regex
'''
clause = clause.strip()
for method_key,method in pyeature.Loader.loaded_clauses.iteritems():
clause_wo_prefix = self.patterns.remove_clause_name_prefix(clause)
# string
if isinstance(method_key, types.StringType):
any_chances = [clause, self.clause2methodname(clause), clause_wo_prefix]
if method_key in any_chances:
return method
# re
elif isinstance(method_key, Matcher.re_type):
md = method_key.search(clause) or method_key.search(clause_wo_prefix)
if md:
args = [md.group(0)] + list(md.groups())
method.func_globals['args'] = args #
return method
@staticmethod
def clause_methods_of(modules):
""" return list of clause methods from given modules """
if not isinstance(modules, types.ListType):
modules = [modules]
# search each modules for functions
methods = []
for module in modules:
name2item = lambda x: vars(module)[x]
new_methods = [name2item(x) for x in dir(module)]
# filter functions from module
new_methods = filter(lambda x: type(x) == types.FunctionType, new_methods)
methods.extend(new_methods)
# filter by clause names
methods = filter(lambda x: Matcher.is_clause_method_name(x.__name__), methods)
return methods
@staticmethod
def is_clause_method_name(method_name):
""" returns true if method name starts with given_, when_, or then_,
or if is before or after """
if method_name in ["before", "after"]:
return True
return Patterns.starts_with_clause_name_and_underscore(method_name)
def is_feature(self, clause): return self.patterns.match_feature(clause)
def is_scenario(self, clause): return self.patterns.match_scenario(clause)
class Reporter:
def __init__(self, output=sys.stdout):
self.output = output
def report(self, content, status=None):
# skip stop fail sucess
status_key = {
"skip": "-",
"stop": "X",
"fail": "F",
"success": ".",
}
try:
self.write("("+ status_key[status] +") ")
except KeyError:
pass
self.write(content)
if status == "fail":
self.write("\n")
self.write('-'*60 + "\n")
traceback.print_exc(file=self.output)
self.write('-'*60)
def write(self, msg, code=None):
self.output.write(msg)
class ColorReporter(Reporter):
ANSI_CODES = {
"reset" : "\x1b[0m",
"bold" : "\x1b[01m",
"boldcyan" : "\x1b[36;01m",
"cyan" : "\x1b[36;06m",
"fuscia" : "\x1b[35;01m",
"purple" : "\x1b[35;06m",
"boldblue" : "\x1b[34;01m",
"blue" : "\x1b[34;06m",
"boldgreen" : "\x1b[32;01m",
"green" : "\x1b[32;06m",
"boldyellow": "\x1b[33;01m",
"yellow" : "\x1b[33;06m",
"boldred" : "\x1b[31;01m",
"red" : "\x1b[31;06m",
}
def report(self, content, status=None):
# skip stop fail sucess
color_key = {
"skip": "cyan",
"stop": "yellow",
"fail": "red",
"success": "green",
}
try:
code = color_key[status]
except KeyError:
code = None
self.write(content, code)
if status == "fail":
self.write("\n")
self.write('-'*60 + "\n")
traceback.print_exc(file=self.output)
self.write('-'*60)
def write(self, msg, code=None):
if code and code in self.ANSI_CODES:
self.output.write(self.ANSI_CODES[code])
self.output.write(msg)
if code:
self.output.write(self.ANSI_CODES["reset"])
class Runner:
def __init__(self, clause_methods=[], output=sys.stdout):
self.matcher = Matcher(clause_methods)
self.reporter = ColorReporter(output)
def run_clauses(self, clauses):
""" given clauses and set of method candidates,
run each clause in order and
write result in output.
Exceptions will not be raised, but just written to output """
# run before method
self.matcher.find_method_by_name('before', default=lambda:None)()
# run until finish or error
unimplemented, i = [], 0
for i,clause in enumerate(clauses):
if i is not 0: self.reporter.write("\n")
# skip feature and scenario
if self.matcher.is_feature(clause) or self.matcher.is_scenario(clause):
self.reporter.report(clause)
continue
# find method for clause
#print 1
clause_method = self.matcher.find_method_by_clause(clause)
# stop if no method found
if not clause_method:
self.reporter.report(clause, "stop")
unimplemented.append(clause)
break
# run method
success = self.run_method(clause_method, clause)
if not success:
break
success_count = i
if i is not 0: self.reporter.write("\n")
# run after method
self.matcher.find_method_by_name('after', default=lambda: None)()
unimplemented = self.report_remaining_methods(clauses[i+1:], unimplemented)
self.report_suggesting_unimplemented_methods(unimplemented)
return success_count
def report_remaining_methods(self, remaining_clauses, unimplemented):
for clause in remaining_clauses:
if self.matcher.find_method_by_clause(clause):
self.reporter.report(clause+"\n", "skip")
else:
self.reporter.report(clause+"\n", "stop")
unimplemented.append(clause)
self.reporter.write("\n")
return unimplemented
def report_suggesting_unimplemented_methods(self, unimplemented):
if unimplemented:
methods_to_suggest = [self.matcher.clause2methodname(c) for c in unimplemented]
method_definitions = ["""def %s():\n\tassert False, "Implement me!"\n""" % m for m in methods_to_suggest]
suggesting_method_doc = """\nCreate the following method: \n\n%s\n""" % "\n".join(method_definitions)
self.reporter.report(suggesting_method_doc, "stop")
def run_method(self, method, clause):
""" run a clause with method given, and report result to output """
try:
method()
except:
self.reporter.report(clause, "fail")
return False
else:
self.reporter.report(clause, "success")
return True
def extract(text, keyword_dict={}):
""" extracts a list of clauses from given text """
extracts = [line.rstrip("\n") for line in text.split("\n")]
extracts = filter(Patterns(keyword_dict).match_any_keyword, extracts)
return extracts
def extract_file(filename, keyword_dict={}):
text = open(filename).read()
return extract(text, keyword_dict)
def run(feature_file, step_file_dir, options, output=sys.stdout):
""" default method for pyeature: run given feature file with given step file(or directory)
returns number of successful steps ran
"""
# load clauses from feature and methods from step definition
clauses = extract_file(feature_file, lang[options.lang])
clause_methods = Loader().load_steps(step_file_dir)
# run clauses
return Runner(clause_methods, output=output).run_clauses(clauses)
def parse_args(args=sys.argv[1:]):
# set options to parse
usage = "usage: %prog [options] some.feature [some_step.py]"
parser = optparse.OptionParser(usage=usage)
parser.add_option("-q", "--quiet", dest="quiet", action="store_true", default=False, help="be quite, and only return 0 or some other value that indicates whether all test ran successfully or not")
parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help="be more verbose, and print additional information while running")
parser.add_option("-l", "--language", metavar="LANG", dest="lang", default='en', help="target language to parse (default en)")
(options, args) = parser.parse_args(args)
# rest of arguments
if len(args) < 1:
parser.error("You must tell me the feature file and step definition file.")
feature_file = args[0]
if len(args) >= 2:
step_definition_directory = args[1]
else:
dirname = os.path.dirname(os.path.abspath(feature_file))
step_definition_directory = os.path.join( dirname, 'step_definitions' )
if not os.path.isdir(step_definition_directory):
raise IOError("no such directory: '%s'" % step_definition_directory)
return feature_file, step_definition_directory, options
if __name__ == '__main__':
feature_file, step_definition_directory, options = parse_args()
run(feature_file, step_definition_directory, options)