/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: Lalo Martins
  • Date: 2005-09-16 12:53:47 UTC
  • mto: (1185.1.26)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: lalo@exoweb.net-20050916125347-e134440582d72d53
blackbox tests for diff

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
import os
 
31
 
 
32
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
 
33
from bzrlib.branch import Branch
 
34
from bzrlib.commands import run_bzr
 
35
 
 
36
 
 
37
class ExternalBase(TestCaseInTempDir):
 
38
    def runbzr(self, args, retcode=0,backtick=False):
 
39
        if isinstance(args, basestring):
 
40
            args = args.split()
 
41
 
 
42
        if backtick:
 
43
            return self.backtick(['python', self.BZRPATH,] + args,
 
44
                           retcode=retcode)
 
45
        else:
 
46
            return self.runcmd(['python', self.BZRPATH,] + args,
 
47
                           retcode=retcode)
 
48
 
 
49
 
 
50
class TestCommands(ExternalBase):
 
51
 
 
52
    def test_help_commands(self):
 
53
        self.runbzr('--help')
 
54
        self.runbzr('help')
 
55
        self.runbzr('help commands')
 
56
        self.runbzr('help help')
 
57
        self.runbzr('commit -h')
 
58
 
 
59
    def test_init_branch(self):
 
60
        self.runbzr(['init'])
 
61
 
 
62
    def test_whoami(self):
 
63
        # this should always identify something, if only "john@localhost"
 
64
        self.runbzr("whoami")
 
65
        self.runbzr("whoami --email")
 
66
 
 
67
        self.assertEquals(self.runbzr("whoami --email",
 
68
                                      backtick=True).count('@'), 1)
 
69
        
 
70
    def test_whoami_branch(self):
 
71
        """branch specific user identity works."""
 
72
        self.runbzr('init')
 
73
        f = file('.bzr/email', 'wt')
 
74
        f.write('Branch Identity <branch@identi.ty>')
 
75
        f.close()
 
76
        whoami = self.runbzr("whoami",backtick=True)
 
77
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
78
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
 
79
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
 
80
 
 
81
    def test_invalid_commands(self):
 
82
        self.runbzr("pants", retcode=1)
 
83
        self.runbzr("--pants off", retcode=1)
 
84
        self.runbzr("diff --message foo", retcode=1)
 
85
 
 
86
    def test_empty_commit(self):
 
87
        self.runbzr("init")
 
88
        self.build_tree(['hello.txt'])
 
89
        self.runbzr("commit -m empty", retcode=1)
 
90
        self.runbzr("add hello.txt")
 
91
        self.runbzr("commit -m added")
 
92
 
 
93
    def test_ignore_patterns(self):
 
94
        from bzrlib.branch import Branch
 
95
        
 
96
        b = Branch.initialize('.')
 
97
        self.assertEquals(list(b.unknowns()), [])
 
98
 
 
99
        file('foo.tmp', 'wt').write('tmp files are ignored')
 
100
        self.assertEquals(list(b.unknowns()), [])
 
101
        assert self.backtick('bzr unknowns') == ''
 
102
 
 
103
        file('foo.c', 'wt').write('int main() {}')
 
104
        self.assertEquals(list(b.unknowns()), ['foo.c'])
 
105
        assert self.backtick('bzr unknowns') == 'foo.c\n'
 
106
 
 
107
        self.runbzr(['add', 'foo.c'])
 
108
        assert self.backtick('bzr unknowns') == ''
 
109
 
 
110
        # 'ignore' works when creating the .bzignore file
 
111
        file('foo.blah', 'wt').write('blah')
 
112
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
 
113
        self.runbzr('ignore *.blah')
 
114
        self.assertEquals(list(b.unknowns()), [])
 
115
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
116
 
 
117
        # 'ignore' works when then .bzrignore file already exists
 
118
        file('garh', 'wt').write('garh')
 
119
        self.assertEquals(list(b.unknowns()), ['garh'])
 
120
        assert self.backtick('bzr unknowns') == 'garh\n'
 
121
        self.runbzr('ignore garh')
 
122
        self.assertEquals(list(b.unknowns()), [])
 
123
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
124
 
 
125
    def test_revert(self):
 
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.initialize('.')
 
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_diff(self):
 
188
        self.example_branch()
 
189
        file('hello', 'wt').write('hello world!')
 
190
        self.runbzr('commit -m fixing hello')
 
191
        output = self.runbzr('diff -r 2..3', backtick=1)
 
192
        self.assert_('\n+hello world!' in output)
 
193
        output = self.runbzr('diff -r last:3..last:1', backtick=1)
 
194
        self.assert_('\n+baz' in output)
 
195
 
 
196
    def test_revert(self):
 
197
        self.example_branch()
 
198
        file('hello', 'wt').write('bar')
 
199
        file('goodbye', 'wt').write('qux')
 
200
        self.runbzr('revert hello')
 
201
        self.check_file_contents('hello', 'foo')
 
202
        self.check_file_contents('goodbye', 'qux')
 
203
        self.runbzr('revert')
 
