~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
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
29
import sys
1102 by Martin Pool
- merge test refactoring from robertc
30
from bzrlib.selftest import InTempDir, BzrTestBase
898 by Martin Pool
- add new runbzr method for external tests
31
32
904 by Martin Pool
- more selftest external-command fixes
33
class ExternalBase(InTempDir):
1102 by Martin Pool
- merge test refactoring from robertc
34
1074 by Martin Pool
- check for email address in BRANCH_ROOT/.bzr/email, so you can
35
    def runbzr(self, args, retcode=0,backtick=False):
898 by Martin Pool
- add new runbzr method for external tests
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()
1074 by Martin Pool
- check for email address in BRANCH_ROOT/.bzr/email, so you can
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,
904 by Martin Pool
- more selftest external-command fixes
51
                           retcode=retcode)
52
1102 by Martin Pool
- merge test refactoring from robertc
53
class TestCommands(ExternalBase):
54
55
    def test_help_commands(self):
898 by Martin Pool
- add new runbzr method for external tests
56
        self.runbzr('--help')
57
        self.runbzr('help')
58
        self.runbzr('help commands')
59
        self.runbzr('help help')
60
        self.runbzr('commit -h')
727 by Martin Pool
- move more code to run external commands from testbzr to selftest
61
1102 by Martin Pool
- merge test refactoring from robertc
62
    def test_init_branch(self):
726 by Martin Pool
- more rearrangement of blackbox tests
63
        import os
898 by Martin Pool
- add new runbzr method for external tests
64
        self.runbzr(['init'])
732 by Martin Pool
- move more tests into bzr selftest
65
1102 by Martin Pool
- merge test refactoring from robertc
66
    def test_whoami(self):
732 by Martin Pool
- move more tests into bzr selftest
67
        # this should always identify something, if only "john@localhost"
898 by Martin Pool
- add new runbzr method for external tests
68
        self.runbzr("whoami")
69
        self.runbzr("whoami --email")
1074 by Martin Pool
- check for email address in BRANCH_ROOT/.bzr/email, so you can
70
71
        self.assertEquals(self.runbzr("whoami --email",
72
                                      backtick=True).count('@'), 1)
73
        
1102 by Martin Pool
- merge test refactoring from robertc
74
    def test_whoami_branch(self):
75
        """branch specific user identity works."""
1074 by Martin Pool
- check for email address in BRANCH_ROOT/.bzr/email, so you can
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'))
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
84
1102 by Martin Pool
- merge test refactoring from robertc
85
    def test_invalid_commands(self):
898 by Martin Pool
- add new runbzr method for external tests
86
        self.runbzr("pants", retcode=1)
87
        self.runbzr("--pants off", retcode=1)
88
        self.runbzr("diff --message foo", retcode=1)
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
89
1102 by Martin Pool
- merge test refactoring from robertc
90
    def test_empty_commit(self):
898 by Martin Pool
- add new runbzr method for external tests
91
        self.runbzr("init")
885 by Martin Pool
- commit command refuses unless something is changed or --unchanged is given
92
        self.build_tree(['hello.txt'])
898 by Martin Pool
- add new runbzr method for external tests
93
        self.runbzr("commit -m empty", retcode=1)
94
        self.runbzr("add hello.txt")
95
        self.runbzr("commit -m added")
885 by Martin Pool
- commit command refuses unless something is changed or --unchanged is given
96
1102 by Martin Pool
- merge test refactoring from robertc
97
    def test_ignore_patterns(self):
906 by Martin Pool
- split out black-box ignore commands
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'
1102 by Martin Pool
- merge test refactoring from robertc
128
129
    def test_revert(self):
130
        import os
131
132
        self.runbzr('init')
133
134
        file('hello', 'wt').write('foo')
135
        self.runbzr('add hello')
136
        self.runbzr('commit -m setup hello')
137
138
        file('goodbye', 'wt').write('baz')
139
        self.runbzr('add goodbye')
140
        self.runbzr('commit -m setup goodbye')
