~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-11 07:05:34 UTC
  • Revision ID: mbp@sourcefrog.net-20050711070534-5227696ab167ccde
- merge aaron's append_multiple.patch

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