~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/blackbox.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-22 01:17:38 UTC
  • Revision ID: mbp@sourcefrog.net-20050322011738-5e778270d06836bb
ignore generated changelog

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