/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

  • Committer: Aaron Bentley
  • Date: 2005-09-22 03:23:22 UTC
  • mto: (1185.1.37)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: aaron.bentley@utoronto.ca-20050922032322-345dab5d05a96893
Fixed non-tree-root bug in branch, revert, merge

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
from cStringIO import StringIO
 
30
import sys
 
31
import os
 
32
 
 
33
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
 
34
from bzrlib.branch import Branch
 
35
 
 
36
 
 
37
class ExternalBase(TestCaseInTempDir):
 
38
 
 
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.run_bzr_captured(args, retcode=retcode)[0]
 
45
        else:
 
46
            return self.run_bzr_captured(args, retcode=retcode)
 
47
 
 
48
 
 
49
class TestCommands(ExternalBase):
 
50
 
 
51
    def test_help_commands(self):
 
52
        self.runbzr('--help')
 
53
        self.runbzr('help')
 
54
        self.runbzr('help commands')
 
55
        self.runbzr('help help')
 
56
        self.runbzr('commit -h')
 
57
 
 
58
    def test_init_branch(self):
 
59
        self.runbzr(['init'])
 
60
 
 
61
    def test_whoami(self):
 
62
        # this should always identify something, if only "john@localhost"
 
63
        self.runbzr("whoami")
 
64
        self.runbzr("whoami --email")
 
65
 
 
66
        self.assertEquals(self.runbzr("whoami --email",
 
67
                                      backtick=True).count('@'), 1)
 
68
        
 
69
    def test_whoami_branch(self):
 
70
        """branch specific user identity works."""
 
71
        self.runbzr('init')
 
72
        f = file('.bzr/email', 'wt')
 
73
        f.write('Branch Identity <branch@identi.ty>')
 
74
        f.close()
 
75
        bzr_email = os.environ.get('BZREMAIL')
 
76
        if bzr_email is not None:
 
77
            del os.environ['BZREMAIL']
 
78
        whoami = self.runbzr("whoami",backtick=True)
 
79
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
80
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
 
81
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
 
82
        # Verify that the environment variable overrides the value 
 
83
        # in the file
 
84
        os.environ['BZREMAIL'] = 'Different ID <other@environ.ment>'
 
85
        whoami = self.runbzr("whoami",backtick=True)
 
86
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
87
        self.assertTrue(whoami.startswith('Different ID <other@environ.ment>'))
 
88
        self.assertTrue(whoami_email.startswith('other@environ.ment'))
 
89
        if bzr_email is not None:
 
90
            os.environ['BZREMAIL'] = bzr_email
 
91
 
 
92
    def test_invalid_commands(self):
 
93
        self.runbzr("pants", retcode=1)
 
94
        self.runbzr("--pants off", retcode=1)
 
95
        self.runbzr("diff --message foo", retcode=1)
 
96
 
 
97
    def test_empty_commit(self):
 
98
        self.runbzr("init")
 
99
        self.build_tree(['hello.txt'])
 
100
        self.runbzr("commit -m empty", retcode=1)
 
101
        self.runbzr("add hello.txt")
 
102
        self.runbzr("commit -m added")
 
103
 
 
104
    def test_ignore_patterns(self):
 
105
        from bzrlib.branch import Branch
 
106
        
 
107
        b = Branch.initialize('.')
 
108
        self.assertEquals(list(b.unknowns()), [])
 
109
 
 
110
        file('foo.tmp', 'wt').write('tmp files are ignored')
 
111
        self.assertEquals(list(b.unknowns()), [])
 
112
        assert self.capture('unknowns') == ''
 
113
 
 
114
        file('foo.c', 'wt').write('int main() {}')
 
115
        self.assertEquals(list(b.unknowns()), ['foo.c'])
 
116
        assert self.capture('unknowns') == 'foo.c\n'
 
117
 
 
118
        self.runbzr(['add', 'foo.c'])
 
119
        assert self.capture('unknowns') == ''
 
