~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

Change the use of run_bzr to run_bzr_captured in blackbox tests - inspired by David Clymers patch to change run_bzr usage to runbzr

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
29
30
import sys
 
31
import os
 
32
 
30
33
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
 
34
from bzrlib.branch import Branch
 
35
 
31
36
 
32
37
class ExternalBase(TestCaseInTempDir):
33
38
 
34
 
    def runbzr(self, args, retcode=0,backtick=False):
35
 
        try:
36
 
            import shutil
37
 
            from subprocess import call
38
 
        except ImportError, e:
39
 
            _need_subprocess()
40
 
            raise
41
 
 
 
39
    def runbzr(self, args, retcode=0, backtick=False):
42
40
        if isinstance(args, basestring):
43
41
            args = args.split()
44
42
 
45
43
        if backtick:
46
 
            return self.backtick(['python', self.BZRPATH,] + args,
47
 
                           retcode=retcode)
 
44
            return self.run_bzr_captured(args, retcode=retcode)[0]
48
45
        else:
49
 
            return self.runcmd(['python', self.BZRPATH,] + args,
50
 
                           retcode=retcode)
 
46
            return self.run_bzr_captured(args, retcode=retcode)
 
47
 
51
48
 
52
49
class TestCommands(ExternalBase):
53
50
 
59
56
        self.runbzr('commit -h')
60
57
 
61
58
    def test_init_branch(self):
62
 
        import os
63
59
        self.runbzr(['init'])
64
60
 
65
61
    def test_whoami(self):
76
72
        f = file('.bzr/email', 'wt')
77
73
        f.write('Branch Identity <branch@identi.ty>')
78
74
        f.close()
 
75
        bzr_email = os.environ.get('BZREMAIL')
 
76
        if bzr_email is not None:
 
77
            del os.environ['BZREMAIL']
79
78
        whoami = self.runbzr("whoami",backtick=True)
80
79
        whoami_email = self.runbzr("whoami --email",backtick=True)
81
80
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
82
81
        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
83
91
 
84
92
    def test_invalid_commands(self):
85
93
        self.runbzr("pants", retcode=1)
96
104
    def test_ignore_patterns(self):
97
105
        from bzrlib.branch import Branch
98
106
        
99
 
        b = Branch('.', init=True)
 
107
        b = Branch.initialize('.')
100
108
        self.assertEquals(list(b.unknowns()), [])
101
109
 
102
110
        file('foo.tmp', 'wt').write('tmp files are ignored')
103
111
        self.assertEquals(list(b.unknowns()), [])
104
 
        assert self.backtick('bzr unknowns') == ''
 
112
        assert self.capture('unknowns') == ''
105
113
 
106
114
        file('foo.c', 'wt').write('int main() {}')
107
115
        self.assertEquals(list(b.unknowns()), ['foo.c'])
108
 
        assert self.backtick('bzr unknowns') == 'foo.c\n'
 
116
        assert self.capture('unknowns') == 'foo.c\n'
109
117
 
110
118
        self.runbzr(['add', 'foo.c'])
111
 
        assert self.backtick('bzr unknowns') == ''
 
119
        assert self.capture('unknowns') == ''
112
120
 
113
121
        # 'ignore' works when creating the .bzignore file
114
122
        file('foo.blah', 'wt').write('blah')
120
128
        # 'ignore' works when then .bzrignore file already exists
121
129
        file('garh', 'wt').write('garh')
122
130
        self.assertEquals(list(b.unknowns()), ['garh'])
123
 
        assert self.backtick('bzr unknowns') == 'garh\n'
 
131
        assert self.capture('unknowns') == 'garh\n'
124
132
        self.runbzr('ignore garh')
125
133
        self.assertEquals(list(b.unknowns()), [])
126
134
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
127
135
 
128
136
    def test_revert(self):
129
 
        import os
130
137
        self.runbzr('init')
131
138
 
132
139
        file('hello', 'wt').write('foo')
151
158
        os.rmdir('revertdir')
152
159
        self.runbzr('revert')
153
160
 
154
 
    def skipped_test_mv_modes(self):
 
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
    def test_mv_modes(self):
155
169
        """Test two modes of operation for mv"""
156
170
        from bzrlib.branch import Branch
157
 
        b = Branch('.', init=True)
 
171
        b = Branch.initialize('.')
158
172
        self.build_tree(['a', 'c', 'subdir/'])
159
 
        self.run_bzr('mv', 'a', 'b')
160
 
        self.run_bzr('mv', 'b', 'subdir')
161
 
        self.run_bzr('mv', 'subdir/b', 'a')
