/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

David Clymers patch to use rename rather than mv for two argument non-directory target bzr mv calls.

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