/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

merge merge tweaks from aaron, which includes latest .dev

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
30
 
 
31
 
from bzrlib.selftest import TestBase, InTempDir, BzrTestBase
32
 
 
33
 
 
34
 
 
35
 
class ExternalBase(InTempDir):
36
 
    def runbzr(self, args, retcode=0):
37
 
        try:
38
 
            import shutil
39
 
            from subprocess import call
40
 
        except ImportError, e:
41
 
            _need_subprocess()
42
 
            raise
43
 
 
 
31
import os
 
32
 
 
33
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
 
34
from bzrlib.branch import Branch
 
35
from bzrlib.commands import run_bzr
 
36
 
 
37
 
 
38
class ExternalBase(TestCaseInTempDir):
 
39
    def runbzr(self, args, retcode=0, backtick=False):
44
40
        if isinstance(args, basestring):
45
41
            args = args.split()
46
 
            
47
 
        return self.runcmd(['python', self.BZRPATH,] + args,
48
 
                           retcode=retcode)
49
 
 
50
 
 
51
 
 
52
 
class MvCommand(BzrTestBase):
53
 
    def runbzr(self):
54
 
        """Test two modes of operation for mv"""
55
 
        b = Branch('.', init=True)
56
 
        self.build_tree(['a', 'c', 'subdir/'])
57
 
        self.run_bzr('mv', 'a', 'b')
58
 
        self.run_bzr('mv', 'b', 'subdir')
59
 
        self.run_bzr('mv', 'subdir/b', 'a')
60
 
        self.run_bzr('mv', 'a', 'b', 'subdir')
61
 
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
62
 
 
63
 
 
64
 
 
65
 
class TestVersion(BzrTestBase):
66
 
    """Check output from version command and master option is reasonable"""
67
 
    def runTest(self):
68
 
        # output is intentionally passed through to stdout so that we
69
 
        # can see the version being tested
70
 
        from cStringIO import StringIO
71
 
        save_out = sys.stdout
72
 
        try:
73
 
            sys.stdout = tmp_out = StringIO()
74
 
            
75
 
            self.run_bzr('version')
76
 
        finally:
77
 
            sys.stdout = save_out
78
 
 
79
 
        output = tmp_out.getvalue()
80
 
        self.log('bzr version output:')
81
 
        self.log(output)
82
 
        
83
 
        self.assert_(output.startswith('bzr (bazaar-ng) '))
84
 
        self.assertNotEqual(output.index('Canonical'), -1)
85
 
 
86
 
        # make sure --version is consistent
87
 
        try:
88
 
            sys.stdout = tmp_out = StringIO()
89
 
            
90
 
            self.run_bzr('--version')
91
 
        finally:
92
 
            sys.stdout = save_out
93
 
 
94
 
        self.log('bzr --version output:')
95
 
        self.log(tmp_out.getvalue())
96
 
 
97
 
        self.assertEquals(output, tmp_out.getvalue())
98
 
 
99
 
 
100
 
        
101
 
 
102
 
 
103
 
class HelpCommands(ExternalBase):
104
 
    def runTest(self):
 
42
 
 
43
        if backtick:
 
44
            return self.run_bzr_captured(args, retcode=retcode)[0]
 
45
        else:
 
46
            return self.run_bzr_captured(args, retcode=retcode)
 
47
 
 
48
 
 
49
class TestCommands(ExternalBase):
 
50
 
 
51
    def test_help_commands(self):
105
52
        self.runbzr('--help')
106
53
        self.runbzr('help')
107
54
        self.runbzr('help commands')
108
55
        self.runbzr('help help')
109
56
        self.runbzr('commit -h')
110
57
 
111
 
 
112
 
class InitBranch(ExternalBase):
113
 
    def runTest(self):
114
 
        import os
 
58
    def test_init_branch(self):
115
59
        self.runbzr(['init'])
116
60
 
117
 
 
118
 
 
119
 
class UserIdentity(ExternalBase):
120
 
    def runTest(self):
 
61
    def test_whoami(self):
121
62
        # this should always identify something, if only "john@localhost"
122
63
        self.runbzr("whoami")
123
64
        self.runbzr("whoami --email")
124
 
        self.assertEquals(self.backtick("bzr whoami --email").count('@'),
125
 
                          1)
126
 
 
127
 
 
128
 
class InvalidCommands(ExternalBase):
129
 
    def runTest(self):
 
65
 
 
66
        self.assertEquals(self.runbzr("whoami --email",
 
67
                                      backtick=True).count('@'), 1)
 
68
        
 
69
    def test_whoami_branch(self):
 
70
        """branch specific user identity works."""
 
71
        self.runbzr('init')
 
