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.
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
whoami = self.runbzr("whoami",backtick=True)
78
whoami_email = self.runbzr("whoami --email",backtick=True)
79
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
80
self.assertTrue(whoami_email.startswith('branch@identi.ty'))
82
def test_invalid_commands(self):
83
self.runbzr("pants", retcode=1)
84
self.runbzr("--pants off", retcode=1)
85
self.runbzr("diff --message foo", retcode=1)
87
def test_empty_commit(self):
89
self.build_tree(['hello.txt'])
90
self.runbzr("commit -m empty", retcode=1)
91
self.runbzr("add hello.txt")
92
self.runbzr("commit -m added")
94
def test_ignore_patterns(self):
95
from bzrlib.branch import Branch
97
b = Branch('.', init=True)
98
self.assertEquals(list(b.unknowns()), [])
100
file('foo.tmp', 'wt').write('tmp files are ignored')
101
self.assertEquals(list(b.unknowns()), [])
102
assert self.backtick('bzr unknowns') == ''
104
file('foo.c', 'wt').write('int main() {}')
105
self.assertEquals(list(b.unknowns()), ['foo.c'])
106
assert self.backtick('bzr unknowns') == 'foo.c\n'
108
self.runbzr(['add', 'foo.c'])
109
assert self.backtick('bzr unknowns') == ''
111
# 'ignore' works when creating the .bzignore file
112
file('foo.blah', 'wt').write('blah')
113
self.assertEquals(list(b.unknowns()), ['foo.blah'])
114
self.runbzr('ignore *.blah')
115
self.assertEquals(list(b.unknowns()), [])
116
assert file('.bzrignore', 'rb').read() == '*.blah\n'
118
# 'ignore' works when then .bzrignore file already exists
119
file('garh', 'wt').write('garh')
120
self.assertEquals(list(b.unknowns()), ['garh'])
121
assert self.backtick('bzr unknowns') == 'garh\n'
122
self.runbzr('ignore garh')
123
self.assertEquals(list(b.unknowns()), [])
124
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
126
def test_revert(self):
129
file('hello', 'wt').write('foo')
130
self.runbzr('add hello')
131
self.runbzr('commit -m setup hello')
133
file('goodbye', 'wt').write('baz')
134
self.runbzr('add goodbye')
135
self.runbzr('commit -m setup goodbye')
137
file('hello', 'wt').write('bar')
138
file('goodbye', 'wt').write('qux')
139
self.runbzr('revert hello')
140
self.check_file_contents('hello', 'foo')
141
self.check_file_contents('goodbye', 'qux')
142
self.runbzr('revert')
143
self.check_file_contents('goodbye', 'baz')
145
os.mkdir('revertdir')
146
self.runbzr('add revertdir')
147
self.runbzr('commit -m f')
148
os.rmdir('revertdir')
149
self.runbzr('revert')
151
def skipped_test_mv_modes(self):
152
"""Test two modes of operation for mv"""
153
from bzrlib.branch import Branch
154
b = Branch('.', init=True)
155
self.build_tree(['a', 'c', 'subdir/'])
156
self.run_bzr('mv', 'a', 'b')
157
self.run_bzr('mv', 'b', 'subdir')
158
self.run_bzr('mv', 'subdir/b', 'a')
159
self.run_bzr('mv', 'a', 'b', 'subdir')
160
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
162
def test_main_version(self):
163
"""Check output from version command and master option is reasonable"""
164
# output is intentionally passed through to stdout so that we
165
# can see the version being tested
166
output = self.runbzr('version', backtick=1)
167
self.log('bzr version output:')
169
self.assert_(output.startswith('bzr (bazaar-ng) '))
170
self.assertNotEqual(output.index('Canonical'), -1)
171
# make sure --version is consistent
172
tmp_output = self.runbzr('--version', backtick=1)
173
self.log('bzr --version output:')
175
self.assertEquals(output, tmp_output)
177
def example_branch(test):
179
file('hello', 'wt').write('foo')
180
test.runbzr('add hello')
181
test.runbzr('commit -m setup hello')
182
file('goodbye', 'wt').write('baz')
183
test.runbzr('add goodbye')
184
test.runbzr('commit -m setup goodbye')
186
def test_revert(self):
187
self.example_branch()
188
file('hello', 'wt').write('bar')
189
file('goodbye', 'wt').write('qux')
190
self.runbzr('revert hello')
191
self.check_file_contents('hello', 'foo')
192
self.check_file_contents('goodbye', 'qux')
193
self.runbzr('revert')
194
self.check_file_contents('goodbye', 'baz')
196
def test_merge(self):
197
from bzrlib.branch import Branch
201
self.example_branch()
203
self.runbzr('branch a b')
205
file('goodbye', 'wt').write('quux')
206
self.runbzr(['commit', '-m', "more u's are always good"])
209
file('hello', 'wt').write('quuux')
210
# We can't merge when there are in-tree changes
211
self.runbzr('merge ../b', retcode=1)
212
self.runbzr(['commit', '-m', "Like an epidemic of u's"])
213
self.runbzr('merge ../b')
214
self.check_file_contents('goodbye', 'quux')
215
# Merging a branch pulls its revision into the tree
218
a.get_revision_xml(b.last_patch())
219
self.log('pending merges: %s', a.pending_merges())
220
# assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
221
# % (a.pending_merges(), b.last_patch())
224
"""Pull changes from one branch to another."""
228
self.example_branch()
230
self.runbzr('branch a b')
232
self.runbzr('commit -m blah --unchanged')
236
assert a.revision_history() == b.revision_history()[:-1]
237
self.runbzr('pull ../b')
238
assert a.revision_history() == b.revision_history()
239
self.runbzr('commit -m blah2 --unchanged')
241
self.runbzr('commit -m blah3 --unchanged')
242
self.runbzr('pull ../a', retcode=1)
244
self.runbzr('merge ../b')
245
self.runbzr('commit -m blah4 --unchanged')
247
self.runbzr('pull ../a')
248
assert a.revision_history()[-1] == b.revision_history()[-1]
251
def test_add_reports(self):
252
"""add command prints the names of added files."""
253
b = Branch('.', init=True)
254
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
256
from cStringIO import StringIO
259
ret = self.apply_redirected(None, out, None,
262
self.assertEquals(ret, 0)
264
# the ordering is not defined at the moment
265
results = sorted(out.getvalue().rstrip('\n').split('\n'))
266
self.assertEquals(['added dir',
273
if hasattr(os, 'symlink'):
278
def listdir_sorted(dir):
284
class OldTests(ExternalBase):
285
"""old tests moved from ./testbzr."""
288
from os import chdir, mkdir
289
from os.path import exists
292
backtick = self.backtick
295
progress("basic branch creation")
300
self.assertEquals(backtick('bzr root').rstrip(),
301
os.path.join(self.test_dir, 'branch1'))
303
progress("status of new file")
305
f = file('test.txt', 'wt')
306
f.write('hello world!\n')
309
out = backtick("bzr unknowns")
310
self.assertEquals(out, 'test.txt\n')
312
out = backtick("bzr status")
313
assert out == 'unknown:\n test.txt\n'
315
out = backtick("bzr status --all")
316
assert out == "unknown:\n test.txt\n"
318
out = backtick("bzr status test.txt --all")
319
assert out == "unknown:\n test.txt\n"
321
f = file('test2.txt', 'wt')
322
f.write('goodbye cruel world...\n')
325
out = backtick("bzr status test.txt")
326
assert out == "unknown:\n test.txt\n"
328
out = backtick("bzr status")
329
assert out == ("unknown:\n"
333
os.unlink('test2.txt')
335
progress("command aliases")
336
out = backtick("bzr st --all")
337
assert out == ("unknown:\n"
340
out = backtick("bzr stat")
341
assert out == ("unknown:\n"
344
progress("command help")
347
runbzr("help commands")
348
runbzr("help slartibartfast", 1)
350
out = backtick("bzr help ci")
351
out.index('aliases: ')
353
progress("can't rename unversioned file")
354
runbzr("rename test.txt new-test.txt", 1)
356
progress("adding a file")
358
runbzr("add test.txt")
359
assert backtick("bzr unknowns") == ''
360
assert backtick("bzr status --all") == ("added:\n"
363
progress("rename newly-added file")
364
runbzr("rename test.txt hello.txt")
365
assert os.path.exists("hello.txt")
366
assert not os.path.exists("test.txt")
368
assert backtick("bzr revno") == '0\n'
370
progress("add first revision")
371
runbzr(['commit', '-m', 'add first revision'])
373
progress("more complex renames")
375
runbzr("rename hello.txt sub1", 1)
376
runbzr("rename hello.txt sub1/hello.txt", 1)
377
runbzr("move hello.txt sub1", 1)
380
runbzr("rename sub1 sub2")
381
runbzr("move hello.txt sub2")
382
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
384
assert exists("sub2")
385
assert exists("sub2/hello.txt")
386
assert not exists("sub1")
387
assert not exists("hello.txt")
389
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
393
runbzr('move sub2/hello.txt sub1')
394
assert not exists('sub2/hello.txt')
395
assert exists('sub1/hello.txt')
396
runbzr('move sub2 sub1')
397
assert not exists('sub2')
398
assert exists('sub1/sub2')
400
runbzr(['commit', '-m', 'rename nested subdirectories'])
403
self.assertEquals(backtick('bzr root')[:-1],
404
os.path.join(self.test_dir, 'branch1'))
405
runbzr('move ../hello.txt .')
406
assert exists('./hello.txt')
407
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
408
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
409
runbzr(['commit', '-m', 'move to parent directory'])
411
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
413
runbzr('move sub2/hello.txt .')
414
assert exists('hello.txt')
416
f = file('hello.txt', 'wt')
417
f.write('some nice new content\n')
420
f = file('msg.tmp', 'wt')
421
f.write('this is my new commit\n')
424
runbzr('commit -F msg.tmp')
426
assert backtick('bzr revno') == '5\n'
427
runbzr('export -r 5 export-5.tmp')
428
runbzr('export export.tmp')
432
runbzr('log -v --forward')
433
runbzr('log -m', retcode=1)
434
log_out = backtick('bzr log -m commit')
435
assert "this is my new commit" in log_out
436
assert "rename nested" not in log_out
437
assert 'revision-id' not in log_out
438
assert 'revision-id' in backtick('bzr log --show-ids -m commit')
441
progress("file with spaces in name")
442
mkdir('sub directory')
443
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
446
runbzr('commit -m add-spaces')
450
runbzr('log --forward')
459
os.symlink("NOWHERE1", "link1")
461
assert backtick('bzr unknowns') == ''
462
runbzr(['commit', '-m', '1: added symlink link1'])
466
assert backtick('bzr unknowns') == ''
467
os.symlink("NOWHERE2", "d1/link2")
468
assert backtick('bzr unknowns') == 'd1/link2\n'
469
# is d1/link2 found when adding d1
471
assert backtick('bzr unknowns') == ''
472
os.symlink("NOWHERE3", "d1/link3")
473
assert backtick('bzr unknowns') == 'd1/link3\n'
474
runbzr(['commit', '-m', '2: added dir, symlink'])
476
runbzr('rename d1 d2')
477
runbzr('move d2/link2 .')
478
runbzr('move link1 d2')
479
assert os.readlink("./link2") == "NOWHERE2"
480
assert os.readlink("d2/link1") == "NOWHERE1"
481
runbzr('add d2/link3')
483
runbzr(['commit', '-m', '3: rename of dir, move symlinks, add link3'])
486
os.symlink("TARGET 2", "link2")
487
os.unlink("d2/link1")
488
os.symlink("TARGET 1", "d2/link1")
490
assert backtick("bzr relpath d2/link1") == "d2/link1\n"
491
runbzr(['commit', '-m', '4: retarget of two links'])
493
runbzr('remove d2/link1')
494
assert backtick('bzr unknowns') == 'd2/link1\n'
495
runbzr(['commit', '--unchanged', '-m', '5: remove d2/link1'])
496
print ("commit --uchanged is needed to delete a file with no other"
497
" changes. this is a bug.")
501
runbzr('rename d2/link3 d1/link3new')
502
assert backtick('bzr unknowns') == 'd2/link1\n'
503
runbzr(['commit', '-m', '6: remove d2/link1, move/rename link3'])
507
runbzr(['export', '-r', '1', 'exp1.tmp'])
509
assert listdir_sorted(".") == [ "link1" ]
510
assert os.readlink("link1") == "NOWHERE1"
513
runbzr(['export', '-r', '2', 'exp2.tmp'])
515
assert listdir_sorted(".") == [ "d1", "link1" ]
518
runbzr(['export', '-r', '3', 'exp3.tmp'])
520
assert listdir_sorted(".") == [ "d2", "link2" ]
521
assert listdir_sorted("d2") == [ "link1", "link3" ]
522
assert os.readlink("d2/link1") == "NOWHERE1"
523
assert os.readlink("link2") == "NOWHERE2"
526
runbzr(['export', '-r', '4', 'exp4.tmp'])
528
assert listdir_sorted(".") == [ "d2", "link2" ]
529
assert os.readlink("d2/link1") == "TARGET 1"
530
assert os.readlink("link2") == "TARGET 2"
531
assert listdir_sorted("d2") == [ "link1", "link3" ]
534
runbzr(['export', '-r', '5', 'exp5.tmp'])
536
assert listdir_sorted(".") == [ "d2", "link2" ]
537
assert os.path.islink("link2")
538
assert listdir_sorted("d2")== [ "link3" ]
541
runbzr(['export', '-r', '6', 'exp6.tmp'])
543
assert listdir_sorted(".") == [ "d1", "d2", "link2" ]
544
assert listdir_sorted("d1") == [ "link3new" ]
545
assert listdir_sorted("d2") == []
546
assert os.readlink("d1/link3new") == "NOWHERE3"
549
progress("skipping symlink tests")