summaryrefslogtreecommitdiffstats
path: root/QMTest/TestCommon.py
blob: 37d3f9c7b385605ac678948f3c1f658ee8a0b3c9 (plain)
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
"""
TestCommon.py:  a testing framework for commands and scripts
                with commonly useful error handling

The TestCommon module provides a simple, high-level interface for writing
tests of executable commands and scripts, especially commands and scripts
that interact with the file system.  All methods throw exceptions and
exit on failure, with useful error messages.  This makes a number of
explicit checks unnecessary, making the test scripts themselves simpler
to write and easier to read.

The TestCommon class is a subclass of the TestCmd class.  In essence,
TestCommon is a wrapper that handles common TestCmd error conditions in
useful ways.  You can use TestCommon directly, or subclass it for your
program and add additional (or override) methods to tailor it to your
program's specific needs.  Alternatively, the TestCommon class serves
as a useful example of how to define your own TestCmd subclass.

As a subclass of TestCmd, TestCommon provides access to all of the
variables and methods from the TestCmd module.  Consequently, you can
use any variable or method documented in the TestCmd module without
having to explicitly import TestCmd.

A TestCommon environment object is created via the usual invocation:

    import TestCommon
    test = TestCommon.TestCommon()

You can use all of the TestCmd keyword arguments when instantiating a
TestCommon object; see the TestCmd documentation for details.

Here is an overview of the methods and keyword arguments that are
provided by the TestCommon class:

    test.must_be_writable('file1', ['file2', ...])

    test.must_contain('file', 'required text\n')

    test.must_contain_all_lines(output, lines, ['title', find])

    test.must_contain_any_line(output, lines, ['title', find])

    test.must_contain_exactly_lines(output, lines, ['title', find])

    test.must_exist('file1', ['file2', ...])

    test.must_match('file', "expected contents\n")

    test.must_not_be_writable('file1', ['file2', ...])

    test.must_not_contain('file', 'banned text\n')

    test.must_not_contain_any_line(output, lines, ['title', find])

    test.must_not_exist('file1', ['file2', ...])

    test.run(options = "options to be prepended to arguments",
             stdout = "expected standard output from the program",
             stderr = "expected error output from the program",
             status = expected_status,
             match = match_function)

The TestCommon module also provides the following variables

    TestCommon.python
    TestCommon._python_
    TestCommon.exe_suffix
    TestCommon.obj_suffix
    TestCommon.shobj_prefix
    TestCommon.shobj_suffix
    TestCommon.lib_prefix
    TestCommon.lib_suffix
    TestCommon.dll_prefix
    TestCommon.dll_suffix

"""

# Copyright 2000-2010 Steven Knight
# This module is free software, and you may redistribute it and/or modify
# it under the same terms as Python itself, so long as this copyright message
# and disclaimer are retained in their original form.
#
# IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
# SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
# THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#
# THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
# AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

__author__ = "Steven Knight <knight at baldmt dot com>"
__revision__ = "TestCommon.py 1.3.D001 2010/06/03 12:58:27 knight"
__version__ = "1.3"

import copy
import os
import stat
import sys

try:
    from collections import UserList
except ImportError:
    # no 'collections' module or no UserList in collections
    exec('from UserList import UserList')

from TestCmd import *
from TestCmd import __all__

__all__.extend([ 'TestCommon',
                 'exe_suffix',
                 'obj_suffix',
                 'shobj_prefix',
                 'shobj_suffix',
                 'lib_prefix',
                 'lib_suffix',
                 'dll_prefix',
                 'dll_suffix',
               ])

try:
    sorted
except NameError:
    # Pre-2.4 Python has no sorted() function.
    #
    # The pre-2.4 Python list.sort() method does not support
    # list.sort(key=) nor list.sort(reverse=) keyword arguments, so
    # we must implement the functionality of those keyword arguments
    # by hand instead of passing them to list.sort().
    def sorted(iterable, cmp=None, key=None, reverse=False):
        if key is not None:
            result = [(key(x), x) for x in iterable]
        else:
            result = iterable[:]
        if cmp is None:
            # Pre-2.3 Python does not support list.sort(None).
            result.sort()
        else:
            result.sort(cmp)
        if key is not None:
            result = [t1 for t0,t1 in result]
        if reverse:
            result.reverse()
        return result