120
 
 
121
        # 'ignore' works when creating the .bzignore file
 
122
        file('foo.blah', 'wt').write('blah')
 
123
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
 
124
        self.runbzr('ignore *.blah')
 
125
        self.assertEquals(list(b.unknowns()), [])
 
126
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
127
 
 
128
        # 'ignore' works when then .bzrignore file already exists
 
129
        file('garh', 'wt').write('garh')
 
130
        self.assertEquals(list(b.unknowns()), ['garh'])
 
131
        assert self.capture('unknowns') == 'garh\n'
 
132
        self.runbzr('ignore garh')
 
133
        self.assertEquals(list(b.unknowns()), [])
 
134
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
135
 
 
136
    def test_revert(self):
 
137
        self.runbzr('init')
 
138
 
 
139
        file('hello', 'wt').write('foo')
 
140
        self.runbzr('add hello')
 
141
        self.runbzr('commit -m setup hello')
 
142
 
 
143
        file('goodbye', 'wt').write('baz')
 
144
        self.runbzr('add goodbye')
 
145
        self.runbzr('commit -m setup goodbye')
 
146
        
 
147
        file('hello', 'wt').write('bar')
 
148
        file('goodbye', 'wt').write('qux')
 
149
        self.runbzr('revert hello')
 
150
        self.check_file_contents('hello', 'foo')
 
151
        self.check_file_contents('goodbye', 'qux')
 
152
        self.runbzr('revert')
 
153
        self.check_file_contents('goodbye', 'baz')
 
154
 
 
155
        os.mkdir('revertdir')
 
156
        self.runbzr('add revertdir')
 
157
        self.runbzr('commit -m f')
 
158
        os.rmdir('revertdir')
 
159
        self.runbzr('revert')
 
160
 
 
161
        file('hello', 'wt').write('xyz')
 
162
        self.runbzr('commit -m xyz hello')
 
163
        self.runbzr('revert -r 1 hello')
 
164
        self.check_file_contents('hello', 'foo')
 
165
        self.runbzr('revert hello')
 
166
        self.check_file_contents('hello', 'xyz')
 
167
        os.chdir('revertdir')
 
168
        self.runbzr('revert')
 
169
        os.chdir('..')
 
170
 
 
171
 
 
172
    def test_mv_modes(self):
 
173
        """Test two modes of operation for mv"""
 
174
        from bzrlib.branch import Branch
 
175
        b = Branch.initialize('.')
 
176
        self.build_tree(['a', 'c', 'subdir/'])
 
177
        self.run_bzr_captured(['add', self.test_dir])
 
178
        self.run_bzr_captured(['mv', 'a', 'b'])
 
179
        self.run_bzr_captured(['mv', 'b', 'subdir'])
 
180
        self.run_bzr_captured(['mv', 'subdir/b', 'a'])
 
181
        self.run_bzr_captured(['mv', 'a', 'c', 'subdir'])
 
182
        self.run_bzr_captured(['mv', 'subdir/a', 'subdir/newa'])
 
183
 
 
184
 
 
185
    def test_main_version(self):
 
186
        """Check output from version command and master option is reasonable"""
 
187
        # output is intentionally passed through to stdout so that we
 
188
        # can see the version being tested
 
189
        output = self.runbzr('version', backtick=1)
 
190
        self.log('bzr version output:')
 
191
        self.log(output)
 
192
        self.assert_(output.startswith('bzr (bazaar-ng) '))
 
193
        self.assertNotEqual(output.index('Canonical'), -1)
 
194
        # make sure --version is consistent
 
195
        tmp_output = self.runbzr('--version', backtick=1)
 
196
        self.log('bzr --version output:')
 
197
        self.log(tmp_output)
 
198
        self.assertEquals(output, tmp_output)
 
199
 
 
200
    def example_branch(test):
 
201
        test.runbzr('init')
 
202
        file('hello', 'wt').write('foo')
 
203
        test.runbzr('add hello')
 