162
 
        self.run_bzr('mv', 'a', 'b', 'subdir')
163
 
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
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'])
 
179
 
164
180
 
165
181
    def test_main_version(self):
166
182
        """Check output from version command and master option is reasonable"""
186
202
        test.runbzr('add goodbye')
187
203
        test.runbzr('commit -m setup goodbye')
188
204
 
189
 
    def test_revert(self):
190
 
        self.example_branch()
191
 
        file('hello', 'wt').write('bar')
192
 
        file('goodbye', 'wt').write('qux')
193
 
        self.runbzr('revert hello')
194
 
        self.check_file_contents('hello', 'foo')
195
 
        self.check_file_contents('goodbye', 'qux')
196
 
        self.runbzr('revert')
197
 
        self.check_file_contents('goodbye', 'baz')
 
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)
198
222
 
199
223
    def test_merge(self):
200
224
        from bzrlib.branch import Branch
201
 
        from bzrlib.commands import run_bzr
202
 
        import os
203
225
        
204
226
        os.mkdir('a')
205
227
        os.chdir('a')
206
 
 
207
228
        self.example_branch()
208
229
        os.chdir('..')
209
230
        self.runbzr('branch a b')
219
240
        self.runbzr('merge ../b')
220
241
        self.check_file_contents('goodbye', 'quux')
221
242
        # Merging a branch pulls its revision into the tree
222
 
        a = Branch('.')
223
 
        b = Branch('../b')
 
243
        a = Branch.open('.')
 
244
        b = Branch.open('../b')
224
245
        a.get_revision_xml(b.last_patch())
225
 
 
226
246
        self.log('pending merges: %s', a.pending_merges())
227
 
#        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
228
 
#        % (a.pending_merges(), b.last_patch())
 
247
        #        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
 
248
        #        % (a.pending_merges(), b.last_patch())
 
249
 
 
250
    def test_pull(self):
 
251
        """Pull changes from one branch to another."""
 
252
        os.mkdir('a')
 
253
        os.chdir('a')
 
254
 
 
255
        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
        os.chdir('..')
 
265
        self.runbzr('branch a b')
 
266
        os.chdir('b')
 
267
        self.runbzr('pull')
 
268
        self.runbzr('commit -m blah --unchanged')
 
269
        os.chdir('../a')
 
270
        a = Branch.open('.')
 
271
        b = Branch.open('../b')
 
272
        assert a.revision_history() == b.revision_history()[:-1]
 
273
        self.runbzr('pull ../b')
 
274
        assert a.revision_history() == b.revision_history()
 
275
        self.runbzr('commit -m blah2 --unchanged')
 
276
        os.chdir('../b')
 
277
        self.runbzr('commit -m blah3 --unchanged')
 
278
        self.runbzr('pull ../a', retcode=1)
 
279
        os.chdir('../a')
 
280
        self.runbzr('merge ../b')
 
281
        self.runbzr('commit -m blah4 --unchanged')
 
282
        os.chdir('../b')
 
283
        self.runbzr('pull ../a')
 
284
        assert a.revision_history()[-1] == b.revision_history()[-1]
 
285
        
 
286
    def test_add_reports(self):
 
287
        """add command prints the names of added files."""
 
288
        b = Branch.initialize('.')
 
289
        self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
 
290
        out = self.run_bzr_captured(['add'], retcode = 0)[0]
 
291
        # the ordering is not defined at the moment
 
292
        results = sorted(out.rstrip('\n').split('\n'))
 
293
        self.assertEquals(['added dir',
 
294
                           'added dir/sub.txt',
 
295
                           'added top.txt',],
 
296
                          results)
 
297
 
 
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
 
229
306
 
230
307
class OldTests(ExternalBase):
231
308
    """old tests moved from ./testbzr."""
233
310
    def test_bzr(self):
234
311
        from os import chdir, mkdir
235
312
        from os.path import exists
236
 
        import os
237
313
 
238
314
        runbzr = self.runbzr
239
 
        backtick = self.backtick
 
315
        capture = self.capture
240
316
        progress = self.log
241
317
 
242
318
        progress("basic branch creation")
244
320
        chdir('branch1')
245
321
        runbzr('init')
246
322
 
