~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to testbzr

  • Committer: Martin Pool
  • Date: 2005-06-06 05:13:21 UTC
  • Revision ID: mbp@sourcefrog.net-20050606051321-096739d92aa50664
- make sure bzr is always explicitly invoked through 
  python in case it's not executable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#! /usr/bin/python
 
2
# -*- coding: utf-8 -*-
2
3
 
3
4
# Copyright (C) 2005 Canonical Ltd
4
5
 
22
23
This always runs bzr as an external process to try to catch bugs
23
24
related to argument processing, startup, etc.
24
25
 
 
26
usage:
 
27
 
 
28
    testbzr [-p PYTHON] [BZR]
 
29
 
 
30
By default this tests the copy of bzr found in the same directory as
 
31
testbzr, or the first one found on the $PATH.  A copy of bzr may be
 
32
given on the command line to override this, for example when applying
 
33
a new test suite to an old copy of bzr or vice versa.
 
34
 
 
35
testbzr normally invokes bzr using the same version of python as it
 
36
would normally use to run -- that is, the system default python,
 
37
unless that is older than 2.3.  The -p option allows specification of
 
38
a different Python interpreter, such as when testing that bzr still
 
39
works on python2.3.
 
40
 
25
41
This replaces the previous test.sh which was not very portable."""
26
42
 
27
43
import sys, os, traceback
 
44
from os import mkdir
 
45
from os.path import exists
 
46
 
 
47
TESTDIR = "testbzr.tmp"
 
48
 
 
49
 
 
50
# we always invoke bzr as 'python bzr' (or e.g. 'python2.3 bzr')
 
51
# partly so as to cope if the bzr binary is not marked executable
 
52
OVERRIDE_PYTHON = 'python'
 
53
 
 
54
LOGFILENAME = 'testbzr.log'
28
55
 
29
56
try:
30
57
    import shutil
41
68
 
42
69
def formcmd(cmd):
43
70
    if isinstance(cmd, basestring):
44
 
        logfile.write('$ %s\n' % cmd)
45
71
        cmd = cmd.split()
46
 
    else:
47
 
        logfile.write('$ %r\n' % cmd)
48
 
 
 
72
 
 
73
    if cmd[0] == 'bzr':
 
74
        cmd[0] = BZRPATH
 
75
        if OVERRIDE_PYTHON:
 
76
            cmd.insert(0, OVERRIDE_PYTHON)
 
77
 
 
78
    logfile.write('$ %r\n' % cmd)
 
79
    
49
80
    return cmd
50
81
 
51
82
 
104
135
    logfile.write('   at %s:%d\n' % stack[:2])
105
136
 
106
137
 
107
 
TESTDIR = "testbzr.tmp"
108
138
 
109
139
# prepare an empty scratch directory
110
140
if os.path.exists(TESTDIR):
111
141
    shutil.rmtree(TESTDIR)
112
142
 
113
 
 
114
 
logfile = open('testbzr.log', 'wt', buffering=1)
 
143
start_dir = os.getcwd()
 
144
 
 
145
 
 
146
logfile = open(LOGFILENAME, 'wt', buffering=1)
115
147
 
116
148
 
117
149
try:
 
150
    from getopt import getopt
 
151
    opts, args = getopt(sys.argv[1:], 'p:')
 
152
 
 
153
    for option, value in opts:
 
154
        if option == '-p':
 
155
            OVERRIDE_PYTHON = value
 
156
            
 
157
    
 
158
    mypath = os.path.abspath(sys.argv[0])
 
159
    print '%-30s %s' % ('running tests from', mypath)
 
160
 
 
161
    global BZRPATH
 
162
 
 
163
    if args:
 
164
        BZRPATH = args[0]
 
165
    else:
 
166
        BZRPATH = os.path.join(os.path.split(mypath)[0], 'bzr')
 
167
 
 
168
    print '%-30s %s' % ('against bzr', BZRPATH)
 
169
    print '%-30s %s' % ('in directory', os.getcwd())
 
170
    print '%-30s %s' % ('with python', (OVERRIDE_PYTHON or '(default)'))
 
171
    print
 
172
    print backtick('bzr version')
 
173
    
118
174
    runcmd(['mkdir', TESTDIR])
119
175
    cd(TESTDIR)
 
176
    test_root = os.getcwd()
120
177
 
121
178
    progress("introductory commands")
122
179
    runcmd("bzr version")
 
180
    runcmd("bzr --version")
123
181
    runcmd("bzr help")
124
182
    runcmd("bzr --help")
125
183
 
 
184
    progress("internal tests")
 
185
    runcmd("bzr selftest")
 
186
 
126
187
    progress("user identity")
127
188
    # this should always identify something, if only "john@localhost"
128
189
    runcmd("bzr whoami")
132
193
    progress("invalid commands")
133
194
    runcmd("bzr pants", retcode=1)
134
195
    runcmd("bzr --pants off", retcode=1)
 
196
    runcmd("bzr diff --message foo", retcode=1)
135
197
 
136
198
    progress("basic branch creation")
137
199
    runcmd(['mkdir', 'branch1'])
138
200
    cd('branch1')
139
201
    runcmd('bzr init')
140
202
 
 
203
    assert backtick('bzr root')[:-1] == os.path.join(test_root, 'branch1')
 
204
 
141
205
    progress("status of new file")
142
206
    
143
207
    f = file('test.txt', 'wt')
148
212
    assert out == 'test.txt\n'
149
213
 
150
214
    out = backtick("bzr status")
151
 
    assert out == '''?       test.txt\n'''
 
215
    assert out == 'unknown:\n  test.txt\n'
152
216
 
153
217
    out = backtick("bzr status --all")
154
 
    assert out == "?       test.txt\n"
 
218
    assert out == "unknown:\n  test.txt\n"
 
219
 
 
220
    out = backtick("bzr status test.txt --all")
 
221
    assert out == "unknown:\n  test.txt\n"
 
222
 
 
223
    f = file('test2.txt', 'wt')
 
224
    f.write('goodbye cruel world...\n')
 
225
    f.close()
 
226
 
 
227
    out = backtick("bzr status test.txt")
 
228
    assert out == "unknown:\n  test.txt\n"
 
229
 
 
230
    out = backtick("bzr status")
 
231
    assert out == ("unknown:\n"
 
232
                   "  test.txt\n"
 
233
                   "  test2.txt\n")
 
234
 
 
235
    os.unlink('test2.txt')
 
236
 
 
237
    progress("command aliases")
 
238
    out = backtick("bzr st --all")
 
239
    assert out == ("unknown:\n"
 
240
                   "  test.txt\n")
 
241
    
 
242
    out = backtick("bzr stat")
 
243
    assert out == ("unknown:\n"
 
244
                   "  test.txt\n")
 
245
 
 
246
    progress("command help")
 
247
    runcmd("bzr help st")
 
248
    runcmd("bzr help")
 
249
    runcmd("bzr help commands")
 
250
    runcmd("bzr help slartibartfast", 1)
 
251
 
 
252
    out = backtick("bzr help ci")
 
253
    out.index('aliases: ')
155
254
 
156
255
    progress("can't rename unversioned file")
157
256
    runcmd("bzr rename test.txt new-test.txt", 1)
160
259
 
161
260
    runcmd("bzr add test.txt")
162
261
    assert backtick("bzr unknowns") == ''
163
 
    assert backtick("bzr status --all") == "A       test.txt\n"
 
262
    assert backtick("bzr status --all") == ("added:\n"
 
263
                                            "  test.txt\n")
164
264
 
165
265
    progress("rename newly-added file")
166
266
    runcmd("bzr rename test.txt hello.txt")
169
269
 
170
270
    assert backtick("bzr revno") == '0\n'
171
271
 
172
 
    cd('..')
 
272
    progress("add first revision")
 
273
    runcmd(["bzr", "commit", "-m", 'add first revision'])
 
274
 
 
275
    progress("more complex renames")
 
276
    os.mkdir("sub1")
 
277
    runcmd("bzr rename hello.txt sub1", 1)
 
278
    runcmd("bzr rename hello.txt sub1/hello.txt", 1)
 
279
    runcmd("bzr move hello.txt sub1", 1)
 
280
 
 
281
    runcmd("bzr add sub1")
 
282
    runcmd("bzr rename sub1 sub2")
 
283
    runcmd("bzr move hello.txt sub2")
 
284
    assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
285
 
 
286
    assert exists("sub2")
 
287
    assert exists("sub2/hello.txt")
 
288
    assert not exists("sub1")
 
289
    assert not exists("hello.txt")
 
290
 
 
291
    runcmd(['bzr', 'commit', '-m', 'commit with some things moved to subdirs'])
 
292
 
 
293
    mkdir("sub1")
 
294
    runcmd('bzr add sub1')
 
295
    runcmd('bzr move sub2/hello.txt sub1')
 
296
    assert not exists('sub2/hello.txt')
 
297
    assert exists('sub1/hello.txt')
 
298
    runcmd('bzr move sub2 sub1')
 
299
    assert not exists('sub2')
 
300
    assert exists('sub1/sub2')
 
301
 
 
302
    runcmd(['bzr', 'commit', '-m', 'rename nested subdirectories'])
 
303
 
 
304
    cd('sub1/sub2')
 
305
    assert backtick('bzr root')[:-1] == os.path.join(test_root, 'branch1')
 
306
    runcmd('bzr move ../hello.txt .')
 
307
    assert exists('./hello.txt')
 
308
    assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
309
    assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
310
    runcmd(['bzr', 'commit', '-m', 'move to parent directory'])
 
311
    cd('..')
 
312
    assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
313
 
 
314
    runcmd('bzr move sub2/hello.txt .')
 
315
    assert exists('hello.txt')
 
316
 
 
317
    f = file('hello.txt', 'wt')
 
318
    f.write('some nice new content\n')
 
319
    f.close()
 
320
 
 
321
    f = file('msg.tmp', 'wt')
 
322
    f.write('this is my new commit\n')
 
323
    f.close()
 
324
 
 
325
    runcmd('bzr commit -F msg.tmp')
 
326
 
 
327
    assert backtick('bzr revno') == '5\n'
 
328
    runcmd('bzr export -r 5 export-5.tmp')
 
329
    runcmd('bzr export export.tmp')
 
330
 
 
331
    runcmd('bzr log')
 
332
    runcmd('bzr log -v')
 
333
 
 
334
 
 
335
 
 
336
    progress("file with spaces in name")
 
337
    mkdir('sub directory')
 
338
    file('sub directory/file with spaces ', 'wt').write('see how this works\n')
 
339
    runcmd('bzr add .')
 
340
    runcmd('bzr diff')
 
341
    runcmd('bzr commit -m add-spaces')
 
342
    runcmd('bzr check')
 
343
 
 
344
    runcmd('bzr log')
 
345
    runcmd('bzr log --forward')
 
346
 
 
347
    runcmd('bzr info')
 
348
 
 
349
 
 
350
    
 
351
 
 
352
 
 
353
 
 
354
    cd('..')
 
355
    cd('..')
 
356
 
 
357
    progress('status after remove')
 
358
    mkdir('status-after-remove')
 
359
    # see mail from William Dodé, 2005-05-25
 
360
    # $ bzr init; touch a; bzr add a; bzr commit -m "add a"
 
361
    #     * looking for changes...
 
362
    #     added a
 
363
    #     * commited r1
 
364
    #     $ bzr remove a
 
365
    #     $ bzr status
 
366
    #     bzr: local variable 'kind' referenced before assignment
 
367
    #     at /vrac/python/bazaar-ng/bzrlib/diff.py:286 in compare_trees()
 
368
    #     see ~/.bzr.log for debug information
 
369
    cd('status-after-remove')
 
370
    runcmd('bzr init')
 
371
    file('a', 'w').write('foo')
 
372
    runcmd('bzr add a')
 
373
    runcmd(['bzr', 'commit', '-m', 'add a'])
 
374
    runcmd('bzr remove a')
 
375
    runcmd('bzr status')
 
376
 
 
377
    cd('..')
 
378
 
 
379
    progress('ignore patterns')
 
380
    mkdir('ignorebranch')
 
381
    cd('ignorebranch')
 
382
    runcmd('bzr init')
 
383
    assert backtick('bzr unknowns') == ''
 
384
 
 
385
    file('foo.tmp', 'wt').write('tmp files are ignored')
 
386
    assert backtick('bzr unknowns') == ''
 
387
 
 
388
    file('foo.c', 'wt').write('int main() {}')
 
389
    assert backtick('bzr unknowns') == 'foo.c\n'
 
390
    runcmd('bzr add foo.c')
 
391
    assert backtick('bzr unknowns') == ''
 
392
 
 
393
    # 'ignore' works when creating the .bzignore file
 
394
    file('foo.blah', 'wt').write('blah')
 
395
    assert backtick('bzr unknowns') == 'foo.blah\n'
 
396
    runcmd('bzr ignore *.blah')
 
397
    assert backtick('bzr unknowns') == ''
 
398
    assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
399
 
 
400
    # 'ignore' works when then .bzrignore file already exists
 
401
    file('garh', 'wt').write('garh')
 
402
    assert backtick('bzr unknowns') == 'garh\n'
 
403
    runcmd('bzr ignore garh')
 
404
    assert backtick('bzr unknowns') == ''
 
405
    assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
406
 
 
407
    cd('..')
 
408
 
 
409
 
 
410
 
 
411
 
 
412
    progress("recursive and non-recursive add")
 
413
    mkdir('no-recurse')
 
414
    cd('no-recurse')
 
415
    runcmd('bzr init')
 
416
    mkdir('foo')
 
417
    fp = os.path.join('foo', 'test.txt')
 
418
    f = file(fp, 'w')
 
419
    f.write('hello!\n')
 
420
    f.close()
 
421
    runcmd('bzr add --no-recurse foo')
 
422
    runcmd('bzr file-id foo')
 
423
    runcmd('bzr file-id ' + fp, 1)      # not versioned yet
 
424
    runcmd('bzr commit -m add-dir-only')
 
425
 
 
426
    runcmd('bzr file-id ' + fp, 1)      # still not versioned 
 
427
 
 
428
    runcmd('bzr add foo')
 
429
    runcmd('bzr file-id ' + fp)
 
430
    runcmd('bzr commit -m add-sub-file')
 
431
    
 
432
    cd('..')
 
433
 
 
434
 
 
435
 
173
436
 
174
437
    progress("all tests passed!")
175
438
except Exception, e:
176
439
    sys.stderr.write('*' * 50 + '\n'
177
440
                     + 'testbzr: tests failed\n'
178
 
                     + 'see testbzr.log for more information\n'
 
441
                     + 'see ' + LOGFILENAME + ' for more information\n'
179
442
                     + '*' * 50 + '\n')
180
443
    logfile.write('tests failed!\n')
181
444
    traceback.print_exc(None, logfile)
 
445
    logfile.close()
 
446
 
 
447
    sys.stdout.writelines(file(os.path.join(start_dir, LOGFILENAME), 'rt').readlines()[-50:])
 
448
    
182
449
    sys.exit(1)
 
450