204
        test.runbzr('commit -m setup hello')
 
205
        file('goodbye', 'wt').write('baz')
 
206
        test.runbzr('add goodbye')
 
207
        test.runbzr('commit -m setup goodbye')
 
208
 
 
209
    def test_diff(self):
 
210
        self.example_branch()
 
211
        file('hello', 'wt').write('hello world!')
 
212
        self.runbzr('commit -m fixing hello')
 
213
        output = self.runbzr('diff -r 2..3', backtick=1)
 
214
        self.assert_('\n+hello world!' in output)
 
215
        output = self.runbzr('diff -r last:3..last:1', backtick=1)
 
216
        self.assert_('\n+baz' in output)
 
217
 
 
218
    def test_diff(self):
 
219
        self.example_branch()
 
220
        file('hello', 'wt').write('hello world!')
 
221
        self.runbzr('commit -m fixing hello')
 
222
        output = self.runbzr('diff -r 2..3', backtick=1)
 
223
        self.assert_('\n+hello world!' in output)
 
224
        output = self.runbzr('diff -r last:3..last:1', backtick=1)
 
225
        self.assert_('\n+baz' in output)
 
226
 
 
227
    def test_branch(self):
 
228
        """Branch from one branch to another."""
 
229
        os.mkdir('a')
 
230
        os.chdir('a')
 
231
        self.example_branch()
 
232
        os.chdir('..')
 
233
        self.runbzr('branch a b')
 
234
        self.runbzr('branch a c -r 1')
 
235
 
 
236
    def test_merge(self):
 
237
        from bzrlib.branch import Branch
 
238
        
 
239
        os.mkdir('a')
 
240
        os.chdir('a')
 
241
        self.example_branch()
 
242
        os.chdir('..')
 
243
        self.runbzr('branch a b')
 
244
        os.chdir('b')
 
245
        file('goodbye', 'wt').write('quux')
 
246
        self.runbzr(['commit',  '-m',  "more u's are always good"])
 
247
 
 
248
        os.chdir('../a')
 
249
        file('hello', 'wt').write('quuux')
 
250
        # We can't merge when there are in-tree changes
 
251
        self.runbzr('merge ../b', retcode=1)
 
252
        self.runbzr(['commit', '-m', "Like an epidemic of u's"])
 
253
        self.runbzr('merge ../b')
 
254
        self.check_file_contents('goodbye', 'quux')
 
255
        # Merging a branch pulls its revision into the tree
 
256
        a = Branch.open('.')
 
257
        b = Branch.open('../b')
 
258
        a.get_revision_xml(b.last_patch())
 
259
        self.log('pending merges: %s', a.pending_merges())
 
260
        #        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
 
261
        #        % (a.pending_merges(), b.last_patch())
 
262
 
 
263
    def test_pull(self):
 
264
        """Pull changes from one branch to another."""
 
265
        os.mkdir('a')
 
266
        os.chdir('a')
 
267
 
 
268
        self.example_branch()
 
269
        self.runbzr('pull', retcode=1)
 
270
        self.runbzr('missing', retcode=1)
 
271
        self.runbzr('missing .')
 
272
        self.runbzr('missing')
 
273
        self.runbzr('pull')
 
274
        self.runbzr('pull /', retcode=1)
 
275
        self.runbzr('pull')
 
276
 
 
277
        os.chdir('..')
 
278
        self.runbzr('branch a b')
 
279
        os.chdir('b')
 
280
        self.runbzr('pull')
 
281
        os.mkdir('subdir')
 
282
        self.runbzr('add subdir')
 
283
        self.runbzr('commit -m blah --unchanged')
 
284
        os.chdir('../a')
 
285
        a = Branch.open('.')
 
286
        b = Branch.open('../b')
 
287
        assert a.revision_history() == b.revision_history()[:-1]
 
288
        self.runbzr('pull ../b')
 
289
        assert a.revision_history() == b.revision_history()
 
