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