~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 06:46:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050711064655-1f47c2be222cfe7c
todo

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