~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-29 01:16:16 UTC
  • Revision ID: mbp@sourcefrog.net-20050329011616-04e45f8670fe5141
Store.add defaults to adding gzipped 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
 
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
 
        self.runbzr('init')
132
 
 
133
 
        file('hello', 'wt').write('foo')
134
 
        self.runbzr('add hello')
135
 
        self.runbzr('commit -m setup hello')
136
 
 
137
 
        file('goodbye', 'wt').write('baz')
138
 
        self.runbzr('add goodbye')
139
 
        self.runbzr('commit -m setup goodbye')
140
 
        
141
 
        file('hello', 'wt').write('bar')
142
 
        file('goodbye', 'wt').write('qux')
143
 
        self.runbzr('revert hello')
144
 
        self.check_file_contents('hello', 'foo')
145
 
        self.check_file_contents('goodbye', 'qux')
146
 
        self.runbzr('revert')
147
 
        self.check_file_contents('goodbye', 'baz')
148
 
 
149
 
        os.mkdir('revertdir')
150
 
        self.runbzr('add revertdir')
151
 
        self.runbzr('commit -m f')
152
 
        os.rmdir('revertdir')
153
 
        self.runbzr('revert')
154
 
 
155
 
    def skipped_test_mv_modes(self):
156
 
        """Test two modes of operation for mv"""
157
 
        from bzrlib.branch import Branch
158
 
        b = Branch('.', init=True)
159
 
        self.build_tree(['a', 'c', 'subdir/'])
160
 
        self.run_bzr('mv', 'a', 'b')
161
 
        self.run_bzr('mv', 'b', 'subdir')
162
 
        self.run_bzr('mv', 'subdir/b', 'a')
163
 
        self.run_bzr('mv', 'a', 'b', 'subdir')
164
 
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
165
 
 
166
 
    def test_main_version(self):
167
 
        """Check output from version command and master option is reasonable"""
168
 
        # output is intentionally passed through to stdout so that we
169
 
        # can see the version being tested
170
 
        output = self.runbzr('version', backtick=1)
171
 
        self.log('bzr version output:')
172
 
        self.log(output)
173
 
        self.assert_(output.startswith('bzr (bazaar-ng) '))
174
 
        self.assertNotEqual(output.index('Canonical'), -1)
175
 
        # make sure --version is consistent
176
 
        tmp_output = self.runbzr('--version', backtick=1)
177
 
        self.log('bzr --version output:')
178
 
        self.log(tmp_output)
179
 
        self.assertEquals(output, tmp_output)
180
 
 
181
 
    def example_branch(test):
182
 
        test.runbzr('init')
183
 
        file('hello', 'wt').write('foo')
184
 
        test.runbzr('add hello')
185
 
        test.runbzr('commit -m setup hello')
186
 
        file('goodbye', 'wt').write('baz')
187
 
        test.runbzr('add goodbye')
188
 
        test.runbzr('commit -m setup goodbye')
189
 
 
190
 
    def test_revert(self):
191
 
        self.example_branch()
192
 
        file('hello', 'wt').write('bar')
193
 
        file('goodbye', 'wt').write('qux')
194
 
        self.runbzr('revert hello')
195
 
        self.check_file_contents('hello', 'foo')
196
 
        self.check_file_contents('goodbye', 'qux')
197
 
        self.runbzr('revert')
198
 
        self.check_file_contents('goodbye', 'baz')
199
 
 
200
 
    def test_merge(self):
201
 
        from bzrlib.branch import Branch
202
 
        import os
203
 
        os.mkdir('a')
204
 
        os.chdir('a')
205
 
        self.example_branch()
206
 
        os.chdir('..')
207
 
        self.runbzr('branch a b')
208
 
        os.chdir('b')
209
 
        file('goodbye', 'wt').write('quux')
210
 
        self.runbzr(['commit',  '-m',  "more u's are always good"])
211
 
 
212
 
        os.chdir('../a')
213
 
        file('hello', 'wt').write('quuux')
214
 
        # We can't merge when there are in-tree changes
215
 
        self.runbzr('merge ../b', retcode=1)
216
 
        self.runbzr(['commit', '-m', "Like an epidemic of u's"])
217
 
        self.runbzr('merge ../b')
218
 
        self.check_file_contents('goodbye', 'quux')
219
 
        # Merging a branch pulls its revision into the tree
220
 
        a = Branch('.')
221
 
        b = Branch('../b')
222
 
        a.get_revision_xml(b.last_patch())
223
 
        print "Pending: %s" % a.pending_merges()
224
 
#        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
225
 
#        % (a.pending_merges(), b.last_patch())
226
 
 
227
 
class OldTests(ExternalBase):
228
 
    """old tests moved from ./testbzr."""
229
 
 
230
 
    def test_bzr(self):
231
 
        from os import chdir, mkdir
232
 
        from os.path import exists
233
 
        import os
234
 
 
235
 
        runbzr = self.runbzr
236
 
        backtick = self.backtick
237
 
        progress = self.log
238
 
 
239
 
        progress("basic branch creation")
240
 
        mkdir('branch1')
241
 
        chdir('branch1')
242
 
        runbzr('init')
243
 
 
244
 
        self.assertEquals(backtick('bzr root').rstrip(),
245
 
                          os.path.join(self.test_dir, 'branch1'))
246
 
 
247
 
        progress("status of new file")
248
 
 
249
 
        f = file('test.txt', 'wt')
250
 
        f.write('hello world!\n')
251
 
        f.close()
252
 
 
253
 
        out = backtick("bzr unknowns")
254
 
        self.assertEquals(out, 'test.txt\n')
