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.
 
 
31
from bzrlib.selftest import TestBase, InTempDir, BzrTestBase
 
 
35
class ExternalBase(InTempDir):
 
 
36
    def runbzr(self, args, retcode=0,backtick=False):
 
 
39
            from subprocess import call
 
 
40
        except ImportError, e:
 
 
44
        if isinstance(args, basestring):
 
 
48
            return self.backtick(['python', self.BZRPATH,] + args,
 
 
51
            return self.runcmd(['python', self.BZRPATH,] + args,
 
 
56
class MvCommand(BzrTestBase):
 
 
58
        """Test two modes of operation for mv"""
 
 
59
        b = Branch('.', init=True)
 
 
60
        self.build_tree(['a', 'c', 'subdir/'])
 
 
61
        self.run_bzr('mv', 'a', 'b')
 
 
62
        self.run_bzr('mv', 'b', 'subdir')
 
 
63
        self.run_bzr('mv', 'subdir/b', 'a')
 
 
64
        self.run_bzr('mv', 'a', 'b', 'subdir')
 
 
65
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
 
69
class TestVersion(BzrTestBase):
 
 
70
    """Check output from version command and master option is reasonable"""
 
 
72
        # output is intentionally passed through to stdout so that we
 
 
73
        # can see the version being tested
 
 
74
        from cStringIO import StringIO
 
 
77
            sys.stdout = tmp_out = StringIO()
 
 
79
            self.run_bzr('version')
 
 
83
        output = tmp_out.getvalue()
 
 
84
        self.log('bzr version output:')
 
 
87
        self.assert_(output.startswith('bzr (bazaar-ng) '))
 
 
88
        self.assertNotEqual(output.index('Canonical'), -1)
 
 
90
        # make sure --version is consistent
 
 
92
            sys.stdout = tmp_out = StringIO()
 
 
94
            self.run_bzr('--version')
 
 
98
        self.log('bzr --version output:')
 
 
99
        self.log(tmp_out.getvalue())
 
 
101
        self.assertEquals(output, tmp_out.getvalue())
 
 
107
class HelpCommands(ExternalBase):
 
 
109
        self.runbzr('--help')
 
 
111
        self.runbzr('help commands')
 
 
112
        self.runbzr('help help')
 
 
113
        self.runbzr('commit -h')
 
 
116
class InitBranch(ExternalBase):
 
 
119
        self.runbzr(['init'])
 
 
123
class UserIdentity(ExternalBase):
 
 
125
        # this should always identify something, if only "john@localhost"
 
 
126
        self.runbzr("whoami")
 
 
127
        self.runbzr("whoami --email")
 
 
129
        self.assertEquals(self.runbzr("whoami --email",
 
 
130
                                      backtick=True).count('@'), 1)
 
 
132
class UserIdentityBranch(ExternalBase):
 
 
134
        # tests branch specific user identity
 
 
136
        f = file('.bzr/email', 'wt')
 
 
137
        f.write('Branch Identity <branch@identi.ty>')
 
 
139
        whoami = self.runbzr("whoami",backtick=True)
 
 
140
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
 
141
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
 
 
142
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
 
 
145
class InvalidCommands(ExternalBase):
 
 
147
        self.runbzr("pants", retcode=1)
 
 
148
        self.runbzr("--pants off", retcode=1)
 
 
149
        self.runbzr("diff --message foo", retcode=1)
 
 
153
class EmptyCommit(ExternalBase):
 
 
156
        self.build_tree(['hello.txt'])
 
 
157
        self.runbzr("commit -m empty", retcode=1)
 
 
158
        self.runbzr("add hello.txt")
 
 
159
        self.runbzr("commit -m added")
 
 
163
class IgnorePatterns(ExternalBase):
 
 
165
        from bzrlib.branch import Branch
 
 
167
        b = Branch('.', init=True)
 
 
168
        self.assertEquals(list(b.unknowns()), [])
 
 
170
        file('foo.tmp', 'wt').write('tmp files are ignored')
 
 
171
        self.assertEquals(list(b.unknowns()), [])
 
 
172
        assert self.backtick('bzr unknowns') == ''
 
 
174
        file('foo.c', 'wt').write('int main() {}')
 
 
175
        self.assertEquals(list(b.unknowns()), ['foo.c'])
 
 
176
        assert self.backtick('bzr unknowns') == 'foo.c\n'
 
 
178
        self.runbzr(['add', 'foo.c'])
 
 
179
        assert self.backtick('bzr unknowns') == ''
 
 
181
        # 'ignore' works when creating the .bzignore file
 
 
182
        file('foo.blah', 'wt').write('blah')
 
 
183
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
 
 
184
        self.runbzr('ignore *.blah')
 
 
185
        self.assertEquals(list(b.unknowns()), [])
 
 
186
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
 
188
        # 'ignore' works when then .bzrignore file already exists
 
 
189
        file('garh', 'wt').write('garh')
 
 
190
        self.assertEquals(list(b.unknowns()), ['garh'])
 
 
191
        assert self.backtick('bzr unknowns') == 'garh\n'
 
 
192
        self.runbzr('ignore garh')
 
 
193
        self.assertEquals(list(b.unknowns()), [])
 
 
194
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
 
199
class OldTests(ExternalBase):
 
 
200
    # old tests moved from ./testbzr
 
 
202
        from os import chdir, mkdir
 
 
203
        from os.path import exists
 
 
207
        backtick = self.backtick
 
 
210
        progress("basic branch creation")
 
 
215
        self.assertEquals(backtick('bzr root').rstrip(),
 
 
216
                          os.path.join(self.test_dir, 'branch1'))
 
 
218
        progress("status of new file")
 
 
220
        f = file('test.txt', 'wt')
 
 
221
        f.write('hello world!\n')
 
 
224
        out = backtick("bzr unknowns")
 
 
225
        self.assertEquals(out, 'test.txt\n')
 
 
227
        out = backtick("bzr status")
 
 
228
        assert out == 'unknown:\n  test.txt\n'
 
 
230
        out = backtick("bzr status --all")
 
 
231
        assert out == "unknown:\n  test.txt\n"
 
 
233
        out = backtick("bzr status test.txt --all")
 
 
234
        assert out == "unknown:\n  test.txt\n"
 
 
236
        f = file('test2.txt', 'wt')
 
 
237
        f.write('goodbye cruel world...\n')
 
 
240
        out = backtick("bzr status test.txt")
 
 
241
        assert out == "unknown:\n  test.txt\n"
 
 
243
        out = backtick("bzr status")
 
 
244
        assert out == ("unknown:\n"
 
 
248
        os.unlink('test2.txt')
 
 
250
        progress("command aliases")
 
 
251
        out = backtick("bzr st --all")
 
 
252
        assert out == ("unknown:\n"
 
 
255
        out = backtick("bzr stat")
 
 
256
        assert out == ("unknown:\n"
 
 
259
        progress("command help")
 
 
262
        runbzr("help commands")
 
 
263
        runbzr("help slartibartfast", 1)
 
 
265
        out = backtick("bzr help ci")
 
 
266
        out.index('aliases: ')
 
 
268
        progress("can't rename unversioned file")
 
 
269
        runbzr("rename test.txt new-test.txt", 1)
 
 
271
        progress("adding a file")
 
 
273
        runbzr("add test.txt")
 
 
274
        assert backtick("bzr unknowns") == ''
 
 
275
        assert backtick("bzr status --all") == ("added:\n"
 
 
278
        progress("rename newly-added file")
 
 
279
        runbzr("rename test.txt hello.txt")
 
 
280
        assert os.path.exists("hello.txt")
 
 
281
        assert not os.path.exists("test.txt")
 
 
283
        assert backtick("bzr revno") == '0\n'
 
 
285
        progress("add first revision")
 
 
286
        runbzr(['commit', '-m', 'add first revision'])
 
 
288
        progress("more complex renames")
 
 
290
        runbzr("rename hello.txt sub1", 1)
 
 
291
        runbzr("rename hello.txt sub1/hello.txt", 1)
 
 
292
        runbzr("move hello.txt sub1", 1)
 
 
295
        runbzr("rename sub1 sub2")
 
 
296
        runbzr("move hello.txt sub2")
 
 
297
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
 
299
        assert exists("sub2")
 
 
300
        assert exists("sub2/hello.txt")
 
 
301
        assert not exists("sub1")
 
 
302
        assert not exists("hello.txt")
 
 
304
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
 
 
308
        runbzr('move sub2/hello.txt sub1')
 
 
309
        assert not exists('sub2/hello.txt')
 
 
310
        assert exists('sub1/hello.txt')
 
 
311
        runbzr('move sub2 sub1')
 
 
312
        assert not exists('sub2')
 
 
313
        assert exists('sub1/sub2')
 
 
315
        runbzr(['commit', '-m', 'rename nested subdirectories'])
 
 
318
        self.assertEquals(backtick('bzr root')[:-1],
 
 
319
                          os.path.join(self.test_dir, 'branch1'))
 
 
320
        runbzr('move ../hello.txt .')
 
 
321
        assert exists('./hello.txt')
 
 
322
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
 
323
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
 
324
        runbzr(['commit', '-m', 'move to parent directory'])
 
 
326
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
 
328
        runbzr('move sub2/hello.txt .')
 
 
329
        assert exists('hello.txt')
 
 
331
        f = file('hello.txt', 'wt')
 
 
332
        f.write('some nice new content\n')
 
 
335
        f = file('msg.tmp', 'wt')
 
 
336
        f.write('this is my new commit\n')
 
 
339
        runbzr('commit -F msg.tmp')
 
 
341
        assert backtick('bzr revno') == '5\n'
 
 
342
        runbzr('export -r 5 export-5.tmp')
 
 
343
        runbzr('export export.tmp')
 
 
347
        runbzr('log -v --forward')
 
 
348
        runbzr('log -m', retcode=1)
 
 
349
        log_out = backtick('bzr log -m commit')
 
 
350
        assert "this is my new commit" in log_out
 
 
351
        assert "rename nested" not in log_out
 
 
352
        assert 'revision-id' not in log_out
 
 
353
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
 
356
        progress("file with spaces in name")
 
 
357
        mkdir('sub directory')
 
 
358
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
 
 
361
        runbzr('commit -m add-spaces')
 
 
365
        runbzr('log --forward')
 
 
374
class RevertCommand(ExternalBase):
 
 
379
        file('hello', 'wt').write('foo')
 
 
380
        self.runbzr('add hello')
 
 
381
        self.runbzr('commit -m setup hello')
 
 
383
        file('goodbye', 'wt').write('baz')
 
 
384
        self.runbzr('add goodbye')
 
 
385
        self.runbzr('commit -m setup goodbye')
 
 
387
        file('hello', 'wt').write('bar')
 
 
388
        file('goodbye', 'wt').write('qux')
 
 
389
        self.runbzr('revert hello')
 
 
390
        self.check_file_contents('hello', 'foo')
 
 
391
        self.check_file_contents('goodbye', 'qux')
 
 
392
        self.runbzr('revert')
 
 
393
        self.check_file_contents('goodbye', 'baz')
 
 
394
        os.mkdir('revertdir')
 
 
395
        self.runbzr('add revertdir')
 
 
396
        self.runbzr('commit -m f')
 
 
397
        os.rmdir('revertdir')
 
 
398
        self.runbzr('revert')