~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

  • Committer: Robert Collins
  • Date: 2005-09-12 12:49:28 UTC
  • mfrom: (1092.2.12)
  • mto: (1092.2.15)
  • mto: This revision was merged to the branch mainline in revision 1397.
  • Revision ID: robertc@robertcollins.net-20050912124928-1da074d2e9c3344b
mergeĀ fromĀ baz2bzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
 
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
 
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
 
 
19
"""Black-box tests for bzr.
 
20
 
 
21
These check that it behaves properly when it's invoked through the regular
 
22
command-line interface.
 
23
 
 
24
This always reinvokes bzr through a new Python interpreter, which is a
 
25
bit inefficient but arguably tests in a way more representative of how
 
26
it's normally invoked.
 
27
"""
 
28
 
 
29
import os;
 
30
import sys
 
31
import os
 
32
 
 
33
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
 
34
from bzrlib.branch import Branch
 
35
from bzrlib.commands import run_bzr
 
36
 
 
37
 
 
38
class ExternalBase(TestCaseInTempDir):
 
39
    def runbzr(self, args, retcode=0,backtick=False):
 
40
        if isinstance(args, basestring):
 
41
            args = args.split()
 
42
 
 
43
        if backtick:
 
44
            return self.backtick(['python', self.BZRPATH,] + args,
 
45
                           retcode=retcode)
 
46
        else:
 
47
            return self.runcmd(['python', self.BZRPATH,] + args,
 
48
                           retcode=retcode)
 
49
 
 
50
 
 
51
class TestCommands(ExternalBase):
 
52
 
 
53
    def test_help_commands(self):
 
54
        self.runbzr('--help')
 
55
        self.runbzr('help')
 
56
        self.runbzr('help commands')
 
57
        self.runbzr('help help')
 
58
        self.runbzr('commit -h')
 
59
 
 
60
    def test_init_branch(self):
 
61
        self.runbzr(['init'])
 
62
 
 
63
    def test_whoami(self):
 
64
        # this should always identify something, if only "john@localhost"
 
65
        self.runbzr("whoami")
 
66
        self.runbzr("whoami --email")
 
67
 
 
68
        self.assertEquals(self.runbzr("whoami --email",
 
69
                                      backtick=True).count('@'), 1)
 
70
        
 
71
    def test_whoami_branch(self):
 
72
        """branch specific user identity works."""
 
73
        self.runbzr('init')
 
74
        f = file('.bzr/email', 'wt')
 
75
        f.write('Branch Identity <branch@identi.ty>')
 
76
        f.close()
 
77
        whoami = self.runbzr("whoami",backtick=True)
 
78
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
79
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
 
80
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
 
81
 
 
82
    def test_invalid_commands(self):
 
83
        self.runbzr("pants", retcode=1)
 
84
        self.runbzr("--pants off", retcode=1)
 
85
        self.runbzr("diff --message foo", retcode=1)
 
86
 
 
87
    def test_empty_commit(self):
 
88
        self.runbzr("init")
 
89
        self.build_tree(['hello.txt'])
 
90
        self.runbzr("commit -m empty", retcode=1)
 
91
        self.runbzr("add hello.txt")
 
92
        self.runbzr("commit -m added")
 
93
 
 
94
    def test_ignore_patterns(self):
 
95
        from bzrlib.branch import Branch
 
96
        
 
97
        b = Branch('.', init=True)
 
98
        self.assertEquals(list(b.unknowns()), [])
 
99
 
 
100
        file('foo.tmp', 'wt').write('tmp files are ignored')
 
101
        self.assertEquals(list(b.unknowns()), [])
 
102
        assert self.backtick('bzr unknowns') == ''
 
103
 
 
104
        file('foo.c', 'wt').write('int main() {}')
 
105
        self.assertEquals(list(b.unknowns()), ['foo.c'])
 
106
        assert self.backtick('bzr unknowns') == 'foo.c\n'
 
107
 
 
108
        self.runbzr(['add', 'foo.c'])
 
109
        assert self.backtick('bzr unknowns') == ''
 
110
 
 
111
        # 'ignore' works when creating the .bzignore file
 
112
        file('foo.blah', 'wt').write('blah')
 
113
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
 
114
        self.runbzr('ignore *.blah')
 
115
        self.assertEquals(list(b.unknowns()), [])
 
116
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
117
 
 
118
        # 'ignore' works when then .bzrignore file already exists
 
119
        file('garh', 'wt').write('garh')
 
120
        self.assertEquals(list(b.unknowns()), ['garh'])
 
121
        assert self.backtick('bzr unknowns') == 'garh\n'
 