906 by Martin Pool
- split out black-box ignore commands
141
        
1102 by Martin Pool
- merge test refactoring from robertc
142
        file('hello', 'wt').write('bar')
143
        file('goodbye', 'wt').write('qux')
144
        self.runbzr('revert hello')
145
        self.check_file_contents('hello', 'foo')
146
        self.check_file_contents('goodbye', 'qux')
147
        self.runbzr('revert')
148
        self.check_file_contents('goodbye', 'baz')
149
150
        os.mkdir('revertdir')
151
        self.runbzr('add revertdir')
152
        self.runbzr('commit -m f')
153
        os.rmdir('revertdir')
154
        self.runbzr('revert')
155
156
157
    def skipped_test_mv_modes(self):
158
        """Test two modes of operation for mv"""
159
        from bzrlib.branch import Branch
160
        b = Branch('.', init=True)
161
        self.build_tree(['a', 'c', 'subdir/'])
162
        self.run_bzr('mv', 'a', 'b')
163
        self.run_bzr('mv', 'b', 'subdir')
164
        self.run_bzr('mv', 'subdir/b', 'a')
165
        self.run_bzr('mv', 'a', 'b', 'subdir')
166
        self.run_bzr('mv', 'subdir/a', 'subdir/newa')
167
168
    def test_main_version(self):
169
        """Check output from version command and master option is reasonable"""
170
        # output is intentionally passed through to stdout so that we
171
        # can see the version being tested
172
        output = self.runbzr('version', backtick=1)
173
        self.log('bzr version output:')
174
        self.log(output)
175
        self.assert_(output.startswith('bzr (bazaar-ng) '))
176
        self.assertNotEqual(output.index('Canonical'), -1)
177
        # make sure --version is consistent
178
        tmp_output = self.runbzr('--version', backtick=1)
179
        self.log('bzr --version output:')
180
        self.log(tmp_output)
181
        self.assertEquals(output, tmp_output)
906 by Martin Pool
- split out black-box ignore commands
182
904 by Martin Pool
- more selftest external-command fixes
183
class OldTests(ExternalBase):
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
184
    # old tests moved from ./testbzr
1102 by Martin Pool
- merge test refactoring from robertc
185
    def test_bzr(self):
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
186
        from os import chdir, mkdir
187
        from os.path import exists
188
        import os
189
904 by Martin Pool
- more selftest external-command fixes
190
        runbzr = self.runbzr
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
191
        backtick = self.backtick
192
        progress = self.log
193
194
        progress("basic branch creation")
904 by Martin Pool
- more selftest external-command fixes
195
        mkdir('branch1')
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
196
        chdir('branch1')
898 by Martin Pool
- add new runbzr method for external tests
197
        runbzr('init')
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
198
199
        self.assertEquals(backtick('bzr root').rstrip(),
200
                          os.path.join(self.test_dir, 'branch1'))
201
202
        progress("status of new file")
203
204
        f = file('test.txt', 'wt')
205
        f.write('hello world!\n')
206
        f.close()
207
208
        out = backtick("bzr unknowns")
780 by Martin Pool
- test message improvement
209
        self.assertEquals(out, 'test.txt\n')
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
210
211
        out = backtick("bzr status")
212
        assert out == 'unknown:\n  test.txt\n'
213
214
        out = backtick("bzr status --all")
215
        assert out == "unknown:\n  test.txt\n"
216
217
        out = backtick("bzr status test.txt --all")
218
        assert out == "unknown:\n  test.txt\n"
219
220
        f = file('test2.txt', 'wt')
221
        f.write('goodbye cruel world...\n')
222
        f.close()
223
224
        out = backtick("bzr status test.txt")
225
        assert out == "unknown:\n  test.txt\n"
226
227
        out = backtick("bzr status")
228
        assert out == ("unknown:\n"
229
                       "  test.txt\n"
230
                       "  test2.txt\n")
231
232
        os.unlink('test2.txt')
233
234
        progress("command aliases")