72
        f = file('.bzr/email', 'wt')
 
73
        f.write('Branch Identity <branch@identi.ty>')
 
74
        f.close()
 
75
        bzr_email = os.environ.get('BZREMAIL')
 
76
        if bzr_email is not None:
 
77
            del os.environ['BZREMAIL']
 
78
        whoami = self.runbzr("whoami",backtick=True)
 
79
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
80
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
 
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
 
91
 
 
92
    def test_invalid_commands(self):
130
93
        self.runbzr("pants", retcode=1)
131
94
        self.runbzr("--pants off", retcode=1)
132
95
        self.runbzr("diff --message foo", retcode=1)
133
96
 
134
 
 
135
 
 
136
 
class EmptyCommit(ExternalBase):
137
 
    def runTest(self):
 
97
    def test_empty_commit(self):
138
98
        self.runbzr("init")
139
99
        self.build_tree(['hello.txt'])
140
100
        self.runbzr("commit -m empty", retcode=1)
141
101
        self.runbzr("add hello.txt")
142
102
        self.runbzr("commit -m added")
143
103
 
144
 
 
145
 
 
146
 
class IgnorePatterns(ExternalBase):
147
 
    def runTest(self):
 
104
    def test_ignore_patterns(self):
148
105
        from bzrlib.branch import Branch
149
106
        
150
 
        b = Branch('.', init=True)
 
107
        b = Branch.initialize('.')
151
108
        self.assertEquals(list(b.unknowns()), [])
152
109
 
153
110
        file('foo.tmp', 'wt').write('tmp files are ignored')
154
111
        self.assertEquals(list(b.unknowns()), [])
155
 
        assert self.backtick('bzr unknowns') == ''
 
112
        assert self.capture('unknowns') == ''
156
113
 
157
114
        file('foo.c', 'wt').write('int main() {}')
158
115
        self.assertEquals(list(b.unknowns()), ['foo.c'])
159
 
        assert self.backtick('bzr unknowns') == 'foo.c\n'
 
116
        assert self.capture('unknowns') == 'foo.c\n'
160
117
 
161
118
        self.runbzr(['add', 'foo.c'])
162
 
        assert self.backtick('bzr unknowns') == ''
 
119
        assert self.capture('unknowns') == ''
163
120
 
164
121
        # 'ignore' works when creating the .bzignore file
165
122
        file('foo.blah', 'wt').write('blah')
171
128
        # 'ignore' works when then .bzrignore file already exists
172
129
        file('garh', 'wt').write('garh')
173
130
        self.assertEquals(list(b.unknowns()), ['garh'])
174
 
        assert self.backtick('bzr unknowns') == 'garh\n'
 
131
        assert self.capture('unknowns') == 'garh\n'
175
132
        self.runbzr('ignore garh')
176
133
        self.assertEquals(list(b.unknowns()), [])
177
134
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
178
 
        
179
 
 
 
135
 
 
136
    def test_revert(self):
 
137
        self.runbzr('init')
 
138
 
 
139
        file('hello', 'wt').write('foo')
 
140
        self.runbzr('add hello')
 
141
        self.runbzr('commit -m setup hello')
 
142
 
 
143
        file('goodbye', 'wt').write('baz')
 
144
        self.runbzr('add goodbye')
 
145
        self.runbzr('commit -m setup goodbye')
 
146
        
 
147
        file('hello', 'wt').write('bar')
 
148
        file('goodbye', 'wt').write('qux')
 
149
        self.runbzr('revert hello')
 
150
        self.check_file_contents('hello', 'foo')
 
151
        self.check_file_contents('goodbye', 'qux')
 
152
        self.runbzr('revert')
 
153
        self.check_file_contents('goodbye', 'baz')
 
154
 
 
155
        os.mkdir('revertdir')
 
156
        self.runbzr('add revertdir')
 
157
        self.runbzr('commit -m f')
 
158
        os.rmdir('revertdir')
 
159
        self.runbzr('revert')
 
160
 
 
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):
 
169
        """Test two modes of operation for mv"""
 
170
        from bzrlib.branch import Branch
 
171
        b = Branch.initialize('.')
 
172
        self.build_tree(['a', 'c', 'subdir/'])
 
173
        self.run_bzr('add', self.test_dir)
 
174
        self.run_bzr('mv', 'a', 'b')
 
175
        self.run_bzr('mv', 'b', 'subdir')
 
176
        self.run_bzr('mv', 'subdir/b', 'a')
 
177
        self.run_bzr('mv', 'a', 'c', 'subdir')
 
178
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
179
 
 
180
 
 
181
    def test_main_version(self):
 
182
        """Check output from version command and master option is reasonable"""
 
