~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
 
from bzrlib.selftest import InTempDir, BzrTestBase
31
 
 
32
 
 
33
 
class ExternalBase(InTempDir):
34
 
 
35
 
    def runbzr(self, args, retcode=0,backtick=False):
36
 
        try:
37
 
            import shutil
38
 
            from subprocess import call
39
 
        except ImportError, e:
40
 
            _need_subprocess()
41
 
            raise
42
 
 
 
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):
43
39
        if isinstance(args, basestring):
44
40
            args = args.split()
45
41
 
46
42
        if backtick:
47
 
            return self.backtick(['python', self.BZRPATH,] + args,
48
 
                           retcode=retcode)
 
43
            return self.run_bzr_captured(args, retcode=retcode)[0]
49
44
        else:
50
 
            return self.runcmd(['python', self.BZRPATH,] + args,
51
 
                           retcode=retcode)
 
45
            return self.run_bzr_captured(args, retcode=retcode)
 
46
 
52
47
 
53
48
class TestCommands(ExternalBase):
54
49
 
60
55
        self.runbzr('commit -h')
61
56
 
62
57
    def test_init_branch(self):
63
 
        import os
64
58
        self.runbzr(['init'])
65
59
 
66
60
    def test_whoami(self):
97
91
    def test_ignore_patterns(self):
98
92
        from bzrlib.branch import Branch
99
93
        
100
 
        b = Branch('.', init=True)
 
94
        b = Branch.initialize('.')
101
95
        self.assertEquals(list(b.unknowns()), [])
102
96
 
103
97
        file('foo.tmp', 'wt').write('tmp files are ignored')
104
98
        self.assertEquals(list(b.unknowns()), [])
105
 
        assert self.backtick('bzr unknowns') == ''
 
99
        assert self.capture('unknowns') == ''
106
100
 
107
101
        file('foo.c', 'wt').write('int main() {}')
108
102
        self.assertEquals(list(b.unknowns()), ['foo.c'])
109
 
        assert self.backtick('bzr unknowns') == 'foo.c\n'
 
103
        assert self.capture('unknowns') == 'foo.c\n'
110
104
 
111
105
        self.runbzr(['add', 'foo.c'])
112
 
        assert self.backtick('bzr unknowns') == ''
 
106
        assert self.capture('unknowns') == ''
113
107
 
114
108
        # 'ignore' works when creating the .bzignore file
115
109
        file('foo.blah', 'wt').write('blah')
121
115
        # 'ignore' works when then .bzrignore file already exists
122
116
        file('garh', 'wt').write('garh')
123
117
        self.assertEquals(list(b.unknowns()), ['garh'])
124
 
        assert self.backtick('bzr unknowns') == 'garh\n'
 
118
        assert self.capture('unknowns') == 'garh\n'
125
119
        self.runbzr('ignore garh')
126
120
        self.assertEquals(list(b.unknowns()), [])
127
121
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
128
122
 
129
123
    def test_revert(self):
130
 
        import os
131
 
 
132
124
        self.runbzr('init')
133
125
 
134
126
        file('hello', 'wt').write('foo')
153
145
        os.rmdir('revertdir')
154
146
        self.runbzr('revert')
155
147
 
156
 
 
157
 
    def skipped_test_mv_modes(self):
 
148
    def test_mv_modes(self):
158
149
        """Test two modes of operation for mv"""
159
150
        from bzrlib.branch import Branch
160
 
        b = Branch('.', init=True)
 
151
        b = Branch.initialize('.')
161
152
        self.build_tree(['a', 'c', 'subdir/'])
 
153
        self.run_bzr('add', self.test_dir)
162
154
        self.run_bzr('mv', 'a', 'b')
163
155
        self.run_bzr('mv', 'b', 'subdir')
164
156
        self.run_bzr('mv', 'subdir/b', 'a')
165
 
        self.run_bzr('mv', 'a', 'b', 'subdir')
 
157
        self.run_bzr('mv', 'a', 'c', 'subdir')
166
158
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
167
159
 
 
160
 
168
161
    def test_main_version(self):
