~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to testbzr

  • Committer: Martin Pool
  • Date: 2005-05-03 07:48:35 UTC
  • Revision ID: mbp@sourcefrog.net-20050503074835-0133bd0c16440fcd
doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#! /usr/bin/python
2
 
# -*- coding: utf-8 -*-
3
2
 
4
3
# Copyright (C) 2005 Canonical Ltd
5
4
 
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
19
18
 
20
 
print 'please use "bzr selftest" instead'
21
 
import sys
22
 
sys.exit(1)
23
 
 
 
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
 
 
27
import sys, os, traceback
 
28
 
 
29
try:
 
30
    import shutil
 
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"
 
34
                     + '    ' + str(e))
 
35
    sys.exit(1)
 
36
 
 
37
 
 
38
class CommandFailed(Exception):
 
39
    pass
 
40
 
 
41
 
 
42
def formcmd(cmd):
 
43
    if isinstance(cmd, basestring):
 
44
        logfile.write('$ %s\n' % cmd)
 
45
        cmd = cmd.split()
 
46
    else:
 
47
        logfile.write('$ %r\n' % cmd)
 
48
 
 
49
    return cmd
 
50
 
 
51
 
 
52
def runcmd(cmd, retcode=0):
 
53
    """Run one command and check the return code.
 
54
 
 
55
    Returns a tuple of (stdout,stderr) strings.
 
56
 
 
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."""
 
60
    cmd = formcmd(cmd)
 
61
    log_linenumber()
 
62
    
 
63
    actual_retcode = call(cmd, stdout=logfile, stderr=logfile)
 
64
    
 
65
    if retcode != actual_retcode:
 
66
        raise CommandFailed("test failed: %r returned %d, expected %d"
 
67
                            % (cmd, actual_retcode, retcode))
 
68
 
 
69
 
 
70
 
 
71
def backtick(cmd, retcode=0):
 
72
    cmd = formcmd(cmd)
 
73
    log_linenumber()
 
74
    child = Popen(cmd, stdout=PIPE, stderr=logfile)
 
75
    outd, errd = child.communicate()
 
76
    logfile.write(outd)
 
77
    actual_retcode = child.wait()
 
78
 
 
79
    outd = outd.replace('\r', '')
 
80
    
 
81
    if retcode != actual_retcode:
 
82
        raise CommandFailed("test failed: %r returned %d, expected %d"
 
83
                            % (cmd, actual_retcode, retcode))
 
84
 
 
85
    return outd
 
86
 
 
87
 
 
88
 
 
89
def progress(msg):
 
90
    print '* ' + msg
 
91
    logfile.write('* '+ msg + '\n')
 
92
    log_linenumber()
 
93
 
 
94
 
 
95
def cd(dirname):
 
96
    logfile.write('$ cd %s\n' % dirname)
 
97
    os.chdir(dirname)
 
98
 
 
99
 
 
100
 
 
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])
 
105
 
 
106
 
 
107
TESTDIR = "testbzr.tmp"
 
108
 
 
109
# prepare an empty scratch directory
 
110
if os.path.exists(TESTDIR):
 
111
    shutil.rmtree(TESTDIR)
 
112
 
 
113
 
 
114
logfile = open('testbzr.log', 'wt', buffering=1)
 
115
 
 
116
 
 
117
try:
 
118
    runcmd(['mkdir', TESTDIR])
 
119
    cd(TESTDIR)
 
120
 
 
121
    progress("introductory commands")
 
122
    runcmd("bzr version")
 
123
    runcmd("bzr help")
 
124
    runcmd("bzr --help")
 
125
 
 
126
    progress("user identity")
 
127
    # this should always identify something, if only "john@localhost"
 
128
    runcmd("bzr whoami")
 
129
    runcmd("bzr whoami --email")
 
130
    assert backtick("bzr whoami --email").count('@') == 1
 
131
 
 
132
    progress("invalid commands")
 
133
    runcmd("bzr pants", retcode=1)
 
134
    runcmd("bzr --pants off", retcode=1)
 
135
 
 
136
    progress("basic branch creation")
 
137
    runcmd(['mkdir', 'branch1'])
 
138
    cd('branch1')
 
139
    runcmd('bzr init')
 
140
 
 
141
    progress("status of new file")
 
142
    
 
143
    f = file('test.txt', 'wt')
 
144
    f.write('hello world!\n')
 
145
    f.close()
 
146
 
 
147
    out = backtick("bzr unknowns")
 
148
    assert out == 'test.txt\n'
 
149
 
 
150
    out = backtick("bzr status")
 
151
    assert out == '''?       test.txt\n'''
 
152
 
 
153
    out = backtick("bzr status --all")
 
154
    assert out == "?       test.txt\n"
 
155
 
 
156
    progress("can't rename unversioned file")
 
157
    runcmd("bzr rename test.txt new-test.txt", 1)
 
158
 
 
159
    progress("adding a file")
 
160
 
 
161
    runcmd("bzr add test.txt")
 
162
    assert backtick("bzr unknowns") == ''
 
163
    assert backtick("bzr status --all") == "A       test.txt\n"
 
164
 
 
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")
 
169
 
 
170
    assert backtick("bzr revno") == '0\n'
 
171
 
 
172
    cd('..')
 
173
 
 
174
    progress("all tests passed!")
 
175
except Exception, e:
 
176
    sys.stderr.write('*' * 50 + '\n'
 
177
                     + 'testbzr: tests failed\n'
 
178
                     + 'see testbzr.log for more information\n'
 
179
                     + '*' * 50 + '\n')
 
180
    logfile.write('tests failed!\n')
 
181
    traceback.print_exc(None, logfile)
 
182
    sys.exit(1)