~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

  • Committer: Martin Pool
  • Date: 2005-08-25 07:46:11 UTC
  • Revision ID: mbp@sourcefrog.net-20050825074611-98130ea6d05d9d2a
- add functions to enable and disable default logging, so that we can
  turn it off while running the tests

- default logging gets turned on from the bzr main function so that
  other applications using the library can make their own decisions

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
it's normally invoked.
27
27
"""
28
28
 
29
 
# this code was previously in testbzr
30
 
 
31
 
from unittest import TestCase
32
 
from bzrlib.selftest import TestBase, InTempDir
33
 
 
 
29
import sys
 
30
from bzrlib.selftest import InTempDir, BzrTestBase
34
31
 
35
32
 
36
33
class ExternalBase(InTempDir):
37
 
    def runbzr(self, args, retcode=0):
 
34
 
 
35
    def runbzr(self, args, retcode=0,backtick=False):
38
36
        try:
39
37
            import shutil
40
38
            from subprocess import call
44
42
 
45
43
        if isinstance(args, basestring):
46
44
            args = args.split()
47
 
            
48
 
        return self.runcmd(['python', self.BZRPATH,] + args,
49
 
                           retcode=retcode)
50
 
 
51
 
 
52
 
 
53
 
class TestVersion(ExternalBase):
54
 
    def runTest(self):
55
 
        # output is intentionally passed through to stdout so that we
56
 
        # can see the version being tested
57
 
        self.runbzr(['version'])
58
 
 
59
 
 
60
 
 
61
 
class HelpCommands(ExternalBase):
62
 
    def runTest(self):
 
45
 
 
46
        if backtick:
 
47
            return self.backtick(['python', self.BZRPATH,] + args,
 
48
                           retcode=retcode)
 
49
        else:
 
50
            return self.runcmd(['python', self.BZRPATH,] + args,
 
51
                           retcode=retcode)
 
52
 
 
53
class TestCommands(ExternalBase):
 
54
 
 
55
    def test_help_commands(self):
63
56
        self.runbzr('--help')
64
57
        self.runbzr('help')
65
58
        self.runbzr('help commands')
66
59
        self.runbzr('help help')
67
60
        self.runbzr('commit -h')
68
61
 
69
 
 
70
 
class InitBranch(ExternalBase):
71
 
    def runTest(self):
 
62
    def test_init_branch(self):
72
63
        import os
73
64
        self.runbzr(['init'])
74
65
 
75
 
 
76
 
 
77
 
class UserIdentity(ExternalBase):
78
 
    def runTest(self):
 
66
    def test_whoami(self):
79
67
        # this should always identify something, if only "john@localhost"
80
68
        self.runbzr("whoami")
81
69
        self.runbzr("whoami --email")
82
 
        self.assertEquals(self.backtick("bzr whoami --email").count('@'),
83
 
                          1)
84
 
 
85
 
 
86
 
class InvalidCommands(ExternalBase):
87
 
    def runTest(self):
 
70
 
 
71
        self.assertEquals(self.runbzr("whoami --email",
 
72
                                      backtick=True).count('@'), 1)
 
73
        
 
74
    def test_whoami_branch(self):
 
75
        """branch specific user identity works."""
 
76
        self.runbzr('init')
 
77
        f = file('.bzr/email', 'wt')
 
78
        f.write('Branch Identity <branch@identi.ty>')
 
79
        f.close()
 
80
        whoami = self.runbzr("whoami",backtick=True)
 
81
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
82
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
 
83
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
 
84
 
 
85
    def test_invalid_commands(self):
88
86
        self.runbzr("pants", retcode=1)
89
87
        self.runbzr("--pants off", retcode=1)
90
88
        self.runbzr("diff --message foo", retcode=1)
91
89
 
92
 
 
93
 
 
94
 
class EmptyCommit(ExternalBase):
95
 
    def runTest(self):
 
90
    def test_empty_commit(self):
96
91
        self.runbzr("init")
97
92
        self.build_tree(['hello.txt'])
98
93
        self.runbzr("commit -m empty", retcode=1)
99
94
        self.runbzr("add hello.txt")
100
95
        self.runbzr("commit -m added")
101
96
 
102
 
 
103
 
 
104
 
class IgnorePatterns(ExternalBase):
105
 
    def runTest(self):
 
97
    def test_ignore_patterns(self):
106
98
        from bzrlib.branch import Branch
107
99
        
108
100
        b = Branch('.', init=True)
133
125
        self.runbzr('ignore garh')
134
126
        self.assertEquals(list(b.unknowns()), [])
135
127
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
128
 
 
129
    def test_revert(self):
 
130
        import os
 
131
 
 
132
        self.runbzr('init')
 
133
 
 
134
        file('hello', 'wt').write('foo')
 
135
        self.runbzr('add hello')
 
136
        self.runbzr('commit -m setup hello')
 
137
 
 
138
        file('goodbye', 'wt').write('baz')
 
139
        self.runbzr('add goodbye')
 
140
        self.runbzr('commit -m setup goodbye')
136
141
        
137
 
 
138
 
 
 
142
        file('hello', 'wt').write('bar')
 
143
        file('goodbye', 'wt').write('qux')
 
144
        self.runbzr('revert hello')
 
145
        self.check_file_contents('hello', 'foo')
 
146
        self.check_file_contents('goodbye', 'qux')
 
147
        self.runbzr('revert')
 
148
        self.check_file_contents('goodbye', 'baz')
 
149
 
 
150
        os.mkdir('revertdir')
 
151
        self.runbzr('add revertdir')
 
152
        self.runbzr('commit -m f')
 
153
        os.rmdir('revertdir')
 
154
        self.runbzr('revert')
 
155
 
 
156
 
 
157
    def skipped_test_mv_modes(self):
 
158
        """Test two modes of operation for mv"""
 
159
        from bzrlib.branch import Branch
 
160
        b = Branch('.', init=True)
 
161
        self.build_tree(['a', 'c', 'subdir/'])
 
162
        self.run_bzr('mv', 'a', 'b')
 
163
        self.run_bzr('mv', 'b', 'subdir')
 
164
        self.run_bzr('mv', 'subdir/b', 'a')
 
165
        self.run_bzr('mv', 'a', 'b', 'subdir')
 
166
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
167
 
 
168
    def test_main_version(self):
 
169
        """Check output from version command and master option is reasonable"""
 
170
        # output is intentionally passed through to stdout so that we
 
171
        # can see the version being tested
 
172
        output = self.runbzr('version', backtick=1)
 
173
        self.log('bzr version output:')
 
174
        self.log(output)
 
175
        self.assert_(output.startswith('bzr (bazaar-ng) '))
 
176
        self.assertNotEqual(output.index('Canonical'), -1)
 
177
        # make sure --version is consistent
 
178
        tmp_output = self.runbzr('--version', backtick=1)
 
179
        self.log('bzr --version output:')
 
180
        self.log(tmp_output)
 
181
        self.assertEquals(output, tmp_output)
139
182
 
140
183
class OldTests(ExternalBase):
141
184
    # old tests moved from ./testbzr
142
 
    def runTest(self):
 
185
    def test_bzr(self):
143
186
        from os import chdir, mkdir
144
187
        from os.path import exists
145
188
        import os
309
352
 
310
353
 
311
354
 
312
 
 
313
 
 
314
 
 
315
 
        chdir('..')
316
 
        chdir('..')
317
 
        progress('branch')
318
 
        assert os.path.exists('branch1')
319
 
        assert not os.path.exists('branch2')
320
 
        # Can't create a branch if it already exists
321
 
        runbzr('branch branch1', retcode=1)
322
 
        # Can't create a branch if its parent doesn't exist
323
 
        runbzr('branch /unlikely/to/exist', retcode=1)
324
 
        runbzr('branch branch1 branch2')
325
 
        assert exists('branch2')
326
 
        assert exists('branch2/sub1')
327
 
        assert exists('branch2/sub1/hello.txt')
328
 
        
329
 
        runbzr('branch --revision 0 branch1 branch3')
330
 
        assert not exists('branch3/sub1/hello.txt')
331
 
        runbzr('branch --revision 0..3 branch1 branch4', retcode=1)
332
 
 
333
 
        progress("pull")
334
 
        chdir('branch1')
335
 
        runbzr('pull', retcode=1)
336
 
        runbzr('pull ../branch2')
337
 
        chdir('.bzr')
338
 
        runbzr('pull')
339
 
        runbzr('commit --unchanged -m empty')
340
 
        runbzr('pull')
341
 
        chdir('../../branch2')
342
 
        runbzr('pull')
343
 
        runbzr('commit --unchanged -m empty')
344
 
        chdir('../branch1')
345
 
        runbzr('commit --unchanged -m empty')
346
 
        runbzr('pull', retcode=1)
347
 
        chdir ('..')
348
 
 
349
 
        progress('status after remove')
350
 
        mkdir('status-after-remove')
351
 
        # see mail from William Dodé, 2005-05-25
352
 
        # $ bzr init; touch a; bzr add a; bzr commit -m "add a"
353
 
        #     * looking for changes...
354
 
        #     added a
355
 
        #     * commited r1
356
 
        #     $ bzr remove a
357
 
        #     $ bzr status
358
 
        #     bzr: local variable 'kind' referenced before assignment
359
 
        #     at /vrac/python/bazaar-ng/bzrlib/diff.py:286 in compare_trees()
360
 
        #     see ~/.bzr.log for debug information
361
 
        chdir('status-after-remove')
362
 
        runbzr('init')
363
 
        file('a', 'w').write('foo')
364
 
        runbzr('add a')
365
 
        runbzr(['commit', '-m', 'add a'])
366
 
        runbzr('remove a')
367
 
        runbzr('status')
368
 
 
369
 
        chdir('..')
370
 
 
371
 
 
372
 
 
373
 
        progress("recursive and non-recursive add")
374
 
        mkdir('no-recurse')
375
 
        chdir('no-recurse')
376
 
        runbzr('init')
377
 
        mkdir('foo')
378
 
        fp = os.path.join('foo', 'test.txt')
379
 
        f = file(fp, 'w')
380
 
        f.write('hello!\n')
381
 
        f.close()
382
 
        runbzr('add --no-recurse foo')
383
 
        runbzr('file-id foo')
384
 
        runbzr('file-id ' + fp, 1)      # not versioned yet
385
 
        runbzr('commit -m add-dir-only')
386
 
 
387
 
        self.runbzr('file-id ' + fp, 1)      # still not versioned 
388
 
 
389
 
        self.runbzr('add foo')
390
 
        self.runbzr('file-id ' + fp)
391
 
        self.runbzr('commit -m add-sub-file')
392
 
 
393
 
        chdir('..')
394
 
 
 
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')
395
365
 
396
366
 
397
367
class RevertCommand(ExternalBase):
398
368
    def runTest(self):
399
 
        self.runbzr('init')
400
 
 
401
 
        file('hello', 'wt').write('foo')
402
 
        self.runbzr('add hello')
403
 
        self.runbzr('commit -m setup hello')
404
 
 
405
 
        file('goodbye', 'wt').write('baz')
406
 
        self.runbzr('add goodbye')
407
 
        self.runbzr('commit -m setup goodbye')
408
 
        
 
369
        example_branch(self)
409
370
        file('hello', 'wt').write('bar')
410
371
        file('goodbye', 'wt').write('qux')
411
372
        self.runbzr('revert hello')
414
375
        self.runbzr('revert')
415
376
        self.check_file_contents('goodbye', 'baz')
416
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