169
162
        """Check output from version command and master option is reasonable"""
170
163
        # output is intentionally passed through to stdout so that we
180
173
        self.log(tmp_output)
181
174
        self.assertEquals(output, tmp_output)
182
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
        
 
286
 
 
287
 
183
288
class OldTests(ExternalBase):
184
 
    # old tests moved from ./testbzr
 
289
    """old tests moved from ./testbzr."""
 
290
 
185
291
    def test_bzr(self):
186
292
        from os import chdir, mkdir
187
293
        from os.path import exists
188
 
        import os
189
294
 
190
295
        runbzr = self.runbzr
191
 
        backtick = self.backtick
 
296
        capture = self.capture
192
297
        progress = self.log
193
298
 
194
299
        progress("basic branch creation")
196
301
        chdir('branch1')
197
302
        runbzr('init')
198
303
 
199
 
        self.assertEquals(backtick('bzr root').rstrip(),
 
304
        self.assertEquals(capture('root').rstrip(),
200
305
                          os.path.join(self.test_dir, 'branch1'))
201
306
 
202
307
        progress("status of new file")
205
310
        f.write('hello world!\n')
206
311
        f.close()
207
312
 
208
 
        out = backtick("bzr unknowns")
209
 
        self.assertEquals(out, 'test.txt\n')
 
313
        self.assertEquals(capture('unknowns'), 'test.txt\n')
210
314
 
211
 
        out = backtick("bzr status")
 
315
        out = capture("status")
212
316
        assert out == 'unknown:\n  test.txt\n'
213
317
 
214
 
        out = backtick("bzr status --all")
 
318
        out = capture("status --all")
215
319
        assert out == "unknown:\n  test.txt\n"
216
320
 
217
 
        out = backtick("bzr status test.txt --all")
 
321
        out = capture("status test.txt --all")
218
322
        assert out == "unknown:\n  test.txt\n"
219
323
 
220
324
        f = file('test2.txt', 'wt')
221
325
        f.write('goodbye cruel world...\n')
222
326
        f.close()
223
327
 
224
 
        out = backtick("bzr status test.txt")
 
328
        out = capture("status test.txt")
225
329
        assert out == "unknown:\n  test.txt\n"
226
330
 
227
 
        out = backtick("bzr status")
 
331
        out = capture("status")
228
332
        assert out == ("unknown:\n"
229
333
                       "  test.txt\n"
230
334
                       "  test2.txt\n")
232
336
        os.unlink('test2.txt')
233
337
 
234
338
        progress("command aliases")
235
 
        out = backtick("bzr st --all")
 
339
        out = capture("st --all")
236
340
        assert out == ("unknown:\n"
237
341
                       "  test.txt\n")
238
342
 
239
 
        out = backtick("bzr stat")
 
343
        out = capture("stat")
240
344
        assert out == ("unknown:\n"
241
345
                       "  test.txt\n")
242
346
 
246
350
        runbzr("help commands")
247
351
        runbzr("help slartibartfast", 1)
248
352
 
249
 
        out = backtick("bzr help ci")
 
353
        out = capture("help ci")
250
354
        out.index('aliases: ')
251
355
 
252
356
        progress("can't rename unversioned file")
255
359
        progress("adding a file")
256
360
 
257
361
        runbzr("add test.txt")
258
 
        assert backtick("bzr unknowns") == ''
259
 
        assert backtick("bzr status --all") == ("added:\n"
 
362
        assert capture("unknowns") == ''
 
363
        assert capture("status --all") == ("added:\n"
260
364
                                                "  test.txt\n")
261
365
 
262
366
        progress("rename newly-added file")
264
368
        assert os.path.exists("hello.txt")
265
369
        assert not os.path.exists("test.txt")
266
370
 
267
 
        assert backtick("bzr revno") == '0\n'
 
371
        assert capture("revno") == '0\n'
268
372
 
269
373
        progress("add first revision")
270
374
        runbzr(['commit', '-m', 'add first revision'])
278
382
        runbzr("add sub1")
279
383
        runbzr("rename sub1 sub2")
280
384
        runbzr("move hello.txt sub2")
281
 
        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")
282
386
 
283
387
        assert exists("sub2")
284
388
        assert exists("sub2/hello.txt")
299
403
        runbzr(['commit', '-m', 'rename nested subdirectories'])
300
404
 
301
405
        chdir('sub1/sub2')
302
 
        self.assertEquals(backtick('bzr root')[:-1],
 
406
        self.assertEquals(capture('root')[:-1],
303
407
                          os.path.join(self.test_dir, 'branch1'))
304
408
        runbzr('move ../hello.txt .')
305
409
        assert exists('./hello.txt')
306
 
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
307
 
        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')
308
413
        runbzr(['commit', '-m', 'move to parent directory'])
309
414
        chdir('..')
310
 
        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')
311
416
 
312
417
        runbzr('move sub2/hello.txt .')
313
418
        assert exists('hello.txt')
322
427
 
323
428
        runbzr('commit -F msg.tmp')
324
429
 
325
 
        assert backtick('bzr revno') == '5\n'
 
430
        assert capture('revno') == '5\n'
326
431
        runbzr('export -r 5 export-5.tmp')
327
432
        runbzr('export export.tmp')
328
433
 
330
435
        runbzr('log -v')
331
436
        runbzr('log -v --forward')
332
437
        runbzr('log -m', retcode=1)
333
 
        log_out = backtick('bzr log -m commit')
 
438
        log_out = capture('log -m commit')
334
439
        assert "this is my new commit" in log_out
335
440
        assert "rename nested" not in log_out
336
441
        assert 'revision-id' not in log_out
337
 
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
442
        assert 'revision-id' in capture('log --show-ids -m commit')
338
443
 
339
444
 
340
445
        progress("file with spaces in name")
350
455
 
351
456
        runbzr('info')
352
457
 
353
 
 
354
 
 
355
 
def example_branch(test):
356
 
    test.runbzr('init')
357
 
 
358
 
    file('hello', 'wt').write('foo')
359
 
    test.runbzr('add hello')
360
 
    test.runbzr('commit -m setup hello')
361
 
 
362
 
    file('goodbye', 'wt').write('baz')
363
 
    test.runbzr('add goodbye')
364
 
    test.runbzr('commit -m setup goodbye')
365
 
 
366
 
 
367
 
class RevertCommand(ExternalBase):
368
 
    def runTest(self):
369
 
        example_branch(self)
370
 
        file('hello', 'wt').write('bar')
371
 
        file('goodbye', 'wt').write('qux')
372
 
        self.runbzr('revert hello')
373
 
        self.check_file_contents('hello', 'foo')
374
 
        self.check_file_contents('goodbye', 'qux')
375
 
        self.runbzr('revert')
376
 
        self.check_file_contents('goodbye', 'baz')
377
 
 
378
 
 
379
 
class MergeCommand(ExternalBase):
380
 
    def runTest(self):
381
 
        from bzrlib.branch import Branch
382
 
        import os
383
 
        os.mkdir('a')
384
 
        os.chdir('a')
385
 
        example_branch(self)
386
 
        os.chdir('..')
387
 
        self.runbzr('branch a b')
388
 
        os.chdir('b')
389
 
        file('goodbye', 'wt').write('quux')
390
 
        self.runbzr(['commit',  '-m',  "more u's are always good"])
391
 
 
392
 
        os.chdir('../a')
393
 
        file('hello', 'wt').write('quuux')
394
 
        # We can't merge when there are in-tree changes
395
 
        self.runbzr('merge ../b', retcode=1)
396
 
        self.runbzr(['commit', '-m', "Like an epidemic of u's"])
397
 
        self.runbzr('merge ../b')
398
 
        self.check_file_contents('goodbye', 'quux')
399
 
        # Merging a branch pulls its revision into the tree
400
 
        a = Branch('.')
401
 
        b = Branch('../b')
402
 
        a.get_revision_xml(b.last_patch())
403
 
        print "Pending: %s" % a.pending_merges()
404
 
#        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
405
 
#        % (a.pending_merges(), b.last_patch())
406