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.
25
from cStringIO import StringIO
31
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
32
from bzrlib.branch import Branch
33
from bzrlib.osutils import has_symlinks
34
from bzrlib.selftest.HTTPTestUtil import TestCaseWithWebserver
37
class ExternalBase(TestCaseInTempDir):
39
def runbzr(self, args, retcode=0, backtick=False):
40
if isinstance(args, basestring):
44
return self.run_bzr_captured(args, retcode=retcode)[0]
46
return self.run_bzr_captured(args, retcode=retcode)
49
class TestCommands(ExternalBase):
51
def test_help_commands(self):
54
self.runbzr('help commands')
55
self.runbzr('help help')
56
self.runbzr('commit -h')
58
def test_init_branch(self):
61
def test_whoami(self):
62
# this should always identify something, if only "john@localhost"
64
self.runbzr("whoami --email")
66
self.assertEquals(self.runbzr("whoami --email",
67
backtick=True).count('@'), 1)
69
def test_whoami_branch(self):
70
"""branch specific user identity works."""
72
f = file('.bzr/email', 'wt')
73
f.write('Branch Identity <branch@identi.ty>')
75
bzr_email = os.environ.get('BZREMAIL')
76
if bzr_email is not None:
77
del os.environ['BZREMAIL']
78
whoami = self.runbzr("whoami",backtick=True)
79
whoami_email = self.runbzr("whoami --email",backtick=True)
80
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
81
self.assertTrue(whoami_email.startswith('branch@identi.ty'))
82
# Verify that the environment variable overrides the value
84
os.environ['BZREMAIL'] = 'Different ID <other@environ.ment>'
85
whoami = self.runbzr("whoami",backtick=True)
86
whoami_email = self.runbzr("whoami --email",backtick=True)
87
self.assertTrue(whoami.startswith('Different ID <other@environ.ment>'))
88
self.assertTrue(whoami_email.startswith('other@environ.ment'))
89
if bzr_email is not None:
90
os.environ['BZREMAIL'] = bzr_email
92
def test_invalid_commands(self):
93
self.runbzr("pants", retcode=1)
94
self.runbzr("--pants off", retcode=1)
95
self.runbzr("diff --message foo", retcode=1)
97
def test_empty_commit(self):
99
self.build_tree(['hello.txt'])
100
self.runbzr("commit -m empty", retcode=1)
101
self.runbzr("add hello.txt")
102
self.runbzr("commit -m added")
104
def test_ignore_patterns(self):
105
from bzrlib.branch import Branch
107
b = Branch.initialize('.')
108
self.assertEquals(list(b.unknowns()), [])
110
file('foo.tmp', 'wt').write('tmp files are ignored')
111
self.assertEquals(list(b.unknowns()), [])
112
assert self.capture('unknowns') == ''
114
file('foo.c', 'wt').write('int main() {}')
115
self.assertEquals(list(b.unknowns()), ['foo.c'])
116
assert self.capture('unknowns') == 'foo.c\n'
118
self.runbzr(['add', 'foo.c'])
119
assert self.capture('unknowns') == ''
121
# 'ignore' works when creating the .bzignore file
122
file('foo.blah', 'wt').write('blah')
123
self.assertEquals(list(b.unknowns()), ['foo.blah'])
124
self.runbzr('ignore *.blah')
125
self.assertEquals(list(b.unknowns()), [])
126
assert file('.bzrignore', 'rU').read() == '*.blah\n'
128
# 'ignore' works when then .bzrignore file already exists
129
file('garh', 'wt').write('garh')
130
self.assertEquals(list(b.unknowns()), ['garh'])
131
assert self.capture('unknowns') == 'garh\n'
132
self.runbzr('ignore garh')
133
self.assertEquals(list(b.unknowns()), [])
134
assert file('.bzrignore', 'rU').read() == '*.blah\ngarh\n'
136
def test_revert(self):
139
file('hello', 'wt').write('foo')
140
self.runbzr('add hello')
141
self.runbzr('commit -m setup hello')
143
file('goodbye', 'wt').write('baz')
144
self.runbzr('add goodbye')
145
self.runbzr('commit -m setup goodbye')
147
file('hello', 'wt').write('bar')
148
file('goodbye', 'wt').write('qux')
149
self.runbzr('revert hello')
150
self.check_file_contents('hello', 'foo')
151
self.check_file_contents('goodbye', 'qux')
152
self.runbzr('revert')
153
self.check_file_contents('goodbye', 'baz')
155
os.mkdir('revertdir')
156
self.runbzr('add revertdir')
157
self.runbzr('commit -m f')
158
os.rmdir('revertdir')
159
self.runbzr('revert')
161
os.symlink('/unlikely/to/exist', 'symlink')
162
self.runbzr('add symlink')
163
self.runbzr('commit -m f')
165
self.runbzr('revert')
167
file('hello', 'wt').write('xyz')
168
self.runbzr('commit -m xyz hello')
169
self.runbzr('revert -r 1 hello')
170
self.check_file_contents('hello', 'foo')
171
self.runbzr('revert hello')
172
self.check_file_contents('hello', 'xyz')
173
os.chdir('revertdir')
174
self.runbzr('revert')
178
def test_mv_modes(self):
179
"""Test two modes of operation for mv"""
180
from bzrlib.branch import Branch
181
b = Branch.initialize('.')
182
self.build_tree(['a', 'c', 'subdir/'])
183
self.run_bzr_captured(['add', self.test_dir])
184
self.run_bzr_captured(['mv', 'a', 'b'])
185
self.run_bzr_captured(['mv', 'b', 'subdir'])
186
self.run_bzr_captured(['mv', 'subdir/b', 'a'])
187
self.run_bzr_captured(['mv', 'a', 'c', 'subdir'])
188
self.run_bzr_captured(['mv', 'subdir/a', 'subdir/newa'])
191
def test_main_version(self):
192
"""Check output from version command and master option is reasonable"""
193
# output is intentionally passed through to stdout so that we
194
# can see the version being tested
195
output = self.runbzr('version', backtick=1)
196
self.log('bzr version output:')
198
self.assert_(output.startswith('bzr (bazaar-ng) '))
199
self.assertNotEqual(output.index('Canonical'), -1)
200
# make sure --version is consistent
201
tmp_output = self.runbzr('--version', backtick=1)
202
self.log('bzr --version output:')
204
self.assertEquals(output, tmp_output)
206
def example_branch(test):
208
file('hello', 'wt').write('foo')
209
test.runbzr('add hello')
210
test.runbzr('commit -m setup hello')
211
file('goodbye', 'wt').write('baz')
212
test.runbzr('add goodbye')
213
test.runbzr('commit -m setup goodbye')
215
def test_export(self):
218
self.example_branch()
219
self.runbzr('export ../latest')
220
self.assertEqual(file('../latest/goodbye', 'rt').read(), 'baz')
221
self.runbzr('export ../first -r 1')
222
assert not os.path.exists('../first/goodbye')
223
self.assertEqual(file('../first/hello', 'rt').read(), 'foo')
224
self.runbzr('export ../first.gz -r 1')
225
self.assertEqual(file('../first.gz/hello', 'rt').read(), 'foo')
226
self.runbzr('export ../first.bz2 -r 1')
227
self.assertEqual(file('../first.bz2/hello', 'rt').read(), 'foo')
228
self.runbzr('export ../first.tar -r 1')
229
assert os.path.isfile('../first.tar')
230
from tarfile import TarFile
231
tf = TarFile('../first.tar')
232
assert 'first/hello' in tf.getnames(), tf.getnames()
233
self.assertEqual(tf.extractfile('first/hello').read(), 'foo')
234
self.runbzr('export ../first.tar.gz -r 1')
235
assert os.path.isfile('../first.tar.gz')
236
self.runbzr('export ../first.tbz2 -r 1')
237
assert os.path.isfile('../first.tbz2')
238
self.runbzr('export ../first.tar.bz2 -r 1')
239
assert os.path.isfile('../first.tar.bz2')
240
self.runbzr('export ../first.tar.tbz2 -r 1')
241
assert os.path.isfile('../first.tar.tbz2')
242
from bz2 import BZ2File
243
tf = TarFile('../first.tar.tbz2',
244
fileobj=BZ2File('../first.tar.tbz2', 'r'))
245
assert 'first.tar/hello' in tf.getnames(), tf.getnames()
246
self.assertEqual(tf.extractfile('first.tar/hello').read(), 'foo')
247
self.runbzr('export ../first2.tar -r 1 --root pizza')
248
tf = TarFile('../first2.tar')
249
assert 'pizza/hello' in tf.getnames(), tf.getnames()
252
self.example_branch()
253
file('hello', 'wt').write('hello world!')
254
self.runbzr('commit -m fixing hello')
255
output = self.runbzr('diff -r 2..3', backtick=1)
256
self.assert_('\n+hello world!' in output)
257
output = self.runbzr('diff -r last:3..last:1', backtick=1)
258
self.assert_('\n+baz' in output)
260
def test_branch(self):
261
"""Branch from one branch to another."""
264
self.example_branch()
266
self.runbzr('branch a b')
267
self.runbzr('branch a c -r 1')
269
self.runbzr('commit -m foo --unchanged')
271
# naughty - abstraction violations RBC 20050928
272
print "test_branch used to delete the stores, how is this meant to work ?"
273
#shutil.rmtree('a/.bzr/revision-store')
274
#shutil.rmtree('a/.bzr/inventory-store', ignore_errors=True)
275
#shutil.rmtree('a/.bzr/text-store', ignore_errors=True)
276
self.runbzr('branch a d --basis b')
278
def test_merge(self):
279
from bzrlib.branch import Branch
283
self.example_branch()
285
self.runbzr('branch a b')
287
file('goodbye', 'wt').write('quux')
288
self.runbzr(['commit', '-m', "more u's are always good"])
291
file('hello', 'wt').write('quuux')
292
# We can't merge when there are in-tree changes
293
self.runbzr('merge ../b', retcode=1)
294
self.runbzr(['commit', '-m', "Like an epidemic of u's"])
295
self.runbzr('merge ../b')
296
self.check_file_contents('goodbye', 'quux')
297
# Merging a branch pulls its revision into the tree
299
b = Branch.open('../b')
300
a.get_revision_xml(b.last_revision())
301
self.log('pending merges: %s', a.pending_merges())
302
# assert a.pending_merges() == [b.last_revision()], "Assertion %s %s" \
303
# % (a.pending_merges(), b.last_revision())
305
def test_merge_with_missing_file(self):
306
"""Merge handles missing file conflicts"""
310
print >> file('sub/a.txt', 'wb'), "hello"
311
print >> file('b.txt', 'wb'), "hello"
312
print >> file('sub/c.txt', 'wb'), "hello"
315
self.runbzr(('commit', '-m', 'added a'))
316
self.runbzr('branch . ../b')
317
print >> file('sub/a.txt', 'ab'), "there"
318
print >> file('b.txt', 'ab'), "there"
319
print >> file('sub/c.txt', 'ab'), "there"
320
self.runbzr(('commit', '-m', 'Added there'))
321
os.unlink('sub/a.txt')
322
os.unlink('sub/c.txt')
325
self.runbzr(('commit', '-m', 'Removed a.txt'))
327
print >> file('sub/a.txt', 'ab'), "something"
328
print >> file('b.txt', 'ab'), "something"
329
print >> file('sub/c.txt', 'ab'), "something"
330
self.runbzr(('commit', '-m', 'Modified a.txt'))
331
self.runbzr('merge ../a/')
332
assert os.path.exists('sub/a.txt.THIS')
333
assert os.path.exists('sub/a.txt.BASE')
335
self.runbzr('merge ../b/')
336
assert os.path.exists('sub/a.txt.OTHER')
337
assert os.path.exists('sub/a.txt.BASE')
340
"""Pull changes from one branch to another."""
344
self.example_branch()
345
self.runbzr('pull', retcode=1)
346
self.runbzr('missing', retcode=1)
347
self.runbzr('missing .')
348
self.runbzr('missing')
350
self.runbzr('pull /', retcode=1)
354
self.runbzr('branch a b')
358
self.runbzr('add subdir')
359
self.runbzr('commit -m blah --unchanged')
362
b = Branch.open('../b')
363
assert a.revision_history() == b.revision_history()[:-1]
364
self.runbzr('pull ../b')
365
assert a.revision_history() == b.revision_history()
366
self.runbzr('commit -m blah2 --unchanged')
368
self.runbzr('commit -m blah3 --unchanged')
369
self.runbzr('pull ../a', retcode=1)
370
print "DECIDE IF PULL CAN CONVERGE, blackbox.py"
373
self.runbzr('merge ../b')
374
self.runbzr('commit -m blah4 --unchanged')
375
os.chdir('../b/subdir')
376
self.runbzr('pull ../../a')
377
assert a.revision_history()[-1] == b.revision_history()[-1]
378
self.runbzr('commit -m blah5 --unchanged')
379
self.runbzr('commit -m blah6 --unchanged')
381
self.runbzr('pull ../a')
383
self.runbzr('commit -m blah7 --unchanged')
384
self.runbzr('merge ../b')
385
self.runbzr('commit -m blah8 --unchanged')
386
self.runbzr('pull ../b')
387
self.runbzr('pull ../b')
389
def test_add_reports(self):
390
"""add command prints the names of added files."""
391
b = Branch.initialize('.')
392
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
393
out = self.run_bzr_captured(['add'], retcode = 0)[0]
394
# the ordering is not defined at the moment
395
results = sorted(out.rstrip('\n').split('\n'))
396
self.assertEquals(['added dir',
397
'added dir'+os.sep+'sub.txt',
401
def test_unknown_command(self):
402
"""Handling of unknown command."""
403
out, err = self.run_bzr_captured(['fluffy-badger'],
405
self.assertEquals(out, '')
406
err.index('unknown command')
409
def listdir_sorted(dir):
415
class OldTests(ExternalBase):
416
"""old tests moved from ./testbzr."""
419
from os import chdir, mkdir
420
from os.path import exists
423
capture = self.capture
426
progress("basic branch creation")
431
self.assertEquals(capture('root').rstrip(),
432
os.path.join(self.test_dir, 'branch1'))
434
progress("status of new file")
436
f = file('test.txt', 'wt')
437
f.write('hello world!\n')
440
self.assertEquals(capture('unknowns'), 'test.txt\n')
442
out = capture("status")
443
assert out == 'unknown:\n test.txt\n'
445
out = capture("status --all")
446
assert out == "unknown:\n test.txt\n"
448
out = capture("status test.txt --all")
449
assert out == "unknown:\n test.txt\n"
451
f = file('test2.txt', 'wt')
452
f.write('goodbye cruel world...\n')
455
out = capture("status test.txt")
456
assert out == "unknown:\n test.txt\n"
458
out = capture("status")
459
assert out == ("unknown:\n"
463
os.unlink('test2.txt')
465
progress("command aliases")
466
out = capture("st --all")
467
assert out == ("unknown:\n"
470
out = capture("stat")
471
assert out == ("unknown:\n"
474
progress("command help")
477
runbzr("help commands")
478
runbzr("help slartibartfast", 1)
480
out = capture("help ci")
481
out.index('aliases: ')
483
progress("can't rename unversioned file")
484
runbzr("rename test.txt new-test.txt", 1)
486
progress("adding a file")
488
runbzr("add test.txt")
489
assert capture("unknowns") == ''
490
assert capture("status --all") == ("added:\n"
493
progress("rename newly-added file")
494
runbzr("rename test.txt hello.txt")
495
assert os.path.exists("hello.txt")
496
assert not os.path.exists("test.txt")
498
assert capture("revno") == '0\n'
500
progress("add first revision")
501
runbzr(['commit', '-m', 'add first revision'])
503
progress("more complex renames")
505
runbzr("rename hello.txt sub1", 1)
506
runbzr("rename hello.txt sub1/hello.txt", 1)
507
runbzr("move hello.txt sub1", 1)
510
runbzr("rename sub1 sub2")
511
runbzr("move hello.txt sub2")
512
assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
514
assert exists("sub2")
515
assert exists("sub2/hello.txt")
516
assert not exists("sub1")
517
assert not exists("hello.txt")
519
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
523
runbzr('move sub2/hello.txt sub1')
524
assert not exists('sub2/hello.txt')
525
assert exists('sub1/hello.txt')
526
runbzr('move sub2 sub1')
527
assert not exists('sub2')
528
assert exists('sub1/sub2')
530
runbzr(['commit', '-m', 'rename nested subdirectories'])
533
self.assertEquals(capture('root')[:-1],
534
os.path.join(self.test_dir, 'branch1'))
535
runbzr('move ../hello.txt .')
536
assert exists('./hello.txt')
537
self.assertEquals(capture('relpath hello.txt'),
538
os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
539
assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
540
runbzr(['commit', '-m', 'move to parent directory'])
542
assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
544
runbzr('move sub2/hello.txt .')
545
assert exists('hello.txt')
547
f = file('hello.txt', 'wt')
548
f.write('some nice new content\n')
551
f = file('msg.tmp', 'wt')
552
f.write('this is my new commit\n')
555
runbzr('commit -F msg.tmp')
557
assert capture('revno') == '5\n'
558
runbzr('export -r 5 export-5.tmp')
559
runbzr('export export.tmp')
563
runbzr('log -v --forward')
564
runbzr('log -m', retcode=1)
565
log_out = capture('log -m commit')
566
assert "this is my new commit" in log_out
567
assert "rename nested" not in log_out
568
assert 'revision-id' not in log_out
569
assert 'revision-id' in capture('log --show-ids -m commit')
572
progress("file with spaces in name")
573
mkdir('sub directory')
574
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
577
runbzr('commit -m add-spaces')
581
runbzr('log --forward')
590
os.symlink("NOWHERE1", "link1")
592
assert self.capture('unknowns') == ''
593
runbzr(['commit', '-m', '1: added symlink link1'])
597
assert self.capture('unknowns') == ''
598
os.symlink("NOWHERE2", "d1/link2")
599
assert self.capture('unknowns') == 'd1/link2\n'
600
# is d1/link2 found when adding d1
602
assert self.capture('unknowns') == ''
603
os.symlink("NOWHERE3", "d1/link3")
604
assert self.capture('unknowns') == 'd1/link3\n'
605
runbzr(['commit', '-m', '2: added dir, symlink'])
607
runbzr('rename d1 d2')
608
runbzr('move d2/link2 .')
609
runbzr('move link1 d2')
610
assert os.readlink("./link2") == "NOWHERE2"
611
assert os.readlink("d2/link1") == "NOWHERE1"
612
runbzr('add d2/link3')
614
runbzr(['commit', '-m', '3: rename of dir, move symlinks, add link3'])
617
os.symlink("TARGET 2", "link2")
618
os.unlink("d2/link1")
619
os.symlink("TARGET 1", "d2/link1")
621
assert self.capture("relpath d2/link1") == "d2/link1\n"
622
runbzr(['commit', '-m', '4: retarget of two links'])
624
runbzr('remove d2/link1')
625
assert self.capture('unknowns') == 'd2/link1\n'
626
runbzr(['commit', '-m', '5: remove d2/link1'])
630
runbzr('rename d2/link3 d1/link3new')
631
assert self.capture('unknowns') == 'd2/link1\n'
632
runbzr(['commit', '-m', '6: remove d2/link1, move/rename link3'])
636
runbzr(['export', '-r', '1', 'exp1.tmp'])
638
assert listdir_sorted(".") == [ "link1" ]
639
assert os.readlink("link1") == "NOWHERE1"
642
runbzr(['export', '-r', '2', 'exp2.tmp'])
644
assert listdir_sorted(".") == [ "d1", "link1" ]
647
runbzr(['export', '-r', '3', 'exp3.tmp'])
649
assert listdir_sorted(".") == [ "d2", "link2" ]
650
assert listdir_sorted("d2") == [ "link1", "link3" ]
651
assert os.readlink("d2/link1") == "NOWHERE1"
652
assert os.readlink("link2") == "NOWHERE2"
655
runbzr(['export', '-r', '4', 'exp4.tmp'])
657
assert listdir_sorted(".") == [ "d2", "link2" ]
658
assert os.readlink("d2/link1") == "TARGET 1"
659
assert os.readlink("link2") == "TARGET 2"
660
assert listdir_sorted("d2") == [ "link1", "link3" ]
663
runbzr(['export', '-r', '5', 'exp5.tmp'])
665
assert listdir_sorted(".") == [ "d2", "link2" ]
666
assert os.path.islink("link2")
667
assert listdir_sorted("d2")== [ "link3" ]
670
runbzr(['export', '-r', '6', 'exp6.tmp'])
672
assert listdir_sorted(".") == [ "d1", "d2", "link2" ]
673
assert listdir_sorted("d1") == [ "link3new" ]
674
assert listdir_sorted("d2") == []
675
assert os.readlink("d1/link3new") == "NOWHERE3"
678
progress("skipping symlink tests")
681
class HttpTests(TestCaseWithWebserver):
682
"""Test bzr ui commands against remote branches."""
684
def test_branch(self):
686
branch = Branch.initialize('from')
687
branch.commit('empty commit for nonsense', allow_pointless=True)
688
url = self.get_remote_url('from')
689
self.run_bzr('branch', url, 'to')
690
branch = Branch.open('to')
691
self.assertEqual(1, len(branch.revision_history()))