~bzr-pqm/bzr/bzr.dev

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