/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

revert out the revision spec from revision spec change

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