204
        self.check_file_contents('goodbye', 'baz')
 
205
 
 
206
    def test_merge(self):
 
207
        from bzrlib.branch import Branch
 
208
        
 
209
        os.mkdir('a')
 
210
        os.chdir('a')
 
211
        self.example_branch()
 
212
        os.chdir('..')
 
213
        self.runbzr('branch a b')
 
214
        os.chdir('b')
 
215
        file('goodbye', 'wt').write('quux')
 
216
        self.runbzr(['commit',  '-m',  "more u's are always good"])
 
217
 
 
218
        os.chdir('../a')
 
219
        file('hello', 'wt').write('quuux')
 
220
        # We can't merge when there are in-tree changes
 
221
        self.runbzr('merge ../b', retcode=1)
 
222
        self.runbzr(['commit', '-m', "Like an epidemic of u's"])
 
223
        self.runbzr('merge ../b')
 
224
        self.check_file_contents('goodbye', 'quux')
 
225
        # Merging a branch pulls its revision into the tree
 
226
        a = Branch.open('.')
 
227
        b = Branch.open('../b')
 
228
        a.get_revision_xml(b.last_patch())
 
229
        self.log('pending merges: %s', a.pending_merges())
 
230
        #        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
 
231
        #        % (a.pending_merges(), b.last_patch())
 
232
 
 
233
    def test_pull(self):
 
234
        """Pull changes from one branch to another."""
 
235
        os.mkdir('a')
 
236
        os.chdir('a')
 
237
 
 
238
        self.example_branch()
 
239
        self.runbzr('pull', retcode=1)
 
240
        self.runbzr('missing', retcode=1)
 
241
        self.runbzr('missing .')
 
242
        self.runbzr('missing')
 
243
        self.runbzr('pull')
 
244
        self.runbzr('pull /', retcode=1)
 
245
        self.runbzr('pull')
 
246
 
 
247
        os.chdir('..')
 
248
        self.runbzr('branch a b')
 
249
        os.chdir('b')
 
250
        self.runbzr('pull')
 
251
        self.runbzr('commit -m blah --unchanged')
 
252
        os.chdir('../a')
 
253
        a = Branch.open('.')
 
254
        b = Branch.open('../b')
 
255
        assert a.revision_history() == b.revision_history()[:-1]
 
256
        self.runbzr('pull ../b')
 
257
        assert a.revision_history() == b.revision_history()
 
258
        self.runbzr('commit -m blah2 --unchanged')
 
259
        os.chdir('../b')
 
260
        self.runbzr('commit -m blah3 --unchanged')
 
261
        self.runbzr('pull ../a', retcode=1)
 
262
        os.chdir('../a')
 
263
        self.runbzr('merge ../b')
 
264
        self.runbzr('commit -m blah4 --unchanged')
 
265
        os.chdir('../b')
 
266
        self.runbzr('pull ../a')
 
267
        assert a.revision_history()[-1] == b.revision_history()[-1]
 
268
        
 
269
 
 
270
    def test_add_reports(self):
 
271
        """add command prints the names of added files."""
 
272
        b = Branch.initialize('.')
 
273
        self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
 
274
 
 
275
        from cStringIO import StringIO
 
276
        out = StringIO()
 
277
 
 
278
        ret = self.apply_redirected(None, out, None,
 
279
                                    run_bzr,
 
280
                                    ['add'])
 
281
        self.assertEquals(ret, 0)
 
282
 
 
283
        # the ordering is not defined at the moment
 
284
        results = sorted(out.getvalue().rstrip('\n').split('\n'))
 
285
        self.assertEquals(['added dir',
 
286
                           'added dir/sub.txt',
 
287
                           'added top.txt',],
 
288
                          results)
 
289
 
 
290
 
 
291
class OldTests(ExternalBase):
 
292
    """old tests moved from ./testbzr."""
 
293
 
 
294
    def test_bzr(self):
 
295
        from os import chdir, mkdir
 
296
        from os.path import exists
 
297
 
 
298
        runbzr = self.runbzr
 
299
        backtick = self.backtick
 
300
        progress = self.log
 
301
 
 
302
        progress("basic branch creation")
 
303
        mkdir('branch1')
 
304
        chdir('branch1')
 
305
        runbzr('init')
 
306
 
 
307
        self.assertEquals(backtick('bzr root').rstrip(),
 
308
                          os.path.join(self.test_dir, 'branch1'))
 
309
 
 
310
        progress("status of new file")
 
311
 
 
312
        f = file('test.txt', 'wt')
 
313
        f.write('hello world!\n')
 
314
        f.close()
 
315
 
 
316
        out = backtick("bzr unknowns")
 
317
        self.assertEquals(out, 'test.txt\n')
 
318
 
 
319
        out = backtick("bzr status")
 
320
        assert out == 'unknown:\n  test.txt\n'
 
321
 
 
322
        out = backtick("bzr status --all")
 
323
        assert out == "unknown:\n  test.txt\n"
 
324
 
 
325
        out = backtick("bzr status test.txt --all")
 
326
        assert out == "unknown:\n  test.txt\n"
 
