~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-30 05:30:43 UTC
  • Revision ID: mbp@sourcefrog.net-20050830053042-58932432ee958d1e
- make get_parent() be a method of Branch; add simple tests for it

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
 
 
34
 
 
35
 
 
36
 
class ExternalBase(InTempDir):
37
 
    def runbzr(self, args, retcode=0):
38
 
        try:
39
 
            import shutil
40
 
            from subprocess import call
41
 
        except ImportError, e:
42
 
            _need_subprocess()
43
 
            raise
44
 
 
 
29
import sys
 
30
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
 
31
 
 
32
class ExternalBase(TestCaseInTempDir):
 
33
    def runbzr(self, args, retcode=0,backtick=False):
45
34
        if isinstance(args, basestring):
46
35
            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):
 
36
 
 
37
        if backtick:
 
38
            return self.backtick(['python', self.BZRPATH,] + args,
 
39
                           retcode=retcode)
 
40
        else:
 
41
            return self.runcmd(['python', self.BZRPATH,] + args,
 
42
                           retcode=retcode)
 
43
 
 
44
 
 
45
class TestCommands(ExternalBase):
 
46
 
 
47
    def test_help_commands(self):
63
48
        self.runbzr('--help')
64
49
        self.runbzr('help')
65
50
        self.runbzr('help commands')
66
51
        self.runbzr('help help')
67
52
        self.runbzr('commit -h')
68
53
 
69
 
 
70
 
class InitBranch(ExternalBase):
71
 
    def runTest(self):
72
 
        import os
 
54
    def test_init_branch(self):
73
55
        self.runbzr(['init'])
74
56
 
75
 
 
76
 
 
77
 
class UserIdentity(ExternalBase):
78
 
    def runTest(self):
 
57
    def test_whoami(self):
79
58
        # this should always identify something, if only "john@localhost"
80
59
        self.runbzr("whoami")
81
60
        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):
 
61
 
 
62
        self.assertEquals(self.runbzr("whoami --email",
 
63
                                      backtick=True).count('@'), 1)
 
64
        
 
65
    def test_whoami_branch(self):
 
66
        """branch specific user identity works."""
 
67
        self.runbzr('init')
 
68
        f = file('.bzr/email', 'wt')
 
69
        f.write('Branch Identity <branch@identi.ty>')
 
70
        f.close()
 
71
        whoami = self.runbzr("whoami",backtick=True)
 
72
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
73
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
 
74
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
 
75
 
 
76
    def test_invalid_commands(self):
88
77
        self.runbzr("pants", retcode=1)
89
78
        self.runbzr("--pants off", retcode=1)
90
79
        self.runbzr("diff --message foo", retcode=1)
91
80
 
92
 
 
93
 
 
94
 
class EmptyCommit(ExternalBase):
95
 
    def runTest(self):
 
81
    def test_empty_commit(self):
96
82
        self.runbzr("init")
97
83
        self.build_tree(['hello.txt'])
98
84
        self.runbzr("commit -m empty", retcode=1)
99
85
        self.runbzr("add hello.txt")
100
86
        self.runbzr("commit -m added")
101
87
 
102
 
 
103
 
 
104
 
class IgnorePatterns(ExternalBase):
105
 
    def runTest(self):
 
88
    def test_ignore_patterns(self):
106
89
        from bzrlib.branch import Branch
107
90
        
108
91
        b = Branch('.', init=True)
133
116
        self.runbzr('ignore garh')
134
117
        self.assertEquals(list(b.unknowns()), [])
135
118
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
136
 
        
137
 
 
138
 
 
 
119
 
 
120
    def test_revert(self):
 
121
        import os
 
122
        self.runbzr('init')
 
123
 
 
124
        file('hello', 'wt').write('foo')
 
125
        self.runbzr('add hello')
 
126
        self.runbzr('commit -m setup hello')
 
127
 
 
128
        file('goodbye', 'wt').write('baz')
 
129
        self.runbzr('add goodbye')
 
130
        self.runbzr('commit -m setup goodbye')
 
131
        
 
132
        file('hello', 'wt').write('bar')
 
133
        file('goodbye', 'wt').write('qux')
 
134
        self.runbzr('revert hello')
 
135
        self.check_file_contents('hello', 'foo')
 
136
        self.check_file_contents('goodbye', 'qux')
 
137
        self.runbzr('revert')
 
138
        self.check_file_contents('goodbye', 'baz')
 
