~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:37:53 UTC
  • mfrom: (1092.3.4)
  • mto: This revision was merged to the branch mainline in revision 1397.
  • Revision ID: robertc@robertcollins.net-20050928053753-68e6e4c0642eccea
merge from symlink branch

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'
125
 
 
126
 
    def skipped_test_mv_modes(self):
 
135
        assert file('.bzrignore', 'rU').read() == '*.blah\ngarh\n'
 
136
 
 
137
    def test_revert(self):
 
138
        self.runbzr('init')
 
139
 
 
140
        file('hello', 'wt').write('foo')
 
141
        self.runbzr('add hello')
 
142
        self.runbzr('commit -m setup hello')
 
143
 
 
144
        file('goodbye', 'wt').write('baz')
 
145
        self.runbzr('add goodbye')
 
146
        self.runbzr('commit -m setup goodbye')
 
147
 
 
148
        file('hello', 'wt').write('bar')
 
149
        file('goodbye', 'wt').write('qux')
 
150
        self.runbzr('revert hello')
 
151
        self.check_file_contents('hello', 'foo')
 
152
        self.check_file_contents('goodbye', 'qux')
 
153
        self.runbzr('revert')
 
154
        self.check_file_contents('goodbye', 'baz')
 
155
 
 
156
        os.mkdir('revertdir')
 
157
        self.runbzr('add revertdir')
 
158
        self.runbzr('commit -m f')
 
159
        os.rmdir('revertdir')
 
160
        self.runbzr('revert')
 
161
 
 
162
        os.symlink('/unlikely/to/exist', 'symlink')
 
163
        self.runbzr('add symlink')
 
164
        self.runbzr('commit -m f')
 
165
        os.unlink('symlink')
 
166
        self.runbzr('revert')
 
167
        
 
168
        file('hello', 'wt').write('xyz')
 
169
        self.runbzr('commit -m xyz hello')
 
170
        self.runbzr('revert -r 1 hello')
 
171
        self.check_file_contents('hello', 'foo')
 
172
        self.runbzr('revert hello')
 
173
        self.check_file_contents('hello', 'xyz')
 
174
        os.chdir('revertdir')
 
175
        self.runbzr('revert')
 
176
        os.chdir('..')
 
177
 
 
178
 
 
179
    def test_mv_modes(self):
127
180
        """Test two modes of operation for mv"""
128
181
        from bzrlib.branch import Branch
129
 
        b = Branch('.', init=True)
 
182
        b = Branch.initialize('.')
130
183
        self.build_tree(['a', 'c', 'subdir/'])
131
 
        self.run_bzr('mv', 'a', 'b')
132
 
        self.run_bzr('mv', 'b', 'subdir')
133
 
        self.run_bzr('mv', 'subdir/b', 'a')
134
 
        self.run_bzr('mv', 'a', 'b', 'subdir')
135
 
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
184
        self.run_bzr_captured(['add', self.test_dir])
 
185
        self.run_bzr_captured(['mv', 'a', 'b'])
 
186
        self.run_bzr_captured(['mv', 'b', 'subdir'])
 
187
        self.run_bzr_captured(['mv', 'subdir/b', 'a'])
 
188
        self.run_bzr_captured(['mv', 'a', 'c', 'subdir'])
 
189
        self.run_bzr_captured(['mv', 'subdir/a', 'subdir/newa'])
 
190
 
136
191
 
137
192
    def test_main_version(self):
138
193
        """Check output from version command and master option is reasonable"""
158
213
        test.runbzr('add goodbye')
159
214
        test.runbzr('commit -m setup goodbye')
160
215
 
161
 
    def test_revert(self):
162
 
        self.example_branch()
163
 
        file('hello', 'wt').write('bar')
164
 
        file('goodbye', 'wt').write('qux')
165
 
        self.runbzr('revert hello')
166
 
        self.check_file_contents('hello', 'foo')
167
 
        self.check_file_contents('goodbye', 'qux')
168
 
        self.runbzr('revert')
169
 
        self.check_file_contents('goodbye', 'baz')
170
 
        os.mkdir('revertdir')
171
 
        self.runbzr('add revertdir')
172
 
        self.runbzr('commit -m f')
