~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

  • Committer: Martin Pool
  • Date: 2005-06-22 06:37:43 UTC
  • Revision ID: mbp@sourcefrog.net-20050622063743-e395f04c4db8977f
- move old blackbox code from testbzr into bzrlib.selftest.blackbox

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
it's normally invoked.
27
27
"""
28
28
 
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
 
 
43
 
        if isinstance(args, basestring):
44
 
            args = args.split()
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):
56
 
        self.runbzr('--help')
57
 
        self.runbzr('help')
58
 
        self.runbzr('help commands')
59
 
        self.runbzr('help help')
60
 
        self.runbzr('commit -h')
61
 
 
62
 
    def test_init_branch(self):
63
 
        import os
64
 
        self.runbzr(['init'])
65
 
 
66
 
    def test_whoami(self):
67
 
        # this should always identify something, if only "john@localhost"
68
 
        self.runbzr("whoami")
69
 
        self.runbzr("whoami --email")
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):
86
 
        self.runbzr("pants", retcode=1)
87
 
        self.runbzr("--pants off", retcode=1)
88
 
        self.runbzr("diff --message foo", retcode=1)
89
 
 
90
 
    def test_empty_commit(self):
91
 
        self.runbzr("init")
92
 
        self.build_tree(['hello.txt'])
93
 
        self.runbzr("commit -m empty", retcode=1)
94
 
        self.runbzr("add hello.txt")
95
 
        self.runbzr("commit -m added")
96
 
 
97
 
    def test_ignore_patterns(self):
98
 
        from bzrlib.branch import Branch
99
 
        
100
 
        b = Branch('.', init=True)
101
 
        self.assertEquals(list(b.unknowns()), [])
102
 
 
103
 
        file('foo.tmp', 'wt').write('tmp files are ignored')
104
 
        self.assertEquals(list(b.unknowns()), [])
105
 
        assert self.backtick('bzr unknowns') == ''
106
 
 
107
 
        file('foo.c', 'wt').write('int main() {}')
108
 
        self.assertEquals(list(b.unknowns()), ['foo.c'])
109
 
        assert self.backtick('bzr unknowns') == 'foo.c\n'
110
 
 
111
 
        self.runbzr(['add', 'foo.c'])
112
 
        assert self.backtick('bzr unknowns') == ''
113
 
 
114
 
        # 'ignore' works when creating the .bzignore file
115
 
        file('foo.blah', 'wt').write('blah')
116
 
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
117
 
        self.runbzr('ignore *.blah')
118
 
        self.assertEquals(list(b.unknowns()), [])
119
 
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
120
 
 
121
 
        # 'ignore' works when then .bzrignore file already exists
122
 
        file('garh', 'wt').write('garh')
123
 
        self.assertEquals(list(b.unknowns()), ['garh'])
124
 
        assert self.backtick('bzr unknowns') == 'garh\n'
125
 
        self.runbzr('ignore garh')
126
 
        self.assertEquals(list(b.unknowns()), [])
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')
141
 
        
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"""
 
29
# this code was previously in testbzr
 
30
 
 
31
from unittest import TestCase
 
32
from bzrlib.selftest import TestBase, InTempDir
 
33
 
 
34
class TestVersion(TestBase):
 
35
    def runTest(self):
170
36
        # output is intentionally passed through to stdout so that we
171
37
        # 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)
182
 
 
183
 
class OldTests(ExternalBase):
 
38
        self.runcmd(['bzr', 'version'])
 
39
 
 
40
 
 
41
 
 
42
class HelpCommands(TestBase):
 
43
    def runTest(self):
 
44
        self.runcmd('bzr --help')
 
45
        self.runcmd('bzr help')
 
46
        self.runcmd('bzr help commands')
 
47
        self.runcmd('bzr help help')
 
48
        self.runcmd('bzr commit -h')
 
49
 
 
50
 
 
51
class InitBranch(InTempDir):
 
52
    def runTest(self):
 
53
        import os
 
54
        self.runcmd(['bzr', 'init'])
 
55
 
 
56
 
 
57
 
 
58
class UserIdentity(InTempDir):
 
59
    def runTest(self):
 
60
        # this should always identify something, if only "john@localhost"
 
61
        self.runcmd("bzr whoami")
 
62
        self.runcmd("bzr whoami --email")
 