235
        out = backtick("bzr st --all")
236
        assert out == ("unknown:\n"
237
                       "  test.txt\n")
238
239
        out = backtick("bzr stat")
240
        assert out == ("unknown:\n"
241
                       "  test.txt\n")
242
243
        progress("command help")
898 by Martin Pool
- add new runbzr method for external tests
244
        runbzr("help st")
245
        runbzr("help")
246
        runbzr("help commands")
247
        runbzr("help slartibartfast", 1)
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
248
249
        out = backtick("bzr help ci")
250
        out.index('aliases: ')
251
252
        progress("can't rename unversioned file")
898 by Martin Pool
- add new runbzr method for external tests
253
        runbzr("rename test.txt new-test.txt", 1)
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
254
255
        progress("adding a file")
256
898 by Martin Pool
- add new runbzr method for external tests
257
        runbzr("add test.txt")
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
258
        assert backtick("bzr unknowns") == ''
259
        assert backtick("bzr status --all") == ("added:\n"
260
                                                "  test.txt\n")
261
262
        progress("rename newly-added file")
898 by Martin Pool
- add new runbzr method for external tests
263
        runbzr("rename test.txt hello.txt")
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
264
        assert os.path.exists("hello.txt")
265
        assert not os.path.exists("test.txt")
266
267
        assert backtick("bzr revno") == '0\n'
268
269
        progress("add first revision")
904 by Martin Pool
- more selftest external-command fixes
270
        runbzr(['commit', '-m', 'add first revision'])
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
271
272
        progress("more complex renames")
273
        os.mkdir("sub1")
898 by Martin Pool
- add new runbzr method for external tests
274
        runbzr("rename hello.txt sub1", 1)
275
        runbzr("rename hello.txt sub1/hello.txt", 1)
276
        runbzr("move hello.txt sub1", 1)
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
277
898 by Martin Pool
- add new runbzr method for external tests
278
        runbzr("add sub1")
279
        runbzr("rename sub1 sub2")
280
        runbzr("move hello.txt sub2")
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
281
        assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
282
283
        assert exists("sub2")
284
        assert exists("sub2/hello.txt")
285
        assert not exists("sub1")
286
        assert not exists("hello.txt")
287
898 by Martin Pool
- add new runbzr method for external tests
288
        runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
289
290
        mkdir("sub1")
898 by Martin Pool
- add new runbzr method for external tests
291
        runbzr('add sub1')
292
        runbzr('move sub2/hello.txt sub1')
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
293
        assert not exists('sub2/hello.txt')
294
        assert exists('sub1/hello.txt')
898 by Martin Pool
- add new runbzr method for external tests
295
        runbzr('move sub2 sub1')
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
296
        assert not exists('sub2')
297
        assert exists('sub1/sub2')
298
898 by Martin Pool
- add new runbzr method for external tests
299
        runbzr(['commit', '-m', 'rename nested subdirectories'])
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
300
301
        chdir('sub1/sub2')
302
        self.assertEquals(backtick('bzr root')[:-1],
303
                          os.path.join(self.test_dir, 'branch1'))
898 by Martin Pool
- add new runbzr method for external tests
304
        runbzr('move ../hello.txt .')
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
305
        assert exists('./hello.txt')
306
        assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
307
        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
308
        runbzr(['commit', '-m', 'move to parent directory'])
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
309
        chdir('..')
310
        assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
311
898 by Martin Pool
- add new runbzr method for external tests
312
        runbzr('move sub2/hello.txt .')
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
313
        assert exists('hello.txt')
314
315
        f = file('hello.txt', 'wt')
316
        f.write('some nice new content\n')
317
        f.close()
318
319
        f = file('msg.tmp', 'wt')
320
        f.write('this is my new commit\n')
321
        f.close()
322
898 by Martin Pool
- add new runbzr method for external tests
323
        runbzr('commit -F msg.tmp')
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
324
325
        assert backtick('bzr revno') == '5\n'
