~bzr-pqm/bzr/bzr.dev

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
335 by Martin Pool
- add new failing test for command parsing
29
TESTDIR = "testbzr.tmp"
30
31
LOGFILENAME = 'testbzr.log'
32
292 by Martin Pool
- start adding a pure-python blackbox test suite
33
try:
34
    import shutil
301 by Martin Pool
- provide for catching output from shell commands
35
    from subprocess import call, Popen, PIPE
292 by Martin Pool
- start adding a pure-python blackbox test suite
36
except ImportError, e:
37
    sys.stderr.write("testbzr: sorry, this test suite requires modules from python2.4\n"
38
                     + '    ' + str(e))
39
    sys.exit(1)
40
41
296 by Martin Pool
- better reports from testbzr when it fails
42
class CommandFailed(Exception):
43
    pass
44
45
302 by Martin Pool
testbzr: new backtick() helper
46
def formcmd(cmd):
47
    if isinstance(cmd, basestring):
48
        logfile.write('$ %s\n' % cmd)
49
        cmd = cmd.split()
50
    else:
51
        logfile.write('$ %r\n' % cmd)
52
53
    return cmd
54
55
56
def runcmd(cmd, retcode=0):
300 by Martin Pool
- more tests
57
    """Run one command and check the return code.
292 by Martin Pool
- start adding a pure-python blackbox test suite
58
301 by Martin Pool
- provide for catching output from shell commands
59
    Returns a tuple of (stdout,stderr) strings.
60
292 by Martin Pool
- start adding a pure-python blackbox test suite
61
    If a single string is based, it is split into words.
62
    For commands that are not simple space-separated words, please
63
    pass a list instead."""
302 by Martin Pool
testbzr: new backtick() helper
64
    cmd = formcmd(cmd)
65
    log_linenumber()
66
    
67
    actual_retcode = call(cmd, stdout=logfile, stderr=logfile)
68
    
69
    if retcode != actual_retcode:
70
        raise CommandFailed("test failed: %r returned %d, expected %d"
71
                            % (cmd, actual_retcode, retcode))
72
73
74
75
def backtick(cmd, retcode=0):
76
    cmd = formcmd(cmd)
77
    log_linenumber()
78
    child = Popen(cmd, stdout=PIPE, stderr=logfile)
301 by Martin Pool
- provide for catching output from shell commands
79
    outd, errd = child.communicate()
302 by Martin Pool
testbzr: new backtick() helper
80
    logfile.write(outd)
301 by Martin Pool
- provide for catching output from shell commands
81
    actual_retcode = child.wait()
307 by Martin Pool
testbzr: clean up crlf handling
82
83
    outd = outd.replace('\r', '')
301 by Martin Pool
- provide for catching output from shell commands
84
    
298 by Martin Pool
- test some commands known to fail
85
    if retcode != actual_retcode:
86
        raise CommandFailed("test failed: %r returned %d, expected %d"
87
                            % (cmd, actual_retcode, retcode))
292 by Martin Pool
- start adding a pure-python blackbox test suite
88
302 by Martin Pool
testbzr: new backtick() helper
89
    return outd
90
301 by Martin Pool
- provide for catching output from shell commands
91
292 by Martin Pool
- start adding a pure-python blackbox test suite
92
93
def progress(msg):
94
    print '* ' + msg
95
    logfile.write('* '+ msg + '\n')
297 by Martin Pool
- fix intentional testcase failure
96
    log_linenumber()
97
98
299 by Martin Pool
testbzr:
99
def cd(dirname):
100
    logfile.write('$ cd %s\n' % dirname)
101
    os.chdir(dirname)
102
103
300 by Martin Pool
- more tests
104
297 by Martin Pool
- fix intentional testcase failure
105
def log_linenumber():
106
    """Log the stack frame location two things up."""
107
    stack = traceback.extract_stack()[-3]
108
    logfile.write('   at %s:%d\n' % stack[:2])
109
110
292 by Martin Pool
- start adding a pure-python blackbox test suite
111
# prepare an empty scratch directory
112
if os.path.exists(TESTDIR):
113
    shutil.rmtree(TESTDIR)
114
115
335 by Martin Pool
- add new failing test for command parsing
116
logfile = open(LOGFILENAME, 'wt', buffering=1)
292 by Martin Pool
- start adding a pure-python blackbox test suite
117
118
300 by Martin Pool
- more tests
119
try:
120
    runcmd(['mkdir', TESTDIR])
299 by Martin Pool
testbzr:
121
    cd(TESTDIR)
296 by Martin Pool
- better reports from testbzr when it fails
122
300 by Martin Pool
- more tests
123
    progress("introductory commands")
296 by Martin Pool
- better reports from testbzr when it fails
124
    runcmd("bzr version")
335 by Martin Pool
- add new failing test for command parsing
125
    runcmd("bzr --version")
296 by Martin Pool
- better reports from testbzr when it fails
126
    runcmd("bzr help")
297 by Martin Pool
- fix intentional testcase failure
127
    runcmd("bzr --help")
128
300 by Martin Pool
- more tests
129
    progress("user identity")
297 by Martin Pool
- fix intentional testcase failure
130
    # this should always identify something, if only "john@localhost"
131
    runcmd("bzr whoami")
298 by Martin Pool
- test some commands known to fail
132
    runcmd("bzr whoami --email")
302 by Martin Pool
testbzr: new backtick() helper
133
    assert backtick("bzr whoami --email").count('@') == 1
298 by Martin Pool
- test some commands known to fail
134
300 by Martin Pool
- more tests
135
    progress("invalid commands")
298 by Martin Pool
- test some commands known to fail
136
    runcmd("bzr pants", retcode=1)
137
    runcmd("bzr --pants off", retcode=1)
296 by Martin Pool
- better reports from testbzr when it fails
138
300 by Martin Pool
- more tests
139
    progress("basic branch creation")
140
    runcmd(['mkdir', 'branch1'])
141
    cd('branch1')
142
    runcmd('bzr init')
303 by Martin Pool
- more tests for unknown file
143
144
    progress("status of new file")
300 by Martin Pool
- more tests
145
    
146
    f = file('test.txt', 'wt')
147
    f.write('hello world!\n')
148
    f.close()
149
307 by Martin Pool
testbzr: clean up crlf handling
150
    out = backtick("bzr unknowns")
151
    assert out == 'test.txt\n'
303 by Martin Pool
- more tests for unknown file
152
307 by Martin Pool
testbzr: clean up crlf handling
153
    out = backtick("bzr status")
303 by Martin Pool
- more tests for unknown file
154
    assert out == '''?       test.txt\n'''
155
307 by Martin Pool
testbzr: clean up crlf handling
156
    out = backtick("bzr status --all")
304 by Martin Pool
testbzr: test adding a file
157
    assert out == "?       test.txt\n"
158
305 by Martin Pool
testbzr: test renames
159
    progress("can't rename unversioned file")
160
    runcmd("bzr rename test.txt new-test.txt", 1)
161
304 by Martin Pool
testbzr: test adding a file
162
    progress("adding a file")
163
164
    runcmd("bzr add test.txt")
165
    assert backtick("bzr unknowns") == ''
307 by Martin Pool
testbzr: clean up crlf handling
166
    assert backtick("bzr status --all") == "A       test.txt\n"
300 by Martin Pool
- more tests
167
305 by Martin Pool
testbzr: test renames
168
    progress("rename newly-added file")
169
    runcmd("bzr rename test.txt hello.txt")
306 by Martin Pool
testbzr: test renames
170
    assert os.path.exists("hello.txt")
171
    assert not os.path.exists("test.txt")
305 by Martin Pool
testbzr: test renames
172
308 by Martin Pool
fix test suite
173
    assert backtick("bzr revno") == '0\n'
307 by Martin Pool
testbzr: clean up crlf handling
174
300 by Martin Pool
- more tests
175
    cd('..')
176
296 by Martin Pool
- better reports from testbzr when it fails
177
    progress("all tests passed!")
178
except Exception, e:
179
    sys.stderr.write('*' * 50 + '\n'
180
                     + 'testbzr: tests failed\n'
335 by Martin Pool
- add new failing test for command parsing
181
                     + 'see ' + LOGFILENAME + ' for more information\n'
296 by Martin Pool
- better reports from testbzr when it fails
182
                     + '*' * 50 + '\n')
183
    logfile.write('tests failed!\n')
184
    traceback.print_exc(None, logfile)
185
    sys.exit(1)