327
 
 
328
        f = file('test2.txt', 'wt')
 
329
        f.write('goodbye cruel world...\n')
 
330
        f.close()
 
331
 
 
332
        out = backtick("bzr status test.txt")
 
333
        assert out == "unknown:\n  test.txt\n"
 
334
 
 
335
        out = backtick("bzr status")
 
336
        assert out == ("unknown:\n"
 
337
                       "  test.txt\n"
 
338
                       "  test2.txt\n")
 
339
 
 
340
        os.unlink('test2.txt')
 
341
 
 
342
        progress("command aliases")
 
343
        out = backtick("bzr st --all")
 
344
        assert out == ("unknown:\n"
 
345
                       "  test.txt\n")
 
346
 
 
347
        out = backtick("bzr stat")
 
348
        assert out == ("unknown:\n"
 
349
                       "  test.txt\n")
 
350
 
 
351
        progress("command help")
 
352
        runbzr("help st")
 
353
        runbzr("help")
 
354
        runbzr("help commands")
 
355
        runbzr("help slartibartfast", 1)
 
356
 
 
357
        out = backtick("bzr help ci")
 
358
        out.index('aliases: ')
 
359
 
 
360
        progress("can't rename unversioned file")
 
361
        runbzr("rename test.txt new-test.txt", 1)
 
362
 
 
363
        progress("adding a file")
 
364
 
 
365
        runbzr("add test.txt")
 
366
        assert backtick("bzr unknowns") == ''
 
367
        assert backtick("bzr status --all") == ("added:\n"
 
368
                                                "  test.txt\n")
 
369
 
 
370
        progress("rename newly-added file")
 
371
        runbzr("rename test.txt hello.txt")
 
372
        assert os.path.exists("hello.txt")
 
373
        assert not os.path.exists("test.txt")
 
374
 
 
375
        assert backtick("bzr revno") == '0\n'
 
376
 
 
377
        progress("add first revision")
 
378
        runbzr(['commit', '-m', 'add first revision'])
 
379
 
 
380
        progress("more complex renames")
 
381
        os.mkdir("sub1")
 
382
        runbzr("rename hello.txt sub1", 1)
 
383
        runbzr("rename hello.txt sub1/hello.txt", 1)
 
384
        runbzr("move hello.txt sub1", 1)
 
385
 
 
386
        runbzr("add sub1")
 
387
        runbzr("rename sub1 sub2")
 
388
        runbzr("move hello.txt sub2")
 
389
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
390
 
 
391
        assert exists("sub2")
 
392
        assert exists("sub2/hello.txt")
 
393
        assert not exists("sub1")
 
394
        assert not exists("hello.txt")
 
395
 
 
396
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
 
397
 
 
398
        mkdir("sub1")
 
399
        runbzr('add sub1')
 
400
        runbzr('move sub2/hello.txt sub1')
 
401
        assert not exists('sub2/hello.txt')
 
402
        assert exists('sub1/hello.txt')
 
403
        runbzr('move sub2 sub1')
 
404
        assert not exists('sub2')
 
405
        assert exists('sub1/sub2')
 
406
 
 
407
        runbzr(['commit', '-m', 'rename nested subdirectories'])
 
408
 
 
409
        chdir('sub1/sub2')
 
410
        self.assertEquals(backtick('bzr root')[:-1],
 
411
                          os.path.join(self.test_dir, 'branch1'))
 
412
        runbzr('move ../hello.txt .')
 
413
        assert exists('./hello.txt')
 
414
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
415
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
416
        runbzr(['commit', '-m', 'move to parent directory'])
 
417
        chdir('..')
 
418
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
419
 
 
420
        runbzr('move sub2/hello.txt .')
 
421
        assert exists('hello.txt')
 
422
 
 
423
        f = file('hello.txt', 'wt')
 
424
        f.write('some nice new content\n')
 
425
        f.close()
 
426
 
 
427
        f = file('msg.tmp', 'wt')
 
428
        f.write('this is my new commit\n')
 
429
        f.close()
 
430
 
 
431
        runbzr('commit -F msg.tmp')
 
432
 
 
433
        assert backtick('bzr revno') == '5\n'
 
434
        runbzr('export -r 5 export-5.tmp')
 
435
        runbzr('export export.tmp')
 
436
 
 
437
        runbzr('log')
 
438
        runbzr('log -v')
 
439
        runbzr('log -v --forward')
 
440
        runbzr('log -m', retcode=1)
 
441
        log_out = backtick('bzr log -m commit')
 
442
        assert "this is my new commit" in log_out
 
443
        assert "rename nested" not in log_out
 
444
        assert 'revision-id' not in log_out
 
445
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
446
 
 
447
 
 
448
        progress("file with spaces in name")
 
449
        mkdir('sub directory')
 
450
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
 
451
        runbzr('add .')
 
452
        runbzr('diff')
 
453
        runbzr('commit -m add-spaces')
 
454
        runbzr('check')
 
455
 
 
456
        runbzr('log')
 
457
        runbzr('log --forward')
 
458
 
 
459
        runbzr('info')
 
460