~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

and the tutorial patch came back, the very next day

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
it's normally invoked.
27
27
"""
28
28
 
29
 
from cStringIO import StringIO
30
29
import sys
31
30
import os
32
31
 
33
32
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
34
33
from bzrlib.branch import Branch
 
34
from bzrlib.commands import run_bzr
35
35
 
36
36
 
37
37
class ExternalBase(TestCaseInTempDir):
38
 
 
39
 
    def runbzr(self, args, retcode=0, backtick=False):
 
38
    def runbzr(self, args, retcode=0,backtick=False):
40
39
        if isinstance(args, basestring):
41
40
            args = args.split()
42
41
 
43
42
        if backtick:
44
 
            return self.run_bzr_captured(args, retcode=retcode)[0]
 
43
            return self.backtick(['python', self.BZRPATH,] + args,
 
44
                           retcode=retcode)
45
45
        else:
46
 
            return self.run_bzr_captured(args, retcode=retcode)
 
46
            return self.runcmd(['python', self.BZRPATH,] + args,
 
47
                           retcode=retcode)
47
48
 
48
49
 
49
50
class TestCommands(ExternalBase):
72
73
        f = file('.bzr/email', 'wt')
73
74
        f.write('Branch Identity <branch@identi.ty>')
74
75
        f.close()
75
 
        bzr_email = os.environ.get('BZREMAIL')
76
 
        if bzr_email is not None:
77
 
            del os.environ['BZREMAIL']
78
76
        whoami = self.runbzr("whoami",backtick=True)
79
77
        whoami_email = self.runbzr("whoami --email",backtick=True)
80
78
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
81
79
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
82
 
        # Verify that the environment variable overrides the value 
83
 
        # in the file
84
 
        os.environ['BZREMAIL'] = 'Different ID <other@environ.ment>'
85
 
        whoami = self.runbzr("whoami",backtick=True)
86
 
        whoami_email = self.runbzr("whoami --email",backtick=True)
87
 
        self.assertTrue(whoami.startswith('Different ID <other@environ.ment>'))
88
 
        self.assertTrue(whoami_email.startswith('other@environ.ment'))
89
 
        if bzr_email is not None:
90
 
            os.environ['BZREMAIL'] = bzr_email
91
80
 
92
81
    def test_invalid_commands(self):
93
82
        self.runbzr("pants", retcode=1)
104
93
    def test_ignore_patterns(self):
105
94
        from bzrlib.branch import Branch
106
95
        
107
 
        b = Branch.initialize('.')
 
96
        b = Branch('.', init=True)
108
97
        self.assertEquals(list(b.unknowns()), [])
109
98
 
110
99
        file('foo.tmp', 'wt').write('tmp files are ignored')
111
100
        self.assertEquals(list(b.unknowns()), [])
112
 
        assert self.capture('unknowns') == ''
 
101
        assert self.backtick('bzr unknowns') == ''
113
102
 
114
103
        file('foo.c', 'wt').write('int main() {}')
115
104
        self.assertEquals(list(b.unknowns()), ['foo.c'])
116
 
        assert self.capture('unknowns') == 'foo.c\n'
 
105
        assert self.backtick('bzr unknowns') == 'foo.c\n'
117
106
 
118
107
        self.runbzr(['add', 'foo.c'])
119
 
        assert self.capture('unknowns') == ''
 
108
        assert self.backtick('bzr unknowns') == ''
120
109
 
121
110
        # 'ignore' works when creating the .bzignore file
122
111
        file('foo.blah', 'wt').write('blah')
128
117
        # 'ignore' works when then .bzrignore file already exists
129
118
        file('garh', 'wt').write('garh')
130
119
        self.assertEquals(list(b.unknowns()), ['garh'])
131
 
        assert self.capture('unknowns') == 'garh\n'
 
120
        assert self.backtick('bzr unknowns') == 'garh\n'
132
121
        self.runbzr('ignore garh')
133
122
        self.assertEquals(list(b.unknowns()), [])
134
123
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
158
147
        os.rmdir('revertdir')
159
148
        self.runbzr('revert')
160
149
 
161
 
        file('hello', 'wt').write('xyz')
162
 
        self.runbzr('commit -m xyz hello')
163
 
        self.runbzr('revert -r 1 hello')
164
 
        self.check_file_contents('hello', 'foo')
165
 
        self.runbzr('revert hello')
166
 
        self.check_file_contents('hello', 'xyz')
167
 
 
168
150
    def test_mv_modes(self):
169
151
        """Test two modes of operation for mv"""
170
152
        from bzrlib.branch import Branch
171
 
        b = Branch.initialize('.')
 
153
        b = Branch('.', init=True)
172
154
        self.build_tree(['a', 'c', 'subdir/'])
173
 
        self.run_bzr_captured(['add', self.test_dir])
174
 
        self.run_bzr_captured(['mv', 'a', 'b'])
175
 
        self.run_bzr_captured(['mv', 'b', 'subdir'])
176
 
        self.run_bzr_captured(['mv', 'subdir/b', 'a'])
177
 
        self.run_bzr_captured(['mv', 'a', 'c', 'subdir'])
178
 
        self.run_bzr_captured(['mv', 'subdir/a', 'subdir/newa'])
 
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')
179
161
 
180
162
 
181
163
    def test_main_version(self):
202
184
        test.runbzr('add goodbye')
203
185
        test.runbzr('commit -m setup goodbye')
204
186
 
205
 
    def test_diff(self):
206
 
        self.example_branch()
207
 
        file('hello', 'wt').write('hello world!')
208
 
        self.runbzr('commit -m fixing hello')
209
 
        output = self.runbzr('diff -r 2..3', backtick=1)
210
 
        self.assert_('\n+hello world!' in output)
211
 
        output = self.runbzr('diff -r last:3..last:1', backtick=1)
212
 
        self.assert_('\n+baz' in output)
213
 
 
214
 
    def test_diff(self):
215
 
        self.example_branch()
216
 
        file('hello', 'wt').write('hello world!')
217
 
        self.runbzr('commit -m fixing hello')
218
 
        output = self.runbzr('diff -r 2..3', backtick=1)
219
 
        self.assert_('\n+hello world!' in output)
220
 
        output = self.runbzr('diff -r last:3..last:1', backtick=1)
221
 
        self.assert_('\n+baz' in output)
 
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')
222
196
 
223
197
    def test_merge(self):
224
198
        from bzrlib.branch import Branch
240
214
        self.runbzr('merge ../b')
241
215
        self.check_file_contents('goodbye', 'quux')
242
216
        # Merging a branch pulls its revision into the tree
243
 
        a = Branch.open('.')
244
 
        b = Branch.open('../b')
 
217
        a = Branch('.')
 
218
        b = Branch('../b')
245
219
        a.get_revision_xml(b.last_patch())
246
220
        self.log('pending merges: %s', a.pending_merges())
247
221
        #        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
253
227
        os.chdir('a')
254
228
 
255
229
        self.example_branch()
256
 
        self.runbzr('pull', retcode=1)
257
 
        self.runbzr('missing', retcode=1)
258
 
        self.runbzr('missing .')
259
 
        self.runbzr('missing')
260
 
        self.runbzr('pull')
261
 
        self.runbzr('pull /', retcode=1)
262
 
        self.runbzr('pull')
263
 
 
264
230
        os.chdir('..')
265
231
        self.runbzr('branch a b')
266
232
        os.chdir('b')
267
 
        self.runbzr('pull')
268
233
        self.runbzr('commit -m blah --unchanged')
269
234
        os.chdir('../a')
270
 
        a = Branch.open('.')
271
 
        b = Branch.open('../b')
 
235
        a = Branch('.')
 
236
        b = Branch('../b')
272
237
        assert a.revision_history() == b.revision_history()[:-1]
273
238
        self.runbzr('pull ../b')
274
239
        assert a.revision_history() == b.revision_history()
283
248
        self.runbzr('pull ../a')
284
249
        assert a.revision_history()[-1] == b.revision_history()[-1]
285
250
        
 
251
 
286
252
    def test_add_reports(self):
287
253
        """add command prints the names of added files."""
288
 
        b = Branch.initialize('.')
 
254
        b = Branch('.', init=True)
289
255
        self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
290
 
        out = self.run_bzr_captured(['add'], retcode = 0)[0]
 
256
 
 
257
        from cStringIO import StringIO
 
258
        out = StringIO()
 
259
 
 
260
        ret = self.apply_redirected(None, out, None,
 
261
                                    run_bzr,
 
262
                                    ['add'])
 
263
        self.assertEquals(ret, 0)
 
264
 
291
265
        # the ordering is not defined at the moment
292
 
        results = sorted(out.rstrip('\n').split('\n'))
 
266
        results = sorted(out.getvalue().rstrip('\n').split('\n'))
293
267
        self.assertEquals(['added dir',
294
268
                           'added dir/sub.txt',
295
269
                           'added top.txt',],
296
270
                          results)
297
271
 
298
 
    def test_unknown_command(self):
299
 
        """Handling of unknown command."""
300
 
        out, err = self.run_bzr_captured(['fluffy-badger'],
301
 
                                         retcode=1)
302
 
        self.assertEquals(out, '')
303
 
        err.index('unknown command')
304
 
        
305
 
 
306
272
 
307
273
class OldTests(ExternalBase):
308
274
    """old tests moved from ./testbzr."""
312
278
        from os.path import exists
313
279
 
314
280
        runbzr = self.runbzr
315
 
        capture = self.capture
 
281
        backtick = self.backtick
316
282
        progress = self.log
317
283
 
318
284
        progress("basic branch creation")
320
286
        chdir('branch1')
321
287
        runbzr('init')
322
288
 
323
 
        self.assertEquals(capture('root').rstrip(),
 
289
        self.assertEquals(backtick('bzr root').rstrip(),
324
290
                          os.path.join(self.test_dir, 'branch1'))
325
291
 
326
292
        progress("status of new file")
329
295
        f.write('hello world!\n')
330
296
        f.close()
331
297
 
332
 
        self.assertEquals(capture('unknowns'), 'test.txt\n')
 
298
        out = backtick("bzr unknowns")
 
299
        self.assertEquals(out, 'test.txt\n')
333
300
 
334
 
        out = capture("status")
 
301
        out = backtick("bzr status")
335
302
        assert out == 'unknown:\n  test.txt\n'
336
303
 
337
 
        out = capture("status --all")
 
304
        out = backtick("bzr status --all")
338
305
        assert out == "unknown:\n  test.txt\n"
339
306
 
340
 
        out = capture("status test.txt --all")
 
307
        out = backtick("bzr status test.txt --all")
341
308
        assert out == "unknown:\n  test.txt\n"
342
309
 
343
310
        f = file('test2.txt', 'wt')
344
311
        f.write('goodbye cruel world...\n')
345
312
        f.close()
346
313
 
347
 
        out = capture("status test.txt")
 
314
        out = backtick("bzr status test.txt")
348
315
        assert out == "unknown:\n  test.txt\n"
349
316
 
350
 
        out = capture("status")
 
317
        out = backtick("bzr status")
351
318
        assert out == ("unknown:\n"
352
319
                       "  test.txt\n"
353
320
                       "  test2.txt\n")
355
322
        os.unlink('test2.txt')
356
323
 
357
324
        progress("command aliases")
358
 
        out = capture("st --all")
 
325
        out = backtick("bzr st --all")
359
326
        assert out == ("unknown:\n"
360
327
                       "  test.txt\n")
361
328
 
362
 
        out = capture("stat")
 
329
        out = backtick("bzr stat")
363
330
        assert out == ("unknown:\n"
364
331
                       "  test.txt\n")
365
332
 
369
336
        runbzr("help commands")
370
337
        runbzr("help slartibartfast", 1)
371
338
 
372
 
        out = capture("help ci")
 
339
        out = backtick("bzr help ci")
373
340
        out.index('aliases: ')
374
341
 
375
342
        progress("can't rename unversioned file")
378
345
        progress("adding a file")
379
346
 
380
347
        runbzr("add test.txt")
381
 
        assert capture("unknowns") == ''
382
 
        assert capture("status --all") == ("added:\n"
 
348
        assert backtick("bzr unknowns") == ''
 
349
        assert backtick("bzr status --all") == ("added:\n"
383
350
                                                "  test.txt\n")
384
351
 
385
352
        progress("rename newly-added file")
387
354
        assert os.path.exists("hello.txt")
388
355
        assert not os.path.exists("test.txt")
389
356
 
390
 
        assert capture("revno") == '0\n'
 
357
        assert backtick("bzr revno") == '0\n'
391
358
 
392
359
        progress("add first revision")
393
360
        runbzr(['commit', '-m', 'add first revision'])
401
368
        runbzr("add sub1")
402
369
        runbzr("rename sub1 sub2")
403
370
        runbzr("move hello.txt sub2")
404
 
        assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
371
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
405
372
 
406
373
        assert exists("sub2")
407
374
        assert exists("sub2/hello.txt")
422
389
        runbzr(['commit', '-m', 'rename nested subdirectories'])
423
390
 
424
391
        chdir('sub1/sub2')
425
 
        self.assertEquals(capture('root')[:-1],
 
392
        self.assertEquals(backtick('bzr root')[:-1],
426
393
                          os.path.join(self.test_dir, 'branch1'))
427
394
        runbzr('move ../hello.txt .')
428
395
        assert exists('./hello.txt')
429
 
        self.assertEquals(capture('relpath hello.txt'),
430
 
                          os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
431
 
        assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
396
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
397
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
432
398
        runbzr(['commit', '-m', 'move to parent directory'])
433
399
        chdir('..')
434
 
        assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
400
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
435
401
 
436
402
        runbzr('move sub2/hello.txt .')
437
403
        assert exists('hello.txt')
446
412
 
447
413
        runbzr('commit -F msg.tmp')
448
414
 
449
 
        assert capture('revno') == '5\n'
 
415
        assert backtick('bzr revno') == '5\n'
450
416
        runbzr('export -r 5 export-5.tmp')
451
417
        runbzr('export export.tmp')
452
418
 
454
420
        runbzr('log -v')
455
421
        runbzr('log -v --forward')
456
422
        runbzr('log -m', retcode=1)
457
 
        log_out = capture('log -m commit')
 
423
        log_out = backtick('bzr log -m commit')
458
424
        assert "this is my new commit" in log_out
459
425
        assert "rename nested" not in log_out
460
426
        assert 'revision-id' not in log_out
461
 
        assert 'revision-id' in capture('log --show-ids -m commit')
 
427
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
462
428
 
463
429
 
464
430
        progress("file with spaces in name")