247
 
        self.assertEquals(backtick('bzr root').rstrip(),
 
323
        self.assertEquals(capture('root').rstrip(),
248
324
                          os.path.join(self.test_dir, 'branch1'))
249
325
 
250
326
        progress("status of new file")
253
329
        f.write('hello world!\n')
254
330
        f.close()
255
331
 
256
 
        out = backtick("bzr unknowns")
257
 
        self.assertEquals(out, 'test.txt\n')
 
332
        self.assertEquals(capture('unknowns'), 'test.txt\n')
258
333
 
259
 
        out = backtick("bzr status")
 
334
        out = capture("status")
260
335
        assert out == 'unknown:\n  test.txt\n'
261
336
 
262
 
        out = backtick("bzr status --all")
 
337
        out = capture("status --all")
263
338
        assert out == "unknown:\n  test.txt\n"
264
339
 
265
 
        out = backtick("bzr status test.txt --all")
 
340
        out = capture("status test.txt --all")
266
341
        assert out == "unknown:\n  test.txt\n"
267
342
 
268
343
        f = file('test2.txt', 'wt')
269
344
        f.write('goodbye cruel world...\n')
270
345
        f.close()
271
346
 
272
 
        out = backtick("bzr status test.txt")
 
347
        out = capture("status test.txt")
273
348
        assert out == "unknown:\n  test.txt\n"
274
349
 
275
 
        out = backtick("bzr status")
 
350
        out = capture("status")
276
351
        assert out == ("unknown:\n"
277
352
                       "  test.txt\n"
278
353
                       "  test2.txt\n")
280
355
        os.unlink('test2.txt')
281
356
 
282
357
        progress("command aliases")
283
 
        out = backtick("bzr st --all")
 
358
        out = capture("st --all")
284
359
        assert out == ("unknown:\n"
285
360
                       "  test.txt\n")
286
361
 
287
 
        out = backtick("bzr stat")
 
362
        out = capture("stat")
288
363
        assert out == ("unknown:\n"
289
364
                       "  test.txt\n")
290
365
 
294
369
        runbzr("help commands")
295
370
        runbzr("help slartibartfast", 1)
296
371
 
297
 
        out = backtick("bzr help ci")
 
372
        out = capture("help ci")
298
373
        out.index('aliases: ')
299
374
 
300
375
        progress("can't rename unversioned file")
303
378
        progress("adding a file")
304
379
 
305
380
        runbzr("add test.txt")
306
 
        assert backtick("bzr unknowns") == ''
307
 
        assert backtick("bzr status --all") == ("added:\n"
 
381
        assert capture("unknowns") == ''
 
382
        assert capture("status --all") == ("added:\n"
308
383
                                                "  test.txt\n")
309
384
 
310
385
        progress("rename newly-added file")
312
387
        assert os.path.exists("hello.txt")
313
388
        assert not os.path.exists("test.txt")
314
389
 
315
 
        assert backtick("bzr revno") == '0\n'
 
390
        assert capture("revno") == '0\n'
316
391
 
317
392
        progress("add first revision")
318
393
        runbzr(['commit', '-m', 'add first revision'])
326
401
        runbzr("add sub1")
327
402
        runbzr("rename sub1 sub2")
328
403
        runbzr("move hello.txt sub2")
329
 
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
404
        assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
330
405
 
331
406
        assert exists("sub2")
332
407
        assert exists("sub2/hello.txt")
347
422
        runbzr(['commit', '-m', 'rename nested subdirectories'])
348
423
 
349
424
        chdir('sub1/sub2')
350
 
        self.assertEquals(backtick('bzr root')[:-1],
 
425
        self.assertEquals(capture('root')[:-1],
351
426
                          os.path.join(self.test_dir, 'branch1'))
352
427
        runbzr('move ../hello.txt .')
353
428
        assert exists('./hello.txt')
354
 
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
355
 
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
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')
356
432
        runbzr(['commit', '-m', 'move to parent directory'])
357
433
        chdir('..')
358
 
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
434
        assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
359
435
 
360
436
        runbzr('move sub2/hello.txt .')
361
437
        assert exists('hello.txt')
370
446
 
371
447
        runbzr('commit -F msg.tmp')
372
448
 
373
 
        assert backtick('bzr revno') == '5\n'
 
449
        assert capture('revno') == '5\n'
374
450
        runbzr('export -r 5 export-5.tmp')
375
451
        runbzr('export export.tmp')
376
452
 
378
454
        runbzr('log -v')
379
455
        runbzr('log -v --forward')
380
456
        runbzr('log -m', retcode=1)
381
 
        log_out = backtick('bzr log -m commit')
 
457
        log_out = capture('log -m commit')
382
458
        assert "this is my new commit" in log_out
383
459
        assert "rename nested" not in log_out
384
460
        assert 'revision-id' not in log_out
385
 
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
461
        assert 'revision-id' in capture('log --show-ids -m commit')
386
462
 
387
463
 
388
464
        progress("file with spaces in name")