/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
301 by Martin Pool
- provide for catching output from shell commands
31
    from subprocess import call, Popen, PIPE
292 by Martin Pool
- start adding a pure-python blackbox test suite
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
301 by Martin Pool
- provide for catching output from shell commands
42
def runcmd(cmd, retcode=0, catchstdout=False):
300 by Martin Pool
- more tests
43
    """Run one command and check the return code.
292 by Martin Pool
- start adding a pure-python blackbox test suite
44
301 by Martin Pool
- provide for catching output from shell commands
45
    Returns a tuple of (stdout,stderr) strings.
46
292 by Martin Pool
- start adding a pure-python blackbox test suite
47
    If a single string is based, it is split into words.
48
    For commands that are not simple space-separated words, please
49
    pass a list instead."""
50
    
51
    if isinstance(cmd, basestring):
299 by Martin Pool
testbzr:
52
        logfile.write('$ %s\n' % cmd)
292 by Martin Pool
- start adding a pure-python blackbox test suite
53
        cmd = cmd.split()
299 by Martin Pool
testbzr:
54
    else:
55
        logfile.write('$ %r\n' % cmd)
297 by Martin Pool
- fix intentional testcase failure
56
    log_linenumber()
301 by Martin Pool
- provide for catching output from shell commands
57
58
    if catchstdout:
59
        stdout = PIPE
60
    else:
61
        stdout = logfile
62
        
63
    child = Popen(cmd, stdout=stdout, stderr=logfile)
64
    outd, errd = child.communicate()
65
66
    actual_retcode = child.wait()
67
    
298 by Martin Pool
- test some commands known to fail
68
    if retcode != actual_retcode:
69
        raise CommandFailed("test failed: %r returned %d, expected %d"
70
                            % (cmd, actual_retcode, retcode))
292 by Martin Pool
- start adding a pure-python blackbox test suite
71
301 by Martin Pool
- provide for catching output from shell commands
72
    return outd, errd
73
292 by Martin Pool
- start adding a pure-python blackbox test suite
74
75
def progress(msg):
76
    print '* ' + msg
77
    logfile.write('* '+ msg + '\n')
297 by Martin Pool
- fix intentional testcase failure
78
    log_linenumber()
79
80
299 by Martin Pool
testbzr:
81
def cd(dirname):
82
    logfile.write('$ cd %s\n' % dirname)
83
    os.chdir(dirname)
84
85
300 by Martin Pool
- more tests
86
297 by Martin Pool
- fix intentional testcase failure
87
def log_linenumber():
88
    """Log the stack frame location two things up."""
89
    stack = traceback.extract_stack()[-3]
90
    logfile.write('   at %s:%d\n' % stack[:2])
91
92
93
TESTDIR = "testbzr.tmp"
292 by Martin Pool
- start adding a pure-python blackbox test suite
94
95
# prepare an empty scratch directory
96
if os.path.exists(TESTDIR):
97
    shutil.rmtree(TESTDIR)
98
99
297 by Martin Pool
- fix intentional testcase failure
100
logfile = open('testbzr.log', 'wt', buffering=1)
292 by Martin Pool
- start adding a pure-python blackbox test suite
101
102
300 by Martin Pool
- more tests
103
try:
104
    runcmd(['mkdir', TESTDIR])
299 by Martin Pool
testbzr:
105
    cd(TESTDIR)
296 by Martin Pool
- better reports from testbzr when it fails
106
300 by Martin Pool
- more tests
107
    progress("introductory commands")
296 by Martin Pool
- better reports from testbzr when it fails
108
    runcmd("bzr version")
109
    runcmd("bzr help")
297 by Martin Pool
- fix intentional testcase failure
110
    runcmd("bzr --help")
111
300 by Martin Pool
- more tests
112
    progress("user identity")
297 by Martin Pool
- fix intentional testcase failure
113
    # this should always identify something, if only "john@localhost"
114
    runcmd("bzr whoami")
298 by Martin Pool
- test some commands known to fail
115
    runcmd("bzr whoami --email")
301 by Martin Pool
- provide for catching output from shell commands
116
    assert runcmd("bzr whoami --email", catchstdout=True)[0].count('@') == 1
298 by Martin Pool
- test some commands known to fail
117
300 by Martin Pool
- more tests
118
    progress("invalid commands")
298 by Martin Pool
- test some commands known to fail
119
    runcmd("bzr pants", retcode=1)
120
    runcmd("bzr --pants off", retcode=1)
296 by Martin Pool
- better reports from testbzr when it fails
121
300 by Martin Pool
- more tests
122
    progress("basic branch creation")
123
    runcmd(['mkdir', 'branch1'])
124
    cd('branch1')
125
    runcmd('bzr init')
126
    
127
    f = file('test.txt', 'wt')
128
    f.write('hello world!\n')
129
    f.close()
130
131
    
132
133
    cd('..')
134
296 by Martin Pool
- better reports from testbzr when it fails
135
    progress("all tests passed!")
136
except Exception, e:
137
    sys.stderr.write('*' * 50 + '\n'
138
                     + 'testbzr: tests failed\n'
139
                     + 'see bzr-test.log for more information\n'
140
                     + '*' * 50 + '\n')
141
    logfile.write('tests failed!\n')
142
    traceback.print_exc(None, logfile)
143
    sys.exit(1)
300 by Martin Pool
- more tests
144