/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):
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
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
300 by Martin Pool
- more tests
72
297 by Martin Pool
- fix intentional testcase failure
73
def log_linenumber():
74
    """Log the stack frame location two things up."""
75
    stack = traceback.extract_stack()[-3]
76
    logfile.write('   at %s:%d\n' % stack[:2])
77
78
79
TESTDIR = "testbzr.tmp"
292 by Martin Pool
- start adding a pure-python blackbox test suite
80
81
# prepare an empty scratch directory
82
if os.path.exists(TESTDIR):
83
    shutil.rmtree(TESTDIR)
84
85
297 by Martin Pool
- fix intentional testcase failure
86
logfile = open('testbzr.log', 'wt', buffering=1)
292 by Martin Pool
- start adding a pure-python blackbox test suite
87
88
300 by Martin Pool
- more tests
89
try:
90
    runcmd(['mkdir', TESTDIR])
299 by Martin Pool
testbzr:
91
    cd(TESTDIR)
296 by Martin Pool
- better reports from testbzr when it fails
92
300 by Martin Pool
- more tests
93
    progress("introductory commands")
296 by Martin Pool
- better reports from testbzr when it fails
94
    runcmd("bzr version")
95
    runcmd("bzr help")
297 by Martin Pool
- fix intentional testcase failure
96
    runcmd("bzr --help")
97
300 by Martin Pool
- more tests
98
    progress("user identity")
297 by Martin Pool
- fix intentional testcase failure
99
    # this should always identify something, if only "john@localhost"
100
    runcmd("bzr whoami")
298 by Martin Pool
- test some commands known to fail
101
    runcmd("bzr whoami --email")
102
300 by Martin Pool
- more tests
103
    progress("invalid commands")
298 by Martin Pool
- test some commands known to fail
104
    runcmd("bzr pants", retcode=1)
105
    runcmd("bzr --pants off", retcode=1)
296 by Martin Pool
- better reports from testbzr when it fails
106
300 by Martin Pool
- more tests
107
    progress("basic branch creation")
108
    runcmd(['mkdir', 'branch1'])
109
    cd('branch1')
110
    runcmd('bzr init')
111
    
112
    f = file('test.txt', 'wt')
113
    f.write('hello world!\n')
114
    f.close()
115
116
    
117
118
    cd('..')
119
296 by Martin Pool
- better reports from testbzr when it fails
120
    progress("all tests passed!")
121
except Exception, e:
122
    sys.stderr.write('*' * 50 + '\n'
123
                     + 'testbzr: tests failed\n'
124
                     + 'see bzr-test.log for more information\n'
125
                     + '*' * 50 + '\n')
126
    logfile.write('tests failed!\n')
127
    traceback.print_exc(None, logfile)
128
    sys.exit(1)
300 by Martin Pool
- more tests
129