173
 
        os.rmdir('revertdir')
174
 
        self.runbzr('revert')
175
 
        os.symlink('/unlikely/to/exist', 'symlink')
176
 
        self.runbzr('add symlink')
177
 
        self.runbzr('commit -m f')
178
 
        os.unlink('symlink')
179
 
        self.runbzr('revert')
 
216
    def test_diff(self):
 
217
        self.example_branch()
 
218
        file('hello', 'wt').write('hello world!')
 
219
        self.runbzr('commit -m fixing hello')
 
220
        output = self.runbzr('diff -r 2..3', backtick=1)
 
221
        self.assert_('\n+hello world!' in output)
 
222
        output = self.runbzr('diff -r last:3..last:1', backtick=1)
 
223
        self.assert_('\n+baz' in output)
 
224
 
 
225
    def test_branch(self):
 
226
        """Branch from one branch to another."""
 
227
        os.mkdir('a')
 
228
        os.chdir('a')
 
229
        self.example_branch()
 
230
        os.chdir('..')
 
231
        self.runbzr('branch a b')
 
232
        self.runbzr('branch a c -r 1')
180
233
 
181
234
    def test_merge(self):
182
235
        from bzrlib.branch import Branch
198
251
        self.runbzr('merge ../b')
199
252
        self.check_file_contents('goodbye', 'quux')
200
253
        # Merging a branch pulls its revision into the tree
201
 
        a = Branch('.')
202
 
        b = Branch('../b')
 
254
        a = Branch.open('.')
 
255
        b = Branch.open('../b')
203
256
        a.get_revision_xml(b.last_patch())
204
257
        self.log('pending merges: %s', a.pending_merges())
205
258
        #        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
211
264
        os.chdir('a')
212
265
 
213
266
        self.example_branch()
 
267
        self.runbzr('pull', retcode=1)
 
268
        self.runbzr('missing', retcode=1)
 
269
        self.runbzr('missing .')
 
270
        self.runbzr('missing')
 
271
        self.runbzr('pull')
 
272
        self.runbzr('pull /', retcode=1)
 
273
        self.runbzr('pull')
 
274
 
214
275
        os.chdir('..')
215
276
        self.runbzr('branch a b')
216
277
        os.chdir('b')
 
278
        self.runbzr('pull')
 
279
        os.mkdir('subdir')
 
280
        self.runbzr('add subdir')
217
281
        self.runbzr('commit -m blah --unchanged')
218
282
        os.chdir('../a')
219
 
        a = Branch('.')
220
 
        b = Branch('../b')
 
283
        a = Branch.open('.')
 
284
        b = Branch.open('../b')
221
285
        assert a.revision_history() == b.revision_history()[:-1]
222
286
        self.runbzr('pull ../b')
223
287
        assert a.revision_history() == b.revision_history()
228
292
        os.chdir('../a')
229
293
        self.runbzr('merge ../b')
230
294
        self.runbzr('commit -m blah4 --unchanged')
231
 
        os.chdir('../b')
232
 
        self.runbzr('pull ../a')
 
295
        os.chdir('../b/subdir')
 
296
        self.runbzr('pull ../../a')
233
297
        assert a.revision_history()[-1] == b.revision_history()[-1]
234
298
        
235
299
    def test_add_reports(self):
236
300
        """add command prints the names of added files."""
237
 
        b = Branch('.', init=True)
 
301
        b = Branch.initialize('.')
238
302
        self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
239
 
 
240
 
        from cStringIO import StringIO
241
 
        out = StringIO()
242
 
 
243
 
        ret = self.apply_redirected(None, out, None,
244
 
                                    run_bzr,
245
 
                                    ['add'])
246
 
        self.assertEquals(ret, 0)
247
 
 
 
303
        out = self.run_bzr_captured(['add'], retcode = 0)[0]
248
304
        # the ordering is not defined at the moment
249
 
        results = sorted(out.getvalue().rstrip('\n').split('\n'))
 
305
        results = sorted(out.rstrip('\n').split('\n'))
250
306
        self.assertEquals(['added dir',
251
 
                           'added dir/sub.txt',
 
307
                           'added dir'+os.sep+'sub.txt',
252
308
                           'added top.txt',],
253
309
                          results)