122
        self.runbzr('ignore garh')
 
123
        self.assertEquals(list(b.unknowns()), [])
 
124
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
125
 
 
126
    def test_revert(self):
 
127
        self.runbzr('init')
 
128
 
 
129
        file('hello', 'wt').write('foo')
 
130
        self.runbzr('add hello')
 
131
        self.runbzr('commit -m setup hello')
 
132
 
 
133
        file('goodbye', 'wt').write('baz')
 
134
        self.runbzr('add goodbye')
 
135
        self.runbzr('commit -m setup goodbye')
 
136
        
 
137
        file('hello', 'wt').write('bar')
 
138
        file('goodbye', 'wt').write('qux')
 
139
        self.runbzr('revert hello')
 
140
        self.check_file_contents('hello', 'foo')
 
141
        self.check_file_contents('goodbye', 'qux')
 
142
        self.runbzr('revert')
 
143
        self.check_file_contents('goodbye', 'baz')
 
144
 
 
145
        os.mkdir('revertdir')
 
146
        self.runbzr('add revertdir')
 
147
        self.runbzr('commit -m f')
 
148
        os.rmdir('revertdir')
 
149
        self.runbzr('revert')
 
150
 
 
151
    def skipped_test_mv_modes(self):
 
152
        """Test two modes of operation for mv"""
 
153
        from bzrlib.branch import Branch
 
154
        b = Branch('.', init=True)
 
155
        self.build_tree(['a', 'c', 'subdir/'])
 
156
        self.run_bzr('mv', 'a', 'b')
 
157
        self.run_bzr('mv', 'b', 'subdir')
 
158
        self.run_bzr('mv', 'subdir/b', 'a')
 
159
        self.run_bzr('mv', 'a', 'b', 'subdir')
 
160
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
161
 
 
162
    def test_main_version(self):
 
163
        """Check output from version command and master option is reasonable"""
 
164
        # output is intentionally passed through to stdout so that we
 
165
        # can see the version being tested
 
166
        output = self.runbzr('version', backtick=1)
 
167
        self.log('bzr version output:')
 
168
        self.log(output)
 
169
        self.assert_(output.startswith('bzr (bazaar-ng) '))
 
170
        self.assertNotEqual(output.index('Canonical'), -1)
 
171
        # make sure --version is consistent
 
172
        tmp_output = self.runbzr('--version', backtick=1)
 
173
        self.log('bzr --version output:')
 
174
        self.log(tmp_output)
 
175
        self.assertEquals(output, tmp_output)
 
176
 
 
177
    def example_branch(test):
 
178
        test.runbzr('init')
 
179
        file('hello', 'wt').write('foo')
 
180
        test.runbzr('add hello')
 
181
        test.runbzr('commit -m setup hello')
 
182
        file('goodbye', 'wt').write('baz')
 
183
        test.runbzr('add goodbye')
 
184
        test.runbzr('commit -m setup goodbye')
 
185
 
 
186
    def test_revert(self):
 
187
        self.example_branch()
 
188
        file('hello', 'wt').write('bar')
 
189
        file('goodbye', 'wt').write('qux')
 
190
        self.runbzr('revert hello')
 
191
        self.check_file_contents('hello', 'foo')
 
192
        self.check_file_contents('goodbye', 'qux')
 
193
        self.runbzr('revert')
 
194
        self.check_file_contents('goodbye', 'baz')
 
195
 
 
196
    def test_merge(self):
 
197
        from bzrlib.branch import Branch
 
198
        
 
199
        os.mkdir('a')
 
200
        os.chdir('a')
 
201
        self.example_branch()
 
202
        os.chdir('..')
 
203
        self.runbzr('branch a b')
 
204
        os.chdir('b')
 
205
        file('goodbye', 'wt').write('quux')
 
206
        self.runbzr(['commit',  '-m',  "more u's are always good"])
 
207
 
 
208
        os.chdir('../a')
 
209
        file('hello', 'wt').write('quuux')
 
210
        # We can't merge when there are in-tree changes
 
211
        self.runbzr('merge ../b', retcode=1)
 
212
        self.runbzr(['commit', '-m', "Like an epidemic of u's"])
 
213
        self.runbzr('merge ../b')
 
214
        self.check_file_contents('goodbye', 'quux')
 
215
        # Merging a branch pulls its revision into the tree
 
216
        a = Branch('.')
 
217
        b = Branch('../b')
 
218
        a.get_revision_xml(b.last_patch())
 
219
        self.log('pending merges: %s', a.pending_merges())
 
220
        #        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
 
221
        #        % (a.pending_merges(), b.last_patch())
 
222
 
 
223
    def test_pull(self):
 