183
        # output is intentionally passed through to stdout so that we
 
184
        # can see the version being tested
 
185
        output = self.runbzr('version', backtick=1)
 
186
        self.log('bzr version output:')
 
187
        self.log(output)
 
188
        self.assert_(output.startswith('bzr (bazaar-ng) '))
 
189
        self.assertNotEqual(output.index('Canonical'), -1)
 
190
        # make sure --version is consistent
 
191
        tmp_output = self.runbzr('--version', backtick=1)
 
192
        self.log('bzr --version output:')
 
193
        self.log(tmp_output)
 
194
        self.assertEquals(output, tmp_output)
 
195
 
 
196
    def example_branch(test):
 
197
        test.runbzr('init')
 
198
        file('hello', 'wt').write('foo')
 
199
        test.runbzr('add hello')
 
200
        test.runbzr('commit -m setup hello')
 
201
        file('goodbye', 'wt').write('baz')
 
202
        test.runbzr('add goodbye')
 
203
        test.runbzr('commit -m setup goodbye')
 
204
 
 
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)
 
222
 
 
223
    def test_merge(self):
 
224
        from bzrlib.branch import Branch
 
225
        
 
226
        os.mkdir('a')
 
227
        os.chdir('a')
 
228
        self.example_branch()
 
229
        os.chdir('..')
 
230
        self.runbzr('branch a b')
 
231
        os.chdir('b')
 
232
        file('goodbye', 'wt').write('quux')
 
233
        self.runbzr(['commit',  '-m',  "more u's are always good"])
 
234
 
 
235
        os.chdir('../a')
 
236
        file('hello', 'wt').write('quuux')
 
237
        # We can't merge when there are in-tree changes
 
238
        self.runbzr('merge ../b', retcode=1)
 
239
        self.runbzr(['commit', '-m', "Like an epidemic of u's"])
 
240
        self.runbzr('merge ../b')
 
241
        self.check_file_contents('goodbye', 'quux')
 
242
        # Merging a branch pulls its revision into the tree
 
243
        a = Branch.open('.')
 
244
        b = Branch.open('../b')
 
245
        a.get_revision_xml(b.last_patch())
 
246
        self.log('pending merges: %s', a.pending_merges())
 
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 = StringIO()
 
291
        ret = self.apply_redirected(None, out, None,
 
292
                                    run_bzr,
 
293
                                    ['add'])
 
294
        self.assertEquals(ret, 0)
 
295
        # the ordering is not defined at the moment
 
296
        results = sorted(out.getvalue().rstrip('\n').split('\n'))
 
297
        self.assertEquals(['added dir',
 
298
                           'added dir/sub.txt',
 
299
                           'added top.txt',],
 
300
                          results)
 
301
 
 
302
    def test_unknown_command(self):
 
303
        """Handling of unknown command."""
 
304
        out, err = self.run_bzr_captured(['fluffy-badger'],
 
305
                                         retcode=1)
 
306
        self.assertEquals(out, '')
 
307
        err.index('unknown command')
 
308
        
180
309
 
181
310
 
182
311
class OldTests(ExternalBase):
183
 
    # old tests moved from ./testbzr
184
 
    def runTest(self):
 
312
    """old tests moved from ./testbzr."""
 
313
 
 
314
    def test_bzr(self):
185
315
        from os import chdir, mkdir
186
316
        from os.path import exists
187
 
        import os
188
317
 
189
318
        runbzr = self.runbzr
190
 
        backtick = self.backtick
 
319
        capture = self.capture
191
320
        progress = self.log
192
321
 
193
322
        progress("basic branch creation")
195
324
        chdir('branch1')
196
325
        runbzr('init')
197
326
 
