/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
292 by Martin Pool
- start adding a pure-python blackbox test suite
1
#! /usr/bin/python
2
3
# Copyright (C) 2005 Canonical Ltd
4
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
20
"""External black-box test for bzr.
21
22
This always runs bzr as an external process to try to catch bugs
23
related to argument processing, startup, etc.
24
25
This replaces the previous test.sh which was not very portable."""
26
296 by Martin Pool
- better reports from testbzr when it fails
27
import sys, os, traceback
292 by Martin Pool
- start adding a pure-python blackbox test suite
28
29
try:
30
    import shutil
31
    from subprocess import call, Popen
32
except ImportError, e:
33
    sys.stderr.write("testbzr: sorry, this test suite requires modules from python2.4\n"
34
                     + '    ' + str(e))
35
    sys.exit(1)
36
37
296 by Martin Pool
- better reports from testbzr when it fails
38
class CommandFailed(Exception):
39
    pass
40
41
298 by Martin Pool
- test some commands known to fail
42
def runcmd(cmd, retcode=0):
292 by Martin Pool
- start adding a pure-python blackbox test suite
43
    """run one command and check the output.
44
45
    If a single string is based, it is split into words.
46
    For commands that are not simple space-separated words, please
47
    pass a list instead."""
48
    
49
    if isinstance(cmd, basestring):
299 by Martin Pool
testbzr:
50
        logfile.write('$ %s\n' % cmd)
292 by Martin Pool
- start adding a pure-python blackbox test suite
51
        cmd = cmd.split()
299 by Martin Pool
testbzr:
52
    else:
53
        logfile.write('$ %r\n' % cmd)
297 by Martin Pool
- fix intentional testcase failure
54
    log_linenumber()
298 by Martin Pool
- test some commands known to fail
55
    actual_retcode = call(cmd, stdout=logfile, stderr=logfile)
56
    if retcode != actual_retcode:
57
        raise CommandFailed("test failed: %r returned %d, expected %d"
58
                            % (cmd, actual_retcode, retcode))
292 by Martin Pool
- start adding a pure-python blackbox test suite
59
60
61
def progress(msg):
62
    print '* ' + msg
63
    logfile.write('* '+ msg + '\n')
297 by Martin Pool
- fix intentional testcase failure
64
    log_linenumber()
65
66
299 by Martin Pool
testbzr:
67
def cd(dirname):
68
    logfile.write('$ cd %s\n' % dirname)
69
    os.chdir(dirname)
70
71
297 by Martin Pool
- fix intentional testcase failure
72
def log_linenumber():
73
    """Log the stack frame location two things up."""
74
    stack = traceback.extract_stack()[-3]
75
    logfile.write('   at %s:%d\n' % stack[:2])
76
77
78
TESTDIR = "testbzr.tmp"
292 by Martin Pool
- start adding a pure-python blackbox test suite
79
80
# prepare an empty scratch directory
81
if os.path.exists(TESTDIR):
82
    shutil.rmtree(TESTDIR)
83
84
297 by Martin Pool
- fix intentional testcase failure
85
logfile = open('testbzr.log', 'wt', buffering=1)
292 by Martin Pool
- start adding a pure-python blackbox test suite
86
87
296 by Martin Pool
- better reports from testbzr when it fails
88
try: 
89
    os.mkdir(TESTDIR)
299 by Martin Pool
testbzr:
90
    cd(TESTDIR)
296 by Martin Pool
- better reports from testbzr when it fails
91
92
    progress("testing introductory commands")
93
    runcmd("bzr version")
94
    runcmd("bzr help")
297 by Martin Pool
- fix intentional testcase failure
95
    runcmd("bzr --help")
96
97
    progress("testing user identity")
98
    # this should always identify something, if only "john@localhost"
99
    runcmd("bzr whoami")
298 by Martin Pool
- test some commands known to fail
100
    runcmd("bzr whoami --email")
101
102
    progress("testing invalid commands")
103
    runcmd("bzr pants", retcode=1)
104
    runcmd("bzr --pants off", retcode=1)
296 by Martin Pool
- better reports from testbzr when it fails
105
106
    progress("all tests passed!")
107
except Exception, e:
108
    sys.stderr.write('*' * 50 + '\n'
109
                     + 'testbzr: tests failed\n'
110
                     + 'see bzr-test.log for more information\n'
111
                     + '*' * 50 + '\n')
112
    logfile.write('tests failed!\n')
113
    traceback.print_exc(None, logfile)
114
    sys.exit(1)