/
should.py
executable file
·1347 lines (1044 loc) · 40.8 KB
/
should.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# should -- Test command-line applications through .should files
#
# Copyright (C) 2018-2020 by CRIStAL (UMR CNRS 9189, Université Lille) and Inria Lille
# Contributors:
# Mathieu Giraud <mathieu.giraud@vidjil.org>
# Mikaël Salson <mikael.salson@vidjil.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# "should" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with "should". If not, see <http://www.gnu.org/licenses/>
import sys
if not (sys.version_info >= (3, 4)):
print("Python >= 3.4 required")
sys.exit(1)
__version_info__ = ('3','0','0')
__version__ = '.'.join(__version_info__)
import re
import argparse
import subprocess
import time
import random
import os.path
from collections import defaultdict, OrderedDict
import xml.etree.ElementTree as ET
import datetime
import tempfile
import json
# Make sure the output is in utf8
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
sys.stderr = open(sys.stderr.fileno(), mode='w', encoding='utf8', buffering=1)
DEFAULT_CFG = 'should.cfg'
RETRY_FAILED = 'should.retry'
RETRY_FAILED_FLAG = '--retry'
RETRY_WARNED = 'should.warned.retry'
RETRY_WARNED_FLAG = '--retry-warned'
TOKEN_COMMENT = '#'
TOKEN_DIRECTIVE = '!'
TOKEN_NAME = '$'
TOKEN_TEST = ':'
RE_TEST = re.compile('^(\S)*[:]')
DIRECTIVE_REQUIRES = '!REQUIRES:'
DIRECTIVE_NO_LAUNCHER = '!NO_LAUNCHER:'
DIRECTIVE_SCRIPT = '!LAUNCH:'
DIRECTIVE_NO_EXTRA = '!NO_EXTRA:'
DIRECTIVE_OPTIONS = '!OPTIONS:'
DIRECTIVE_SOURCE = '!OUTPUT_FILE:'
DIRECTIVE_EXIT_CODE = '!EXIT_CODE:'
VAR_LAUNCHER = '$LAUNCHER'
VAR_EXTRA = '$EXTRA'
MOD_TODO = 'f'
MOD_ALLOW = 'a'
MOD_REGEX = 'r'
MOD_COUNT_ALL = 'w'
MOD_IGNORE_CASE = 'i'
MOD_BLANKS = 'b'
MOD_MULTI_LINES = 'l'
MOD_KEEP_LEADING_TRAILING_SPACES = 'z'
MOD_JSON = 'j'
MOD_MORE_THAN = '>'
MOD_LESS_THAN = '<'
TIMEOUT = 120
SHOW_ELAPSED_TIME_ABOVE = 1.0
RE_MODIFIERS = re.compile('^(\D*)(\d*)(\D*)$')
OUT_LOG = '.log'
OUT_TAP = '.tap'
OUT_XML = 'should.xml'
TAP = 'tap'
XML = 'xml'
JSON_KEY_NOT_FOUND = 'not found'
LINE = '-' * 40
ENDLINE_CHARS = '\r\n'
CONTINUATION_CHAR = '\\'
MAX_HALF_DUMP_LINES = 45
MAX_DUMP_LINES = 2*MAX_HALF_DUMP_LINES + 10
NOT_ZERO = '+'
# Simple colored output
CSIm = '\033[%sm'
class ANSI:
RESET = 0
BRIGHT = 1
BLACK = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
WHITE = 37
def color(col, text, colorize = True):
if not colorize:
return text
return CSIm % col + text + CSIm % ANSI.RESET
# Status
S_FAILED = 'failed'
S_TODO_PASSED = 'TODO-but-ok'
S_TODO = 'TODO'
S_AF = 'failed-but-ALLOW'
S_SKIP = 'skip'
S_OK = 'ok'
S_NOT_RUN = 'not-run'
FAIL_STATUS = [S_FAILED, S_TODO_PASSED]
WARN_STATUS = FAIL_STATUS + [S_AF, S_TODO, S_SKIP]
NO_ALIAS = 'no-alias'
class Status():
ALL = []
def __init__(self, num, name, color, tap_str, alias=NO_ALIAS):
self.num = num
self.name = name
self.color = color
self.tap_str = tap_str
self.alias = alias
def is_failed(self):
return self.name in FAIL_STATUS
def is_warned(self):
return self.name in WARN_STATUS
def tap(self):
return self.tap_str
def xml(self):
if self.name == S_FAILED:
return 'failure'
if self.name == S_SKIP:
return 'skipped'
return self.name
def out(self, format, colorize=True):
if format == XML:
return self.xml()
if format == TAP:
return self.tap()
return self.__str__(colorize)
def or_alias(self):
if self.alias == NO_ALIAS:
return self.name
return self.alias
def __hash__(self):
return hash(self.name)
def __add__(self, other):
'''
>>> (Sta(S_NOT_RUN) + Sta(S_OK)).name == S_OK
True
>>> (Sta(S_TODO) + Sta(S_FAILED)).name == S_FAILED
True
>>> (Sta(S_OK) + Sta(S_SKIP)).name == S_SKIP
True
>>> (Sta(S_TODO_PASSED) + Sta(S_OK)).name == S_TODO_PASSED
True
'''
return self if self.num > other.num else other
def __str__(self, colorize=True):
return '%s' % color(self.color, self.name, colorize)
Status.ALL = [# name color tap alias
# S_FAILED
Status(99, S_FAILED, ANSI.RED, 'not ok', alias=False),
Status(98, S_TODO_PASSED, ANSI.RED, 'ok # TODO'),
# Warned
Status(12, S_TODO, ANSI.CYAN, 'not ok # TODO',),
Status(11, S_AF, ANSI.CYAN, 'not ok # SKIP'),
Status(10, S_SKIP, ANSI.CYAN, 'ok # SKIP'),
# Passed
Status( 1, S_OK, ANSI.GREEN, 'ok', alias=True),
# Forgotten status when mixed to other tests
Status( 0, S_NOT_RUN, ANSI.CYAN, 'not run', alias=None),
]
def Sta(sta = S_NOT_RUN):
'''
Get a constant status, from Status.ALL, by its name or its alias
'''
for s in Status.ALL:
if s.alias == sta or s.name == sta:
return s
return None
# Modifier parser
MODIFIERS = [
(MOD_TODO, 'todo', 'consider that the test should fail'),
(MOD_ALLOW, 'allow', 'consider that the test is allowed to fail'),
(MOD_REGEX, 'regex', 'consider as a regular expression'),
(MOD_COUNT_ALL, 'count-all', 'count all occurrences, even on a same line'),
(MOD_IGNORE_CASE, 'ignore-case', 'ignore case changes'),
(MOD_BLANKS, 'blanks', "ignore whitespace differences as soon as there is at least one space. Implies 'r'"),
(MOD_MULTI_LINES, 'multi-lines', 'search on all the output rather than on every line'),
(MOD_KEEP_LEADING_TRAILING_SPACES, 'ltspaces', 'keep leading and trailing spaces'),
(MOD_JSON, 'json', "interpret json data. Implies '" + MOD_MULTI_LINES + MOD_COUNT_ALL + "'"),
(MOD_MORE_THAN, 'more-than', 'requires that the expression occurs strictly more than the given number'),
(MOD_LESS_THAN, 'less-than', 'requires that the expression occurs strictly less than the given number'),
]
class ArgParser(argparse.ArgumentParser):
def convert_arg_line_to_args(self, l):
'''
More flexible argument parsing from configuration file:
- ignore leading and trailing spaces
- allow empty lines
'''
ll = l.strip()
return [ ll ] if ll else [ ]
class ModifierParser(ArgParser):
def parse_modifiers(self, modifiers):
mods, unknown = self.parse_known_args(['-' + mod for mod in modifiers])
for m in unknown:
sys.stderr.write("! Unknown modifier '%s'\n" % m[1])
return mods
parser_mod = ModifierParser()
parser_mod.help = 'modifiers (uppercase letters cancel previous modifiers)\n'
for (mod_char, mod_long, mod_help) in MODIFIERS:
parser_mod.add_argument('-' + mod_char, '--' + mod_long, action='store_true', help=mod_help)
if mod_char.upper() != mod_char:
parser_mod.add_argument('-' + mod_char.upper(), dest=mod_long.replace('-', '_'), action='store_const', const=False, default=False,
help='back to default, overriding any previous -%s' % mod_char)
help_upper = '/%s' % mod_char.upper()
else:
help_upper = ' '
parser_mod.help += ' %s%s %s\n' % (mod_char, help_upper, mod_help)
# Main argument parser
parser = ArgParser(description='Test command-line applications through .should files',
fromfile_prefix_chars='@',
epilog='''Example (see also README.md and demo/*.should):
%(prog)s demo/hello.should''',
add_help=False,
formatter_class=argparse.RawTextHelpFormatter)
options = ArgParser(fromfile_prefix_chars='@') # Can be used in !OPTIONS: directive
group = parser.add_argument_group('running tests (can also be set per test in !OPTIONS)')
for p in (group, options):
p.add_argument('--cd', metavar='PATH', help='directory from which to run the test commands')
p.add_argument('--cd-same', action='store_true', help='run the test commands from the same directory as the .should files')
p.add_argument('--launcher', metavar='CMD', default='', help='launcher preceding each command (or replacing %s)' % VAR_LAUNCHER)
p.add_argument('--extra', metavar='ARG', default='', help='extra argument after the first word of each command (or replacing %s)' % VAR_EXTRA)
p.add_argument('--mod', metavar='MODIFIERS', action='append', help='global ' + parser_mod.help)
p.add_argument('--var', metavar='NAME=value', action='append', help='variable definition (then use $NAME in .should files)')
p.add_argument('--timeout', type=int, default = TIMEOUT, help = 'Delay (in seconds) after which the task is stopped (default: %(default)d)')
group = parser.add_argument_group('selecting tests to be run')
group.add_argument('--shuffle', action='store_true', help='shuffle the tests')
group.add_argument('--no-a', action='store_true', help="do not launch 'a' tests")
group.add_argument('--no-f', action='store_true', help="do not launch 'f' tests")
group.add_argument('--only-a', action='store_true', help="launches only 'a' tests")
group.add_argument('--only-f', action='store_true', help="launches only 'f' tests")
group.add_argument(RETRY_FAILED_FLAG, action='store_true', help='launches only the last failed tests')
group.add_argument(RETRY_WARNED_FLAG, action='store_true', help='launches only the last failed or warned tests')
output = parser.add_argument_group('controlling output')
output.add_argument('--log', action='append_const', dest='output', const=OUT_LOG, help='stores the output into .log files')
output.add_argument('--tap', action='append_const', dest='output', const=OUT_TAP, help='outputs .tap files')
output.add_argument('--xml', action='append_const', dest='output', const=OUT_XML, help='outputs JUnit-like XML into %s' % OUT_XML)
output.add_argument('-v', '--verbose', action='count', help='increase verbosity', default=1)
output.add_argument('-q', '--quiet', action='store_const', dest='verbose', const=0, help='verbosity to zero')
output.add_argument('--fail-a', action='store_true', help="fail on passing 'a' tests")
output.add_argument("-h", "--help", action="help", help="show this help message and exit")
output.add_argument('--version', action='version',
version='%(prog)s {version}'.format(version=__version__))
parser.add_argument('file', metavar='should-file', nargs='+', help='''input files (.should)''')
class ShouldException(BaseException):
pass
def write_to_file(f, what, phony=True):
if phony:
print('==> %s' % f)
with open(f, 'w', encoding='utf-8') as ff:
ff.write(what)
RE_GETITEM = re.compile('(\S*?)\[(\S+?)\](\S*)$')
def deep_get(d, key, sep='.'):
'''
>>> d = {'1':{ '2': 3, '4': 5}, 'z': [6, {'a': 7}, [8, {'b': 9}]]}
>>> deep_get(d, '1.2')
3
>>> deep_get(d, '3')
Traceback (most recent call last):
KeyError: '3'
>>> deep_get(d, 'z[1].a')
7
>>> deep_get(d, 'z[2][0]')
8
>>> deep_get(d, 'z[2][1].b')
9
>>> deep_get(d, 'z[3]')
Traceback (most recent call last):
KeyError: 'z[3]'
>>> deep_get(9, '')
9
>>> deep_get([1, 2, 3], '[1]')
2
'''
def deep_get_(d, keys):
if not keys:
return d
if not keys[0]:
return d
m = RE_GETITEM.match(keys[0])
if m:
key = m.group(1)
index = int(m.group(2))
s_index_next = m.group(3)
if key:
d = d[key]
obj = d[index]
if s_index_next:
keys = [s_index_next] + keys[1:]
else:
keys = keys[1:]
else:
obj = d[keys[0]]
keys = keys[1:]
return deep_get_(obj, keys)
try:
return deep_get_(d, key.split(sep))
except:
raise KeyError(key)
# Command pre-processing
def pre_process(cmd):
cc = cmd.split(' ')
if not VAR_EXTRA in cmd:
cc = [cc[0], VAR_EXTRA] + cc[1:]
if not VAR_LAUNCHER in cmd:
cc = [VAR_LAUNCHER] + cc
return ' '.join(cc)
# Variables definition and expansion
def populate_variables(var):
'''
>>> populate_variables(['ab=cd', 'ef=xyz'])
[('$ef', 'xyz'), ('$ab', 'cd')]
'''
variables = []
if var:
for v in var:
try:
key, var = v.split('=')
variables = [('$' + key, var)] + variables
except IOError:
raise ShouldException('Error in parsing variable definition: ' + v)
return variables
def print_variables(variables):
for (k, v) in variables:
print('%s=%s' % (k, v))
print('')
def replace_variables(s, variables):
'''
>>> replace_variables('hello', None)
'hello'
>>> replace_variables('hello', [('hell', 'w'), ('o', 'orld')])
'world'
>>> replace_variables('xyz xyz', [('y', 'abc')])
'xabcz xabcz'
'''
if variables:
for (key, val) in variables:
s = s.replace(key, val)
return s
class OrderedDefaultListDict(OrderedDict):
def __missing__(self, key):
self[key] = value = []
return value
class Stats():
'''
>>> s = Stats('foo')
>>> s.up(2)
>>> list(s.keys())
[2]
>>> s[2]
[1]
>>> t = Stats()
>>> t.up(2, 'hello')
>>> t.up(3)
>>> u = s + t
>>> sorted(u.keys())
[2, 3]
>>> list(s.keys())
[2]
>>> sorted(u.items())
[(2, [1, 'hello']), (3, [1])]
'''
def __init__(self, item=''):
self.stats = OrderedDefaultListDict()
self.item = item
def __getitem__(self, key):
return self.stats[key]
def up(self, key, data=1):
self.stats[key].append(data)
def __setitem__(self, key, value):
self.stats[key] = value
def keys(self):
return self.stats.keys()
def items(self):
return self.stats.items()
def __iter__(self):
'''Ordered according to Status.ALL'''
for key in Status.ALL[::-1]:
if key in self.stats:
yield (key, self[key])
def values(self):
return self.stats.values()
def total(self):
return sum(map(len, self.stats.values()))
def __add__(self, other):
result = Stats(self.item)
for data in (self, other):
for key in data.keys():
result.stats[key] += data.stats[key]
return result
def str_status(self, status, colorize=True):
s = '==> '
s += status.name
s += ' - '
s = color(status.color, s, colorize)
s += ' '.join([color(key.color, '%s:%d', colorize) % (key.name, len(val)) for (key, val) in self])
nb_items = '- total:%s' % self.total()
if self.item:
nb_items += ' ' + self.item + ('s' if self.total() > 1 else '')
s += ' ' + color(status.color, nb_items, colorize)
return s
class TestCaseAbstract:
def __init__(self):
raise NotImplemented
def str_additional_status(self, verbose=False):
return ''
def str(self, format=None, verbose=False, colorize=True):
s = ''
s += self.status.out(format=format,colorize=colorize)
s += self.str_additional_status(verbose=verbose)
if self.name:
s += ' - ' + self.name
return s
def xml(self):
x = ET.Element('testcase', {'name': self.name, 'status': self.status.xml()})
if self.status.is_warned():
x.append(ET.Element(self.status.xml(),
{'message': repr(self) + '\n' + self.str(format=XML, colorize=False)}))
return x
def tap(self, verbose=False):
return self.str(format=TAP, colorize=False, verbose=False)
def __str__(self):
return self.str(colorize=True, verbose=True)
def __repr__(self):
raise NotImplemented
class ExternalTestCase(TestCaseAbstract):
def __init__(self, name, sta, info=''):
self.name = name
self.status = Sta(sta)
self.info = info
self.modifiers = ''
self.raw = None
self.json_data = None
def str_additional_status(self, verbose = False):
s = ''
if self.status.is_warned() or verbose:
s += ' (%s)' % self.info
return s
def test(self, *args, **kwargs):
pass
def __repr__(self):
return self.info
class TestCase(TestCaseAbstract):
'''
>>> test = TestCase('', 'hello')
>>> repr(test)
':hello'
>>> test.str(colorize=False)
'not-run'
>>> test.test(['world'])
False
>>> test.status.or_alias()
False
>>> test.test(['hello'])
True
>>> test = TestCase('3', 'hello')
>>> repr(test)
'3:hello'
>>> test.test(['hello'])
False
>>> test.count
1
>>> print(test.str(colorize=False))
failed (1/3)
>>> test.tap()
'not ok (1/3)'
>>> test.test(['hello'] * 3)
True
>>> TestCase('r2', ' e.*o ').test(['hello', 'ello', 'world'])
True
>>> TestCase('z1', ' e').test(['hello', 'h ello'])
True
>>> TestCase('rl', 'e.*o').test(['hel', 'lo'])
True
>>> TestCase('', 'e o').test(['e o'])
False
>>> TestCase('f', 'e o').test(['e o'])
'TODO'
>>> TestCase('b', 'e o').test(['e o'])
True
>>> TestCase('b', 'e o').test(['e o'])
True
>>> TestCase('w2', 'o').test(['hello world'])
True
>>> TestCase('wW2', 'o').test(['hello world'])
False
>>> TestCase('wr2', 'a.c').test(['bli abc axc bla'])
True
>>> repr(TestCase('x3y', 'hello'))
'xy3:hello'
>>> print(TestCase('1x2', 'hello')) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ShouldException: Error in parsing modifiers: 1x2
'''
def __init__(self, modifiers, expression, name='', raw=''):
self.name = name
self.status = Sta()
self.count = '?'
self.raw = raw
self.json_data = None
# Extract self.expected_count from modifiers
m = RE_MODIFIERS.match(modifiers)
if not m:
raise ShouldException('Error in parsing modifiers: ' + modifiers)
self.modifiers = m.group(1) + m.group(3)
self.expected_count = int(m.group(2)) if m.group(2) else NOT_ZERO
# Parse modifiers
self.mods = parser_mod.parse_modifiers(self.modifiers)
if self.mods.json:
es = expression.split(':')
key, expression = es[0], ':'.join(es[1:])
self.key = key.strip()
self.mods.multi_lines = True
self.mods.count_all = True
self.expression = expression if self.mods.ltspaces else expression.strip()
if self.mods.blanks:
while ' ' in self.expression:
self.expression = self.expression.replace(' ', ' ')
self.expression = self.expression.replace(' ', '\s+')
self.mods.regex = True
self.regex = None
if self.mods.regex:
if self.mods.ignore_case:
self.regex = re.compile(self.expression, re.IGNORECASE)
else:
self.regex = re.compile(self.expression)
def test(self, lines, variables=None, verbose=0):
if self.mods.multi_lines:
lines = [' '.join([l.rstrip(ENDLINE_CHARS) for l in lines])]
expression_var = replace_variables(self.expression, variables)
if not self.regex and self.mods.ignore_case:
expression_var = expression_var.upper()
self.count = None
# json handling
if self.mods.json:
try:
d = json.loads(lines[0])
self.json_data = deep_get(d, self.key)
if expression_var:
# An expression is provided: prepare data for further count
if type(self.json_data) is list:
lines = [json.dumps(x) for x in self.json_data]
elif type(self.json_data) is dict:
lines = [json.dumps(x) for x in self.json_data.values()]
else:
lines = [str(self.json_data)]
else:
# No expression provided: we just count the keys
if type(self.json_data) in [list, dict]:
self.count = len(self.json_data)
else:
self.count = 1
except (ValueError, KeyError):
# No json, or non-existent key: count is 0
self.json_data = JSON_KEY_NOT_FOUND
self.count = 0
# Main count
if self.count is None:
self.count = 0
for l in lines:
if self.regex:
if self.mods.count_all:
self.count += len(self.regex.findall(l))
elif self.regex.search(l):
self.count += 1
else:
if self.mods.ignore_case:
l = l.upper()
if expression_var in l:
self.count += l.count(expression_var) if self.mods.count_all else 1
# Compute status
if self.expected_count == NOT_ZERO:
sta = (self.count > 0)
elif self.mods.less_than:
sta = (self.count < self.expected_count)
elif self.mods.more_than:
sta = (self.count > self.expected_count)
else:
sta = (self.count == self.expected_count)
if self.mods.todo:
sta = [S_TODO, S_TODO_PASSED][sta]
if self.mods.allow:
sta = [S_AF, True][sta]
self.status = Sta(sta)
return self.status.or_alias()
def str_additional_status(self, verbose=False):
s = ''
if self.status.is_warned() or verbose:
s += ' (%s/%s%s)' % (self.count,
MOD_LESS_THAN if self.mods.less_than else MOD_MORE_THAN if self.mods.more_than else '',
self.expected_count)
return s
def __repr__(self):
return '%s%s:%s' % (self.modifiers, self.expected_count if self.expected_count != NOT_ZERO else '', self.expression)
class TestSuite():
'''
>>> s = TestSuite()
>>> s.test(['echo "hello"', '$My test', ':hello'], colorize = False)
True
>>> s.tests[0].status.or_alias()
True
>>> s2 = TestSuite('r')
>>> s2.variables.append(("$LAUNCHER", ""))
>>> s2.variables.append(("$EXTRA", ""))
>>> s2.test(['echo "hello"', '$ A nice test', ':e.*o'], verbose = 1, colorize = False) # doctest: +NORMALIZE_WHITESPACE
echo "hello"
stdout --> 1 lines
stderr --> 0 lines
ok (0) - Exit code is 0 -- echo "hello"
ok (1/+) - A nice test
:e.*o
True
>>> s2.str_status(colorize = False)
'==> ok - ok:2 - total:2 tests'
>>> print(s2.tap()) # doctest: +NORMALIZE_WHITESPACE
1..2
ok - Exit code is 0 -- echo "hello"
ok - A nice test
'''
def __init__(self, modifiers = '', cd = None, name = '', timeout = TIMEOUT):
self.name = name
self.requires = True
self.requires_cmd = None
self.requires_stderr = []
self.cmds = []
self.tests = []
self.stdin = []
self.stdout = []
self.test_lines = []
self.skip = False
self.status = None
self.modifiers = modifiers
self.opt_modifiers = ''
self.variables = []
self.status = Sta()
self.stats = Stats('test')
self.source = None
self.cd = cd
self.use_launcher = True
self.expected_exit_code = 0
self.elapsed_time = None
self.timeout = timeout
def cmd_variables_cd(self, cmd, verbose, colorize):
cmd = replace_variables(cmd, self.variables_all)
if self.cd:
cmd = 'cd %s ; ' % self.cd + cmd
if verbose > 0:
print(color(ANSI.MAGENTA, cmd, colorize))
return cmd
def test(self, should_lines, variables=[], verbose=0, colorize=True, only=None):
name = ''
current_cmd = '' # multi-line command
current_test_lines = [] # currently evaluated output lines
current_tests = False # tests were evaluated since the last command run
self.only = only
self.variables_all = self.variables + variables
# Iterate over should_lines
# then use once DIRECTIVE_SCRIPT to flush the last tests
for l in list(should_lines) + [DIRECTIVE_SCRIPT]:
l = l.lstrip().rstrip(ENDLINE_CHARS)
if not l:
continue
# Comment
if l.startswith(TOKEN_COMMENT):
continue
# Directive -- Requires
if l.startswith(DIRECTIVE_REQUIRES):
self.requires_cmd = l[len(DIRECTIVE_REQUIRES):].strip()
self.variables_all = self.variables + variables
requires_cmd = self.cmd_variables_cd(self.requires_cmd, verbose, colorize)
p = subprocess.Popen(requires_cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
self.requires = (p.wait() == 0)
self.requires_stderr = [l.decode(errors='replace') for l in p.stderr.readlines()]
if not self.requires:
self.skip_set('Condition is not met: %s' % self.requires_cmd, verbose)
if verbose > 0:
print(color(ANSI.CYAN, ''.join(self.requires_stderr), colorize))
continue
# Directive -- No launcher
if l.startswith(DIRECTIVE_NO_LAUNCHER):
self.use_launcher = False
if replace_variables(VAR_LAUNCHER, self.variables_all):
self.skip_set('%s while %s is given' % (DIRECTIVE_NO_LAUNCHER, VAR_LAUNCHER), verbose)
continue
# Directive -- No extra options
if l.startswith(DIRECTIVE_NO_EXTRA):
self.variables = [(VAR_EXTRA, '')] + self.variables
self.variables_all = self.variables + variables
continue
# Directive -- Source
if l.startswith(DIRECTIVE_SOURCE):
self.source = os.path.join(self.cd if self.cd else '', l[len(DIRECTIVE_SOURCE):].strip())
continue
# Directive -- Options
if l.startswith(DIRECTIVE_OPTIONS):
opts, unknown = options.parse_known_args(l[len(DIRECTIVE_OPTIONS):].split())
self.variables = populate_variables(opts.var) + self.variables
self.variables_all = self.variables + variables
self.opt_modifiers = ''.join(opts.mod) if opts.mod else ''
continue
# Directive -- Exit code
if l.startswith(DIRECTIVE_EXIT_CODE):
e = l[len(DIRECTIVE_EXIT_CODE):].strip()
self.expected_exit_code = NOT_ZERO if e == NOT_ZERO else int(e)
continue
# Name
if l.startswith(TOKEN_NAME):
name = l[1:].strip()
continue
# Directive -- Command
if l.startswith(DIRECTIVE_SCRIPT):
l = l[len(DIRECTIVE_SCRIPT):]
# Directive -- Others
if l.startswith(TOKEN_DIRECTIVE):
sys.stderr.write('! Unknown directive: %s\n' % l)
continue
# Test
if RE_TEST.search(l):
pos = l.find(TOKEN_TEST)
modifiers, expression = l[:pos], l[pos+1:]
test = TestCase(modifiers + self.opt_modifiers + self.modifiers, expression, name, l)
if self.skip:
self.skip_tests([test])
continue
self.one_test(test, current_test_lines, verbose, colorize)
self.tests.append(test)
current_tests = True
continue
l = l.strip()
# The command possibly continues on the next line
next_cmd_continues = l.endswith(CONTINUATION_CHAR)
if next_cmd_continues:
l = l[:-1]
current_cmd += l
if next_cmd_continues:
continue
# Flush tasks on the previous tests
if current_tests:
# Debug the previous tests
self.debug(self.status, current_cmd, test_lines, verbose, colorize)
# Empty the buffers
current_test_lines = []
current_tests = False
# If we are at the end (DIRECTIVE_SCRIPT)
if not l:
continue
# Launch the command
if not self.skip:
test_lines, exit_test = self.launch([current_cmd], verbose, colorize)
self.one_test(exit_test, test_lines, verbose, colorize)
self.cmds.append(current_cmd)
if not self.source:
# Regular testing, stdout added to 'current_test_lines'
current_test_lines += test_lines
self.test_lines += test_lines
else:
# !OUTPUT_FILE:, the content of the file overrides 'current_test_lines'
try:
with open(self.source) as f:
current_test_lines = f.readlines()
except OSError:
msg = self.source + ' not found'