63
        self.assertEquals(self.backtick("bzr whoami --email").count('@'),
 
64
                          1)
 
65
 
 
66
 
 
67
class InvalidCommands(InTempDir):
 
68
    def runTest(self):
 
69
        self.runcmd("bzr pants", retcode=1)
 
70
        self.runcmd("bzr --pants off", retcode=1)
 
71
        self.runcmd("bzr diff --message foo", retcode=1)
 
72
 
 
73
 
 
74
 
 
75
class OldTests(InTempDir):
184
76
    # old tests moved from ./testbzr
185
 
    def test_bzr(self):
 
77
    def runTest(self):
186
78
        from os import chdir, mkdir
187
79
        from os.path import exists
188
80
        import os
189
81
 
190
 
        runbzr = self.runbzr
 
82
        runcmd = self.runcmd
191
83
        backtick = self.backtick
192
84
        progress = self.log
193
85
 
194
86
        progress("basic branch creation")
195
 
        mkdir('branch1')
 
87
        runcmd(['mkdir', 'branch1'])
196
88
        chdir('branch1')
197
 
        runbzr('init')
 
89
        runcmd('bzr init')
198
90
 
199
91
        self.assertEquals(backtick('bzr root').rstrip(),
200
92
                          os.path.join(self.test_dir, 'branch1'))
206
98
        f.close()
207
99
 
208
100
        out = backtick("bzr unknowns")
209
 
        self.assertEquals(out, 'test.txt\n')
 
101
        assert out == 'test.txt\n'
210
102
 
211
103
        out = backtick("bzr status")
212
104
        assert out == 'unknown:\n  test.txt\n'
241
133
                       "  test.txt\n")
242
134
 
243
135
        progress("command help")
244
 
        runbzr("help st")
245
 
        runbzr("help")
246
 
        runbzr("help commands")
247
 
        runbzr("help slartibartfast", 1)
 
136
        runcmd("bzr help st")
 
137
        runcmd("bzr help")
 
138
        runcmd("bzr help commands")
 
139
        runcmd("bzr help slartibartfast", 1)
248
140
 
249
141
        out = backtick("bzr help ci")
250
142
        out.index('aliases: ')
251
143
 
252
144
        progress("can't rename unversioned file")
253
 
        runbzr("rename test.txt new-test.txt", 1)
 
145
        runcmd("bzr rename test.txt new-test.txt", 1)
254
146
 
255
147
        progress("adding a file")
256
148
 
257
 
        runbzr("add test.txt")
 
149
        runcmd("bzr add test.txt")
258
150
        assert backtick("bzr unknowns") == ''
259
151
        assert backtick("bzr status --all") == ("added:\n"
260
152
                                                "  test.txt\n")
261
153
 
262
154
        progress("rename newly-added file")
263
 
        runbzr("rename test.txt hello.txt")
 
155
        runcmd("bzr rename test.txt hello.txt")
264
156
        assert os.path.exists("hello.txt")
265
157
        assert not os.path.exists("test.txt")
266
158
 
267
159
        assert backtick("bzr revno") == '0\n'
268
160
 
269
161
        progress("add first revision")
270
 
        runbzr(['commit', '-m', 'add first revision'])
 
162
        runcmd(["bzr", "commit", "-m", 'add first revision'])
271
163
 
272
164
        progress("more complex renames")
273
165
        os.mkdir("sub1")
274
 
        runbzr("rename hello.txt sub1", 1)
275
 
        runbzr("rename hello.txt sub1/hello.txt", 1)
276
 
        runbzr("move hello.txt sub1", 1)
 
166
        runcmd("bzr rename hello.txt sub1", 1)
 
167
        runcmd("bzr rename hello.txt sub1/hello.txt", 1)
 
168
        runcmd("bzr move hello.txt sub1", 1)
277
169
 
278
 
        runbzr("add sub1")
279
 
        runbzr("rename sub1 sub2")
280
 
        runbzr("move hello.txt sub2")
 
170
        runcmd("bzr add sub1")
 
171
        runcmd("bzr rename sub1 sub2")
 
172
        runcmd("bzr move hello.txt sub2")
281
173
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
282
174
 
283
175
        assert exists("sub2")
285
177
        assert not exists("sub1")
286
178
        assert not exists("hello.txt")
287
179
 
288
 
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
 
180
        runcmd(['bzr', 'commit', '-m', 'commit with some things moved to subdirs'])