# Variables that describe the prefixes and suffixes on this system.
if sys.platform == 'win32':
    exe_suffix   = '.exe'
    obj_suffix   = '.obj'
    shobj_suffix = '.obj'
    shobj_prefix = ''
    lib_prefix   = ''
    lib_suffix   = '.lib'
    dll_prefix   = ''
    dll_suffix   = '.dll'
elif sys.platform == 'cygwin':
    exe_suffix   = '.exe'
    obj_suffix   = '.o'
    shobj_suffix = '.os'
    shobj_prefix = ''
    lib_prefix   = 'lib'
    lib_suffix   = '.a'
    dll_prefix   = ''
    dll_suffix   = '.dll'
elif sys.platform.find('irix') != -1:
    exe_suffix   = ''
    obj_suffix   = '.o'
    shobj_suffix = '.o'
    shobj_prefix = ''
    lib_prefix   = 'lib'
    lib_suffix   = '.a'
    dll_prefix   = 'lib'
    dll_suffix   = '.so'
elif sys.platform.find('darwin') != -1:
    exe_suffix   = ''
    obj_suffix   = '.o'
    shobj_suffix = '.os'
    shobj_prefix = ''
    lib_prefix   = 'lib'
    lib_suffix   = '.a'
    dll_prefix   = 'lib'
    dll_suffix   = '.dylib'
elif sys.platform.find('sunos') != -1:
    exe_suffix   = ''
    obj_suffix   = '.o'
    shobj_suffix = '.o'
    shobj_prefix = 'so_'
    lib_prefix   = 'lib'
    lib_suffix   = '.a'
    dll_prefix   = 'lib'
    dll_suffix   = '.so'
else:
    exe_suffix   = ''
    obj_suffix   = '.o'
    shobj_suffix = '.os'
    shobj_prefix = ''
    lib_prefix   = 'lib'
    lib_suffix   = '.a'
    dll_prefix   = 'lib'
    dll_suffix   = '.so'

def is_List(e):
    return isinstance(e, (list, UserList))

def is_writable(f):
    mode = os.stat(f)[stat.ST_MODE]
    return mode & stat.S_IWUSR

def separate_files(flist):
    existing = []
    missing = []
    for f in flist:
        if os.path.exists(f):
            existing.append(f)
        else:
            missing.append(f)
    return existing, missing

if os.name == 'posix':
    def _failed(self, status = 0):
        if self.status is None or status is None:
            return None
        return _status(self) != status
    def _status(self):
        return self.status
elif os.name == 'nt':
    def _failed(self, status = 0):
        return not (self.status is None or status is None) and \
               self.status != status
    def _status(self):
        return self.status

