~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

  • Committer: Aaron Bentley
  • Date: 2005-09-19 02:52:24 UTC
  • mto: (1185.1.29)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: aaron.bentley@utoronto.ca-20050919025224-1cc3c70640086e09
TODO re tests

Show diffs side-by-side

added added

removed removed

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