289
181
 
290
182
        mkdir("sub1")
291
 
        runbzr('add sub1')
292
 
        runbzr('move sub2/hello.txt sub1')
 
183
        runcmd('bzr add sub1')
 
184
        runcmd('bzr move sub2/hello.txt sub1')
293
185
        assert not exists('sub2/hello.txt')
294
186
        assert exists('sub1/hello.txt')
295
 
        runbzr('move sub2 sub1')
 
187
        runcmd('bzr move sub2 sub1')
296
188
        assert not exists('sub2')
297
189
        assert exists('sub1/sub2')
298
190
 
299
 
        runbzr(['commit', '-m', 'rename nested subdirectories'])
 
191
        runcmd(['bzr', 'commit', '-m', 'rename nested subdirectories'])
300
192
 
301
193
        chdir('sub1/sub2')
302
194
        self.assertEquals(backtick('bzr root')[:-1],
303
195
                          os.path.join(self.test_dir, 'branch1'))
304
 
        runbzr('move ../hello.txt .')
 
196
        runcmd('bzr move ../hello.txt .')
305
197
        assert exists('./hello.txt')
306
198
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
307
199
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
308
 
        runbzr(['commit', '-m', 'move to parent directory'])
 
200
        runcmd(['bzr', 'commit', '-m', 'move to parent directory'])
309
201
        chdir('..')
310
202
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
311
203
 
312
 
        runbzr('move sub2/hello.txt .')
 
204
        runcmd('bzr move sub2/hello.txt .')
313
205
        assert exists('hello.txt')
314
206
 
315
207
        f = file('hello.txt', 'wt')
320
212
        f.write('this is my new commit\n')
321
213
        f.close()
322
214
 
323
 
        runbzr('commit -F msg.tmp')
 
215
        runcmd('bzr commit -F msg.tmp')
324
216
 
325
217
        assert backtick('bzr revno') == '5\n'
326
 
        runbzr('export -r 5 export-5.tmp')
327
 
        runbzr('export export.tmp')
328
 
 
329
 
        runbzr('log')
330
 
        runbzr('log -v')
331
 
        runbzr('log -v --forward')
332
 
        runbzr('log -m', retcode=1)
333
 
        log_out = backtick('bzr log -m commit')
334
 
        assert "this is my new commit" in log_out
335
 
        assert "rename nested" not in log_out
336
 
        assert 'revision-id' not in log_out
337
 
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
218
        runcmd('bzr export -r 5 export-5.tmp')
 
219
        runcmd('bzr export export.tmp')
 
220
 
 
221
        runcmd('bzr log')
 
222
        runcmd('bzr log -v')
 
223
 
338
224
 
339
225
 
340
226
        progress("file with spaces in name")
341
227
        mkdir('sub directory')
342
228
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
343
 
        runbzr('add .')
344
 
        runbzr('diff')
345
 
        runbzr('commit -m add-spaces')
346
 
        runbzr('check')
347
 
 
348
 
        runbzr('log')
349
 
        runbzr('log --forward')
350
 
 
351
 
        runbzr('info')
352
 
 
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
 
 
 
229
        runcmd('bzr add .')
 
230
        runcmd('bzr diff')
 
231
        runcmd('bzr commit -m add-spaces')
 
232
        runcmd('bzr check')
 
233
 
 
234
        runcmd('bzr log')
 
235
        runcmd('bzr log --forward')
 
236
 
 
237
        runcmd('bzr info')
 
238
 
 
239
 
 
240
 
 
241
 
 
242
 
 
243
 
 
244
        chdir('..')
 
245
        chdir('..')
 
246
        progress('branch')
 
247
        # Can't create a branch if it already exists
 
248
        runcmd('bzr branch branch1', retcode=1)
 
249
        # Can't create a branch if its parent doesn't exist
 
250
        runcmd('bzr branch /unlikely/to/exist', retcode=1)
 
251
        runcmd('bzr branch branch1 branch2')
 
252
 
 
253
        progress("pull")
 
254
        chdir('branch1')
 
255
        runcmd('bzr pull', retcode=1)
 
256
        runcmd('bzr pull ../branch2')
 
257
        chdir('.bzr')
 
258
        runcmd('bzr pull')
 
259
        runcmd('bzr commit -m empty')
 
260
        runcmd('bzr pull')
 
261
        chdir('../../branch2')
 
