~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

  • Committer: Martin Pool
  • Date: 2005-07-29 12:29:27 UTC
  • Revision ID: mbp@sourcefrog.net-20050729122927-d51c2cedc14dd5d5
doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
 
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
 
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
 
 
19
"""Black-box tests for bzr.
 
20
 
 
21
These check that it behaves properly when it's invoked through the regular
 
22
command-line interface.
 
23
 
 
24
This always reinvokes bzr through a new Python interpreter, which is a
 
25
bit inefficient but arguably tests in a way more representative of how
 
26
it's normally invoked.
 
27
"""
 
28
 
 
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
 
 
44
        if isinstance(args, basestring):
 
45
            args = args.split()
 
46
            
 
47
        return self.runcmd(['python', self.BZRPATH,] + args,
 
48
                           retcode=retcode)
 
49
 
 
50
 
 
51
 
 
52
class TestVersion(BzrTestBase):
 
53
    """Check output from version command and master option is reasonable"""
 
54
    def runTest(self):
 
55
        # output is intentionally passed through to stdout so that we
 
56
        # can see the version being tested
 
57
        from cStringIO import StringIO
 
58
        save_out = sys.stdout
 
59
        try:
 
60
            sys.stdout = tmp_out = StringIO()
 
61
            
 
62
            self.run_bzr('version')
 
63
        finally:
 
64
            sys.stdout = save_out
 
65
 
 
66
        output = tmp_out.getvalue()
 
67
        self.log('bzr version output:')
 
68
        self.log(output)
 
69
        
 
70
        self.assert_(output.startswith('bzr (bazaar-ng) '))
 
71
        self.assertNotEqual(output.index('Canonical'), -1)
 
72
 
 
73
        # make sure --version is consistent
 
74
        try:
 
75
            sys.stdout = tmp_out = StringIO()
 
76
            
 
77
            self.run_bzr('--version')
 
78
        finally:
 
79
            sys.stdout = save_out
 
80
 
 
81
        self.log('bzr --version output:')
 
82
        self.log(tmp_out.getvalue())
 
83
 
 
84
        self.assertEquals(output, tmp_out.getvalue())
 
85
 
 
86
 
 
87
        
 
88
 
 
89
 
 
90
class HelpCommands(ExternalBase):
 
91
    def runTest(self):
 
92
        self.runbzr('--help')
 
93
        self.runbzr('help')
 
94
        self.runbzr('help commands')
 
95
        self.runbzr('help help')
 
96
        self.runbzr('commit -h')
 
97
 
 
98
 
 
99
class InitBranch(ExternalBase):
 
100
    def runTest(self):
 
101
        import os
 
102
        self.runbzr(['init'])
 
103
 
 
104
 
 
105
 
 
106
class UserIdentity(ExternalBase):
 
107
    def runTest(self):
 
108
        # this should always identify something, if only "john@localhost"
 
109
        self.runbzr("whoami")
 
110
        self.runbzr("whoami --email")
 
111
        self.assertEquals(self.backtick("bzr whoami --email").count('@'),
 
112
                          1)
 
113
 
 
114
 
 
115
class InvalidCommands(ExternalBase):
 
116
    def runTest(self):
 
117
        self.runbzr("pants", retcode=1)
 
118
        self.runbzr("--pants off", retcode=1)
 
119
        self.runbzr("diff --message foo", retcode=1)
 
120
 
 
121
 
 
122
 
 
123
class EmptyCommit(ExternalBase):
 
124
    def runTest(self):
 
125
        self.runbzr("init")
 
126
        self.build_tree(['hello.txt'])
 
127
        self.runbzr("commit -m empty", retcode=1)
 
128
        self.runbzr("add hello.txt")
 
129
        self.runbzr("commit -m added")
 
130
 
 
131
 
 
132
 
 
133
class IgnorePatterns(ExternalBase):
 
134
    def runTest(self):
 
135
        from bzrlib.branch import Branch
 
136
        
 
137
        b = Branch('.', init=True)
 
