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 IgnorePatterns(ExternalBase):
106
from bzrlib.branch import Branch
108
b = Branch('.', init=True)
109
self.assertEquals(list(b.unknowns()), [])
111
file('foo.tmp', 'wt').write('tmp files are ignored')
112
self.assertEquals(list(b.unknowns()), [])
113
assert self.backtick('bzr unknowns') == ''
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'
119
self.runbzr(['add', 'foo.c'])
120
assert self.backtick('bzr unknowns') == ''
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'
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'
140
class OldTests(ExternalBase):
141
# old tests moved from ./testbzr
143
from os import chdir, mkdir
144
from os.path import exists
148
backtick = self.backtick
151
progress("basic branch creation")
156
self.assertEquals(backtick('bzr root').rstrip(),
157
os.path.join(self.test_dir, 'branch1'))
159
progress("status of new file")
161
f = file('test.txt', 'wt')
162
f.write('hello world!\n')
165
out = backtick("bzr unknowns")
166
self.assertEquals(out, 'test.txt\n')
168
out = backtick("bzr status")
169
assert out == 'unknown:\n test.txt\n'
171
out = backtick("bzr status --all")
172
assert out == "unknown:\n test.txt\n"
174
out = backtick("bzr status test.txt --all")
175
assert out == "unknown:\n test.txt\n"
177
f = file('test2.txt', 'wt')
178
f.write('goodbye cruel world...\n')
181
out = backtick("bzr status test.txt")
182
assert out == "unknown:\n test.txt\n"
184
out = backtick("bzr status")
185
assert out == ("unknown:\n"
189
os.unlink('test2.txt')
191
progress("command aliases")
192
out = backtick("bzr st --all")
193
assert out == ("unknown:\n"
196
out = backtick("bzr stat")
197
assert out == ("unknown:\n"
200
progress("command help")
203
runbzr("help commands")
204
runbzr("help slartibartfast", 1)
206
out = backtick("bzr help ci")
207
out.index('aliases: ')
209
progress("can't rename unversioned file")
210
runbzr("rename test.txt new-test.txt", 1)
212
progress("adding a file")
214
runbzr("add test.txt")
215
assert backtick("bzr unknowns") == ''
216
assert backtick("bzr status --all") == ("added:\n"
219
progress("rename newly-added file")
220
runbzr("rename test.txt hello.txt")
221
assert os.path.exists("hello.txt")
222
assert not os.path.exists("test.txt")
224
assert backtick("bzr revno") == '0\n'
226
progress("add first revision")
227
runbzr(['commit', '-m', 'add first revision'])
229
progress("more complex renames")
231
runbzr("rename hello.txt sub1", 1)
232
runbzr("rename hello.txt sub1/hello.txt", 1)
233
runbzr("move hello.txt sub1", 1)
236
runbzr("rename sub1 sub2")
237
runbzr("move hello.txt sub2")
238
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
240
assert exists("sub2")
241
assert exists("sub2/hello.txt")
242
assert not exists("sub1")
243
assert not exists("hello.txt")
245
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
249
runbzr('move sub2/hello.txt sub1')
250
assert not exists('sub2/hello.txt')
251
assert exists('sub1/hello.txt')
252
runbzr('move sub2 sub1')
253
assert not exists('sub2')
254
assert exists('sub1/sub2')
256
runbzr(['commit', '-m', 'rename nested subdirectories'])
259
self.assertEquals(backtick('bzr root')[:-1],
260
os.path.join(self.test_dir, 'branch1'))
261
runbzr('move ../hello.txt .')
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')
265
runbzr(['commit', '-m', 'move to parent directory'])
267
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
269
runbzr('move sub2/hello.txt .')
270
assert exists('hello.txt')
272
f = file('hello.txt', 'wt')
273
f.write('some nice new content\n')
276
f = file('msg.tmp', 'wt')
277
f.write('this is my new commit\n')
280
runbzr('commit -F msg.tmp')
282
assert backtick('bzr revno') == '5\n'
283
runbzr('export -r 5 export-5.tmp')
284
runbzr('export export.tmp')
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')
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')
302
runbzr('commit -m add-spaces')
306
runbzr('log --forward')
318
assert os.path.exists('branch1')
319
assert not os.path.exists('branch2')
320
# Can't create a branch if it already exists
321
runbzr('branch branch1', retcode=1)
322
# Can't create a branch if its parent doesn't exist
323
runbzr('branch /unlikely/to/exist', retcode=1)
324
runbzr('branch branch1 branch2')
325
assert exists('branch2')
326
assert exists('branch2/sub1')
327
assert exists('branch2/sub1/hello.txt')
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)
335
runbzr('pull', retcode=1)
336
runbzr('pull ../branch2')
339
runbzr('commit --unchanged -m empty')
341
chdir('../../branch2')
343
runbzr('commit --unchanged -m empty')
345
runbzr('commit --unchanged -m empty')
346
runbzr('pull', retcode=1)
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...
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')
363
file('a', 'w').write('foo')
365
runbzr(['commit', '-m', 'add a'])
373
progress("recursive and non-recursive add")
378
fp = os.path.join('foo', 'test.txt')
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')
387
self.runbzr('file-id ' + fp, 1) # still not versioned
389
self.runbzr('add foo')
390
self.runbzr('file-id ' + fp)
391
self.runbzr('commit -m add-sub-file')
397
class RevertCommand(ExternalBase):
401
file('hello', 'wt').write('foo')
402
self.runbzr('add hello')
403
self.runbzr('commit -m setup hello')
405
file('hello', 'wt').write('bar')
406
self.runbzr('revert hello')
407
self.check_file_contents('hello', 'foo')