255
 
 
256
 
        out = backtick("bzr status")
257
 
        assert out == 'unknown:\n  test.txt\n'
258
 
 
259
 
        out = backtick("bzr status --all")
260
 
        assert out == "unknown:\n  test.txt\n"
261
 
 
262
 
        out = backtick("bzr status test.txt --all")
263
 
        assert out == "unknown:\n  test.txt\n"
264
 
 
265
 
        f = file('test2.txt', 'wt')
266
 
        f.write('goodbye cruel world...\n')
267
 
        f.close()
268
 
 
269
 
        out = backtick("bzr status test.txt")
270
 
        assert out == "unknown:\n  test.txt\n"
271
 
 
272
 
        out = backtick("bzr status")
273
 
        assert out == ("unknown:\n"
274
 
                       "  test.txt\n"
275
 
                       "  test2.txt\n")
276
 
 
277
 
        os.unlink('test2.txt')
278
 
 
279
 
        progress("command aliases")
280
 
        out = backtick("bzr st --all")
281
 
        assert out == ("unknown:\n"
282
 
                       "  test.txt\n")
283
 
 
284
 
        out = backtick("bzr stat")
285
 
        assert out == ("unknown:\n"
286
 
                       "  test.txt\n")
287
 
 
288
 
        progress("command help")
289
 
        runbzr("help st")
290
 
        runbzr("help")
291
 
        runbzr("help commands")
292
 
        runbzr("help slartibartfast", 1)
293
 
 
294
 
        out = backtick("bzr help ci")
295
 
        out.index('aliases: ')
296
 
 
297
 
        progress("can't rename unversioned file")
298
 
        runbzr("rename test.txt new-test.txt", 1)
299
 
 
300
 
        progress("adding a file")
301
 
 
302
 
        runbzr("add test.txt")
303
 
        assert backtick("bzr unknowns") == ''
304
 
        assert backtick("bzr status --all") == ("added:\n"
305
 
                                                "  test.txt\n")
306
 
 
307
 
        progress("rename newly-added file")
308
 
        runbzr("rename test.txt hello.txt")
309
 
        assert os.path.exists("hello.txt")
310
 
        assert not os.path.exists("test.txt")
311
 
 
312
 
        assert backtick("bzr revno") == '0\n'
313
 
 
314
 
        progress("add first revision")
315
 
        runbzr(['commit', '-m', 'add first revision'])
316
 
 
317
 
        progress("more complex renames")
318
 
        os.mkdir("sub1")
319
 
        runbzr("rename hello.txt sub1", 1)
320
 
        runbzr("rename hello.txt sub1/hello.txt", 1)
321
 
        runbzr("move hello.txt sub1", 1)
322
 
 
323
 
        runbzr("add sub1")
324
 
        runbzr("rename sub1 sub2")
325
 
        runbzr("move hello.txt sub2")
326
 
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
327
 
 
328
 
        assert exists("sub2")
329
 
        assert exists("sub2/hello.txt")
330
 
        assert not exists("sub1")
331
 
        assert not exists("hello.txt")
332
 
 
333
 
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
334
 
 
335
 
        mkdir("sub1")
336
 
        runbzr('add sub1')
337
 
        runbzr('move sub2/hello.txt sub1')
338
 
        assert not exists('sub2/hello.txt')
339
 
        assert exists('sub1/hello.txt')
340
 
        runbzr('move sub2 sub1')
341
 
        assert not exists('sub2')
342
 
        assert exists('sub1/sub2')
343
 
 
344
 
        runbzr(['commit', '-m', 'rename nested subdirectories'])
345
 
 
346
 
        chdir('sub1/sub2')
347
 
        self.assertEquals(backtick('bzr root')[:-1],
348
 
                          os.path.join(self.test_dir, 'branch1'))
349
 
        runbzr('move ../hello.txt .')
350
 
        assert exists('./hello.txt')
351
 
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
352
 
        assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
353
 
        runbzr(['commit', '-m', 'move to parent directory'])
354
 
        chdir('..')
355
 
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
356
 
 
357
 
        runbzr('move sub2/hello.txt .')
358
 
        assert exists('hello.txt')
359
 
 
360
 
        f = file('hello.txt', 'wt')
361
 
        f.write('some nice new content\n')
362
 
        f.close()
363
 
 
364
 
        f = file('msg.tmp', 'wt')
365
 
        f.write('this is my new commit\n')
366
 
        f.close()
367
 
 
368
 
        runbzr('commit -F msg.tmp')
369
 
 
370
 
        assert backtick('bzr revno') == '5\n'
371
 
        runbzr('export -r 5 export-5.tmp')
372
 
        runbzr('export export.tmp')
373
 
 
374
 
        runbzr('log')
375
 
        runbzr('log -v')
376
 
        runbzr('log -v --forward')
377
 
        runbzr('log -m', retcode=1)
378
 
        log_out = backtick('bzr log -m commit')
379
 
        assert "this is my new commit" in log_out
380
 
        assert "rename nested" not in log_out
381
 
        assert 'revision-id' not in log_out
382
 
        assert 'revision-id' in backtick('bzr log --show-ids -m commit')
383
 
 
384
 
 
385
 
        progress("file with spaces in name")
386
 
        mkdir('sub directory')
387
 
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
388
 
        runbzr('add .')
389
 
        runbzr('diff')
390
 
        runbzr('commit -m add-spaces')
391
 
        runbzr('check')
392
 
 
393
 
        runbzr('log')
394
 
        runbzr('log --forward')
395
 
 
396
 
        runbzr('info')
397