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