/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: Robert Collins
  • Date: 2005-09-12 12:39:58 UTC
  • mfrom: (1185.3.4)
  • mto: (1185.1.10)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: robertc@robertcollins.net-20050912123958-7982e808f291f439
merge up with head

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