138
        self.assertEquals(list(b.unknowns()), [])
 
139
 
 
140
        file('foo.tmp', 'wt').write('tmp files are ignored')
 
141
        self.assertEquals(list(b.unknowns()), [])
 
142
        assert self.backtick('bzr unknowns') == ''
 
143
 
 
144
        file('foo.c', 'wt').write('int main() {}')
 
145
        self.assertEquals(list(b.unknowns()), ['foo.c'])
 
146
        assert self.backtick('bzr unknowns') == 'foo.c\n'
 
147
 
 
148
        self.runbzr(['add', 'foo.c'])
 
149
        assert self.backtick('bzr unknowns') == ''
 
150
 
 
151
        # 'ignore' works when creating the .bzignore file
 
152
        file('foo.blah', 'wt').write('blah')
 
153
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
 
154
        self.runbzr('ignore *.blah')
 
155
        self.assertEquals(list(b.unknowns()), [])
 
156
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
157
 
 
158
        # 'ignore' works when then .bzrignore file already exists
 
159
        file('garh', 'wt').write('garh')
 
160
        self.assertEquals(list(b.unknowns()), ['garh'])
 
161
        assert self.backtick('bzr unknowns') == 'garh\n'
 
162
        self.runbzr('ignore garh')
 
163
        self.assertEquals(list(b.unknowns()), [])
 
164
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
165
        
 
166
 
 
167
 
 
168
 
 
169
class OldTests(ExternalBase):
 
170
    # old tests moved from ./testbzr
 
171
    def runTest(self):
 
172
        from os import chdir, mkdir
 
173
        from os.path import exists
 
174
        import os
 
175
 
 
176
        runbzr = self.runbzr
 
177
        backtick = self.backtick
 
178
        progress = self.log
 
179
 
 
180
        progress("basic branch creation")
 
181
        mkdir('branch1')
 
182
        chdir('branch1')
 
183
        runbzr('init')
 
184
 
 
185
        self.assertEquals(backtick('bzr root').rstrip(),
 
186
                          os.path.join(self.test_dir, 'branch1'))
 
187
 
 
188
        progress("status of new file")
 
189
 
 
190
        f = file('test.txt', 'wt')
 
191
        f.write('hello world!\n')
 
192
        f.close()
 
193
 
 
194
        out = backtick("bzr unknowns")
 
195
        self.assertEquals(out, 'test.txt\n')
 
196
 
 
197
        out = backtick("bzr status")
 
198
        assert out == 'unknown:\n  test.txt\n'
 
199
 
 
200
        out = backtick("bzr status --all")
 
201
        assert out == "unknown:\n  test.txt\n"
 
202
 
 
203
        out = backtick("bzr status test.txt --all")
 
204
        assert out == "unknown:\n  test.txt\n"
 
205
 
 
206
        f = file('test2.txt', 'wt')
 
207
        f.write('goodbye cruel world...\n')
 
208
        f.close()
 
209
 
 
210
        out = backtick("bzr status test.txt")
 
211
        assert out == "unknown:\n  test.txt\n"
 
212
 
 
213
        out = backtick("bzr status")
 
214
        assert out == ("unknown:\n"
 
215
                       "  test.txt\n"
 
216
                       "  test2.txt\n")
 
217
 
 
218
        os.unlink('test2.txt')
 
219
 
 
220
        progress("command aliases")
 
221
        out = backtick("bzr st --all")
 
222
        assert out == ("unknown:\n"
 
223
                       "  test.txt\n")
 
224
 
 
225
        out = backtick("bzr stat")
 
226
        assert out == ("unknown:\n"
 
227
                       "  test.txt\n")
 
228
 
 
229
        progress("command help")
 
230
        runbzr("help st")
 
231
        runbzr("help")
 
232
        runbzr("help commands")
 
233
        runbzr("help slartibartfast", 1)
 
234
 
 
235
        out = backtick("bzr help ci")
 
236
        out.index('aliases: ')
 
237
 
 
238
        progress("can't rename unversioned file")
 
239
        runbzr("rename test.txt new-test.txt", 1)
 
240
 
 
241
        progress("adding a file")
 
242
 
 
243
        runbzr("add test.txt")
 
244
        assert backtick("bzr unknowns") == ''
 
245
        assert backtick("bzr status --all") == ("added:\n"
 
246
                                                "  test.txt\n")
 
247
 
 
248
        progress("rename newly-added file")
 
249
        runbzr("rename test.txt hello.txt")
 
250
        assert os.path.exists("hello.txt")
 
251
        assert not os.path.exists("test.txt")
 
252
 
 
253
        assert backtick("bzr revno") == '0\n'
 
254
 
 
255
        progress("add first revision")
 
256
        runbzr(['commit', '-m', 'add first revision'])
 
257
 
 
258
        progress("more complex renames")
 
259
        os.mkdir("sub1")
 
260
        runbzr("rename hello.txt sub1", 1)
 
261
        runbzr("rename hello.txt sub1/hello.txt", 1)
 
262
        runbzr("move hello.txt sub1", 1)
 
263
 
 
264
        runbzr("add sub1")
 
265
        runbzr("rename sub1 sub2")
 
266
        runbzr("move hello.txt sub2")
 
267
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
268
 
 
269
        assert exists("sub2")
 
270
        assert exists("sub2/hello.txt")
 
271
        assert not exists("sub1")
 
272
        assert not exists("hello.txt")
 
273
 
 
274
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
 
275
 
 
276
        mkdir("sub1")
 
277
        runbzr('add sub1')
 
278
        runbzr('move sub2/hello.txt sub1')
 
279
        assert not exists('sub2/hello.txt')
 
280
        assert exists('sub1/hello.txt')
 
281
        runbzr('move sub2 sub1')
 
282
        assert not exists('sub2')
 
283
        assert exists('sub1/sub2')
 
284
 
 
285
        runbzr(['commit', '-m', 'rename nested subdirectories'])
 
286
 
 
287
        chdir('sub1/sub2')
 
288
        self.assertEquals(backtick('bzr root')[:-1],
 
289
                          os.path.join(self.test_dir, 'branch1'))
 
290
        runbzr('move ../hello.txt .')
 
291
        assert exists('./hello.txt')
 
292
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
293
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
294
        runbzr(['commit', '-m', 'move to parent directory'])
 
295
        chdir('..')
 
296
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
297
 
 
298
        runbzr('move sub2/hello.txt .')
 
299
        assert exists('hello.txt')
 
300
 
 
301
        f = file('hello.txt', 'wt')
 
302
        f.write('some nice new content\n')
 
303
        f.close()
 
304
 
 
305
        f = file('msg.tmp', 'wt')
 
306
        f.write('this is my new commit\n')
 
307
        f.close()
 
308
 
 
309
        runbzr('commit -F msg.tmp')
 
310
 
 
311
        assert backtick('bzr revno') == '5\n'
 
312
        runbzr('export -r 5 export-5.tmp')
 
313
        runbzr('export export.tmp')
 
314
 
 
315
        runbzr('log')
 
316
        runbzr('log -v')
 
317
        runbzr('log -v --forward')
 
318
        runbzr('log -m', retcode=1)
 
319
        log_out = backtick('bzr log -m commit')
 
320
        assert "this is my new commit" in log_out
 
321
        assert "rename nested" not in log_out
 
322
        assert 'revision-id' not in log_out
 
323
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
324
 
 
325
 
 
326
        progress("file with spaces in name")
 
327
        mkdir('sub directory')
 
328
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
 
329
        runbzr('add .')
 
330
        runbzr('diff')
 
331
        runbzr('commit -m add-spaces')
 
332
        runbzr('check')
 
333
 
 
334
        runbzr('log')
 
335
        runbzr('log --forward')
 
336
 
 
337
        runbzr('info')
 
338
 
 
339
 
 
340
 
 
341
 
 
342
 
 
343
 
 
344
        chdir('..')
 
345
        chdir('..')
 
346
        progress('branch')
 
347
        assert os.path.exists('branch1')
 
348
        assert not os.path.exists('branch2')
 
349
        # Can't create a branch if it already exists
 
350
        runbzr('branch branch1', retcode=1)
 
351
        # Can't create a branch if its parent doesn't exist
 
352
        runbzr('branch /unlikely/to/exist', retcode=1)
 
353
        runbzr('branch branch1 branch2')
 
354
        assert exists('branch2')
 
355
        assert exists('branch2/sub1')
 
356
        assert exists('branch2/sub1/hello.txt')
 
357
        
 
358
        runbzr('branch --revision 0 branch1 branch3')
 
359
        assert not exists('branch3/sub1/hello.txt')
 
360
        runbzr('branch --revision 0..3 branch1 branch4', retcode=1)
 
361
 
 
362
        progress("pull")
 
363
        chdir('branch1')
 
364
        runbzr('pull', retcode=1)
 
365
        runbzr('pull ../branch2')
 
366
        chdir('.bzr')
 
367
        runbzr('pull')
 
368
        runbzr('commit --unchanged -m empty')
 
369
        runbzr('pull')
 
370
        chdir('../../branch2')
 
371
        runbzr('pull')
 
372
        runbzr('commit --unchanged -m empty')
 
373
        chdir('../branch1')
 
374
        runbzr('commit --unchanged -m empty')
 
375
        runbzr('pull', retcode=1)
 
376
        chdir ('..')
 
377
 
 
378
        progress('status after remove')
 
379
        mkdir('status-after-remove')
 
380
        # see mail from William Dodé, 2005-05-25
 
381
        # $ bzr init; touch a; bzr add a; bzr commit -m "add a"
 
382
        #     * looking for changes...
 
383
        #     added a
 
384
        #     * commited r1
 
385
        #     $ bzr remove a
 
386
        #     $ bzr status
 
387
        #     bzr: local variable 'kind' referenced before assignment
 
388
        #     at /vrac/python/bazaar-ng/bzrlib/diff.py:286 in compare_trees()
 
389
        #     see ~/.bzr.log for debug information
 
390
        chdir('status-after-remove')
 
391
        runbzr('init')
 
392
        file('a', 'w').write('foo')
 
393
        runbzr('add a')
 
394
        runbzr(['commit', '-m', 'add a'])
 
395
        runbzr('remove a')
 
396
        runbzr('status')
 
397
 
 
398
        chdir('..')
 
399
 
 
400
 
 
401
 
 
402
        progress("recursive and non-recursive add")
 
403
        mkdir('no-recurse')
 
404
        chdir('no-recurse')
 
405
        runbzr('init')
 
406
        mkdir('foo')
 
407
        fp = os.path.join('foo', 'test.txt')
 
408
        f = file(fp, 'w')
 
409
        f.write('hello!\n')
 
410
        f.close()
 
411
        runbzr('add --no-recurse foo')
 
412
        runbzr('file-id foo')
 
413
        runbzr('file-id ' + fp, 1)      # not versioned yet
 
414
        runbzr('commit -m add-dir-only')
 
415
 
 
416
        self.runbzr('file-id ' + fp, 1)      # still not versioned 
 
417
 
 
418
        self.runbzr('add foo')
 
419
        self.runbzr('file-id ' + fp)
 
420
        self.runbzr('commit -m add-sub-file')
 
421
 
 
422
        chdir('..')
 
423
 
 
424
 
 
425
 
 
426
class RevertCommand(ExternalBase):
 
427
    def runTest(self):
 
428
        self.runbzr('init')
 
429
 
 
430
        file('hello', 'wt').write('foo')
 
431
        self.runbzr('add hello')
 
432
        self.runbzr('commit -m setup hello')
 
433
        
 
434
        file('hello', 'wt').write('bar')
 
435
        self.runbzr('revert hello')
 
436
        self.check_file_contents('hello', 'foo')
 
437