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
29
TESTDIR = "testbzr.tmp"
31
LOGFILENAME = 'testbzr.log'
35
from subprocess import call, Popen, PIPE
36
except ImportError, e:
37
sys.stderr.write("testbzr: sorry, this test suite requires modules from python2.4\n"
42
class CommandFailed(Exception):
47
if isinstance(cmd, basestring):
48
logfile.write('$ %s\n' % cmd)
51
logfile.write('$ %r\n' % cmd)
56
def runcmd(cmd, retcode=0):
57
"""Run one command and check the return code.
59
Returns a tuple of (stdout,stderr) strings.
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."""
67
actual_retcode = call(cmd, stdout=logfile, stderr=logfile)
69
if retcode != actual_retcode:
70
raise CommandFailed("test failed: %r returned %d, expected %d"
71
% (cmd, actual_retcode, retcode))
75
def backtick(cmd, retcode=0):
78
child = Popen(cmd, stdout=PIPE, stderr=logfile)
79
outd, errd = child.communicate()
81
actual_retcode = child.wait()
83
outd = outd.replace('\r', '')
85
if retcode != actual_retcode:
86
raise CommandFailed("test failed: %r returned %d, expected %d"
87
% (cmd, actual_retcode, retcode))
95
logfile.write('* '+ msg + '\n')
100
logfile.write('$ cd %s\n' % dirname)
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])
111
# prepare an empty scratch directory
112
if os.path.exists(TESTDIR):
113
shutil.rmtree(TESTDIR)
116
logfile = open(LOGFILENAME, 'wt', buffering=1)
120
runcmd(['mkdir', TESTDIR])
123
progress("introductory commands")
124
runcmd("bzr version")
125
runcmd("bzr --version")
129
progress("user identity")
130
# this should always identify something, if only "john@localhost"
132
runcmd("bzr whoami --email")
133
assert backtick("bzr whoami --email").count('@') == 1
135
progress("invalid commands")
136
runcmd("bzr pants", retcode=1)
137
runcmd("bzr --pants off", retcode=1)
139
progress("basic branch creation")
140
runcmd(['mkdir', 'branch1'])
144
progress("status of new file")
146
f = file('test.txt', 'wt')
147
f.write('hello world!\n')
150
out = backtick("bzr unknowns")
151
assert out == 'test.txt\n'
153
out = backtick("bzr status")
154
assert out == '''? test.txt\n'''
156
out = backtick("bzr status --all")
157
assert out == "? test.txt\n"
159
progress("can't rename unversioned file")
160
runcmd("bzr rename test.txt new-test.txt", 1)
162
progress("adding a file")
164
runcmd("bzr add test.txt")
165
assert backtick("bzr unknowns") == ''
166
assert backtick("bzr status --all") == "A test.txt\n"
168
progress("rename newly-added file")
169
runcmd("bzr rename test.txt hello.txt")
170
assert os.path.exists("hello.txt")
171
assert not os.path.exists("test.txt")
173
assert backtick("bzr revno") == '0\n'
177
progress("all tests passed!")
179
sys.stderr.write('*' * 50 + '\n'
180
+ 'testbzr: tests failed\n'
181
+ 'see ' + LOGFILENAME + ' for more information\n'
183
logfile.write('tests failed!\n')
184
traceback.print_exc(None, logfile)