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.
32
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
33
from bzrlib.branch import Branch
34
from bzrlib.commands import run_bzr
37
class ExternalBase(TestCaseInTempDir):
38
def runbzr(self, args, retcode=0,backtick=False):
39
if isinstance(args, basestring):
43
return self.backtick(['python', self.BZRPATH,] + args,
46
return self.runcmd(['python', self.BZRPATH,] + args,
50
class TestCommands(ExternalBase):
52
def test_help_commands(self):
55
self.runbzr('help commands')
56
self.runbzr('help help')
57
self.runbzr('commit -h')
59
def test_init_branch(self):
62
def test_whoami(self):
63
# this should always identify something, if only "john@localhost"
65
self.runbzr("whoami --email")
67
self.assertEquals(self.runbzr("whoami --email",
68
backtick=True).count('@'), 1)
70
def test_whoami_branch(self):
71
"""branch specific user identity works."""
73
f = file('.bzr/email', 'wt')
74
f.write('Branch Identity <branch@identi.ty>')
76
whoami = self.runbzr("whoami",backtick=True)
77
whoami_email = self.runbzr("whoami --email",backtick=True)
78
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
79
self.assertTrue(whoami_email.startswith('branch@identi.ty'))
81
def test_invalid_commands(self):
82
self.runbzr("pants", retcode=1)
83
self.runbzr("--pants off", retcode=1)
84
self.runbzr("diff --message foo", retcode=1)
86
def test_empty_commit(self):
88
self.build_tree(['hello.txt'])
89
self.runbzr("commit -m empty", retcode=1)
90
self.runbzr("add hello.txt")
91
self.runbzr("commit -m added")
93
def test_ignore_patterns(self):
94
from bzrlib.branch import Branch
96
b = Branch('.', init=True)
97
self.assertEquals(list(b.unknowns()), [])
99
file('foo.tmp', 'wt').write('tmp files are ignored')
100
self.assertEquals(list(b.unknowns()), [])
101
assert self.backtick('bzr unknowns') == ''
103
file('foo.c', 'wt').write('int main() {}')
104
self.assertEquals(list(b.unknowns()), ['foo.c'])
105
assert self.backtick('bzr unknowns') == 'foo.c\n'
107
self.runbzr(['add', 'foo.c'])
108
assert self.backtick('bzr unknowns') == ''
110
# 'ignore' works when creating the .bzignore file
111
file('foo.blah', 'wt').write('blah')
112
self.assertEquals(list(b.unknowns()), ['foo.blah'])
113
self.runbzr('ignore *.blah')
114
self.assertEquals(list(b.unknowns()), [])
115
assert file('.bzrignore', 'rb').read() == '*.blah\n'
117
# 'ignore' works when then .bzrignore file already exists
118
file('garh', 'wt').write('garh')
119
self.assertEquals(list(b.unknowns()), ['garh'])
120
assert self.backtick('bzr unknowns') == 'garh\n'
121
self.runbzr('ignore garh')
122
self.assertEquals(list(b.unknowns()), [])
123
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
125
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
202
self.example_branch()
204
self.runbzr('branch a b')
206
file('goodbye', 'wt').write('quux')
207
self.runbzr(['commit', '-m', "more u's are always good"])
210
file('hello', 'wt').write('quuux')
211
# We can't merge when there are in-tree changes
212
self.runbzr('merge ../b', retcode=1)
213
self.runbzr(['commit', '-m', "Like an epidemic of u's"])
214
self.runbzr('merge ../b')
215
self.check_file_contents('goodbye', 'quux')
216
# Merging a branch pulls its revision into the tree
219
a.get_revision_xml(b.last_patch())
220
print "Pending: %s" % a.pending_merges()
221
# assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
222
# % (a.pending_merges(), b.last_patch())
226
if hasattr(os, 'symlink'):
231
def listdir_sorted(dir):
236
class OldTests(ExternalBase):
237
"""old tests moved from ./testbzr."""
240
from os import chdir, mkdir
241
from os.path import exists
244
backtick = self.backtick
247
progress("basic branch creation")
252
self.assertEquals(backtick('bzr root').rstrip(),
253
os.path.join(self.test_dir, 'branch1'))
255
progress("status of new file")
257
f = file('test.txt', 'wt')
258
f.write('hello world!\n')
261
out = backtick("bzr unknowns")
262
self.assertEquals(out, 'test.txt\n')
264
out = backtick("bzr status")
265
assert out == 'unknown:\n test.txt\n'
267
out = backtick("bzr status --all")
268
assert out == "unknown:\n test.txt\n"
270
out = backtick("bzr status test.txt --all")
271
assert out == "unknown:\n test.txt\n"
273
f = file('test2.txt', 'wt')
274
f.write('goodbye cruel world...\n')
277
out = backtick("bzr status test.txt")
278
assert out == "unknown:\n test.txt\n"
280
out = backtick("bzr status")
281
assert out == ("unknown:\n"
285
os.unlink('test2.txt')
287
progress("command aliases")
288
out = backtick("bzr st --all")
289
assert out == ("unknown:\n"
292
out = backtick("bzr stat")
293
assert out == ("unknown:\n"
296
progress("command help")
299
runbzr("help commands")
300
runbzr("help slartibartfast", 1)
302
out = backtick("bzr help ci")
303
out.index('aliases: ')
305
progress("can't rename unversioned file")
306
runbzr("rename test.txt new-test.txt", 1)
308
progress("adding a file")
310
runbzr("add test.txt")
311
assert backtick("bzr unknowns") == ''
312
assert backtick("bzr status --all") == ("added:\n"
315
progress("rename newly-added file")
316
runbzr("rename test.txt hello.txt")
317
assert os.path.exists("hello.txt")
318
assert not os.path.exists("test.txt")
320
assert backtick("bzr revno") == '0\n'
322
progress("add first revision")
323
runbzr(['commit', '-m', 'add first revision'])
325
progress("more complex renames")
327
runbzr("rename hello.txt sub1", 1)
328
runbzr("rename hello.txt sub1/hello.txt", 1)
329
runbzr("move hello.txt sub1", 1)
332
runbzr("rename sub1 sub2")
333
runbzr("move hello.txt sub2")
334
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
336
assert exists("sub2")
337
assert exists("sub2/hello.txt")
338
assert not exists("sub1")
339
assert not exists("hello.txt")
341
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
345
runbzr('move sub2/hello.txt sub1')
346
assert not exists('sub2/hello.txt')
347
assert exists('sub1/hello.txt')
348
runbzr('move sub2 sub1')
349
assert not exists('sub2')
350
assert exists('sub1/sub2')
352
runbzr(['commit', '-m', 'rename nested subdirectories'])
355
self.assertEquals(backtick('bzr root')[:-1],
356
os.path.join(self.test_dir, 'branch1'))
357
runbzr('move ../hello.txt .')
358
assert exists('./hello.txt')
359
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
360
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
361
runbzr(['commit', '-m', 'move to parent directory'])
363
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
365
runbzr('move sub2/hello.txt .')
366
assert exists('hello.txt')
368
f = file('hello.txt', 'wt')
369
f.write('some nice new content\n')
372
f = file('msg.tmp', 'wt')
373
f.write('this is my new commit\n')
376
runbzr('commit -F msg.tmp')
378
assert backtick('bzr revno') == '5\n'
379
runbzr('export -r 5 export-5.tmp')
380
runbzr('export export.tmp')
384
runbzr('log -v --forward')
385
runbzr('log -m', retcode=1)
386
log_out = backtick('bzr log -m commit')
387
assert "this is my new commit" in log_out
388
assert "rename nested" not in log_out
389
assert 'revision-id' not in log_out
390
assert 'revision-id' in backtick('bzr log --show-ids -m commit')
393
progress("file with spaces in name")
394
mkdir('sub directory')
395
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
398
runbzr('commit -m add-spaces')
402
runbzr('log --forward')
411
os.symlink("NOWHERE1", "link1")
413
assert backtick('bzr unknowns') == ''
414
runbzr(['commit', '-m', '1: added symlink link1'])
418
assert backtick('bzr unknowns') == ''
419
os.symlink("NOWHERE2", "d1/link2")
420
assert backtick('bzr unknowns') == 'd1/link2\n'
421
# is d1/link2 found when adding d1
423
assert backtick('bzr unknowns') == ''
424
os.symlink("NOWHERE3", "d1/link3")
425
assert backtick('bzr unknowns') == 'd1/link3\n'
426
runbzr(['commit', '-m', '2: added dir, symlink'])
428
runbzr('rename d1 d2')
429
runbzr('move d2/link2 .')
430
runbzr('move link1 d2')
431
assert os.readlink("./link2") == "NOWHERE2"
432
assert os.readlink("d2/link1") == "NOWHERE1"
433
runbzr('add d2/link3')
435
runbzr(['commit', '-m', '3: rename of dir, move symlinks, add link3'])
438
os.symlink("TARGET 2", "link2")
439
os.unlink("d2/link1")
440
os.symlink("TARGET 1", "d2/link1")
442
assert backtick("bzr relpath d2/link1") == "d2/link1\n"
443
runbzr(['commit', '-m', '4: retarget of two links'])
445
runbzr('remove d2/link1')
446
assert backtick('bzr unknowns') == 'd2/link1\n'
447
runbzr(['commit', '--unchanged', '-m', '5: remove d2/link1'])
448
print ("commit --uchanged is needed to delete a file with no other"
449
" changes. this is a bug.")
453
runbzr('rename d2/link3 d1/link3new')
454
assert backtick('bzr unknowns') == 'd2/link1\n'
455
runbzr(['commit', '-m', '6: remove d2/link1, move/rename link3'])
459
runbzr(['export', '-r', '1', 'exp1.tmp'])
461
assert listdir_sorted(".") == [ "link1" ]
462
assert os.readlink("link1") == "NOWHERE1"
465
runbzr(['export', '-r', '2', 'exp2.tmp'])
467
assert listdir_sorted(".") == [ "d1", "link1" ]
470
runbzr(['export', '-r', '3', 'exp3.tmp'])
472
assert listdir_sorted(".") == [ "d2", "link2" ]
473
assert listdir_sorted("d2") == [ "link1", "link3" ]
474
assert os.readlink("d2/link1") == "NOWHERE1"
475
assert os.readlink("link2") == "NOWHERE2"
478
runbzr(['export', '-r', '4', 'exp4.tmp'])
480
assert listdir_sorted(".") == [ "d2", "link2" ]
481
assert os.readlink("d2/link1") == "TARGET 1"
482
assert os.readlink("link2") == "TARGET 2"
483
assert listdir_sorted("d2") == [ "link1", "link3" ]
486
runbzr(['export', '-r', '5', 'exp5.tmp'])
488
assert listdir_sorted(".") == [ "d2", "link2" ]
489
assert os.path.islink("link2")
490
assert listdir_sorted("d2")== [ "link3" ]
493
runbzr(['export', '-r', '6', 'exp6.tmp'])
495
assert listdir_sorted(".") == [ "d1", "d2", "link2" ]
496
assert listdir_sorted("d1") == [ "link3new" ]
497
assert listdir_sorted("d2") == []
498
assert os.readlink("d1/link3new") == "NOWHERE3"
501
progress("skipping symlink tests")