~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

  • Committer: Robert Collins
  • Date: 2005-09-28 05:25:54 UTC
  • mfrom: (1185.1.42)
  • mto: (1092.2.18)
  • mto: This revision was merged to the branch mainline in revision 1397.
  • Revision ID: robertc@robertcollins.net-20050928052554-beb985505f77ea6a
update symlink branch to integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
it's normally invoked.
27
27
"""
28
28
 
29
 
import os;
 
29
from cStringIO import StringIO
 
30
import os
30
31
import sys
31
32
import os
32
33
 
33
34
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
34
35
from bzrlib.branch import Branch
35
 
from bzrlib.commands import run_bzr
36
36
 
37
37
 
38
38
class ExternalBase(TestCaseInTempDir):
39
 
    def runbzr(self, args, retcode=0,backtick=False):
 
39
 
 
40
    def runbzr(self, args, retcode=0, backtick=False):
40
41
        if isinstance(args, basestring):
41
42
            args = args.split()
42
43
 
43
44
        if backtick:
44
 
            return self.backtick(['python', self.BZRPATH,] + args,
45
 
                           retcode=retcode)
 
45
            return self.run_bzr_captured(args, retcode=retcode)[0]
46
46
        else:
47
 
            return self.runcmd(['python', self.BZRPATH,] + args,
48
 
                           retcode=retcode)
 
47
            return self.run_bzr_captured(args, retcode=retcode)
49
48
 
50
49
 
51
50
class TestCommands(ExternalBase):
74
73
        f = file('.bzr/email', 'wt')
75
74
        f.write('Branch Identity <branch@identi.ty>')
76
75
        f.close()
 
76
        bzr_email = os.environ.get('BZREMAIL')
 
77
        if bzr_email is not None:
 
78
            del os.environ['BZREMAIL']
77
79
        whoami = self.runbzr("whoami",backtick=True)
78
80
        whoami_email = self.runbzr("whoami --email",backtick=True)
79
81
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
80
82
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
 
83
        # Verify that the environment variable overrides the value 
 
84
        # in the file
 
85
        os.environ['BZREMAIL'] = 'Different ID <other@environ.ment>'
 
86
        whoami = self.runbzr("whoami",backtick=True)
 
87
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
88
        self.assertTrue(whoami.startswith('Different ID <other@environ.ment>'))
 
89
        self.assertTrue(whoami_email.startswith('other@environ.ment'))
 
90
        if bzr_email is not None:
 
91
            os.environ['BZREMAIL'] = bzr_email
81
92
 
82
93
    def test_invalid_commands(self):
83
94
        self.runbzr("pants", retcode=1)
94
105
    def test_ignore_patterns(self):
95
106
        from bzrlib.branch import Branch
96
107
        
97
 
        b = Branch('.', init=True)
 
108
        b = Branch.initialize('.')
98
109
        self.assertEquals(list(b.unknowns()), [])
99
110
 
100
111
        file('foo.tmp', 'wt').write('tmp files are ignored')
101
112
        self.assertEquals(list(b.unknowns()), [])
102
 
        assert self.backtick('bzr unknowns') == ''
 
113
        assert self.capture('unknowns') == ''
103
114
 
104
115
        file('foo.c', 'wt').write('int main() {}')
105
116
        self.assertEquals(list(b.unknowns()), ['foo.c'])
106
 
        assert self.backtick('bzr unknowns') == 'foo.c\n'
 
117
        assert self.capture('unknowns') == 'foo.c\n'
107
118
 
108
119
        self.runbzr(['add', 'foo.c'])
109
 
        assert self.backtick('bzr unknowns') == ''
 
120
        assert self.capture('unknowns') == ''
110
121
 
111
122
        # 'ignore' works when creating the .bzignore file
112
123
        file('foo.blah', 'wt').write('blah')
113
124
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
114
125
        self.runbzr('ignore *.blah')
115
126
        self.assertEquals(list(b.unknowns()), [])
116
 
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
127
        assert file('.bzrignore', 'rU').read() == '*.blah\n'
117
128
 
118
129
        # 'ignore' works when then .bzrignore file already exists
119
130
        file('garh', 'wt').write('garh')
120
131
        self.assertEquals(list(b.unknowns()), ['garh'])
121
 
        assert self.backtick('bzr unknowns') == 'garh\n'
 
132
        assert self.capture('unknowns') == 'garh\n'
122
133
        self.runbzr('ignore garh')
123
134
        self.assertEquals(list(b.unknowns()), [])
124
 
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
135
        assert file('.bzrignore', 'rU').read() == '*.blah\ngarh\n'
125
136
 
126
137
    def test_revert(self):
127
138
        self.runbzr('init')
148
159
        os.rmdir('revertdir')
149
160
        self.runbzr('revert')
150
161
 
151
 
    def skipped_test_mv_modes(self):
 
162
        file('hello', 'wt').write('xyz')
 
163
        self.runbzr('commit -m xyz hello')
 
164
        self.runbzr('revert -r 1 hello')
 
165
        self.check_file_contents('hello', 'foo')
 
166
        self.runbzr('revert hello')
 
167
        self.check_file_contents('hello', 'xyz')
 
168
        os.chdir('revertdir')
 
169
        self.runbzr('revert')
 
170
        os.chdir('..')
 
171
 
 
172
 
 
173
    def test_mv_modes(self):
152
174
        """Test two modes of operation for mv"""
153
175
        from bzrlib.branch import Branch
154
 
        b = Branch('.', init=True)
 
176
        b = Branch.initialize('.')
155
177
        self.build_tree(['a', 'c', 'subdir/'])
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', 'b', 'subdir')
160
 
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
178
        self.run_bzr_captured(['add', self.test_dir])
 
179
        self.run_bzr_captured(['mv', 'a', 'b'])
 
180
        self.run_bzr_captured(['mv', 'b', 'subdir'])
 
181
        self.run_bzr_captured(['mv', 'subdir/b', 'a'])
 
182
        self.run_bzr_captured(['mv', 'a', 'c', 'subdir'])
 
183
        self.run_bzr_captured(['mv', 'subdir/a', 'subdir/newa'])
 
184
 
161
185
 
162
186
    def test_main_version(self):
163
187
        """Check output from version command and master option is reasonable"""
183
207
        test.runbzr('add goodbye')
184
208
        test.runbzr('commit -m setup goodbye')
185
209
 
186
 
    def test_revert(self):
187
 
        self.example_branch()
188
 
        file('hello', 'wt').write('bar')
189
 
        file('goodbye', 'wt').write('qux')
190
 
        self.runbzr('revert hello')
191
 
        self.check_file_contents('hello', 'foo')
192
 
        self.check_file_contents('goodbye', 'qux')
193
 
        self.runbzr('revert')
194
 
        self.check_file_contents('goodbye', 'baz')
 
210
    def test_diff(self):
 
211
        self.example_branch()
 
212
        file('hello', 'wt').write('hello world!')
 
213
        self.runbzr('commit -m fixing hello')
 
214
        output = self.runbzr('diff -r 2..3', backtick=1)
 
215
        self.assert_('\n+hello world!' in output)
 
216
        output = self.runbzr('diff -r last:3..last:1', backtick=1)
 
217
        self.assert_('\n+baz' in output)
 
218
 
 
219
    def test_branch(self):
 
220
        """Branch from one branch to another."""
 
221
        os.mkdir('a')
 
222
        os.chdir('a')
 
223
        self.example_branch()
 
224
        os.chdir('..')
 
225
        self.runbzr('branch a b')
 
226
        self.runbzr('branch a c -r 1')
195
227
 
196
228
    def test_merge(self):
197
229
        from bzrlib.branch import Branch
213
245
        self.runbzr('merge ../b')
214
246
        self.check_file_contents('goodbye', 'quux')
215
247
        # Merging a branch pulls its revision into the tree
216
 
        a = Branch('.')
217
 
        b = Branch('../b')
 
248
        a = Branch.open('.')
 
249
        b = Branch.open('../b')
218
250
        a.get_revision_xml(b.last_patch())
219
251
        self.log('pending merges: %s', a.pending_merges())
220
252
        #        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
226
258
        os.chdir('a')
227
259
 
228
260
        self.example_branch()
 
261
        self.runbzr('pull', retcode=1)
 
262
        self.runbzr('missing', retcode=1)
 
263
        self.runbzr('missing .')
 
264
        self.runbzr('missing')
 
265
        self.runbzr('pull')
 
266
        self.runbzr('pull /', retcode=1)
 
267
        self.runbzr('pull')
 
268
 
229
269
        os.chdir('..')
230
270
        self.runbzr('branch a b')
231
271
        os.chdir('b')
 
272
        self.runbzr('pull')
 
273
        os.mkdir('subdir')
 
274
        self.runbzr('add subdir')
232
275
        self.runbzr('commit -m blah --unchanged')
233
276
        os.chdir('../a')
234
 
        a = Branch('.')
235
 
        b = Branch('../b')
 
277
        a = Branch.open('.')
 
278
        b = Branch.open('../b')
236
279
        assert a.revision_history() == b.revision_history()[:-1]
237
280
        self.runbzr('pull ../b')
238
281
        assert a.revision_history() == b.revision_history()
243
286
        os.chdir('../a')
244
287
        self.runbzr('merge ../b')
245
288
        self.runbzr('commit -m blah4 --unchanged')
246
 
        os.chdir('../b')
247
 
        self.runbzr('pull ../a')
 
289
        os.chdir('../b/subdir')
 
290
        self.runbzr('pull ../../a')
248
291
        assert a.revision_history()[-1] == b.revision_history()[-1]
249
292
        
250
 
 
251
293
    def test_add_reports(self):
252
294
        """add command prints the names of added files."""
253
 
        b = Branch('.', init=True)
 
295
        b = Branch.initialize('.')
254
296
        self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
255
 
 
256
 
        from cStringIO import StringIO
257
 
        out = StringIO()
258
 
 
259
 
        ret = self.apply_redirected(None, out, None,
260
 
                                    run_bzr,
261
 
                                    ['add'])
262
 
        self.assertEquals(ret, 0)
263
 
 
 
297
        out = self.run_bzr_captured(['add'], retcode = 0)[0]
264
298
        # the ordering is not defined at the moment
265
 
        results = sorted(out.getvalue().rstrip('\n').split('\n'))
 
299
        results = sorted(out.rstrip('\n').split('\n'))
266
300
        self.assertEquals(['added dir',
267
 
                           'added dir/sub.txt',
 
301
                           'added dir'+os.sep+'sub.txt',
268
302
                           'added top.txt',],
269
303
                          results)
270
304
 
 
305
    def test_unknown_command(self):
 
306
        """Handling of unknown command."""
 
307
        out, err = self.run_bzr_captured(['fluffy-badger'],
 
308
                                         retcode=1)
 
309
        self.assertEquals(out, '')
 
310
        err.index('unknown command')
 
311
        
 
312
 
271
313
 
272
314
def has_symlinks():
273
315
    if hasattr(os, 'symlink'):
289
331
        from os.path import exists
290
332
 
291
333
        runbzr = self.runbzr
292
 
        backtick = self.backtick
 
334
        capture = self.capture
293
335
        progress = self.log
294
336
 
295
337
        progress("basic branch creation")
297
339
        chdir('branch1')
298
340
        runbzr('init')
299
341
 
300
 
        self.assertEquals(backtick('bzr root').rstrip(),
 
342
        self.assertEquals(capture('root').rstrip(),
301
343
                          os.path.join(self.test_dir, 'branch1'))
302
344
 
303
345
        progress("status of new file")
306
348
        f.write('hello world!\n')
307
349
        f.close()
308
350
 
309
 
        out = backtick("bzr unknowns")
310
 
        self.assertEquals(out, 'test.txt\n')
 
351
        self.assertEquals(capture('unknowns'), 'test.txt\n')
311
352
 
312
 
        out = backtick("bzr status")
 
353
        out = capture("status")
313
354
        assert out == 'unknown:\n  test.txt\n'
314
355
 
315
 
        out = backtick("bzr status --all")
 
356
        out = capture("status --all")
316
357
        assert out == "unknown:\n  test.txt\n"
317
358
 
318
 
        out = backtick("bzr status test.txt --all")
 
359
        out = capture("status test.txt --all")
319
360
        assert out == "unknown:\n  test.txt\n"
320
361
 
321
362
        f = file('test2.txt', 'wt')
322
363
        f.write('goodbye cruel world...\n')
323
364
        f.close()
324
365
 
325
 
        out = backtick("bzr status test.txt")
 
366
        out = capture("status test.txt")
326
367
        assert out == "unknown:\n  test.txt\n"
327
368
 
328
 
        out = backtick("bzr status")
 
369
        out = capture("status")
329
370
        assert out == ("unknown:\n"
330
371
                       "  test.txt\n"
331
372
                       "  test2.txt\n")
333
374
        os.unlink('test2.txt')
334
375
 
335
376
        progress("command aliases")
336
 
        out = backtick("bzr st --all")
 
377
        out = capture("st --all")
337
378
        assert out == ("unknown:\n"
338
379
                       "  test.txt\n")
339
380
 
340
 
        out = backtick("bzr stat")
 
381
        out = capture("stat")
341
382
        assert out == ("unknown:\n"
342
383
                       "  test.txt\n")
343
384
 
347
388
        runbzr("help commands")
348
389
        runbzr("help slartibartfast", 1)
349
390
 
350
 
        out = backtick("bzr help ci")
 
391
        out = capture("help ci")
351
392
        out.index('aliases: ')
352
393
 
353
394
        progress("can't rename unversioned file")
356
397
        progress("adding a file")
357
398
 
358
399
        runbzr("add test.txt")
359
 
        assert backtick("bzr unknowns") == ''
360
 
        assert backtick("bzr status --all") == ("added:\n"
 
400
        assert capture("unknowns") == ''
 
401
        assert capture("status --all") == ("added:\n"
361
402
                                                "  test.txt\n")
362
403
 
363
404
        progress("rename newly-added file")
365
406
        assert os.path.exists("hello.txt")
366
407
        assert not os.path.exists("test.txt")
367
408
 
368
 
        assert backtick("bzr revno") == '0\n'
 
409
        assert capture("revno") == '0\n'
369
410
 
370
411
        progress("add first revision")
371
412
        runbzr(['commit', '-m', 'add first revision'])
379
420
        runbzr("add sub1")
380
421
        runbzr("rename sub1 sub2")
381
422
        runbzr("move hello.txt sub2")
382
 
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
423
        assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
383
424
 
384
425
        assert exists("sub2")
385
426
        assert exists("sub2/hello.txt")
400
441
        runbzr(['commit', '-m', 'rename nested subdirectories'])
401
442
 
402
443
        chdir('sub1/sub2')
403
 
        self.assertEquals(backtick('bzr root')[:-1],
 
444
        self.assertEquals(capture('root')[:-1],
404
445
                          os.path.join(self.test_dir, 'branch1'))
405
446
        runbzr('move ../hello.txt .')
406
447
        assert exists('./hello.txt')
407
 
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
408
 
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
448
        self.assertEquals(capture('relpath hello.txt'),
 
449
                          os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
 
450
        assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
409
451
        runbzr(['commit', '-m', 'move to parent directory'])
410
452
        chdir('..')
411
 
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
453
        assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
412
454
 
413
455
        runbzr('move sub2/hello.txt .')
414
456
        assert exists('hello.txt')
423
465
 
424
466
        runbzr('commit -F msg.tmp')
425
467
 
426
 
        assert backtick('bzr revno') == '5\n'
 
468
        assert capture('revno') == '5\n'
427
469
        runbzr('export -r 5 export-5.tmp')
428
470
        runbzr('export export.tmp')
429
471
 
431
473
        runbzr('log -v')
432
474
        runbzr('log -v --forward')
433
475
        runbzr('log -m', retcode=1)
434
 
        log_out = backtick('bzr log -m commit')
 
476
        log_out = capture('log -m commit')
435
477
        assert "this is my new commit" in log_out
436
478
        assert "rename nested" not in log_out
437
479
        assert 'revision-id' not in log_out
438
 
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
480
        assert 'revision-id' in capture('log --show-ids -m commit')
439
481
 
440
482
 
441
483
        progress("file with spaces in name")
458
500
            runbzr('init')
459
501
            os.symlink("NOWHERE1", "link1")
460
502
            runbzr('add link1')
461
 
            assert backtick('bzr unknowns') == ''
 
503
            assert self.capture('unknowns') == ''
462
504
            runbzr(['commit', '-m', '1: added symlink link1'])
463
505
    
464
506
            mkdir('d1')
465
507
            runbzr('add d1')
466
 
            assert backtick('bzr unknowns') == ''
 
508
            assert self.capture('unknowns') == ''
467
509
            os.symlink("NOWHERE2", "d1/link2")
468
 
            assert backtick('bzr unknowns') == 'd1/link2\n'
 
510
            assert self.capture('unknowns') == 'd1/link2\n'
469
511
            # is d1/link2 found when adding d1
470
512
            runbzr('add d1')
471
 
            assert backtick('bzr unknowns') == ''
 
513
            assert self.capture('unknowns') == ''
472
514
            os.symlink("NOWHERE3", "d1/link3")
473
 
            assert backtick('bzr unknowns') == 'd1/link3\n'
 
515
            assert self.capture('unknowns') == 'd1/link3\n'
474
516
            runbzr(['commit', '-m', '2: added dir, symlink'])
475
517
    
476
518
            runbzr('rename d1 d2')
487
529
            os.unlink("d2/link1")
488
530
            os.symlink("TARGET 1", "d2/link1")
489
531
            runbzr('diff')
490
 
            assert backtick("bzr relpath d2/link1") == "d2/link1\n"
 
532
            assert self.capture("relpath d2/link1") == "d2/link1\n"
491
533
            runbzr(['commit', '-m', '4: retarget of two links'])
492
534
    
493
535
            runbzr('remove d2/link1')
494
 
            assert backtick('bzr unknowns') == 'd2/link1\n'
 
536
            assert self.capture('unknowns') == 'd2/link1\n'
495
537
            runbzr(['commit', '--unchanged', '-m', '5: remove d2/link1'])
496
538
            print ("commit --uchanged is needed to delete a file with no other"
497
539
                   " changes. this is a bug.")
499
541
            os.mkdir("d1")
500
542
            runbzr('add d1')
501
543
            runbzr('rename d2/link3 d1/link3new')
502
 
            assert backtick('bzr unknowns') == 'd2/link1\n'
 
544
            assert self.capture('unknowns') == 'd2/link1\n'
503
545
            runbzr(['commit', '-m', '6: remove d2/link1, move/rename link3'])
504
546
            
505
547
            runbzr(['check'])