898 by Martin Pool
- add new runbzr method for external tests
326
        runbzr('export -r 5 export-5.tmp')
327
        runbzr('export export.tmp')
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
328
898 by Martin Pool
- add new runbzr method for external tests
329
        runbzr('log')
330
        runbzr('log -v')
909.1.5 by Aaron Bentley
Fixed log -v (mostly)
331
        runbzr('log -v --forward')
332
        runbzr('log -m', retcode=1)
333
        log_out = backtick('bzr log -m commit')
334
        assert "this is my new commit" in log_out
335
        assert "rename nested" not in log_out
336
        assert 'revision-id' not in log_out
337
        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
338
339
340
        progress("file with spaces in name")
341
        mkdir('sub directory')
342
        file('sub directory/file with spaces ', 'wt').write('see how this works\n')
898 by Martin Pool
- add new runbzr method for external tests
343
        runbzr('add .')
344
        runbzr('diff')
345
        runbzr('commit -m add-spaces')
346
        runbzr('check')
347
348
        runbzr('log')
349
        runbzr('log --forward')
350
351
        runbzr('info')
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
352
353
354
974.1.32 by aaron.bentley at utoronto
Made merge do greedy fetching.
355
def example_branch(test):
356
    test.runbzr('init')
357
358
    file('hello', 'wt').write('foo')
359
    test.runbzr('add hello')
360
    test.runbzr('commit -m setup hello')
361
362
    file('goodbye', 'wt').write('baz')
363
    test.runbzr('add goodbye')
364
    test.runbzr('commit -m setup goodbye')
736 by Martin Pool
- move old blackbox code from testbzr into bzrlib.selftest.blackbox
365
366
904 by Martin Pool
- more selftest external-command fixes
367
class RevertCommand(ExternalBase):
785 by Martin Pool
- add test for external revert command
368
    def runTest(self):
974.1.32 by aaron.bentley at utoronto
Made merge do greedy fetching.
369
        example_branch(self)
785 by Martin Pool
- add test for external revert command
370
        file('hello', 'wt').write('bar')
974.1.16 by Aaron Bentley
Ensured that revert FILE only modifies that file.
371
        file('goodbye', 'wt').write('qux')
898 by Martin Pool
- add new runbzr method for external tests
372
        self.runbzr('revert hello')
785 by Martin Pool
- add test for external revert command
373
        self.check_file_contents('hello', 'foo')
974.1.16 by Aaron Bentley
Ensured that revert FILE only modifies that file.
374
        self.check_file_contents('goodbye', 'qux')
375
        self.runbzr('revert')
376
        self.check_file_contents('goodbye', 'baz')
785 by Martin Pool
- add test for external revert command
377
974.1.32 by aaron.bentley at utoronto
Made merge do greedy fetching.
378
379
class MergeCommand(ExternalBase):
380
    def runTest(self):
381
        from bzrlib.branch import Branch
382
        import os
383
        os.mkdir('a')
384
        os.chdir('a')
385
        example_branch(self)
386
        os.chdir('..')
387
        self.runbzr('branch a b')
388
        os.chdir('b')
389
        file('goodbye', 'wt').write('quux')
390
        self.runbzr(['commit',  '-m',  "more u's are always good"])
391
392
        os.chdir('../a')
393
        file('hello', 'wt').write('quuux')
394
        # We can't merge when there are in-tree changes
395
        self.runbzr('merge ../b', retcode=1)
396
        self.runbzr(['commit', '-m', "Like an epidemic of u's"])
397
        self.runbzr('merge ../b')
398
        self.check_file_contents('goodbye', 'quux')
399
        # Merging a branch pulls its revision into the tree
974.1.36 by aaron.bentley at utoronto
Committed it even though the test case doesn't work
400
        a = Branch('.')
401
        b = Branch('../b')
402
        a.get_revision_xml(b.last_patch())
403
        print "Pending: %s" % a.pending_merges()
404
#        assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
405
#        % (a.pending_merges(), b.last_patch())
406