class TestCommon(TestCmd):

    # Additional methods from the Perl Test::Cmd::Common module
    # that we may wish to add in the future:
    #
    #  $test->subdir('subdir', ...);
    #
    #  $test->copy('src_file', 'dst_file');

    def __init__(self, **kw):
        """Initialize a new TestCommon instance.  This involves just
        calling the base class initialization, and then changing directory
        to the workdir.
        """
        TestCmd.__init__(self, **kw)
        os.chdir(self.workdir)

    def options_arguments(self, options, arguments):
        """Merges the "options" keyword argument with the arguments."""
        if options:
            if arguments is None:
                return options
            if isinstance(options, str):
                options = [options]
            if isinstance(arguments, str):
                arguments = [arguments]
            arguments = ' '.join(options + arguments)
        return arguments

    def must_be_writable(self, *files):
        """Ensures that the specified file(s) exist and are writable.
        An individual file can be specified as a list of directory names,
        in which case the pathname will be constructed by concatenating
        them.  Exits FAILED if any of the files does not exist or is
        not writable.
        """
        files = [is_List(x) and os.path.join(*x) or x for x in files]
        existing, missing = separate_files(files)
        unwritable = [x for x in existing if not is_writable(x)]
        if missing:
            print "Missing files: `%s'" % "', `".join(missing)
        if unwritable:
            print "Unwritable files: `%s'" % "', `".join(unwritable)
        self.fail_test(missing + unwritable)

    def must_contain(self, file, required, mode = 'rb'):
        """Ensures that the specified file contains the required text.
        """
        file_contents = self.read(file, mode)
        contains = (file_contents.find(required) != -1)
        if not contains:
            print "File `%s' does not contain required string." % file
            print self.banner('Required string ')
            print required
            print self.banner('%s contents ' % file)
            print file_contents
            self.fail_test(not contains)

    def must_contain_all_lines(self, output, lines, title=None, find=None):
        """Ensures that the specified output string (first argument)
        contains all of the specified lines (second argument).

        An optional third argument can be used to describe the type
        of output being searched, and only shows up in failure output.

        An optional fourth argument can be used to supply a different
        function, of the form "find(line, output), to use when searching
        for lines in the output.
        """
        if find is None:
            def find(o, l):
                try:
                    return o.index(l)
                except ValueError:
                    return None
        missing = []
        for line in lines:
            if find(output, line) is None:
                missing.append(line)

        if missing:
            if title is None:
                title = 'output'
            sys.stdout.write("Missing expected lines from %s:\n" % title)
            for line in missing:
                sys.stdout.write('    ' + repr(line) + '\n')
            sys.stdout.write(self.banner(title + ' ') + '\n')
            sys.stdout.write(output)
            self.fail_test()

    def must_contain_any_line(self, output, lines, title=None, find=None):
        """Ensures that the specified output string (first argument)
        contains at least one of the specified lines (second argument).

        An optional third argument can be used to describe the type
        of output being searched, and only shows up in failure output.

        An optional fourth argument can be used to supply a different
        function, of the form "find(line, output), to use when searching
        for lines in the output.
        """
        if find is None:
            def find(o, l):
                try:
                    return o.index(l)
                except ValueError:
                    return None
        for line in lines:
            if find(output, line) is not None:
                return

        if title is None:
            title = 'output'
        sys.stdout.write("Missing any expected line from %s:\n" % title)
        for line in lines:
            sys.stdout.write('    ' + repr(line) + '\n')
        sys.stdout.write(self.banner(title + ' ') + '\n')
        sys.stdout.write(output)
        self.fail_test()

    def must_contain_exactly_lines(self, output, expect, title=None, find=None):
        """Ensures that the specified output string (first argument)
        contains all of the lines in the expected string (second argument)
        with none left over.

        An optional third argument can be used to describe the type
        of output being searched, and only shows up in failure output.

        An optional fourth argument can be used to supply a different
        function, of the form "find(line, output), to use when searching
        for lines in the output.  The function must return the index
        of the found line in the output, or None if the line is not found.
        """
        out = output.splitlines()
        if is_List(expect):
            exp = [ e.rstrip('\n') for e in expect ]
        else:
            exp = expect.splitlines()
        if sorted(out) == sorted(exp):
            # early out for exact match
            return
        if find is None:
            def find(o, l):
                try:
                    return o.index(l)
                except ValueError:
                    return None
        missing = []
        for line in exp:
            found = find(out, line)
            if found is None:
                missing.append(line)
            else:
                out.pop(found)

        if not missing and not out:
            # all lines were matched
            return

        if title is None:
            title = 'output'
        if missing:
            sys.stdout.write("Missing expected lines from %s:\n" % title)
            for line in missing:
                sys.stdout.write('    ' + repr(line) + '\n')
            sys.stdout.write(self.banner('Missing %s ' % title) + '\n')
        if out:
            sys.stdout.write("Extra unexpected lines from %s:\n" % title)
            for line in out:
                sys.stdout.write('    ' + repr(line) + '\n')
            sys.stdout.write(self.banner('Extra %s ' % title) + '\n')
        sys.stdout.flush()
        self.fail_test()

    def must_contain_lines(self, lines, output, title=None):
        # Deprecated; retain for backwards compatibility.
        return self.must_contain_all_lines(output, lines, title)

    def must_exist(self, *files):
        """Ensures that the specified file(s) must exist.  An individual
        file be specified as a list of directory names, in which case the
        pathname will be constructed by concatenating them.  Exits FAILED
        if any of the files does not exist.
        """
        files = [is_List(x) and os.path.join(*x) or x for x in files]
        missing = [x for x in files if not os.path.exists(x)]
        if missing:
            print "Missing files: `%s'" % "', `".join(missing)
            self.fail_test(missing)

    def must_match(self, file, expect, mode = 'rb'):
        """Matches the contents of the specified file (first argument)
        against the expected contents (second argument).  The expected
        contents are a list of lines or a string which will be split
        on newlines.
        """
        file_contents = self.read(file, mode)
        try:
            self.fail_test(not self.match(file_contents, expect))
        except KeyboardInterrupt:
            raise
        except:
            print "Unexpected contents of `%s'" % file
            self.diff(expect, file_contents, 'contents ')
            raise

    def must_not_contain(self, file, banned, mode = 'rb'):
        """Ensures that the specified file doesn't contain the banned text.
        """
        file_contents = self.read(file, mode)
        contains = (file_contents.find(banned) != -1)
        if contains:
            print "File `%s' contains banned string." % file
            print self.banner('Banned string ')
            print banned
            print self.banner('%s contents ' % file)
            print file_contents
            self.fail_test(contains)

    def must_not_contain_any_line(self, output, lines, title=None, find=None):
        """Ensures that the specified output string (first argument)
        does not contain any of the specified lines (second argument).

        An optional third argument can be used to describe the type
        of output being searched, and only shows up in failure output.

        An optional fourth argument can be used to supply a different
        function, of the form "find(line, output), to use when searching
        for lines in the output.
        """
        if find is None:
            def find(o, l):
                try:
                    return o.index(l)
                except ValueError:
                    return None
        unexpected = []
        for line in lines:
            if find(output, line) is not None:
                unexpected.append(line)

        if unexpected:
            if title is None:
                title = 'output'
            sys.stdout.write("Unexpected lines in %s:\n" % title)
            for line in unexpected:
                sys.stdout.write('    ' + repr(line) + '\n')
            sys.stdout.write(self.banner(title + ' ') + '\n')
            sys.stdout.write(output)
            self.fail_test()

    def must_not_contain_lines(self, lines, output, title=None):
        return self.must_not_contain_any_line(output, lines, title)

    def must_not_exist(self, *files):
        """Ensures that the specified file(s) must not exist.
        An individual file be specified as a list of directory names, in
        which case the pathname will be constructed by concatenating them.
        Exits FAILED if any of the files exists.
        """
        files = [is_List(x) and os.path.join(*x) or x for x in files]
        existing = list(filter(os.path.exists, files))
        if existing:
            print "Unexpected files exist: `%s'" % "', `".join(existing)
            self.fail_test(existing)


    def must_not_be_writable(self, *files):
        """Ensures that the specified file(s) exist and are not writable.
        An individual file can be specified as a list of directory names,
        in which case the pathname will be constructed by concatenating
        them.  Exits FAILED if any of the files does not exist or is
        writable.
        """
        files = [is_List(x) and os.path.join(*x) or x for x in files]
        existing, missing = separate_files(files)
        writable = list(filter(is_writable, existing))
        if missing:
            print "Missing files: `%s'" % "', `".join(missing)
        if writable:
            print "Writable files: `%s'" % "', `".join(writable)
        self.fail_test(missing + writable)

    def _complete(self, actual_stdout, expected_stdout,
                        actual_stderr, expected_stderr, status, match):
        """
        Post-processes running a subcommand, checking for failure
        status and displaying output appropriately.
        """
        if _failed(self, status):
            expect = ''
            if status != 0:
                expect = " (expected %s)" % str(status)
            print "%s returned %s%s" % (self.program, _status(self), expect)
            print self.banner('STDOUT ')
            print actual_stdout
            print self.banner('STDERR ')
            print actual_stderr
            self.fail_test()
        if (expected_stdout is not None
                and not match(actual_stdout, expected_stdout)):
            self.diff(expected_stdout, actual_stdout, 'STDOUT ')
            if actual_stderr:
                print self.banner('STDERR ')
                print actual_stderr
            self.fail_test()
        if (expected_stderr is not None
                and not match(actual_stderr, expected_stderr)):
            print self.banner('STDOUT ')
            print actual_stdout
            self.diff(expected_stderr, actual_stderr, 'STDERR ')
            self.fail_test()

    def start(self, program = None,
                    interpreter = None,
                    options = None,
                    arguments = None,
                    universal_newlines = None,
                    **kw):
        """
        Starts a program or script for the test environment, handling
        any exceptions.
        """
        arguments = self.options_arguments(options, arguments)
        try:
            return TestCmd.start(self, program, interpreter, arguments,
                                 universal_newlines, **kw)
        except KeyboardInterrupt:
            raise
        except Exception, e:
            print self.banner('STDOUT ')
            try:
                print self.stdout()
            except IndexError:
                pass
            print self.banner('STDERR ')
            try:
                print self.stderr()
            except IndexError:
                pass
            cmd_args = self.command_args(program, interpreter, arguments)
            sys.stderr.write('Exception trying to execute: %s\n' % cmd_args)
            raise e

    def finish(self, popen, stdout = None, stderr = '', status = 0, **kw):
        """
        Finishes and waits for the process being run under control of
        the specified popen argument.  Additional arguments are similar
        to those of the run() method:

                stdout  The expected standard output from
                        the command.  A value of None means
                        don't test standard output.

                stderr  The expected error output from
                        the command.  A value of None means
                        don't test error output.

                status  The expected exit status from the
                        command.  A value of None means don't
                        test exit status.
        """
        TestCmd.finish(self, popen, **kw)
        match = kw.get('match', self.match)
        self._complete(self.stdout(), stdout,
                       self.stderr(), stderr, status, match)

    def run(self, options = None, arguments = None,
                  stdout = None, stderr = '', status = 0, **kw):
        """Runs the program under test, checking that the test succeeded.

        The parameters are the same as the base TestCmd.run() method,
        with the addition of:

                options Extra options that get appended to the beginning
                        of the arguments.

                stdout  The expected standard output from
                        the command.  A value of None means
                        don't test standard output.

                stderr  The expected error output from
                        the command.  A value of None means
                        don't test error output.

                status  The expected exit status from the
                        command.  A value of None means don't
                        test exit status.

        By default, this expects a successful exit (status = 0), does
        not test standard output (stdout = None), and expects that error
        output is empty (stderr = "").
        """
        kw['arguments'] = self.options_arguments(options, arguments)
        try:
            match = kw['match']
            del kw['match']
        except KeyError:
            match = self.match
        TestCmd.run(self, **kw)
        self._complete(self.stdout(), stdout,
                       self.stderr(), stderr, status, match)

    def skip_test(self, message="Skipping test.\n"):
        """Skips a test.

        Proper test-skipping behavior is dependent on the external
        TESTCOMMON_PASS_SKIPS environment variable.  If set, we treat
        the skip as a PASS (exit 0), and otherwise treat it as NO RESULT.
        In either case, we print the specified message as an indication
        that the substance of the test was skipped.

        (This was originally added to support development under Aegis.
        Technically, skipping a test is a NO RESULT, but Aegis would
        treat that as a test failure and prevent the change from going to
        the next step.  Since we ddn't want to force anyone using Aegis
        to have to install absolutely every tool used by the tests, we
        would actually report to Aegis that a skipped test has PASSED
        so that the workflow isn't held up.)
        """
        if message:
            sys.stdout.write(message)
            sys.stdout.flush()
        pass_skips = os.environ.get('TESTCOMMON_PASS_SKIPS')
        if pass_skips in [None, 0, '0']:
            # skip=1 means skip this function when showing where this
            # result came from.  They only care about the line where the
            # script called test.skip_test(), not the line number where
            # we call test.no_result().
            self.no_result(skip=1)
        else:
            # We're under the development directory for this change,
            # so this is an Aegis invocation; pass the test (exit 0).
            self.pass_test()

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: