~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-18 07:51:58 UTC
  • Revision ID: mbp@sourcefrog.net-20050818075157-5f69075fa843d558
- add space to store revision-id in weave files

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,backtick=False):
 
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
        if backtick:
 
48
            return self.backtick(['python', self.BZRPATH,] + args,
 
49
                           retcode=retcode)
 
50
        else:
 
51
            return self.runcmd(['python', self.BZRPATH,] + args,
 
52
                           retcode=retcode)
 
53
 
 
54
 
 
55
 
 
56
class MvCommand(BzrTestBase):
 
57
    def runbzr(self):
 
58
        """Test two modes of operation for mv"""
 
59
        b = Branch('.', init=True)
 
60
        self.build_tree(['a', 'c', 'subdir/'])
 
61
        self.run_bzr('mv', 'a', 'b')
 
62
        self.run_bzr('mv', 'b', 'subdir')
 
63
        self.run_bzr('mv', 'subdir/b', 'a')
 
64
        self.run_bzr('mv', 'a', 'b', 'subdir')
 
65
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
 
66
 
 
67
 
 
68
 
 
69
class TestVersion(BzrTestBase):
 
70
    """Check output from version command and master option is reasonable"""
 
71
    def runTest(self):
 
72
        # output is intentionally passed through to stdout so that we
 
73
        # can see the version being tested
 
74
        from cStringIO import StringIO
 
75
        save_out = sys.stdout
 
76
        try:
 
77
            sys.stdout = tmp_out = StringIO()
 
78
            
 
79
            self.run_bzr('version')
 
80
        finally:
 
81
            sys.stdout = save_out
 
82
 
 
83
        output = tmp_out.getvalue()
 
84
        self.log('bzr version output:')
 
85
        self.log(output)
 
86
        
 
87
        self.assert_(output.startswith('bzr (bazaar-ng) '))
 
88
        self.assertNotEqual(output.index('Canonical'), -1)
 
89
 
 
90
        # make sure --version is consistent
 
91
        try:
 
92
            sys.stdout = tmp_out = StringIO()
 
93
            
 
94
            self.run_bzr('--version')
 
95
        finally:
 
96
            sys.stdout = save_out
 
97
 
 
98
        self.log('bzr --version output:')
 
99
        self.log(tmp_out.getvalue())
 
100
 
 
101
        self.assertEquals(output, tmp_out.getvalue())
 
102
 
 
103
 
 
104
        
 
105
 
 
106
 
 
107
class HelpCommands(ExternalBase):
 
108
    def runTest(self):
 
109
        self.runbzr('--help')
 
110
        self.runbzr('help')
 
111
        self.runbzr('help commands')
 
112
        self.runbzr('help help')
 
113
        self.runbzr('commit -h')
 
114
 
 
115
 
 
116
class InitBranch(ExternalBase):
 
117
    def runTest(self):
 
118
        import os
 
119
        self.runbzr(['init'])
 
120
 
 
121
 
 
122
 
 
123
class UserIdentity(ExternalBase):
 
124
    def runTest(self):
 
125
        # this should always identify something, if only "john@localhost"
 
126
        self.runbzr("whoami")
 
127
        self.runbzr("whoami --email")
 
128
 
 
129
        self.assertEquals(self.runbzr("whoami --email",
 
130
                                      backtick=True).count('@'), 1)
 
131
        
 
132
class UserIdentityBranch(ExternalBase):
 
133
    def runTest(self):
 
134
        # tests branch specific user identity
 
135
        self.runbzr('init')
 
136
        f = file('.bzr/email', 'wt')
 
137
        f.write('Branch Identity <branch@identi.ty>')
 
138
        f.close()
 
139
        whoami = self.runbzr("whoami",backtick=True)
 
140
        whoami_email = self.runbzr("whoami --email",backtick=True)
 
141
        self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
 
142
        self.assertTrue(whoami_email.startswith('branch@identi.ty'))
 
143
 
 
144
 
 
145
class InvalidCommands(ExternalBase):
 
146
    def runTest(self):
 
147
        self.runbzr("pants", retcode=1)
 
148
        self.runbzr("--pants off", retcode=1)
 
149
        self.runbzr("diff --message foo", retcode=1)
 
150
 
 
151
 
 
152
 
 
153
class EmptyCommit(ExternalBase):
 
154
    def runTest(self):
 
155
        self.runbzr("init")
 
156
        self.build_tree(['hello.txt'])
 
157
        self.runbzr("commit -m empty", retcode=1)
 
158
        self.runbzr("add hello.txt")
 
159
        self.runbzr("commit -m added")
 
160
 
 
161
 
 
162
 
 
163
class IgnorePatterns(ExternalBase):
 
164
    def runTest(self):
 
165
        from bzrlib.branch import Branch
 
166
        
 
167
        b = Branch('.', init=True)
 
168
        self.assertEquals(list(b.unknowns()), [])
 
169
 
 
170
        file('foo.tmp', 'wt').write('tmp files are ignored')
 
171
        self.assertEquals(list(b.unknowns()), [])
 
172
        assert self.backtick('bzr unknowns') == ''
 
173
 
 
174
        file('foo.c', 'wt').write('int main() {}')
 
175
        self.assertEquals(list(b.unknowns()), ['foo.c'])
 
176
        assert self.backtick('bzr unknowns') == 'foo.c\n'
 
177
 
 
178
        self.runbzr(['add', 'foo.c'])
 
179
        assert self.backtick('bzr unknowns') == ''
 
180
 
 
181
        # 'ignore' works when creating the .bzignore file
 
182
        file('foo.blah', 'wt').write('blah')
 
183
        self.assertEquals(list(b.unknowns()), ['foo.blah'])
 
184
        self.runbzr('ignore *.blah')
 
185
        self.assertEquals(list(b.unknowns()), [])
 
186
        assert file('.bzrignore', 'rb').read() == '*.blah\n'
 
187
 
 
188
        # 'ignore' works when then .bzrignore file already exists
 
189
        file('garh', 'wt').write('garh')
 
190
        self.assertEquals(list(b.unknowns()), ['garh'])
 
191
        assert self.backtick('bzr unknowns') == 'garh\n'
 
192
        self.runbzr('ignore garh')
 
193
        self.assertEquals(list(b.unknowns()), [])
 
194
        assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
 
195
        
 
196
 
 
197
 
 
198
 
 
199
class OldTests(ExternalBase):
 
200
    # old tests moved from ./testbzr
 
201
    def runTest(self):
 
202
        from os import chdir, mkdir
 
203
        from os.path import exists
 
204
        import os
 
205
 
 
206
        runbzr = self.runbzr
 
207
        backtick = self.backtick
 
208
        progress = self.log
 
209
 
 
210
        progress("basic branch creation")
 
211
        mkdir('branch1')
 
212
        chdir('branch1')
 
213
        runbzr('init')
 
214
 
 
215
        self.assertEquals(backtick('bzr root').rstrip(),
 
216
                          os.path.join(self.test_dir, 'branch1'))
 
217
 
 
218
        progress("status of new file")
 
219
 
 
220
        f = file('test.txt', 'wt')
 
221
        f.write('hello world!\n')
 
222
        f.close()
 
223
 
 
224
        out = backtick("bzr unknowns")
 
225
        self.assertEquals(out, 'test.txt\n')
 
226
 
 
227
        out = backtick("bzr status")
 
228
        assert out == 'unknown:\n  test.txt\n'
 
229
 
 
230
        out = backtick("bzr status --all")
 
231
        assert out == "unknown:\n  test.txt\n"
 
232
 
 
233
        out = backtick("bzr status test.txt --all")
 
234
        assert out == "unknown:\n  test.txt\n"
 
235
 
 
236
        f = file('test2.txt', 'wt')
 
237
        f.write('goodbye cruel world...\n')
 
238
        f.close()
 
239
 
 
240
        out = backtick("bzr status test.txt")
 
241
        assert out == "unknown:\n  test.txt\n"
 
242
 
 
243
        out = backtick("bzr status")
 
244
        assert out == ("unknown:\n"
 
245
                       "  test.txt\n"
 
246
                       "  test2.txt\n")
 
247
 
 
248
        os.unlink('test2.txt')
 
249
 
 
250
        progress("command aliases")
 
251
        out = backtick("bzr st --all")
 
252
        assert out == ("unknown:\n"
 
253
                       "  test.txt\n")
 
254
 
 
255
        out = backtick("bzr stat")
 
256
        assert out == ("unknown:\n"
 
257
                       "  test.txt\n")
 
258
 
 
259
        progress("command help")
 
260
        runbzr("help st")
 
261
        runbzr("help")
 
262
        runbzr("help commands")
 
263
        runbzr("help slartibartfast", 1)
 
264
 
 
265
        out = backtick("bzr help ci")
 
266
        out.index('aliases: ')
 
267
 
 
268
        progress("can't rename unversioned file")
 
269
        runbzr("rename test.txt new-test.txt", 1)
 
270
 
 
271
        progress("adding a file")
 
272
 
 
273
        runbzr("add test.txt")
 
274
        assert backtick("bzr unknowns") == ''
 