139
 
 
140
        os.mkdir('revertdir')
 
141
        self.runbzr('add revertdir')
 
142
        self.runbzr('commit -m f')
 
143
        os.rmdir('revertdir')
 
144
        self.runbzr('revert')
 
145
 
 
146
    def skipped_test_mv_modes(self):
 
147
        """Test two modes of operation for mv"""
 
148
        from bzrlib.branch import Branch
 
149
        b = Branch('.', init=True)
 
150
        self.build_tree(['a', 'c', 'subdir/'])
 
151
        self.run_bzr('mv', 'a', 'b')
 
152
        self.run_bzr('mv', 'b', 'subdir')
 
153
        self.run_bzr('mv', 'subdir/b', 'a')
 
154
        self.run_bzr('mv', 'a', 'b', 'subdir')
 
155
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
156
 
 
157
    def test_main_version(self):
 
158
        """Check output from version command and master option is reasonable"""
 
159
        # output is intentionally passed through to stdout so that we
 
160
        # can see the version being tested
 
161
        output = self.runbzr('version', backtick=1)
 
162
        self.log('bzr version output:')
 
163
        self.log(output)
 
164
        self.assert_(output.startswith('bzr (bazaar-ng) '))
 
165
        self.assertNotEqual(output.index('Canonical'), -1)
 
166
        # make sure --version is consistent
 
167
        tmp_output = self.runbzr('--version', backtick=1)
 
168
        self.log('bzr --version output:')
 
169
        self.log(tmp_output)
 
170
        self.assertEquals(output, tmp_output)
 
171
 
 
172
    def example_branch(test):
 
173
        test.runbzr('init')
 
174
        file('hello', 'wt').write('foo')
 
175
        test.runbzr('add hello')
 
176
        test.runbzr('commit -m setup hello')
 
177
        file('goodbye', 'wt').write('baz')
 
178
        test.runbzr('add goodbye')
 
179
        test.runbzr('commit -m setup goodbye')
 
180
 
 
181
    def test_revert(self):
 
182
        self.example_branch()
 
183
        file('hello', 'wt').write('bar')
 
184
        file('goodbye', 'wt').write('qux')
 
185
        self.runbzr('revert hello')
 
186
        self.check_file_contents('hello', 'foo')
 
187
        self.check_file_contents('goodbye', 'qux')
 
188
        self.runbzr('revert')
 
189
        self.check_file_contents('goodbye', 'baz')
 
190
 
 
191
    def test_merge(self):
 
192
        from bzrlib.branch import Branch
 
193
        import os
 
194
        
 
195
        os.mkdir('a')
 
196
        os.chdir('a')
 
197
 
 
198
        self.example_branch()
 
199
        os.chdir('..')
 
200
        self.runbzr('branch a b')
 
201
        os.chdir('b')
 
202
        file('goodbye', 'wt').write('quux')
 
203
        self.runbzr(['commit',  '-m',  "more u's are always good"])
 
204
 
 
205
        os.chdir('../a')
 
206
        file('hello', 'wt').write('quuux')
 
207
        # We can't merge when there are in-tree changes
 
208
        self.runbzr('merge ../b', retcode=1)
 
209
        self.runbzr(['commit', '-m', "Like an epidemic of u's"])
 
210
        self.runbzr('merge ../b')
 
211
        self.check_file_contents('goodbye', 'quux')
 
212
        # Merging a branch pulls its revision into the tree
 
213
        a = Branch('.')
 
214
        b = Branch('../b')
 
215
        a.get_revision_xml(b.last_patch())
 
216
 
 
217
        self.log('pending merges: %s', a.pending_merges())
 
218
#        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
 
219
#        % (a.pending_merges(), b.last_patch())
139
220
 
140
221
class OldTests(ExternalBase):
141
 
    # old tests moved from ./testbzr
142
 
    def runTest(self):
 
222
    """old tests moved from ./testbzr."""
 
223
 
 
224
    def test_bzr(self):
143
225
        from os import chdir, mkdir
144
226
        from os.path import exists
145
227
        import os
307
389
 
308
390
        runbzr('info')
309
391
 
310
 
 
311
 
 
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
 
 
395
 
 
396
 
 
397
 
class RevertCommand(ExternalBase):
398
 
    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('hello', 'wt').write('bar')
406
 
        self.runbzr('revert hello')
407
 
        self.check_file_contents('hello', 'foo')
408