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
from cStringIO import StringIO
33
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
34
from bzrlib.branch import Branch
35
from bzrlib.commands import run_bzr
38
class ExternalBase(TestCaseInTempDir):
39
def runbzr(self, args, retcode=0,backtick=False):
40
if isinstance(args, basestring):
44
return self.backtick(['python', self.BZRPATH,] + args,
47
return self.runcmd(['python', self.BZRPATH,] + args,
51
class TestCommands(ExternalBase):
53
def test_help_commands(self):
56
self.runbzr('help commands')
57
self.runbzr('help help')
58
self.runbzr('commit -h')
60
def test_init_branch(self):
63
def test_whoami(self):
64
# this should always identify something, if only "john@localhost"
66
self.runbzr("whoami --email")
68
self.assertEquals(self.runbzr("whoami --email",
69
backtick=True).count('@'), 1)
71
def test_whoami_branch(self):
72
"""branch specific user identity works."""
74
f = file('.bzr/email', 'wt')
75
f.write('Branch Identity <branch@identi.ty>')
77
bzr_email = os.environ.get('BZREMAIL')
78
if bzr_email is not None:
79
del os.environ['BZREMAIL']
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'))
84
# Verify that the environment variable overrides the value
86
os.environ['BZREMAIL'] = 'Different ID <other@environ.ment>'
87
whoami = self.runbzr("whoami",backtick=True)
88
whoami_email = self.runbzr("whoami --email",backtick=True)
89
self.assertTrue(whoami.startswith('Different ID <other@environ.ment>'))
90
self.assertTrue(whoami_email.startswith('other@environ.ment'))
91
if bzr_email is not None:
92
os.environ['BZREMAIL'] = bzr_email
94
def test_invalid_commands(self):
95
self.runbzr("pants", retcode=1)
96
self.runbzr("--pants off", retcode=1)
97
self.runbzr("diff --message foo", retcode=1)
99
def test_empty_commit(self):
101
self.build_tree(['hello.txt'])
102
self.runbzr("commit -m empty", retcode=1)
103
self.runbzr("add hello.txt")
104
self.runbzr("commit -m added")
106
def test_ignore_patterns(self):
107
from bzrlib.branch import Branch
109
b = Branch.initialize('.')
110
self.assertEquals(list(b.unknowns()), [])
112
file('foo.tmp', 'wt').write('tmp files are ignored')
113
self.assertEquals(list(b.unknowns()), [])
114
assert self.backtick('bzr unknowns') == ''
116
file('foo.c', 'wt').write('int main() {}')
117
self.assertEquals(list(b.unknowns()), ['foo.c'])
118
assert self.backtick('bzr unknowns') == 'foo.c\n'
120
self.runbzr(['add', 'foo.c'])
121
assert self.backtick('bzr unknowns') == ''
123
# 'ignore' works when creating the .bzignore file
124
file('foo.blah', 'wt').write('blah')
125
self.assertEquals(list(b.unknowns()), ['foo.blah'])
126
self.runbzr('ignore *.blah')
127
self.assertEquals(list(b.unknowns()), [])
128
assert file('.bzrignore', 'rb').read() == '*.blah\n'
130
# 'ignore' works when then .bzrignore file already exists
131
file('garh', 'wt').write('garh')
132
self.assertEquals(list(b.unknowns()), ['garh'])
133
assert self.backtick('bzr unknowns') == 'garh\n'
134
self.runbzr('ignore garh')
135
self.assertEquals(list(b.unknowns()), [])
136
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
138
def test_revert(self):
141
file('hello', 'wt').write('foo')
142
self.runbzr('add hello')
143
self.runbzr('commit -m setup hello')
145
file('goodbye', 'wt').write('baz')
146
self.runbzr('add goodbye')
147
self.runbzr('commit -m setup goodbye')
149
file('hello', 'wt').write('bar')
150
file('goodbye', 'wt').write('qux')
151
self.runbzr('revert hello')
152
self.check_file_contents('hello', 'foo')
153
self.check_file_contents('goodbye', 'qux')
154
self.runbzr('revert')
155
self.check_file_contents('goodbye', 'baz')
157
os.mkdir('revertdir')
158
self.runbzr('add revertdir')
159
self.runbzr('commit -m f')
160
os.rmdir('revertdir')
161
self.runbzr('revert')
163
file('hello', 'wt').write('xyz')
164
self.runbzr('commit -m xyz hello')
165
self.runbzr('revert -r 1 hello')
166
self.check_file_contents('hello', 'foo')
167
self.runbzr('revert hello')
168
self.check_file_contents('hello', 'xyz')
170
def test_mv_modes(self):
171
"""Test two modes of operation for mv"""
172
from bzrlib.branch import Branch
173
b = Branch.initialize('.')
174
self.build_tree(['a', 'c', 'subdir/'])
175
self.run_bzr('add', self.test_dir)
176
self.run_bzr('mv', 'a', 'b')
177
self.run_bzr('mv', 'b', 'subdir')
178
self.run_bzr('mv', 'subdir/b', 'a')
179
self.run_bzr('mv', 'a', 'c', 'subdir')
180
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
182
def test_main_version(self):
183
"""Check output from version command and master option is reasonable"""
184
# output is intentionally passed through to stdout so that we
185
# can see the version being tested
186
output = self.runbzr('version', backtick=1)
187
self.log('bzr version output:')
189
self.assert_(output.startswith('bzr (bazaar-ng) '))
190
self.assertNotEqual(output.index('Canonical'), -1)
191
# make sure --version is consistent
192
tmp_output = self.runbzr('--version', backtick=1)
193
self.log('bzr --version output:')
195
self.assertEquals(output, tmp_output)
197
def example_branch(test):
199
file('hello', 'wt').write('foo')
200
test.runbzr('add hello')
201
test.runbzr('commit -m setup hello')
202
file('goodbye', 'wt').write('baz')
203
test.runbzr('add goodbye')
204
test.runbzr('commit -m setup goodbye')
207
self.example_branch()
208
file('hello', 'wt').write('hello world!')
209
self.runbzr('commit -m fixing hello')
210
output = self.runbzr('diff -r 2..3', backtick=1)
211
self.assert_('\n+hello world!' in output)
212
output = self.runbzr('diff -r last:3..last:1', backtick=1)
213
self.assert_('\n+baz' in output)
216
self.example_branch()
217
file('hello', 'wt').write('hello world!')
218
self.runbzr('commit -m fixing hello')
219
output = self.runbzr('diff -r 2..3', backtick=1)
220
self.assert_('\n+hello world!' in output)
221
output = self.runbzr('diff -r last:3..last:1', backtick=1)
222
self.assert_('\n+baz' in output)
224
def test_merge(self):
225
from bzrlib.branch import Branch
229
self.example_branch()
231
self.runbzr('branch a b')
233
file('goodbye', 'wt').write('quux')
234
self.runbzr(['commit', '-m', "more u's are always good"])
237
file('hello', 'wt').write('quuux')
238
# We can't merge when there are in-tree changes
239
self.runbzr('merge ../b', retcode=1)
240
self.runbzr(['commit', '-m', "Like an epidemic of u's"])
241
self.runbzr('merge ../b')
242
self.check_file_contents('goodbye', 'quux')
243
# Merging a branch pulls its revision into the tree
245
b = Branch.open('../b')
246
a.get_revision_xml(b.last_patch())
247
self.log('pending merges: %s', a.pending_merges())
248
# assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
249
# % (a.pending_merges(), b.last_patch())
252
"""Pull changes from one branch to another."""
256
self.example_branch()
257
self.runbzr('pull', retcode=1)
258
self.runbzr('missing', retcode=1)
259
self.runbzr('missing .')
260
self.runbzr('missing')
262
self.runbzr('pull /', retcode=1)
266
self.runbzr('branch a b')
269
self.runbzr('commit -m blah --unchanged')
272
b = Branch.open('../b')
273
assert a.revision_history() == b.revision_history()[:-1]
274
self.runbzr('pull ../b')
275
assert a.revision_history() == b.revision_history()
276
self.runbzr('commit -m blah2 --unchanged')
278
self.runbzr('commit -m blah3 --unchanged')
279
self.runbzr('pull ../a', retcode=1)
281
self.runbzr('merge ../b')
282
self.runbzr('commit -m blah4 --unchanged')
284
self.runbzr('pull ../a')
285
assert a.revision_history()[-1] == b.revision_history()[-1]
287
def test_add_reports(self):
288
"""add command prints the names of added files."""
289
b = Branch.initialize('.')
290
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
292
ret = self.apply_redirected(None, out, None,
295
self.assertEquals(ret, 0)
296
# the ordering is not defined at the moment
297
results = sorted(out.getvalue().rstrip('\n').split('\n'))
298
self.assertEquals(['added dir',
304
class OldTests(ExternalBase):
305
"""old tests moved from ./testbzr."""
308
from os import chdir, mkdir
309
from os.path import exists
312
backtick = self.backtick
315
progress("basic branch creation")
320
self.assertEquals(backtick('bzr root').rstrip(),
321
os.path.join(self.test_dir, 'branch1'))
323
progress("status of new file")
325
f = file('test.txt', 'wt')
326
f.write('hello world!\n')
329
out = backtick("bzr unknowns")
330
self.assertEquals(out, 'test.txt\n')
332
out = backtick("bzr status")
333
assert out == 'unknown:\n test.txt\n'
335
out = backtick("bzr status --all")
336
assert out == "unknown:\n test.txt\n"
338
out = backtick("bzr status test.txt --all")
339
assert out == "unknown:\n test.txt\n"
341
f = file('test2.txt', 'wt')
342
f.write('goodbye cruel world...\n')
345
out = backtick("bzr status test.txt")
346
assert out == "unknown:\n test.txt\n"
348
out = backtick("bzr status")
349
assert out == ("unknown:\n"
353
os.unlink('test2.txt')
355
progress("command aliases")
356
out = backtick("bzr st --all")
357
assert out == ("unknown:\n"
360
out = backtick("bzr stat")
361
assert out == ("unknown:\n"
364
progress("command help")
367
runbzr("help commands")
368
runbzr("help slartibartfast", 1)
370
out = backtick("bzr help ci")
371
out.index('aliases: ')
373
progress("can't rename unversioned file")
374
runbzr("rename test.txt new-test.txt", 1)
376
progress("adding a file")
378
runbzr("add test.txt")
379
assert backtick("bzr unknowns") == ''
380
assert backtick("bzr status --all") == ("added:\n"
383
progress("rename newly-added file")
384
runbzr("rename test.txt hello.txt")
385
assert os.path.exists("hello.txt")
386
assert not os.path.exists("test.txt")
388
assert backtick("bzr revno") == '0\n'
390
progress("add first revision")
391
runbzr(['commit', '-m', 'add first revision'])
393
progress("more complex renames")
395
runbzr("rename hello.txt sub1", 1)
396
runbzr("rename hello.txt sub1/hello.txt", 1)
397
runbzr("move hello.txt sub1", 1)
400
runbzr("rename sub1 sub2")
401
runbzr("move hello.txt sub2")
402
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
404
assert exists("sub2")
405
assert exists("sub2/hello.txt")
406
assert not exists("sub1")
407
assert not exists("hello.txt")
409
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
413
runbzr('move sub2/hello.txt sub1')
414
assert not exists('sub2/hello.txt')
415
assert exists('sub1/hello.txt')
416
runbzr('move sub2 sub1')
417
assert not exists('sub2')
418
assert exists('sub1/sub2')
420
runbzr(['commit', '-m', 'rename nested subdirectories'])
423
self.assertEquals(backtick('bzr root')[:-1],
424
os.path.join(self.test_dir, 'branch1'))
425
runbzr('move ../hello.txt .')
426
assert exists('./hello.txt')
427
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
428
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
429
runbzr(['commit', '-m', 'move to parent directory'])
431
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
433
runbzr('move sub2/hello.txt .')
434
assert exists('hello.txt')
436
f = file('hello.txt', 'wt')
437
f.write('some nice new content\n')
440
f = file('msg.tmp', 'wt')
441
f.write('this is my new commit\n')
444
runbzr('commit -F msg.tmp')
446
assert backtick('bzr revno') == '5\n'
447
runbzr('export -r 5 export-5.tmp')
448
runbzr('export export.tmp')
452
runbzr('log -v --forward')
453
runbzr('log -m', retcode=1)
454
log_out = backtick('bzr log -m commit')
455
assert "this is my new commit" in log_out
456
assert "rename nested" not in log_out
457
assert 'revision-id' not in log_out
458
assert 'revision-id' in backtick('bzr log --show-ids -m commit')
461
progress("file with spaces in name")
462
mkdir('sub directory')
463
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
466
runbzr('commit -m add-spaces')
470
runbzr('log --forward')