275
        assert backtick("bzr status --all") == ("added:\n"
 
276
                                                "  test.txt\n")
 
277
 
 
278
        progress("rename newly-added file")
 
279
        runbzr("rename test.txt hello.txt")
 
280
        assert os.path.exists("hello.txt")
 
281
        assert not os.path.exists("test.txt")
 
282
 
 
283
        assert backtick("bzr revno") == '0\n'
 
284
 
 
285
        progress("add first revision")
 
286
        runbzr(['commit', '-m', 'add first revision'])
 
287
 
 
288
        progress("more complex renames")
 
289
        os.mkdir("sub1")
 
290
        runbzr("rename hello.txt sub1", 1)
 
291
        runbzr("rename hello.txt sub1/hello.txt", 1)
 
292
        runbzr("move hello.txt sub1", 1)
 
293
 
 
294
        runbzr("add sub1")
 
295
        runbzr("rename sub1 sub2")
 
296
        runbzr("move hello.txt sub2")
 
297
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
 
298
 
 
299
        assert exists("sub2")
 
300
        assert exists("sub2/hello.txt")
 
301
        assert not exists("sub1")
 
302
        assert not exists("hello.txt")
 
303
 
 
304
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
 
305
 
 
306
        mkdir("sub1")
 
307
        runbzr('add sub1')
 
308
        runbzr('move sub2/hello.txt sub1')
 
309
        assert not exists('sub2/hello.txt')
 
310
        assert exists('sub1/hello.txt')
 
311
        runbzr('move sub2 sub1')
 
312
        assert not exists('sub2')
 
313
        assert exists('sub1/sub2')
 
314
 
 
315
        runbzr(['commit', '-m', 'rename nested subdirectories'])
 
316
 
 
317
        chdir('sub1/sub2')
 
318
        self.assertEquals(backtick('bzr root')[:-1],
 
319
                          os.path.join(self.test_dir, 'branch1'))
 
320
        runbzr('move ../hello.txt .')
 
321
        assert exists('./hello.txt')
 
322
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
323
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
324
        runbzr(['commit', '-m', 'move to parent directory'])
 
325
        chdir('..')
 
326
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
 
327
 
 
328
        runbzr('move sub2/hello.txt .')
 
329
        assert exists('hello.txt')
 
330
 
 
331
        f = file('hello.txt', 'wt')
 
332
        f.write('some nice new content\n')
 
333
        f.close()
 
334
 
 
335
        f = file('msg.tmp', 'wt')
 
336
        f.write('this is my new commit\n')
 
337
        f.close()
 
338
 
 
339
        runbzr('commit -F msg.tmp')
 
340
 
 
341
        assert backtick('bzr revno') == '5\n'
 
342
        runbzr('export -r 5 export-5.tmp')
 
343
        runbzr('export export.tmp')
 
344
 
 
345
        runbzr('log')
 
346
        runbzr('log -v')
 
347
        runbzr('log -v --forward')
 
348
        runbzr('log -m', retcode=1)
 
349
        log_out = backtick('bzr log -m commit')
 
350
        assert "this is my new commit" in log_out
 
351
        assert "rename nested" not in log_out
 
352
        assert 'revision-id' not in log_out
 
353
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
 
354
 
 
355
 
 
356
        progress("file with spaces in name")
 
357
        mkdir('sub directory')
 
358
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
 
359
        runbzr('add .')
 
360
        runbzr('diff')
 
361
        runbzr('commit -m add-spaces')
 
362
        runbzr('check')
 
363
 
 
364
        runbzr('log')
 
365
        runbzr('log --forward')
 
366
 
 
367
        runbzr('info')
 
368
 
 
369
 
 
370
 
 
371
 
 
372
 
 
373
 
 
374
class RevertCommand(ExternalBase):
 
375
    def runTest(self):
 
376
        self.runbzr('init')
 
377
 
 
378
        file('hello', 'wt').write('foo')
 
379
        self.runbzr('add hello')
 
380
        self.runbzr('commit -m setup hello')
 
381
 
 
382
        file('goodbye', 'wt').write('baz')
 
383
        self.runbzr('add goodbye')
 
384
        self.runbzr('commit -m setup goodbye')
 
385
        
 
386
        file('hello', 'wt').write('bar')
 
387
        file('goodbye', 'wt').write('qux')
 
388
        self.runbzr('revert hello')
 
389
        self.check_file_contents('hello', 'foo')
 
390
        self.check_file_contents('goodbye', 'qux')
 
391
        self.runbzr('revert')
 
392
        self.check_file_contents('goodbye', 'baz')
 
393