224
        """Pull changes from one branch to another."""
 
225
        os.mkdir('a')
 
226
        os.chdir('a')
 
227
 
 
228
        self.example_branch()
 
229
        os.chdir('..')
 
230
        self.runbzr('branch a b')
 
231
        os.chdir('b')
 
232
        self.runbzr('commit -m blah --unchanged')
 
233
        os.chdir('../a')
 
234
        a = Branch('.')
 
235
        b = Branch('../b')
 
236
        assert a.revision_history() == b.revision_history()[:-1]
 
237
        self.runbzr('pull ../b')
 
238
        assert a.revision_history() == b.revision_history()
 
239
        self.runbzr('commit -m blah2 --unchanged')
 
240
        os.chdir('../b')
 
241
        self.runbzr('commit -m blah3 --unchanged')
 
242
        self.runbzr('pull ../a', retcode=1)
 
243
        os.chdir('../a')
 
244
        self.runbzr('merge ../b')
 
245
        self.runbzr('commit -m blah4 --unchanged')
 
246
        os.chdir('../b')
 
247
        self.runbzr('pull ../a')
 
248
        assert a.revision_history()[-1] == b.revision_history()[-1]
 
249
        
 
250
 
 
251
    def test_add_reports(self):
 
252
        """add command prints the names of added files."""
 
253
        b = Branch('.', init=True)
 
254
        self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
 
255
 
 
256
        from cStringIO import StringIO
 
257
        out = StringIO()
 
258
 
 
259
        ret = self.apply_redirected(None, out, None,
 
260
                                    run_bzr,
 
261
                                    ['add'])
 
262
        self.assertEquals(ret, 0)
 
263
 
 
264
        # the ordering is not defined at the moment
 
265
        results = sorted(out.getvalue().rstrip('\n').split('\n'))
 
266
        self.assertEquals(['added dir',
 
267
                           'added dir/sub.txt',
 
268
                           'added top.txt',],
 
269
                          results)
 
270
 
 
271
 
 
272
def has_symlinks():
 
273
    if hasattr(os, 'symlink'):
 
274
        return True
 
275
    else:
 
276
        return False
 
277
 
 
278
def listdir_sorted(dir):
 
279
    L = os.listdir(dir)
 
280
    L.sort()
 
281
    return L
 
282
 
 
283
 
 
284
class OldTests(ExternalBase):
 
285
    """old tests moved from ./testbzr."""
 
286
 
 
287
    def test_bzr(self):
 
288
        from os import chdir, mkdir
 
289
        from os.path import exists
 
290
 
 
291
        runbzr = self.runbzr
 
292
        backtick = self.backtick
 
293
        progress = self.log
 
294
 
 
295
        progress("basic branch creation")
 
296
        mkdir('branch1')
 
297
        chdir('branch1')
 
298
        runbzr('init')
 
299
 
 
300
        self.assertEquals(backtick('bzr root').rstrip(),
 
301
                          os.path.join(self.test_dir, 'branch1'))
 
302
 
 
303
        progress("status of new file")
 
304
 
 
305
        f = file('test.txt', 'wt')
 
306
        f.write('hello world!\n')
 
307
        f.close()
 
308
 
 
309
        out = backtick("bzr unknowns")
 
310
        self.assertEquals(out, 'test.txt\n')
 
311
 
 
312
        out = backtick("bzr status")
 
313
        assert out == 'unknown:\n  test.txt\n'
 
314
 
 
315
        out = backtick("bzr status --all")
 
316
        assert out == "unknown:\n  test.txt\n"
 
317
 
 
318
        out = backtick("bzr status test.txt --all")
 
319
        assert out == "unknown:\n  test.txt\n"
 
320
 
 
321
        f = file('test2.txt', 'wt')
 
322
        f.write('goodbye cruel world...\n')
 
323
        f.close()
 
324
 
 
325
        out = backtick("bzr status test.txt")
 
326
        assert out == "unknown:\n  test.txt\n"
 
327
 
 
328
        out = backtick("bzr status")
 
329
        assert out == ("unknown:\n"
 
330
                       "  test.txt\n"
 
331
                       "  test2.txt\n")
 
332
 
 
333
        os.unlink('test2.txt')
 
334
 
 
335
        progress("command aliases")
 
336
        out = backtick("bzr st --all")
 
337
        assert out == ("unknown:\n"
 
338
                       "  test.txt\n")
 
339
 
 
340
        out = backtick("bzr stat")
 
341
        assert out == ("unknown:\n"
 
342
                       "  test.txt\n")
 
343
 
 
344
        progress("command help")
 
345
        runbzr("help st")
 
