/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: Aaron Bentley
  • Date: 2005-09-12 02:53:07 UTC
  • mto: (1185.3.4)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: aaron.bentley@utoronto.ca-20050912025307-8c21544e8db1cbdb
added all_descendants and node_distances, exception when root doesn't exist

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('.', init=True)
 
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 skipped_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('mv', 'a', 'b')
 
156
        self.run_bzr('mv', 'b', 'subdir')
 
157
        self.run_bzr('mv', 'subdir/b', 'a')
 
158
        self.run_bzr('mv', 'a', 'b', 'subdir')
 
159
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
160
 
 
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:')
 
167
        self.log(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:')
 
173
        self.log(tmp_output)
 
174
        self.assertEquals(output, tmp_output)
 
175
 
 
176
    def example_branch(test):
 
177
        test.runbzr('init')
 
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')
 
184
 
 
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')
 
194
 
 
195
    def test_merge(self):
 
196
        from bzrlib.branch import Branch
 
197
        
 
198
        os.mkdir('a')
 
199
        os.chdir('a')
 
200
 
 
201
        self.example_branch()
 
202
        os.chdir('..')
 
203
        self.runbzr('branch a b')
 
204
        os.chdir('b')
 
205
        file('goodbye', 'wt').write('quux')
 
206
        self.runbzr(['commit',  '-m',  "more u's are always good"])
 
207
 
 
208
        os.chdir('../a')
 
209
        file('hello', 'wt').write('quuux')
 
210
        # We can't merge when there are in-tree changes
 
211
        self.runbzr('merge ../b', retcode=1)
 
212
        self.runbzr(['commit', '-m', "Like an epidemic of u's"])
 
213
        self.runbzr('merge ../b')
 
214
        self.check_file_contents('goodbye', 'quux')
 
215
        # Merging a branch pulls its revision into the tree
 
216
        a = Branch('.')
 
217
        b = Branch('../b')
 
218
        a.get_revision_xml(b.last_patch())
 
219
 
 
220
        self.log('pending merges: %s', a.pending_merges())
 
221
        #        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
 
222
        #        % (a.pending_merges(), b.last_patch())
 
223
 
 
224
    def test_pull(self):
 
225
        """Pull changes from one branch to another."""
 
226
        os.mkdir('a')
 
227
        os.chdir('a')
 
228
 
 
229
        self.example_branch()
 
230
        os.chdir('..')
 
231
        self.runbzr('branch a b')
 
232
        os.chdir('b')
 
233
        self.runbzr('commit -m blah --unchanged')
 
234
        os.chdir('../a')
 
235
        a = Branch('.')
 
236
        b = Branch('../b')
 
237
        assert a.revision_history() == b.revision_history()[:-1]
 
238
        self.runbzr('pull ../b')
 
239
        assert a.revision_history() == b.revision_history()
 
240
        self.runbzr('commit -m blah2 --unchanged')
 
241
        os.chdir('../b')
 
242
        self.runbzr('commit -m blah3 --unchanged')
 
243
        self.runbzr('pull ../a', retcode=1)
 
244
 
 
245
    def test_add_reports(self):
 
246
        """add command prints the names of added files."""
 
247
        b = Branch('.', init=True)
 
248
        self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
 
249
 
 
250
        from cStringIO import StringIO
 
251
        out = StringIO()
 
252
 
 
253
        ret = self.apply_redirected(None, out, None,
 
254
                                    run_bzr,
 
255
                                    ['add'])
 
256
        self.assertEquals(ret, 0)
 
257
 
 
258
        # the ordering is not defined at the moment
 
259
        results = sorted(out.getvalue().rstrip('\n').split('\n'))
 
260
        self.assertEquals(['added dir',
 
261
                           'added dir/sub.txt',
 
262
                           'added top.txt',],
 
263
                          results)
 
264
 
 
265
 
 
266
class OldTests(ExternalBase):
 
267
    """old tests moved from ./testbzr."""
 
268
 
 
269
    def test_bzr(self):
 
270
        from os import chdir, mkdir
 
271
        from os.path import exists
 
272
 
 
273
        runbzr = self.runbzr
 
274
        backtick = self.backtick
 
275
        progress = self.log
 
276
 
 
277
        progress("basic branch creation")
 
278
        mkdir('branch1')
 
279
        chdir('branch1')
 
280
        runbzr('init')
 
281
 
 
282
        self.assertEquals(backtick('bzr root').rstrip(),
 
283
                          os.path.join(self.test_dir, 'branch1'))
 
284
 
 
285
        progress("status of new file")
 
286
 
 
287
        f = file('test.txt', 'wt')
 
288
        f.write('hello world!\n')
 
289
        f.close()
 
290
 
 
291
        out = backtick("bzr unknowns")
 
292
        self.assertEquals(out, 'test.txt\n')
 
293
 
 
294
        out = backtick("bzr status")
 
295
        assert out == 'unknown:\n  test.txt\n'
 
296
 
 
297
        out = backtick("bzr status --all")
 
298
        assert out == "unknown:\n  test.txt\n"
 
299
 
 
300
        out = backtick("bzr status test.txt --all")
 
301
        assert out == "unknown:\n  test.txt\n"
 
302
 
 
303
        f = file('test2.txt', 'wt')
 
304
        f.write('goodbye cruel world...\n')
 
305
        f.close()
 
306
 
 
307
        out = backtick("bzr status test.txt")
 
308
        assert out == "unknown:\n  test.txt\n"
 
309
 
 
310
        out = backtick("bzr status")
 
311
        assert out == ("unknown:\n"
 
312
                       "  test.txt\n"
 
313
                       "  test2.txt\n")
 
314
 
 
315
        os.unlink('test2.txt')
 
316
 
 
317
        progress("command aliases")
 
318
        out = backtick("bzr st --all")
 
319
        assert out == ("unknown:\n"
 
320
                       "  test.txt\n")
 
321
 
 
322
        out = backtick("bzr stat")
 
323
        assert out == ("unknown:\n"
 
324
                       "  test.txt\n")
 
325
 
 
326
        progress("command help")
 
327
        runbzr("help st")
 
328
        runbzr("help")
 
329
        runbzr("help commands")
 
330
        runbzr("help slartibartfast", 1)
 
331
 
 
332
        out = backtick("bzr help ci")
 
333
        out.index('aliases: ')
 
334
 
 
335
        progress("can't rename unversioned file")
 
336
        runbzr("rename test.txt new-test.txt", 1)
 
337
 
 
338
        progress("adding a file")
 
339
 
 
340
        runbzr("add test.txt")
 
341
        assert backtick("bzr unknowns") == ''
 
342
        assert backtick("bzr status --all") == ("added:\n"
 
343
                                                "  test.txt\n")
 
344
 
 
345
        progress("rename newly-added file")
 
346
        runbzr("rename test.txt hello.txt")
 
347
        assert os.path.exists("hello.txt")
 
348
        assert not os.path.exists("test.txt")
 
349
 
 
350
        assert backtick("bzr revno") == '0\n'
 
351
 
 
352
        progress("add first revision")
 
353
        runbzr(['commit', '-m', 'add first revision'])
 
354
 
 
355
        progress("more complex renames")
 
356
        os.mkdir("sub1")
 
357
        runbzr("rename hello.txt sub1", 1)
 
358
        runbzr("rename hello.txt sub1/hello.txt", 1)
 
359
        runbzr("move hello.txt sub1", 1)
 
360
 
 
361
        runbzr("add sub1")
 
362
        runbzr("rename sub1 sub2")
 
363
        runbzr("move hello.txt sub2")
 
364
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
365
 
 
366
        assert exists("sub2")
 
367
        assert exists("sub2/hello.txt")
 
368
        assert not exists("sub1")
 
369
        assert not exists("hello.txt")
 
370
 
 
371
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
 
372
 
 
373
        mkdir("sub1")
 
374
        runbzr('add sub1')
 
375
        runbzr('move sub2/hello.txt sub1')
 
376
        assert not exists('sub2/hello.txt')
 
377
        assert exists('sub1/hello.txt')
 
378
        runbzr('move sub2 sub1')
 
379
        assert not exists('sub2')
 
380
        assert exists('sub1/sub2')
 
381
 
 
382
        runbzr(['commit', '-m', 'rename nested subdirectories'])
 
383
 
 
384
        chdir('sub1/sub2')
 
385
        self.assertEquals(backtick('bzr root')[:-1],
 
386
                          os.path.join(self.test_dir, 'branch1'))
 
387
        runbzr('move ../hello.txt .')
 
388
        assert exists('./hello.txt')
 
389
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
390
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
391
        runbzr(['commit', '-m', 'move to parent directory'])
 
392
        chdir('..')
 
393
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
394
 
 
395
        runbzr('move sub2/hello.txt .')
 
396
        assert exists('hello.txt')
 
397
 
 
398
        f = file('hello.txt', 'wt')
 
399
        f.write('some nice new content\n')
 
400
        f.close()
 
401
 
 
402
        f = file('msg.tmp', 'wt')
 
403
        f.write('this is my new commit\n')
 
404
        f.close()
 
405
 
 
406
        runbzr('commit -F msg.tmp')
 
407
 
 
408
        assert backtick('bzr revno') == '5\n'
 
409
        runbzr('export -r 5 export-5.tmp')
 
410
        runbzr('export export.tmp')
 
411
 
 
412
        runbzr('log')
 
413
        runbzr('log -v')
 
414
        runbzr('log -v --forward')
 
415
        runbzr('log -m', retcode=1)
 
416
        log_out = backtick('bzr log -m commit')
 
417
        assert "this is my new commit" in log_out
 
418
        assert "rename nested" not in log_out
 
419
        assert 'revision-id' not in log_out
 
420
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
421
 
 
422
 
 
423
        progress("file with spaces in name")
 
424
        mkdir('sub directory')
 
425
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
 
426
        runbzr('add .')
 
427
        runbzr('diff')
 
428
        runbzr('commit -m add-spaces')
 
429
        runbzr('check')
 
430
 
 
431
        runbzr('log')
 
432
        runbzr('log --forward')
 
433
 
 
434
        runbzr('info')
 
435