290
        self.runbzr('commit -m blah2 --unchanged')
 
291
        os.chdir('../b')
 
292
        self.runbzr('commit -m blah3 --unchanged')
 
293
        self.runbzr('pull ../a', retcode=1)
 
294
        os.chdir('../a')
 
295
        self.runbzr('merge ../b')
 
296
        self.runbzr('commit -m blah4 --unchanged')
 
297
        os.chdir('../b/subdir')
 
298
        self.runbzr('pull ../../a')
 
299
        assert a.revision_history()[-1] == b.revision_history()[-1]
 
300
        
 
301
    def test_add_reports(self):
 
302
        """add command prints the names of added files."""
 
303
        b = Branch.initialize('.')
 
304
        self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
 
305
        out = self.run_bzr_captured(['add'], retcode = 0)[0]
 
306
        # the ordering is not defined at the moment
 
307
        results = sorted(out.rstrip('\n').split('\n'))
 
308
        self.assertEquals(['added dir',
 
309
                           'added dir/sub.txt',
 
310
                           'added top.txt',],
 
311
                          results)
 
312
 
 
313
    def test_unknown_command(self):
 
314
        """Handling of unknown command."""
 
315
        out, err = self.run_bzr_captured(['fluffy-badger'],
 
316
                                         retcode=1)
 
317
        self.assertEquals(out, '')
 
318
        err.index('unknown command')
 
319
        
 
320
 
 
321
 
 
322
class OldTests(ExternalBase):
 
323
    """old tests moved from ./testbzr."""
 
324
 
 
325
    def test_bzr(self):
 
326
        from os import chdir, mkdir
 
327
        from os.path import exists
 
328
 
 
329
        runbzr = self.runbzr
 
330
        capture = self.capture
 
331
        progress = self.log
 
332
 
 
333
        progress("basic branch creation")
 
334
        mkdir('branch1')
 
335
        chdir('branch1')
 
336
        runbzr('init')
 
337
 
 
338
        self.assertEquals(capture('root').rstrip(),
 
339
                          os.path.join(self.test_dir, 'branch1'))
 
340
 
 
341
        progress("status of new file")
 
342
 
 
343
        f = file('test.txt', 'wt')
 
344
        f.write('hello world!\n')
 
345
        f.close()
 
346
 
 
347
        self.assertEquals(capture('unknowns'), 'test.txt\n')
 
348
 
 
349
        out = capture("status")
 
350
        assert out == 'unknown:\n  test.txt\n'
 
351
 
 
352
        out = capture("status --all")
 
353
        assert out == "unknown:\n  test.txt\n"
 
354
 
 
355
        out = capture("status test.txt --all")
 
356
        assert out == "unknown:\n  test.txt\n"
 
357
 
 
358
        f = file('test2.txt', 'wt')
 
359
        f.write('goodbye cruel world...\n')
 
360
        f.close()
 
361
 
 
362
        out = capture("status test.txt")
 
363
        assert out == "unknown:\n  test.txt\n"
 
364
 
 
365
        out = capture("status")
 
366
        assert out == ("unknown:\n"
 
367
                       "  test.txt\n"
 
368
                       "  test2.txt\n")
 
369
 
 
370
        os.unlink('test2.txt')
 
371
 
 
372
        progress("command aliases")
 
373
        out = capture("st --all")
 
374
        assert out == ("unknown:\n"
 
375
                       "  test.txt\n")
 
376
 
 
377
        out = capture("stat")
 
378
        assert out == ("unknown:\n"
 
379
                       "  test.txt\n")
 
380
 
 
381
        progress("command help")
 
382
        runbzr("help st")
 
383
        runbzr("help")
 
384
        runbzr("help commands")
 
385
        runbzr("help slartibartfast", 1)
 
386
 
 
387
        out = capture("help ci")
 
388
        out.index('aliases: ')
 
389
 
 
390
        progress("can't rename unversioned file")
 
