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.
31
from bzrlib.selftest import TestBase, InTempDir, BzrTestBase
35
class ExternalBase(InTempDir):
36
def runbzr(self, args, retcode=0):
39
from subprocess import call
40
except ImportError, e:
44
if isinstance(args, basestring):
47
return self.runcmd(['python', self.BZRPATH,] + args,
52
class TestVersion(BzrTestBase):
53
"""Check output from version command and master option is reasonable"""
55
# output is intentionally passed through to stdout so that we
56
# can see the version being tested
57
from cStringIO import StringIO
60
sys.stdout = tmp_out = StringIO()
62
self.run_bzr('version')
66
output = tmp_out.getvalue()
67
self.log('bzr version output:')
70
self.assert_(output.startswith('bzr (bazaar-ng) '))
71
self.assertNotEqual(output.index('Canonical'), -1)
73
# make sure --version is consistent
75
sys.stdout = tmp_out = StringIO()
77
self.run_bzr('--version')
81
self.log('bzr --version output:')
82
self.log(tmp_out.getvalue())
84
self.assertEquals(output, tmp_out.getvalue())
90
class HelpCommands(ExternalBase):
94
self.runbzr('help commands')
95
self.runbzr('help help')
96
self.runbzr('commit -h')
99
class InitBranch(ExternalBase):
102
self.runbzr(['init'])
106
class UserIdentity(ExternalBase):
108
# this should always identify something, if only "john@localhost"
109
self.runbzr("whoami")
110
self.runbzr("whoami --email")
111
self.assertEquals(self.backtick("bzr whoami --email").count('@'),
115
class InvalidCommands(ExternalBase):
117
self.runbzr("pants", retcode=1)
118
self.runbzr("--pants off", retcode=1)
119
self.runbzr("diff --message foo", retcode=1)
123
class EmptyCommit(ExternalBase):
126
self.build_tree(['hello.txt'])
127
self.runbzr("commit -m empty", retcode=1)
128
self.runbzr("add hello.txt")
129
self.runbzr("commit -m added")
133
class IgnorePatterns(ExternalBase):
135
from bzrlib.branch import Branch
137
b = Branch('.', init=True)
138
self.assertEquals(list(b.unknowns()), [])
140
file('foo.tmp', 'wt').write('tmp files are ignored')
141
self.assertEquals(list(b.unknowns()), [])
142
assert self.backtick('bzr unknowns') == ''
144
file('foo.c', 'wt').write('int main() {}')
145
self.assertEquals(list(b.unknowns()), ['foo.c'])
146
assert self.backtick('bzr unknowns') == 'foo.c\n'
148
self.runbzr(['add', 'foo.c'])
149
assert self.backtick('bzr unknowns') == ''
151
# 'ignore' works when creating the .bzignore file
152
file('foo.blah', 'wt').write('blah')
153
self.assertEquals(list(b.unknowns()), ['foo.blah'])
154
self.runbzr('ignore *.blah')
155
self.assertEquals(list(b.unknowns()), [])
156
assert file('.bzrignore', 'rb').read() == '*.blah\n'
158
# 'ignore' works when then .bzrignore file already exists
159
file('garh', 'wt').write('garh')
160
self.assertEquals(list(b.unknowns()), ['garh'])
161
assert self.backtick('bzr unknowns') == 'garh\n'
162
self.runbzr('ignore garh')
163
self.assertEquals(list(b.unknowns()), [])
164
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
169
class OldTests(ExternalBase):
170
# old tests moved from ./testbzr
172
from os import chdir, mkdir
173
from os.path import exists
177
backtick = self.backtick
180
progress("basic branch creation")
185
self.assertEquals(backtick('bzr root').rstrip(),
186
os.path.join(self.test_dir, 'branch1'))
188
progress("status of new file")
190
f = file('test.txt', 'wt')
191
f.write('hello world!\n')
194
out = backtick("bzr unknowns")
195
self.assertEquals(out, 'test.txt\n')
197
out = backtick("bzr status")
198
assert out == 'unknown:\n test.txt\n'
200
out = backtick("bzr status --all")
201
assert out == "unknown:\n test.txt\n"
203
out = backtick("bzr status test.txt --all")
204
assert out == "unknown:\n test.txt\n"
206
f = file('test2.txt', 'wt')
207
f.write('goodbye cruel world...\n')
210
out = backtick("bzr status test.txt")
211
assert out == "unknown:\n test.txt\n"
213
out = backtick("bzr status")
214
assert out == ("unknown:\n"
218
os.unlink('test2.txt')
220
progress("command aliases")
221
out = backtick("bzr st --all")
222
assert out == ("unknown:\n"
225
out = backtick("bzr stat")
226
assert out == ("unknown:\n"
229
progress("command help")
232
runbzr("help commands")
233
runbzr("help slartibartfast", 1)
235
out = backtick("bzr help ci")
236
out.index('aliases: ')
238
progress("can't rename unversioned file")
239
runbzr("rename test.txt new-test.txt", 1)
241
progress("adding a file")
243
runbzr("add test.txt")
244
assert backtick("bzr unknowns") == ''
245
assert backtick("bzr status --all") == ("added:\n"
248
progress("rename newly-added file")
249
runbzr("rename test.txt hello.txt")
250
assert os.path.exists("hello.txt")
251
assert not os.path.exists("test.txt")
253
assert backtick("bzr revno") == '0\n'
255
progress("add first revision")
256
runbzr(['commit', '-m', 'add first revision'])
258
progress("more complex renames")
260
runbzr("rename hello.txt sub1", 1)
261
runbzr("rename hello.txt sub1/hello.txt", 1)
262
runbzr("move hello.txt sub1", 1)
265
runbzr("rename sub1 sub2")
266
runbzr("move hello.txt sub2")
267
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
269
assert exists("sub2")
270
assert exists("sub2/hello.txt")
271
assert not exists("sub1")
272
assert not exists("hello.txt")
274
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
278
runbzr('move sub2/hello.txt sub1')
279
assert not exists('sub2/hello.txt')
280
assert exists('sub1/hello.txt')
281
runbzr('move sub2 sub1')
282
assert not exists('sub2')
283
assert exists('sub1/sub2')
285
runbzr(['commit', '-m', 'rename nested subdirectories'])
288
self.assertEquals(backtick('bzr root')[:-1],
289
os.path.join(self.test_dir, 'branch1'))
290
runbzr('move ../hello.txt .')
291
assert exists('./hello.txt')
292
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
293
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
294
runbzr(['commit', '-m', 'move to parent directory'])
296
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
298
runbzr('move sub2/hello.txt .')
299
assert exists('hello.txt')
301
f = file('hello.txt', 'wt')
302
f.write('some nice new content\n')
305
f = file('msg.tmp', 'wt')
306
f.write('this is my new commit\n')
309
runbzr('commit -F msg.tmp')
311
assert backtick('bzr revno') == '5\n'
312
runbzr('export -r 5 export-5.tmp')
313
runbzr('export export.tmp')
317
runbzr('log -v --forward')
318
runbzr('log -m', retcode=1)
319
log_out = backtick('bzr log -m commit')
320
assert "this is my new commit" in log_out
321
assert "rename nested" not in log_out
322
assert 'revision-id' not in log_out
323
assert 'revision-id' in backtick('bzr log --show-ids -m commit')
326
progress("file with spaces in name")
327
mkdir('sub directory')
328
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
331
runbzr('commit -m add-spaces')
335
runbzr('log --forward')
347
assert os.path.exists('branch1')
348
assert not os.path.exists('branch2')
349
# Can't create a branch if it already exists
350
runbzr('branch branch1', retcode=1)
351
# Can't create a branch if its parent doesn't exist
352
runbzr('branch /unlikely/to/exist', retcode=1)
353
runbzr('branch branch1 branch2')
354
assert exists('branch2')
355
assert exists('branch2/sub1')
356
assert exists('branch2/sub1/hello.txt')
358
runbzr('branch --revision 0 branch1 branch3')
359
assert not exists('branch3/sub1/hello.txt')
360
runbzr('branch --revision 0..3 branch1 branch4', retcode=1)
364
runbzr('pull', retcode=1)
365
runbzr('pull ../branch2')
368
runbzr('commit --unchanged -m empty')
370
chdir('../../branch2')
372
runbzr('commit --unchanged -m empty')
374
runbzr('commit --unchanged -m empty')
375
runbzr('pull', retcode=1)
378
progress('status after remove')
379
mkdir('status-after-remove')
380
# see mail from William Dodé, 2005-05-25
381
# $ bzr init; touch a; bzr add a; bzr commit -m "add a"
382
# * looking for changes...
387
# bzr: local variable 'kind' referenced before assignment
388
# at /vrac/python/bazaar-ng/bzrlib/diff.py:286 in compare_trees()
389
# see ~/.bzr.log for debug information
390
chdir('status-after-remove')
392
file('a', 'w').write('foo')
394
runbzr(['commit', '-m', 'add a'])
402
progress("recursive and non-recursive add")
407
fp = os.path.join('foo', 'test.txt')
411
runbzr('add --no-recurse foo')
412
runbzr('file-id foo')
413
runbzr('file-id ' + fp, 1) # not versioned yet
414
runbzr('commit -m add-dir-only')
416
self.runbzr('file-id ' + fp, 1) # still not versioned
418
self.runbzr('add foo')
419
self.runbzr('file-id ' + fp)
420
self.runbzr('commit -m add-sub-file')
426
class RevertCommand(ExternalBase):
430
file('hello', 'wt').write('foo')
431
self.runbzr('add hello')
432
self.runbzr('commit -m setup hello')
434
file('hello', 'wt').write('bar')
435
self.runbzr('revert hello')
436
self.check_file_contents('hello', 'foo')