17
16
# along with this program; if not, write to the Free Software
18
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
print 'please use "bzr selftest" instead'
20
"""External black-box test for bzr.
22
This always runs bzr as an external process to try to catch bugs
23
related to argument processing, startup, etc.
25
This replaces the previous test.sh which was not very portable."""
27
import sys, os, traceback
31
from subprocess import call, Popen, PIPE
32
except ImportError, e:
33
sys.stderr.write("testbzr: sorry, this test suite requires modules from python2.4\n"
38
class CommandFailed(Exception):
43
if isinstance(cmd, basestring):
44
logfile.write('$ %s\n' % cmd)
47
logfile.write('$ %r\n' % cmd)
52
def runcmd(cmd, retcode=0):
53
"""Run one command and check the return code.
55
Returns a tuple of (stdout,stderr) strings.
57
If a single string is based, it is split into words.
58
For commands that are not simple space-separated words, please
59
pass a list instead."""
63
actual_retcode = call(cmd, stdout=logfile, stderr=logfile)
65
if retcode != actual_retcode:
66
raise CommandFailed("test failed: %r returned %d, expected %d"
67
% (cmd, actual_retcode, retcode))
71
def backtick(cmd, retcode=0):
74
child = Popen(cmd, stdout=PIPE, stderr=logfile)
75
outd, errd = child.communicate()
77
actual_retcode = child.wait()
79
outd = outd.replace('\r', '')
81
if retcode != actual_retcode:
82
raise CommandFailed("test failed: %r returned %d, expected %d"
83
% (cmd, actual_retcode, retcode))
91
logfile.write('* '+ msg + '\n')
96
logfile.write('$ cd %s\n' % dirname)
101
def log_linenumber():
102
"""Log the stack frame location two things up."""
103
stack = traceback.extract_stack()[-3]
104
logfile.write(' at %s:%d\n' % stack[:2])
107
TESTDIR = "testbzr.tmp"
109
# prepare an empty scratch directory
110
if os.path.exists(TESTDIR):
111
shutil.rmtree(TESTDIR)
114
logfile = open('testbzr.log', 'wt', buffering=1)
118
runcmd(['mkdir', TESTDIR])
121
progress("introductory commands")
122
runcmd("bzr version")
126
progress("user identity")
127
# this should always identify something, if only "john@localhost"
129
runcmd("bzr whoami --email")
130
assert backtick("bzr whoami --email").count('@') == 1
132
progress("invalid commands")
133
runcmd("bzr pants", retcode=1)
134
runcmd("bzr --pants off", retcode=1)
136
progress("basic branch creation")
137
runcmd(['mkdir', 'branch1'])
141
progress("status of new file")
143
f = file('test.txt', 'wt')
144
f.write('hello world!\n')
147
out = backtick("bzr unknowns")
148
assert out == 'test.txt\n'
150
out = backtick("bzr status")
151
assert out == '''? test.txt\n'''
153
out = backtick("bzr status --all")
154
assert out == "? test.txt\n"
156
progress("can't rename unversioned file")
157
runcmd("bzr rename test.txt new-test.txt", 1)
159
progress("adding a file")
161
runcmd("bzr add test.txt")
162
assert backtick("bzr unknowns") == ''
163
assert backtick("bzr status --all") == "A test.txt\n"
165
progress("rename newly-added file")
166
runcmd("bzr rename test.txt hello.txt")
167
assert os.path.exists("hello.txt")
168
assert not os.path.exists("test.txt")
170
assert backtick("bzr revno") == '0\n'
174
progress("all tests passed!")
176
sys.stderr.write('*' * 50 + '\n'
177
+ 'testbzr: tests failed\n'
178
+ 'see testbzr.log for more information\n'
180
logfile.write('tests failed!\n')
181
traceback.print_exc(None, logfile)