1
# Copyright (C) 2005 by Canonical Ltd
2
# -*- coding: utf-8 -*-
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.
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.
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
19
"""Black-box tests for bzr.
21
These check that it behaves properly when it's invoked through the regular
22
command-line interface.
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.
29
# this code was previously in testbzr
31
from unittest import TestCase
32
from bzrlib.selftest import TestBase, InTempDir
36
class ExternalBase(InTempDir):
37
def runbzr(self, args, retcode=0):
40
from subprocess import call
41
except ImportError, e:
45
if isinstance(args, basestring):
48
return self.runcmd(['python', self.BZRPATH,] + args,
53
class TestVersion(ExternalBase):
55
# output is intentionally passed through to stdout so that we
56
# can see the version being tested
57
self.runbzr(['version'])
61
class HelpCommands(ExternalBase):
65
self.runbzr('help commands')
66
self.runbzr('help help')
67
self.runbzr('commit -h')
70
class InitBranch(ExternalBase):
77
class UserIdentity(ExternalBase):
79
# this should always identify something, if only "john@localhost"
81
self.runbzr("whoami --email")
82
self.assertEquals(self.backtick("bzr whoami --email").count('@'),
86
class InvalidCommands(ExternalBase):
88
self.runbzr("pants", retcode=1)
89
self.runbzr("--pants off", retcode=1)
90
self.runbzr("diff --message foo", retcode=1)
94
class EmptyCommit(ExternalBase):
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")
104
class OldTests(ExternalBase):
105
# old tests moved from ./testbzr
107
from os import chdir, mkdir
108
from os.path import exists
112
backtick = self.backtick
115
progress("basic branch creation")
120
self.assertEquals(backtick('bzr root').rstrip(),
121
os.path.join(self.test_dir, 'branch1'))
123
progress("status of new file")
125
f = file('test.txt', 'wt')
126
f.write('hello world!\n')
129
out = backtick("bzr unknowns")
130
self.assertEquals(out, 'test.txt\n')
132
out = backtick("bzr status")
133
assert out == 'unknown:\n test.txt\n'
135
out = backtick("bzr status --all")
136
assert out == "unknown:\n test.txt\n"
138
out = backtick("bzr status test.txt --all")
139
assert out == "unknown:\n test.txt\n"
141
f = file('test2.txt', 'wt')
142
f.write('goodbye cruel world...\n')
145
out = backtick("bzr status test.txt")
146
assert out == "unknown:\n test.txt\n"
148
out = backtick("bzr status")
149
assert out == ("unknown:\n"
153
os.unlink('test2.txt')
155
progress("command aliases")
156
out = backtick("bzr st --all")
157
assert out == ("unknown:\n"
160
out = backtick("bzr stat")
161
assert out == ("unknown:\n"
164
progress("command help")
167
runbzr("help commands")
168
runbzr("help slartibartfast", 1)
170
out = backtick("bzr help ci")
171
out.index('aliases: ')
173
progress("can't rename unversioned file")
174
runbzr("rename test.txt new-test.txt", 1)
176
progress("adding a file")
178
runbzr("add test.txt")
179
assert backtick("bzr unknowns") == ''
180
assert backtick("bzr status --all") == ("added:\n"
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")
188
assert backtick("bzr revno") == '0\n'
190
progress("add first revision")
191
runbzr(['commit', '-m', 'add first revision'])
193
progress("more complex renames")
195
runbzr("rename hello.txt sub1", 1)
196
runbzr("rename hello.txt sub1/hello.txt", 1)
197
runbzr("move hello.txt sub1", 1)
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")
204
assert exists("sub2")
205
assert exists("sub2/hello.txt")
206
assert not exists("sub1")
207
assert not exists("hello.txt")
209
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
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')
220
runbzr(['commit', '-m', 'rename nested subdirectories'])
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'])
231
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
233
runbzr('move sub2/hello.txt .')
234
assert exists('hello.txt')
236
f = file('hello.txt', 'wt')
237
f.write('some nice new content\n')
240
f = file('msg.tmp', 'wt')
241
f.write('this is my new commit\n')
244
runbzr('commit -F msg.tmp')
246
assert backtick('bzr revno') == '5\n'
247
runbzr('export -r 5 export-5.tmp')
248
runbzr('export export.tmp')
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')
260
runbzr('commit -m add-spaces')
264
runbzr('log --forward')
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')
284
runbzr('pull', retcode=1)
285
runbzr('pull ../branch2')
288
runbzr('commit --unchanged -m empty')
290
chdir('../../branch2')
292
runbzr('commit --unchanged -m empty')
294
runbzr('commit --unchanged -m empty')
295
runbzr('pull', retcode=1)
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...
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')
312
file('a', 'w').write('foo')
314
runbzr(['commit', '-m', 'add a'])
320
progress('ignore patterns')
321
mkdir('ignorebranch')
322
chdir('ignorebranch')
324
assert backtick('bzr unknowns') == ''
326
file('foo.tmp', 'wt').write('tmp files are ignored')
327
assert backtick('bzr unknowns') == ''
329
file('foo.c', 'wt').write('int main() {}')
330
assert backtick('bzr unknowns') == 'foo.c\n'
332
assert backtick('bzr unknowns') == ''
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'
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'
353
progress("recursive and non-recursive add")
358
fp = os.path.join('foo', 'test.txt')
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')
367
self.runbzr('file-id ' + fp, 1) # still not versioned
369
self.runbzr('add foo')
370
self.runbzr('file-id ' + fp)
371
self.runbzr('commit -m add-sub-file')
377
class RevertCommand(ExternalBase):
381
file('hello', 'wt').write('foo')
382
self.runbzr('add hello')
383
self.runbzr('commit -m setup hello')
385
file('hello', 'wt').write('bar')
386
self.runbzr('revert hello')
387
self.check_file_contents('hello', 'foo')
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,