254
310
 
 
311
    def test_unknown_command(self):
 
312
        """Handling of unknown command."""
 
313
        out, err = self.run_bzr_captured(['fluffy-badger'],
 
314
                                         retcode=1)
 
315
        self.assertEquals(out, '')
 
316
        err.index('unknown command')
 
317
        
 
318
 
255
319
 
256
320
def has_symlinks():
257
321
    if hasattr(os, 'symlink'):
273
337
        from os.path import exists
274
338
 
275
339
        runbzr = self.runbzr
276
 
        backtick = self.backtick
 
340
        capture = self.capture
277
341
        progress = self.log
278
342
 
279
343
        progress("basic branch creation")
281
345
        chdir('branch1')
282
346
        runbzr('init')
283
347
 
284
 
        self.assertEquals(backtick('bzr root').rstrip(),
 
348
        self.assertEquals(capture('root').rstrip(),
285
349
                          os.path.join(self.test_dir, 'branch1'))
286
350
 
287
351
        progress("status of new file")
290
354
        f.write('hello world!\n')
291
355
        f.close()
292
356
 
293
 
        out = backtick("bzr unknowns")
294
 
        self.assertEquals(out, 'test.txt\n')
 
357
        self.assertEquals(capture('unknowns'), 'test.txt\n')
295
358
 
296
 
        out = backtick("bzr status")
 
359
        out = capture("status")
297
360
        assert out == 'unknown:\n  test.txt\n'
298
361
 
299
 
        out = backtick("bzr status --all")
 
362
        out = capture("status --all")
300
363
        assert out == "unknown:\n  test.txt\n"
301
364
 
302
 
        out = backtick("bzr status test.txt --all")
 
365
        out = capture("status test.txt --all")
303
366
        assert out == "unknown:\n  test.txt\n"
304
367
 
305
368
        f = file('test2.txt', 'wt')
306
369
        f.write('goodbye cruel world...\n')
307
370
        f.close()
308
371
 
309
 
        out = backtick("bzr status test.txt")
 
372
        out = capture("status test.txt")
310
373
        assert out == "unknown:\n  test.txt\n"
311
374
 
312
 
        out = backtick("bzr status")
 
375
        out = capture("status")
313
376
        assert out == ("unknown:\n"
314
377
                       "  test.txt\n"
315
378
                       "  test2.txt\n")
317
380
        os.unlink('test2.txt')
318
381
 
319
382
        progress("command aliases")
320
 
        out = backtick("bzr st --all")
 
383
        out = capture("st --all")
321
384
        assert out == ("unknown:\n"
322
385
                       "  test.txt\n")
323
386
 
324
 
        out = backtick("bzr stat")
 
387
        out = capture("stat")
325
388
        assert out == ("unknown:\n"
326
389
                       "  test.txt\n")
327
390
 
331
394
        runbzr("help commands")
332
395
        runbzr("help slartibartfast", 1)
333
396
 
334
 
        out = backtick("bzr help ci")
 
397
        out = capture("help ci")
335
398
        out.index('aliases: ')
336
399
 
337
400
        progress("can't rename unversioned file")
340
403
        progress("adding a file")
341
404
 
342
405
        runbzr("add test.txt")
343
 
        assert backtick("bzr unknowns") == ''
344
 
        assert backtick("bzr status --all") == ("added:\n"
 
406
        assert capture("unknowns") == ''
 
407
        assert capture("status --all") == ("added:\n"
345
408
                                                "  test.txt\n")
346
409
 
347
410
        progress("rename newly-added file")
349
412
        assert os.path.exists("hello.txt")
350
413
        assert not os.path.exists("test.txt")
351
414
 
352
 
        assert backtick("bzr revno") == '0\n'
 
415
        assert capture("revno") == '0\n'
353
416
 
354
417
        progress("add first revision")
355
418
        runbzr(['commit', '-m', 'add first revision'])
363
426
        runbzr("add sub1")
364
427
        runbzr("rename sub1 sub2")
365
428
        runbzr("move hello.txt sub2")
366
 
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
429
        assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
367
430
 
368
431
        assert exists("sub2")
369
432
        assert exists("sub2/hello.txt")
384
447
        runbzr(['commit', '-m', 'rename nested subdirectories'])
385
448
 
386
449
        chdir('sub1/sub2')
387
 
        self.assertEquals(backtick('bzr root')[:-1],
 
450
        self.assertEquals(capture('root')[:-1],
388
451
                          os.path.join(self.test_dir, 'branch1'))
389
452
        runbzr('move ../hello.txt .')
390
453
        assert exists('./hello.txt')
391
 
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
392
 
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
454
        self.assertEquals(capture('relpath hello.txt'),
 
455
                          os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
 
456
        assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
393
457
        runbzr(['commit', '-m', 'move to parent directory'])
394
458
        chdir('..')
395
 
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
459
        assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
396
460
 
397
461
        runbzr('move sub2/hello.txt .')
398
462
        assert exists('hello.txt')
407
471
 
408
472
        runbzr('commit -F msg.tmp')
409
473
 
410
 
        assert backtick('bzr revno') == '5\n'
 
474
        assert capture('revno') == '5\n'
411
475
        runbzr('export -r 5 export-5.tmp')
412
476
        runbzr('export export.tmp')
413
477
 
415
479
        runbzr('log -v')
416
480
        runbzr('log -v --forward')
417
481
        runbzr('log -m', retcode=1)
418
 
        log_out = backtick('bzr log -m commit')
 
482
        log_out = capture('log -m commit')
419
483
        assert "this is my new commit" in log_out
420
484
        assert "rename nested" not in log_out
421
485
        assert 'revision-id' not in log_out
422
 
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
486
        assert 'revision-id' in capture('log --show-ids -m commit')
423
487
 
424
488
 
425
489
        progress("file with spaces in name")
442
506
            runbzr('init')
443
507
            os.symlink("NOWHERE1", "link1")
444
508
            runbzr('add link1')
445
 
            assert backtick('bzr unknowns') == ''
 
509
            assert self.capture('unknowns') == ''
446
510
            runbzr(['commit', '-m', '1: added symlink link1'])
447
511
    
448
512
            mkdir('d1')
449
513
            runbzr('add d1')
450
 
            assert backtick('bzr unknowns') == ''
 
514
            assert self.capture('unknowns') == ''
451
515
            os.symlink("NOWHERE2", "d1/link2")
452
 
            assert backtick('bzr unknowns') == 'd1/link2\n'
 
516
            assert self.capture('unknowns') == 'd1/link2\n'
453
517
            # is d1/link2 found when adding d1
454
518
            runbzr('add d1')
455
 
            assert backtick('bzr unknowns') == ''
 
519
            assert self.capture('unknowns') == ''
456
520
            os.symlink("NOWHERE3", "d1/link3")
457
 
            assert backtick('bzr unknowns') == 'd1/link3\n'
 
521
            assert self.capture('unknowns') == 'd1/link3\n'
458
522
            runbzr(['commit', '-m', '2: added dir, symlink'])
459
523
    
460
524
            runbzr('rename d1 d2')
471
535
            os.unlink("d2/link1")
472
536
            os.symlink("TARGET 1", "d2/link1")
473
537
            runbzr('diff')
474
 
            assert backtick("bzr relpath d2/link1") == "d2/link1\n"
 
538
            assert self.capture("relpath d2/link1") == "d2/link1\n"
475
539
            runbzr(['commit', '-m', '4: retarget of two links'])
476
540
    
477
541
            runbzr('remove d2/link1')
478
 
            assert backtick('bzr unknowns') == 'd2/link1\n'
 
542
            assert self.capture('unknowns') == 'd2/link1\n'
479
543
            runbzr(['commit', '--unchanged', '-m', '5: remove d2/link1'])
480
544
            print ("commit --uchanged is needed to delete a file with no other"
481
545
                   " changes. this is a bug.")
483
547
            os.mkdir("d1")
484
548
            runbzr('add d1')
485
549
            runbzr('rename d2/link3 d1/link3new')
486
 
            assert backtick('bzr unknowns') == 'd2/link1\n'
 
550
            assert self.capture('unknowns') == 'd2/link1\n'
487
551
            runbzr(['commit', '-m', '6: remove d2/link1, move/rename link3'])
488
552
            
489
553
            runbzr(['check'])