391
        runbzr("rename test.txt new-test.txt", 1)
 
392
 
 
393
        progress("adding a file")
 
394
 
 
395
        runbzr("add test.txt")
 
396
        assert capture("unknowns") == ''
 
397
        assert capture("status --all") == ("added:\n"
 
398
                                                "  test.txt\n")
 
399
 
 
400
        progress("rename newly-added file")
 
401
        runbzr("rename test.txt hello.txt")
 
402
        assert os.path.exists("hello.txt")
 
403
        assert not os.path.exists("test.txt")
 
404
 
 
405
        assert capture("revno") == '0\n'
 
406
 
 
407
        progress("add first revision")
 
408
        runbzr(['commit', '-m', 'add first revision'])
 
409
 
 
410
        progress("more complex renames")
 
411
        os.mkdir("sub1")
 
412
        runbzr("rename hello.txt sub1", 1)
 
413
        runbzr("rename hello.txt sub1/hello.txt", 1)
 
414
        runbzr("move hello.txt sub1", 1)
 
415
 
 
416
        runbzr("add sub1")
 
417
        runbzr("rename sub1 sub2")
 
418
        runbzr("move hello.txt sub2")
 
419
        assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
420
 
 
421
        assert exists("sub2")
 
422
        assert exists("sub2/hello.txt")
 
423
        assert not exists("sub1")
 
424
        assert not exists("hello.txt")
 
425
 
 
426
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
 
427
 
 
428
        mkdir("sub1")
 
429
        runbzr('add sub1')
 
430
        runbzr('move sub2/hello.txt sub1')
 
431
        assert not exists('sub2/hello.txt')
 
432
        assert exists('sub1/hello.txt')
 
433
        runbzr('move sub2 sub1')
 
434
        assert not exists('sub2')
 
435
        assert exists('sub1/sub2')
 
436
 
 
437
        runbzr(['commit', '-m', 'rename nested subdirectories'])
 
438
 
 
439
        chdir('sub1/sub2')
 
440
        self.assertEquals(capture('root')[:-1],
 
441
                          os.path.join(self.test_dir, 'branch1'))
 
442
        runbzr('move ../hello.txt .')
 
443
        assert exists('./hello.txt')
 
444
        self.assertEquals(capture('relpath hello.txt'),
 
445
                          os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
 
446
        assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
447
        runbzr(['commit', '-m', 'move to parent directory'])
 
448
        chdir('..')
 
449
        assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
450
 
 
451
        runbzr('move sub2/hello.txt .')
 
452
        assert exists('hello.txt')
 
453
 
 
454
        f = file('hello.txt', 'wt')
 
455
        f.write('some nice new content\n')
 
456
        f.close()
 
457
 
 
458
        f = file('msg.tmp', 'wt')
 
459
        f.write('this is my new commit\n')
 
460
        f.close()
 
461
 
 
462
        runbzr('commit -F msg.tmp')
 
463
 
 
464
        assert capture('revno') == '5\n'
 
465
        runbzr('export -r 5 export-5.tmp')
 
466
        runbzr('export export.tmp')
 
467
 
 
468
        runbzr('log')
 
469
        runbzr('log -v')
 
470
        runbzr('log -v --forward')
 
471
        runbzr('log -m', retcode=1)
 
472
        log_out = capture('log -m commit')
 
473
        assert "this is my new commit" in log_out
 
474
        assert "rename nested" not in log_out
 
475
        assert 'revision-id' not in log_out
 
476
        assert 'revision-id' in capture('log --show-ids -m commit')
 
477
 
 
478
 
 
479
        progress("file with spaces in name")
 
480
        mkdir('sub directory')
 
481
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
 
482
        runbzr('add .')
 
483
        runbzr('diff')
 
484
        runbzr('commit -m add-spaces')
 
485
        runbzr('check')
 
486
 
 
487
        runbzr('log')
 
488
        runbzr('log --forward')
 
489
 
 
490
        runbzr('info')
 
491