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 |
||
398
by Martin Pool
- testbzr finds the right version of bzr to test |
55 |
if cmd[0] == 'bzr': |
56 |
cmd[0] = BZRPATH |
|
57 |
||
302
by Martin Pool
testbzr: new backtick() helper |
58 |
return cmd |
59 |
||
60 |
||
61 |
def runcmd(cmd, retcode=0): |
|
300
by Martin Pool
- more tests |
62 |
"""Run one command and check the return code.
|
292
by Martin Pool
- start adding a pure-python blackbox test suite |
63 |
|
301
by Martin Pool
- provide for catching output from shell commands |
64 |
Returns a tuple of (stdout,stderr) strings.
|
65 |
||
292
by Martin Pool
- start adding a pure-python blackbox test suite |
66 |
If a single string is based, it is split into words.
|
67 |
For commands that are not simple space-separated words, please
|
|
68 |
pass a list instead."""
|
|
302
by Martin Pool
testbzr: new backtick() helper |
69 |
cmd = formcmd(cmd) |
70 |
log_linenumber() |
|
71 |
||
72 |
actual_retcode = call(cmd, stdout=logfile, stderr=logfile) |
|
73 |
||
74 |
if retcode != actual_retcode: |
|
75 |
raise CommandFailed("test failed: %r returned %d, expected %d" |
|
76 |
% (cmd, actual_retcode, retcode)) |
|
77 |
||
78 |
||
79 |
||
80 |
def backtick(cmd, retcode=0): |
|
81 |
cmd = formcmd(cmd) |
|
82 |
log_linenumber() |
|
83 |
child = Popen(cmd, stdout=PIPE, stderr=logfile) |
|
301
by Martin Pool
- provide for catching output from shell commands |
84 |
outd, errd = child.communicate() |
302
by Martin Pool
testbzr: new backtick() helper |
85 |
logfile.write(outd) |
301
by Martin Pool
- provide for catching output from shell commands |
86 |
actual_retcode = child.wait() |
307
by Martin Pool
testbzr: clean up crlf handling |
87 |
|
88 |
outd = outd.replace('\r', '') |
|
301
by Martin Pool
- provide for catching output from shell commands |
89 |
|
298
by Martin Pool
- test some commands known to fail |
90 |
if retcode != actual_retcode: |
91 |
raise CommandFailed("test failed: %r returned %d, expected %d" |
|
92 |
% (cmd, actual_retcode, retcode)) |
|
292
by Martin Pool
- start adding a pure-python blackbox test suite |
93 |
|
302
by Martin Pool
testbzr: new backtick() helper |
94 |
return outd |
95 |
||
301
by Martin Pool
- provide for catching output from shell commands |
96 |
|
292
by Martin Pool
- start adding a pure-python blackbox test suite |
97 |
|
98 |
def progress(msg): |
|
99 |
print '* ' + msg |
|
100 |
logfile.write('* '+ msg + '\n') |
|
297
by Martin Pool
- fix intentional testcase failure |
101 |
log_linenumber() |
102 |
||
103 |
||
299
by Martin Pool
testbzr: |
104 |
def cd(dirname): |
105 |
logfile.write('$ cd %s\n' % dirname) |
|
106 |
os.chdir(dirname) |
|
107 |
||
108 |
||
300
by Martin Pool
- more tests |
109 |
|
297
by Martin Pool
- fix intentional testcase failure |
110 |
def log_linenumber(): |
111 |
"""Log the stack frame location two things up."""
|
|
112 |
stack = traceback.extract_stack()[-3] |
|
113 |
logfile.write(' at %s:%d\n' % stack[:2]) |
|
114 |
||
115 |
||
340
by Martin Pool
- more testcase fixes |
116 |
|
292
by Martin Pool
- start adding a pure-python blackbox test suite |
117 |
# prepare an empty scratch directory
|
118 |
if os.path.exists(TESTDIR): |
|
119 |
shutil.rmtree(TESTDIR) |
|
120 |
||
121 |
||
335
by Martin Pool
- add new failing test for command parsing |
122 |
logfile = open(LOGFILENAME, 'wt', buffering=1) |
292
by Martin Pool
- start adding a pure-python blackbox test suite |
123 |
|
124 |
||
300
by Martin Pool
- more tests |
125 |
try: |
398
by Martin Pool
- testbzr finds the right version of bzr to test |
126 |
mypath = os.path.abspath(sys.argv[0]) |
127 |
print 'running tests from', mypath |
|
128 |
||
129 |
global BZRPATH |
|
130 |
||
131 |
if len(sys.argv) > 1: |
|
132 |
BZRPATH = sys.argv[1] |
|
133 |
else: |
|
134 |
BZRPATH = os.path.join(os.path.split(mypath)[0], 'bzr') |
|
135 |
||
136 |
print 'against bzr', BZRPATH |
|
137 |
print
|
|
138 |
print backtick([BZRPATH, 'version']) |
|
139 |
||
300
by Martin Pool
- more tests |
140 |
runcmd(['mkdir', TESTDIR]) |
299
by Martin Pool
testbzr: |
141 |
cd(TESTDIR) |
296
by Martin Pool
- better reports from testbzr when it fails |
142 |
|
300
by Martin Pool
- more tests |
143 |
progress("introductory commands") |
296
by Martin Pool
- better reports from testbzr when it fails |
144 |
runcmd("bzr version") |
335
by Martin Pool
- add new failing test for command parsing |
145 |
runcmd("bzr --version") |
296
by Martin Pool
- better reports from testbzr when it fails |
146 |
runcmd("bzr help") |
297
by Martin Pool
- fix intentional testcase failure |
147 |
runcmd("bzr --help") |
148 |
||
399
by Martin Pool
- testbzr also runs selftests |
149 |
progress("internal tests") |
150 |
runcmd("bzr selftest") |
|
151 |
||
300
by Martin Pool
- more tests |
152 |
progress("user identity") |
297
by Martin Pool
- fix intentional testcase failure |
153 |
# this should always identify something, if only "john@localhost"
|
154 |
runcmd("bzr whoami") |
|
298
by Martin Pool
- test some commands known to fail |
155 |
runcmd("bzr whoami --email") |
302
by Martin Pool
testbzr: new backtick() helper |
156 |
assert backtick("bzr whoami --email").count('@') == 1 |
298
by Martin Pool
- test some commands known to fail |
157 |
|
300
by Martin Pool
- more tests |
158 |
progress("invalid commands") |
298
by Martin Pool
- test some commands known to fail |
159 |
runcmd("bzr pants", retcode=1) |
160 |
runcmd("bzr --pants off", retcode=1) |
|
382
by Martin Pool
- test previous commit |
161 |
runcmd("bzr diff --message foo", retcode=1) |
296
by Martin Pool
- better reports from testbzr when it fails |
162 |
|
300
by Martin Pool
- more tests |
163 |
progress("basic branch creation") |
164 |
runcmd(['mkdir', 'branch1']) |
|
165 |
cd('branch1') |
|
166 |
runcmd('bzr init') |
|
303
by Martin Pool
- more tests for unknown file |
167 |
|
168 |
progress("status of new file") |
|
300
by Martin Pool
- more tests |
169 |
|
170 |
f = file('test.txt', 'wt') |
|
171 |
f.write('hello world!\n') |
|
172 |
f.close() |
|
173 |
||
307
by Martin Pool
testbzr: clean up crlf handling |
174 |
out = backtick("bzr unknowns") |
175 |
assert out == 'test.txt\n' |
|
303
by Martin Pool
- more tests for unknown file |
176 |
|
307
by Martin Pool
testbzr: clean up crlf handling |
177 |
out = backtick("bzr status") |
303
by Martin Pool
- more tests for unknown file |
178 |
assert out == '''? test.txt\n''' |
179 |
||
307
by Martin Pool
testbzr: clean up crlf handling |
180 |
out = backtick("bzr status --all") |
304
by Martin Pool
testbzr: test adding a file |
181 |
assert out == "? test.txt\n" |
182 |
||
404
by Martin Pool
- bzr status now optionally takes filenames to check |
183 |
out = backtick("bzr status test.txt --all") |
184 |
assert out == "? test.txt\n" |
|
185 |
||
186 |
f = file('test2.txt', 'wt') |
|
187 |
f.write('goodbye cruel world...\n') |
|
188 |
f.close() |
|
189 |
||
190 |
out = backtick("bzr status test.txt") |
|
191 |
assert out == "? test.txt\n" |
|
192 |
||
193 |
out = backtick("bzr status") |
|
194 |
assert out == "? test.txt\n" \ |
|
195 |
+ "? test2.txt\n" |
|
196 |
||
197 |
os.unlink('test2.txt') |
|
198 |
||
350
by Martin Pool
- refactor command aliases into command classes |
199 |
progress("command aliases") |
200 |
out = backtick("bzr st --all") |
|
201 |
assert out == "? test.txt\n" |
|
202 |
out = backtick("bzr stat") |
|
203 |
assert out == "? test.txt\n" |
|
204 |
||
205 |
progress("command help") |
|
206 |
runcmd("bzr help st") |
|
207 |
runcmd("bzr help") |
|
208 |
runcmd("bzr help commands") |
|
352
by Martin Pool
- Show aliases in command help |
209 |
runcmd("bzr help slartibartfast", 1) |
210 |
||
211 |
out = backtick("bzr help ci") |
|
212 |
out.index('aliases: ') |
|
350
by Martin Pool
- refactor command aliases into command classes |
213 |
|
305
by Martin Pool
testbzr: test renames |
214 |
progress("can't rename unversioned file") |
215 |
runcmd("bzr rename test.txt new-test.txt", 1) |
|
216 |
||
304
by Martin Pool
testbzr: test adding a file |
217 |
progress("adding a file") |
218 |
||
219 |
runcmd("bzr add test.txt") |
|
220 |
assert backtick("bzr unknowns") == '' |
|
307
by Martin Pool
testbzr: clean up crlf handling |
221 |
assert backtick("bzr status --all") == "A test.txt\n" |
300
by Martin Pool
- more tests |
222 |
|
305
by Martin Pool
testbzr: test renames |
223 |
progress("rename newly-added file") |
224 |
runcmd("bzr rename test.txt hello.txt") |
|
306
by Martin Pool
testbzr: test renames |
225 |
assert os.path.exists("hello.txt") |
226 |
assert not os.path.exists("test.txt") |
|
305
by Martin Pool
testbzr: test renames |
227 |
|
308
by Martin Pool
fix test suite |
228 |
assert backtick("bzr revno") == '0\n' |
307
by Martin Pool
testbzr: clean up crlf handling |
229 |
|
339
by Martin Pool
many more diffs |
230 |
progress("add first revision") |
231 |
runcmd(["bzr", "commit", "-m", 'add first revision']) |
|
232 |
||
233 |
progress("more complex renames") |
|
234 |
os.mkdir("sub1") |
|
235 |
runcmd("bzr rename hello.txt sub1", 1) |
|
236 |
runcmd("bzr rename hello.txt sub1/hello.txt", 1) |
|
237 |
runcmd("bzr move hello.txt sub1", 1) |
|
238 |
||
239 |
runcmd("bzr add sub1") |
|
240 |
runcmd("bzr rename sub1 sub2") |
|
241 |
runcmd("bzr move hello.txt sub2") |
|
392
by Martin Pool
- fix relpath and add tests |
242 |
assert backtick("bzr relpath sub2/hello.txt") == "sub2/hello.txt\n" |
339
by Martin Pool
many more diffs |
243 |
|
244 |
assert exists("sub2") |
|
245 |
assert exists("sub2/hello.txt") |
|
246 |
assert not exists("sub1") |
|
247 |
assert not exists("hello.txt") |
|
248 |
||
249 |
runcmd(['bzr', 'commit', '-m', 'commit with some things moved to subdirs']) |
|
250 |
||
251 |
mkdir("sub1") |
|
252 |
runcmd('bzr add sub1') |
|
253 |
runcmd('bzr move sub2/hello.txt sub1') |
|
254 |
assert not exists('sub2/hello.txt') |
|
255 |
assert exists('sub1/hello.txt') |
|
256 |
runcmd('bzr move sub2 sub1') |
|
257 |
assert not exists('sub2') |
|
258 |
assert exists('sub1/sub2') |
|
259 |
||
260 |
runcmd(['bzr', 'commit', '-m', 'rename nested subdirectories']) |
|
261 |
||
262 |
cd('sub1/sub2') |
|
263 |
runcmd('bzr move ../hello.txt .') |
|
340
by Martin Pool
- more testcase fixes |
264 |
assert exists('./hello.txt') |
392
by Martin Pool
- fix relpath and add tests |
265 |
assert backtick('bzr relpath hello.txt') == 'sub1/sub2/hello.txt\n' |
266 |
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == 'sub1/sub2/hello.txt\n' |
|
339
by Martin Pool
many more diffs |
267 |
runcmd(['bzr', 'commit', '-m', 'move to parent directory']) |
268 |
cd('..') |
|
392
by Martin Pool
- fix relpath and add tests |
269 |
assert backtick('bzr relpath sub2/hello.txt') == 'sub1/sub2/hello.txt\n' |
340
by Martin Pool
- more testcase fixes |
270 |
|
271 |
runcmd('bzr move sub2/hello.txt .') |
|
272 |
assert exists('hello.txt') |
|
389
by Martin Pool
- new commit --file option! |
273 |
|
274 |
f = file('hello.txt', 'wt') |
|
275 |
f.write('some nice new content\n') |
|
276 |
f.close() |
|
277 |
||
278 |
f = file('msg.tmp', 'wt') |
|
279 |
f.write('this is my new commit\n') |
|
280 |
f.close() |
|
281 |
||
282 |
runcmd('bzr commit -F msg.tmp') |
|
394
by Martin Pool
- Fix argument handling in export command |
283 |
|
284 |
assert backtick('bzr revno') == '5\n' |
|
285 |
runcmd('bzr export -r 5 export-5.tmp') |
|
286 |
runcmd('bzr export export.tmp') |
|
339
by Martin Pool
many more diffs |
287 |
|
300
by Martin Pool
- more tests |
288 |
cd('..') |
289 |
||
296
by Martin Pool
- better reports from testbzr when it fails |
290 |
progress("all tests passed!") |
291 |
except Exception, e: |
|
292 |
sys.stderr.write('*' * 50 + '\n' |
|
293 |
+ 'testbzr: tests failed\n' |
|
335
by Martin Pool
- add new failing test for command parsing |
294 |
+ 'see ' + LOGFILENAME + ' for more information\n' |
296
by Martin Pool
- better reports from testbzr when it fails |
295 |
+ '*' * 50 + '\n') |
296 |
logfile.write('tests failed!\n') |
|
297 |
traceback.print_exc(None, logfile) |
|
298 |
sys.exit(1) |