198
 
        self.assertEquals(backtick('bzr root').rstrip(),
 
327
        self.assertEquals(capture('root').rstrip(),
199
328
                          os.path.join(self.test_dir, 'branch1'))
200
329
 
201
330
        progress("status of new file")
204
333
        f.write('hello world!\n')
205
334
        f.close()
206
335
 
207
 
        out = backtick("bzr unknowns")
208
 
        self.assertEquals(out, 'test.txt\n')
 
336
        self.assertEquals(capture('unknowns'), 'test.txt\n')
209
337
 
210
 
        out = backtick("bzr status")
 
338
        out = capture("status")
211
339
        assert out == 'unknown:\n  test.txt\n'
212
340
 
213
 
        out = backtick("bzr status --all")
 
341
        out = capture("status --all")
214
342
        assert out == "unknown:\n  test.txt\n"
215
343
 
216
 
        out = backtick("bzr status test.txt --all")
 
344
        out = capture("status test.txt --all")
217
345
        assert out == "unknown:\n  test.txt\n"
218
346
 
219
347
        f = file('test2.txt', 'wt')
220
348
        f.write('goodbye cruel world...\n')
221
349
        f.close()
222
350
 
223
 
        out = backtick("bzr status test.txt")
 
351
        out = capture("status test.txt")
224
352
        assert out == "unknown:\n  test.txt\n"
225
353
 
226
 
        out = backtick("bzr status")
 
354
        out = capture("status")
227
355
        assert out == ("unknown:\n"
228
356
                       "  test.txt\n"
229
357
                       "  test2.txt\n")
231
359
        os.unlink('test2.txt')
232
360
 
233
361
        progress("command aliases")
234
 
        out = backtick("bzr st --all")
 
362
        out = capture("st --all")
235
363
        assert out == ("unknown:\n"
236
364
                       "  test.txt\n")
237
365
 
238
 
        out = backtick("bzr stat")
 
366
        out = capture("stat")
239
367
        assert out == ("unknown:\n"
240
368
                       "  test.txt\n")
241
369
 
245
373
        runbzr("help commands")
246
374
        runbzr("help slartibartfast", 1)
247
375
 
248
 
        out = backtick("bzr help ci")
 
376
        out = capture("help ci")
249
377
        out.index('aliases: ')
250
378
 
251
379
        progress("can't rename unversioned file")
254
382
        progress("adding a file")
255
383
 
256
384
        runbzr("add test.txt")
257
 
        assert backtick("bzr unknowns") == ''
258
 
        assert backtick("bzr status --all") == ("added:\n"
 
385
        assert capture("unknowns") == ''
 
386
        assert capture("status --all") == ("added:\n"
259
387
                                                "  test.txt\n")
260
388
 
261
389
        progress("rename newly-added file")
263
391
        assert os.path.exists("hello.txt")
264
392
        assert not os.path.exists("test.txt")
265
393
 
266
 
        assert backtick("bzr revno") == '0\n'
 
394
        assert capture("revno") == '0\n'
267
395
 
268
396
        progress("add first revision")
269
397
        runbzr(['commit', '-m', 'add first revision'])
277
405
        runbzr("add sub1")
278
406
        runbzr("rename sub1 sub2")
279
407
        runbzr("move hello.txt sub2")
280
 
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
408
        assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
281
409
 
282
410
        assert exists("sub2")
283
411
        assert exists("sub2/hello.txt")
298
426
        runbzr(['commit', '-m', 'rename nested subdirectories'])
299
427
 
300
428
        chdir('sub1/sub2')
301
 
        self.assertEquals(backtick('bzr root')[:-1],
 
429
        self.assertEquals(capture('root')[:-1],
302
430
                          os.path.join(self.test_dir, 'branch1'))
303
431
        runbzr('move ../hello.txt .')
304
432
        assert exists('./hello.txt')
305
 
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
306
 
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
433
        self.assertEquals(capture('relpath hello.txt'),
 
434
                          os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
 
435
        assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
307
436
        runbzr(['commit', '-m', 'move to parent directory'])
308
437
        chdir('..')
309
 
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
438
        assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
310
439
 
311
440
        runbzr('move sub2/hello.txt .')
312
441
        assert exists('hello.txt')
321
450
 
322
451
        runbzr('commit -F msg.tmp')
323
452
 
324
 
        assert backtick('bzr revno') == '5\n'
 
453
        assert capture('revno') == '5\n'
325
454
        runbzr('export -r 5 export-5.tmp')
326
455
        runbzr('export export.tmp')
327
456
 
329
458
        runbzr('log -v')
330
459
        runbzr('log -v --forward')
331
460
        runbzr('log -m', retcode=1)
332
 
        log_out = backtick('bzr log -m commit')
 
461
        log_out = capture('log -m commit')
333
462
        assert "this is my new commit" in log_out
334
463
        assert "rename nested" not in log_out
335
464
        assert 'revision-id' not in log_out
336
 
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
465
        assert 'revision-id' in capture('log --show-ids -m commit')
337
466
 
338
467
 
339
468
        progress("file with spaces in name")
349
478
 
350
479
        runbzr('info')
351
480
 
352
 
 
353
 
 
354
 
 
355
 
 
356
 
 
357
 
class RevertCommand(ExternalBase):
358
 
    def runTest(self):
359
 
        self.runbzr('init')
360
 
 
361
 
        file('hello', 'wt').write('foo')
362
 
        self.runbzr('add hello')
363
 
        self.runbzr('commit -m setup hello')
364
 
        
365
 
        file('hello', 'wt').write('bar')
366
 
        self.runbzr('revert hello')
367
 
        self.check_file_contents('hello', 'foo')
368