1
# Copyright (C) 2005 by Canonical Ltd
 
 
2
# -*- coding: utf-8 -*-
 
 
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.
 
 
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.
 
 
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
 
 
19
"""Black-box tests for bzr.
 
 
21
These check that it behaves properly when it's invoked through the regular
 
 
22
command-line interface.
 
 
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.
 
 
32
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
 
 
33
from bzrlib.branch import Branch
 
 
34
from bzrlib.commands import run_bzr
 
 
37
class ExternalBase(TestCaseInTempDir):
 
 
38
    def runbzr(self, args, retcode=0, backtick=False):
 
 
39
        if isinstance(args, basestring):
 
 
43
            return self.run_bzr_captured(args, retcode=retcode)[0]
 
 
45
            return self.run_bzr_captured(args, retcode=retcode)
 
 
48
class TestCommands(ExternalBase):
 
 
50
    def test_help_commands(self):
 
 
53
        self.runbzr('help commands')
 
 
54
        self.runbzr('help help')
 
 
55
        self.runbzr('commit -h')
 
 
57
    def test_init_branch(self):
 
 
60
    def test_whoami(self):
 
 
61
        # this should always identify something, if only "john@localhost"
 
 
63
        self.runbzr("whoami --email")
 
 
65
        self.assertEquals(self.runbzr("whoami --email",
 
 
66
                                      backtick=True).count('@'), 1)
 
 
68
    def test_whoami_branch(self):
 
 
69
        """branch specific user identity works."""
 
 
71
        f = file('.bzr/email', 'wt')
 
 
72
        f.write('Branch Identity <branch@identi.ty>')
 
 
74
        whoami = self.runbzr("whoami",backtick=True)
 
 
75
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
 
76
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
 
 
77
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
 
 
79
    def test_invalid_commands(self):
 
 
80
        self.runbzr("pants", retcode=1)
 
 
81
        self.runbzr("--pants off", retcode=1)
 
 
82
        self.runbzr("diff --message foo", retcode=1)
 
 
84
    def test_empty_commit(self):
 
 
86
        self.build_tree(['hello.txt'])
 
 
87
        self.runbzr("commit -m empty", retcode=1)
 
 
88
        self.runbzr("add hello.txt")
 
 
89
        self.runbzr("commit -m added")
 
 
91
    def test_ignore_patterns(self):
 
 
92
        from bzrlib.branch import Branch
 
 
94
        b = Branch.initialize('.')
 
 
95
        self.assertEquals(list(b.unknowns()), [])
 
 
97
        file('foo.tmp', 'wt').write('tmp files are ignored')
 
 
98
        self.assertEquals(list(b.unknowns()), [])
 
 
99
        assert self.capture('unknowns') == ''
 
 
101
        file('foo.c', 'wt').write('int main() {}')
 
 
102
        self.assertEquals(list(b.unknowns()), ['foo.c'])
 
 
103
        assert self.capture('unknowns') == 'foo.c\n'
 
 
105
        self.runbzr(['add', 'foo.c'])
 
 
106
        assert self.capture('unknowns') == ''
 
 
108
        # 'ignore' works when creating the .bzignore file
 
 
109
        file('foo.blah', 'wt').write('blah')
 
 
110
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
 
 
111
        self.runbzr('ignore *.blah')
 
 
112
        self.assertEquals(list(b.unknowns()), [])
 
 
113
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
 
115
        # 'ignore' works when then .bzrignore file already exists
 
 
116
        file('garh', 'wt').write('garh')
 
 
117
        self.assertEquals(list(b.unknowns()), ['garh'])
 
 
118
        assert self.capture('unknowns') == 'garh\n'
 
 
119
        self.runbzr('ignore garh')
 
 
120
        self.assertEquals(list(b.unknowns()), [])
 
 
121
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
 
123
    def test_revert(self):
 
 
126
        file('hello', 'wt').write('foo')
 
 
127
        self.runbzr('add hello')
 
 
128
        self.runbzr('commit -m setup hello')
 
 
130
        file('goodbye', 'wt').write('baz')
 
 
131
        self.runbzr('add goodbye')
 
 
132
        self.runbzr('commit -m setup goodbye')
 
 
134
        file('hello', 'wt').write('bar')
 
 
135
        file('goodbye', 'wt').write('qux')
 
 
136
        self.runbzr('revert hello')
 
 
137
        self.check_file_contents('hello', 'foo')
 
 
138
        self.check_file_contents('goodbye', 'qux')
 
 
139
        self.runbzr('revert')
 
 
140
        self.check_file_contents('goodbye', 'baz')
 
 
142
        os.mkdir('revertdir')
 
 
143
        self.runbzr('add revertdir')
 
 
144
        self.runbzr('commit -m f')
 
 
145
        os.rmdir('revertdir')
 
 
146
        self.runbzr('revert')
 
 
148
    def test_mv_modes(self):
 
 
149
        """Test two modes of operation for mv"""
 
 
150
        from bzrlib.branch import Branch
 
 
151
        b = Branch.initialize('.')
 
 
152
        self.build_tree(['a', 'c', 'subdir/'])
 
 
153
        self.run_bzr('add', self.test_dir)
 
 
154
        self.run_bzr('mv', 'a', 'b')
 
 
155
        self.run_bzr('mv', 'b', 'subdir')
 
 
156
        self.run_bzr('mv', 'subdir/b', 'a')
 
 
157
        self.run_bzr('mv', 'a', 'c', 'subdir')
 
 
158
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
 
161
    def test_main_version(self):
 
 
162
        """Check output from version command and master option is reasonable"""
 
 
163
        # output is intentionally passed through to stdout so that we
 
 
164
        # can see the version being tested
 
 
165
        output = self.runbzr('version', backtick=1)
 
 
166
        self.log('bzr version output:')
 
 
168
        self.assert_(output.startswith('bzr (bazaar-ng) '))
 
 
169
        self.assertNotEqual(output.index('Canonical'), -1)
 
 
170
        # make sure --version is consistent
 
 
171
        tmp_output = self.runbzr('--version', backtick=1)
 
 
172
        self.log('bzr --version output:')
 
 
174
        self.assertEquals(output, tmp_output)
 
 
176
    def example_branch(test):
 
 
178
        file('hello', 'wt').write('foo')
 
 
179
        test.runbzr('add hello')
 
 
180
        test.runbzr('commit -m setup hello')
 
 
181
        file('goodbye', 'wt').write('baz')
 
 
182
        test.runbzr('add goodbye')
 
 
183
        test.runbzr('commit -m setup goodbye')
 
 
185
    def test_revert(self):
 
 
186
        self.example_branch()
 
 
187
        file('hello', 'wt').write('bar')
 
 
188
        file('goodbye', 'wt').write('qux')
 
 
189
        self.runbzr('revert hello')
 
 
190
        self.check_file_contents('hello', 'foo')
 
 
191
        self.check_file_contents('goodbye', 'qux')
 
 
192
        self.runbzr('revert')
 
 
193
        self.check_file_contents('goodbye', 'baz')
 
 
195
    def test_merge(self):
 
 
196
        from bzrlib.branch import Branch
 
 
200
        self.example_branch()
 
 
202
        self.runbzr('branch a b')
 
 
204
        file('goodbye', 'wt').write('quux')
 
 
205
        self.runbzr(['commit',  '-m',  "more u's are always good"])
 
 
208
        file('hello', 'wt').write('quuux')
 
 
209
        # We can't merge when there are in-tree changes
 
 
210
        self.runbzr('merge ../b', retcode=1)
 
 
211
        self.runbzr(['commit', '-m', "Like an epidemic of u's"])
 
 
212
        self.runbzr('merge ../b')
 
 
213
        self.check_file_contents('goodbye', 'quux')
 
 
214
        # Merging a branch pulls its revision into the tree
 
 
216
        b = Branch.open('../b')
 
 
217
        a.get_revision_xml(b.last_patch())
 
 
218
        self.log('pending merges: %s', a.pending_merges())
 
 
219
        #        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
 
 
220
        #        % (a.pending_merges(), b.last_patch())
 
 
223
        """Pull changes from one branch to another."""
 
 
227
        self.example_branch()
 
 
228
        self.runbzr('pull', retcode=1)
 
 
229
        self.runbzr('missing', retcode=1)
 
 
230
        self.runbzr('missing .')
 
 
231
        self.runbzr('missing')
 
 
233
        self.runbzr('pull /', retcode=1)
 
 
237
        self.runbzr('branch a b')
 
 
240
        self.runbzr('commit -m blah --unchanged')
 
 
243
        b = Branch.open('../b')
 
 
244
        assert a.revision_history() == b.revision_history()[:-1]
 
 
245
        self.runbzr('pull ../b')
 
 
246
        assert a.revision_history() == b.revision_history()
 
 
247
        self.runbzr('commit -m blah2 --unchanged')
 
 
249
        self.runbzr('commit -m blah3 --unchanged')
 
 
250
        self.runbzr('pull ../a', retcode=1)
 
 
252
        self.runbzr('merge ../b')
 
 
253
        self.runbzr('commit -m blah4 --unchanged')
 
 
255
        self.runbzr('pull ../a')
 
 
256
        assert a.revision_history()[-1] == b.revision_history()[-1]
 
 
259
    def test_add_reports(self):
 
 
260
        """add command prints the names of added files."""
 
 
261
        b = Branch.initialize('.')
 
 
262
        self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
 
 
264
        from cStringIO import StringIO
 
 
267
        ret = self.apply_redirected(None, out, None,
 
 
270
        self.assertEquals(ret, 0)
 
 
272
        # the ordering is not defined at the moment
 
 
273
        results = sorted(out.getvalue().rstrip('\n').split('\n'))
 
 
274
        self.assertEquals(['added dir',
 
 
279
    def test_unknown_command(self):
 
 
280
        """Handling of unknown command."""
 
 
281
        out, err = self.run_bzr_captured(['fluffy-badger'],
 
 
283
        self.assertEquals(out, '')
 
 
284
        err.index('unknown command')
 
 
288
class OldTests(ExternalBase):
 
 
289
    """old tests moved from ./testbzr."""
 
 
292
        from os import chdir, mkdir
 
 
293
        from os.path import exists
 
 
296
        capture = self.capture
 
 
299
        progress("basic branch creation")
 
 
304
        self.assertEquals(capture('root').rstrip(),
 
 
305
                          os.path.join(self.test_dir, 'branch1'))
 
 
307
        progress("status of new file")
 
 
309
        f = file('test.txt', 'wt')
 
 
310
        f.write('hello world!\n')
 
 
313
        self.assertEquals(capture('unknowns'), 'test.txt\n')
 
 
315
        out = capture("status")
 
 
316
        assert out == 'unknown:\n  test.txt\n'
 
 
318
        out = capture("status --all")
 
 
319
        assert out == "unknown:\n  test.txt\n"
 
 
321
        out = capture("status test.txt --all")
 
 
322
        assert out == "unknown:\n  test.txt\n"
 
 
324
        f = file('test2.txt', 'wt')
 
 
325
        f.write('goodbye cruel world...\n')
 
 
328
        out = capture("status test.txt")
 
 
329
        assert out == "unknown:\n  test.txt\n"
 
 
331
        out = capture("status")
 
 
332
        assert out == ("unknown:\n"
 
 
336
        os.unlink('test2.txt')
 
 
338
        progress("command aliases")
 
 
339
        out = capture("st --all")
 
 
340
        assert out == ("unknown:\n"
 
 
343
        out = capture("stat")
 
 
344
        assert out == ("unknown:\n"
 
 
347
        progress("command help")
 
 
350
        runbzr("help commands")
 
 
351
        runbzr("help slartibartfast", 1)
 
 
353
        out = capture("help ci")
 
 
354
        out.index('aliases: ')
 
 
356
        progress("can't rename unversioned file")
 
 
357
        runbzr("rename test.txt new-test.txt", 1)
 
 
359
        progress("adding a file")
 
 
361
        runbzr("add test.txt")
 
 
362
        assert capture("unknowns") == ''
 
 
363
        assert capture("status --all") == ("added:\n"
 
 
366
        progress("rename newly-added file")
 
 
367
        runbzr("rename test.txt hello.txt")
 
 
368
        assert os.path.exists("hello.txt")
 
 
369
        assert not os.path.exists("test.txt")
 
 
371
        assert capture("revno") == '0\n'
 
 
373
        progress("add first revision")
 
 
374
        runbzr(['commit', '-m', 'add first revision'])
 
 
376
        progress("more complex renames")
 
 
378
        runbzr("rename hello.txt sub1", 1)
 
 
379
        runbzr("rename hello.txt sub1/hello.txt", 1)
 
 
380
        runbzr("move hello.txt sub1", 1)
 
 
383
        runbzr("rename sub1 sub2")
 
 
384
        runbzr("move hello.txt sub2")
 
 
385
        assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
 
387
        assert exists("sub2")
 
 
388
        assert exists("sub2/hello.txt")
 
 
389
        assert not exists("sub1")
 
 
390
        assert not exists("hello.txt")
 
 
392
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
 
 
396
        runbzr('move sub2/hello.txt sub1')
 
 
397
        assert not exists('sub2/hello.txt')
 
 
398
        assert exists('sub1/hello.txt')
 
 
399
        runbzr('move sub2 sub1')
 
 
400
        assert not exists('sub2')
 
 
401
        assert exists('sub1/sub2')
 
 
403
        runbzr(['commit', '-m', 'rename nested subdirectories'])
 
 
406
        self.assertEquals(capture('root')[:-1],
 
 
407
                          os.path.join(self.test_dir, 'branch1'))
 
 
408
        runbzr('move ../hello.txt .')
 
 
409
        assert exists('./hello.txt')
 
 
410
        self.assertEquals(capture('relpath hello.txt'),
 
 
411
                          os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
 
 
412
        assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
 
413
        runbzr(['commit', '-m', 'move to parent directory'])
 
 
415
        assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
 
417
        runbzr('move sub2/hello.txt .')
 
 
418
        assert exists('hello.txt')
 
 
420
        f = file('hello.txt', 'wt')
 
 
421
        f.write('some nice new content\n')
 
 
424
        f = file('msg.tmp', 'wt')
 
 
425
        f.write('this is my new commit\n')
 
 
428
        runbzr('commit -F msg.tmp')
 
 
430
        assert capture('revno') == '5\n'
 
 
431
        runbzr('export -r 5 export-5.tmp')
 
 
432
        runbzr('export export.tmp')
 
 
436
        runbzr('log -v --forward')
 
 
437
        runbzr('log -m', retcode=1)
 
 
438
        log_out = capture('log -m commit')
 
 
439
        assert "this is my new commit" in log_out
 
 
440
        assert "rename nested" not in log_out
 
 
441
        assert 'revision-id' not in log_out
 
 
442
        assert 'revision-id' in capture('log --show-ids -m commit')
 
 
445
        progress("file with spaces in name")
 
 
446
        mkdir('sub directory')
 
 
447
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
 
 
450
        runbzr('commit -m add-spaces')
 
 
454
        runbzr('log --forward')