346
        runbzr("help")
 
347
        runbzr("help commands")
 
348
        runbzr("help slartibartfast", 1)
 
349
 
 
350
        out = backtick("bzr help ci")
 
351
        out.index('aliases: ')
 
352
 
 
353
        progress("can't rename unversioned file")
 
354
        runbzr("rename test.txt new-test.txt", 1)
 
355
 
 
356
        progress("adding a file")
 
357
 
 
358
        runbzr("add test.txt")
 
359
        assert backtick("bzr unknowns") == ''
 
360
        assert backtick("bzr status --all") == ("added:\n"
 
361
                                                "  test.txt\n")
 
362
 
 
363
        progress("rename newly-added file")
 
364
        runbzr("rename test.txt hello.txt")
 
365
        assert os.path.exists("hello.txt")
 
366
        assert not os.path.exists("test.txt")
 
367
 
 
368
        assert backtick("bzr revno") == '0\n'
 
369
 
 
370
        progress("add first revision")
 
371
        runbzr(['commit', '-m', 'add first revision'])
 
372
 
 
373
        progress("more complex renames")
 
374
        os.mkdir("sub1")
 
375
        runbzr("rename hello.txt sub1", 1)
 
376
        runbzr("rename hello.txt sub1/hello.txt", 1)
 
377
        runbzr("move hello.txt sub1", 1)
 
378
 
 
379
        runbzr("add sub1")
 
380
        runbzr("rename sub1 sub2")
 
381
        runbzr("move hello.txt sub2")
 
382
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
383
 
 
384
        assert exists("sub2")
 
385
        assert exists("sub2/hello.txt")
 
386
        assert not exists("sub1")
 
387
        assert not exists("hello.txt")
 
388
 
 
389
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
 
390
 
 
391
        mkdir("sub1")
 
392
        runbzr('add sub1')
 
393
        runbzr('move sub2/hello.txt sub1')
 
394
        assert not exists('sub2/hello.txt')
 
395
        assert exists('sub1/hello.txt')
 
396
        runbzr('move sub2 sub1')
 
397
        assert not exists('sub2')
 
398
        assert exists('sub1/sub2')
 
399
 
 
400
        runbzr(['commit', '-m', 'rename nested subdirectories'])
 
401
 
 
402
        chdir('sub1/sub2')
 
403
        self.assertEquals(backtick('bzr root')[:-1],
 
404
                          os.path.join(self.test_dir, 'branch1'))
 
405
        runbzr('move ../hello.txt .')
 
406
        assert exists('./hello.txt')
 
407
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
408
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
409
        runbzr(['commit', '-m', 'move to parent directory'])
 
410
        chdir('..')
 
411
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
412
 
 
413
        runbzr('move sub2/hello.txt .')
 
414
        assert exists('hello.txt')
 
415
 
 
416
        f = file('hello.txt', 'wt')
 
417
        f.write('some nice new content\n')
 
418
        f.close()
 
419
 
 
420
        f = file('msg.tmp', 'wt')
 
421
        f.write('this is my new commit\n')
 
422
        f.close()
 
423
 
 
424
        runbzr('commit -F msg.tmp')
 
425
 
 
426
        assert backtick('bzr revno') == '5\n'
 
427
        runbzr('export -r 5 export-5.tmp')
 
428
        runbzr('export export.tmp')
 
429
 
 
430
        runbzr('log')
 
431
        runbzr('log -v')
 
432
        runbzr('log -v --forward')
 
433
        runbzr('log -m', retcode=1)
 
434
        log_out = backtick('bzr log -m commit')
 
435
        assert "this is my new commit" in log_out
 
436
        assert "rename nested" not in log_out
 
437
        assert 'revision-id' not in log_out
 
438
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
439
 
 
440
 
 
441
        progress("file with spaces in name")
 
442
        mkdir('sub directory')
 
443
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
 
444
        runbzr('add .')
 
445
        runbzr('diff')
 
446
        runbzr('commit -m add-spaces')
 
447
        runbzr('check')
 
448
 
 
449
        runbzr('log')
 
450
        runbzr('log --forward')
 
451
 
 
452
        runbzr('info')
 
453
 
 
454
        if has_symlinks():
 
455
            progress("symlinks")
 
456
            mkdir('symlinks')
 
457
            chdir('symlinks')
 
458
            runbzr('init')
 
459
            os.symlink("NOWHERE1", "link1")
 
460
            runbzr('add link1')
 
461
            assert backtick('bzr unknowns') == ''
 
462
            runbzr(['commit', '-m', '1: added symlink link1'])
 
463
    
 
464
            mkdir('d1')
 