262
        runcmd('bzr pull')
 
263
        runcmd('bzr commit -m empty')
 
264
        chdir('../branch1')
 
265
        runcmd('bzr commit -m empty')
 
266
        runcmd('bzr pull', retcode=1)
 
267
        chdir ('..')
 
268
 
 
269
        progress('status after remove')
 
270
        mkdir('status-after-remove')
 
271
        # see mail from William Dodé, 2005-05-25
 
272
        # $ bzr init; touch a; bzr add a; bzr commit -m "add a"
 
273
        #     * looking for changes...
 
274
        #     added a
 
275
        #     * commited r1
 
276
        #     $ bzr remove a
 
277
        #     $ bzr status
 
278
        #     bzr: local variable 'kind' referenced before assignment
 
279
        #     at /vrac/python/bazaar-ng/bzrlib/diff.py:286 in compare_trees()
 
280
        #     see ~/.bzr.log for debug information
 
281
        chdir('status-after-remove')
 
282
        runcmd('bzr init')
 
283
        file('a', 'w').write('foo')
 
284
        runcmd('bzr add a')
 
285
        runcmd(['bzr', 'commit', '-m', 'add a'])
 
286
        runcmd('bzr remove a')
 
287
        runcmd('bzr status')
 
288
 
 
289
        chdir('..')
 
290
 
 
291
        progress('ignore patterns')
 
292
        mkdir('ignorebranch')
 
293
        chdir('ignorebranch')
 
294
        runcmd('bzr init')
 
295
        assert backtick('bzr unknowns') == ''
 
296
 
 
297
        file('foo.tmp', 'wt').write('tmp files are ignored')
 
298
        assert backtick('bzr unknowns') == ''
 
299
 
 
300
        file('foo.c', 'wt').write('int main() {}')
 
301
        assert backtick('bzr unknowns') == 'foo.c\n'
 
302
        runcmd('bzr add foo.c')
 
303
        assert backtick('bzr unknowns') == ''
 
304
 
 
305
        # 'ignore' works when creating the .bzignore file
 
306
        file('foo.blah', 'wt').write('blah')
 
307
        assert backtick('bzr unknowns') == 'foo.blah\n'
 
308
        runcmd('bzr ignore *.blah')
 
309
        assert backtick('bzr unknowns') == ''
 
310
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
311
 
 
312
        # 'ignore' works when then .bzrignore file already exists
 
313
        file('garh', 'wt').write('garh')
 
314
        assert backtick('bzr unknowns') == 'garh\n'
 
315
        runcmd('bzr ignore garh')
 
316
        assert backtick('bzr unknowns') == ''
 
317
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
318
 
 
319
        chdir('..')
 
320
 
 
321
 
 
322
 
 
323
 
 
324
        progress("recursive and non-recursive add")
 
325
        mkdir('no-recurse')
 
326
        chdir('no-recurse')
 
327
        runcmd('bzr init')
 
328
        mkdir('foo')
 
329
        fp = os.path.join('foo', 'test.txt')
 
330
        f = file(fp, 'w')
 
331
        f.write('hello!\n')
 
332
        f.close()
 
333
        runcmd('bzr add --no-recurse foo')
 
334
        runcmd('bzr file-id foo')
 
335
        runcmd('bzr file-id ' + fp, 1)      # not versioned yet
 
336
        runcmd('bzr commit -m add-dir-only')
 
337
 
 
338
        runcmd('bzr file-id ' + fp, 1)      # still not versioned 
 
339
 
 
340
        runcmd('bzr add foo')
 
341
        runcmd('bzr file-id ' + fp)
 
342
        runcmd('bzr commit -m add-sub-file')
 
343
 
 
344
        chdir('..')
 
345
 
 
346
 
 
347
 
 
348
    
 
349
        
 
350
 
 
351
 
 
352
# lists all tests from this module in the best order to run them.  we
 
353
# do it this way rather than just discovering them all because it
 
354
# allows us to test more basic functions first where failures will be
 
355
# easiest to understand.
 
356
 
 
357
def suite():
 
358
    from unittest import TestSuite
 
359
    s = TestSuite()
 
360
    s.addTests([TestVersion(),
 
361
                InitBranch(),
 
362
                HelpCommands(),
 
363
                UserIdentity(),
 
364
                InvalidCommands(),
 
365
                OldTests()])
 
366
    return s