465
            runbzr('add d1')
 
466
            assert backtick('bzr unknowns') == ''
 
467
            os.symlink("NOWHERE2", "d1/link2")
 
468
            assert backtick('bzr unknowns') == 'd1/link2\n'
 
469
            # is d1/link2 found when adding d1
 
470
            runbzr('add d1')
 
471
            assert backtick('bzr unknowns') == ''
 
472
            os.symlink("NOWHERE3", "d1/link3")
 
473
            assert backtick('bzr unknowns') == 'd1/link3\n'
 
474
            runbzr(['commit', '-m', '2: added dir, symlink'])
 
475
    
 
476
            runbzr('rename d1 d2')
 
477
            runbzr('move d2/link2 .')
 
478
            runbzr('move link1 d2')
 
479
            assert os.readlink("./link2") == "NOWHERE2"
 
480
            assert os.readlink("d2/link1") == "NOWHERE1"
 
481
            runbzr('add d2/link3')
 
482
            runbzr('diff')
 
483
            runbzr(['commit', '-m', '3: rename of dir, move symlinks, add link3'])
 
484
    
 
485
            os.unlink("link2")
 
486
            os.symlink("TARGET 2", "link2")
 
487
            os.unlink("d2/link1")
 
488
            os.symlink("TARGET 1", "d2/link1")
 
489
            runbzr('diff')
 
490
            assert backtick("bzr relpath d2/link1") == "d2/link1\n"
 
491
            runbzr(['commit', '-m', '4: retarget of two links'])
 
492
    
 
493
            runbzr('remove d2/link1')
 
494
            assert backtick('bzr unknowns') == 'd2/link1\n'
 
495
            runbzr(['commit', '--unchanged', '-m', '5: remove d2/link1'])
 
496
            print ("commit --uchanged is needed to delete a file with no other"
 
497
                   " changes. this is a bug.")
 
498
    
 
499
            os.mkdir("d1")
 
500
            runbzr('add d1')
 
501
            runbzr('rename d2/link3 d1/link3new')
 
502
            assert backtick('bzr unknowns') == 'd2/link1\n'
 
503
            runbzr(['commit', '-m', '6: remove d2/link1, move/rename link3'])
 
504
            
 
505
            runbzr(['check'])
 
506
            
 
507
            runbzr(['export', '-r', '1', 'exp1.tmp'])
 
508
            chdir("exp1.tmp")
 
509
            assert listdir_sorted(".") == [ "link1" ]
 
510
            assert os.readlink("link1") == "NOWHERE1"
 
511
            chdir("..")
 
512
            
 
513
            runbzr(['export', '-r', '2', 'exp2.tmp'])
 
514
            chdir("exp2.tmp")
 
515
            assert listdir_sorted(".") == [ "d1", "link1" ]
 
516
            chdir("..")
 
517
            
 
518
            runbzr(['export', '-r', '3', 'exp3.tmp'])
 
519
            chdir("exp3.tmp")
 
520
            assert listdir_sorted(".") == [ "d2", "link2" ]
 
521
            assert listdir_sorted("d2") == [ "link1", "link3" ]
 
522
            assert os.readlink("d2/link1") == "NOWHERE1"
 
523
            assert os.readlink("link2")    == "NOWHERE2"
 
524
            chdir("..")
 
525
            
 
526
            runbzr(['export', '-r', '4', 'exp4.tmp'])
 
527
            chdir("exp4.tmp")
 
528
            assert listdir_sorted(".") == [ "d2", "link2" ]
 
529
            assert os.readlink("d2/link1") == "TARGET 1"
 
530
            assert os.readlink("link2")    == "TARGET 2"
 
531
            assert listdir_sorted("d2") == [ "link1", "link3" ]
 
532
            chdir("..")
 
533
            
 
534
            runbzr(['export', '-r', '5', 'exp5.tmp'])
 
535
            chdir("exp5.tmp")
 
536
            assert listdir_sorted(".") == [ "d2", "link2" ]
 
537
            assert os.path.islink("link2")
 
538
            assert listdir_sorted("d2")== [ "link3" ]
 
539
            chdir("..")
 
540
            
 
541
            runbzr(['export', '-r', '6', 'exp6.tmp'])
 
542
            chdir("exp6.tmp")
 
543
            assert listdir_sorted(".") == [ "d1", "d2", "link2" ]
 
544
            assert listdir_sorted("d1") == [ "link3new" ]
 
545
            assert listdir_sorted("d2") == []
 
546
            assert os.readlink("d1/link3new") == "NOWHERE3"
 
547
            chdir("..")
 
548
        else:
 
549
            progress("skipping symlink tests")
 
550