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.
30
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
32
class ExternalBase(TestCaseInTempDir):
33
def runbzr(self, args, retcode=0,backtick=False):
34
if isinstance(args, basestring):
38
return self.backtick(['python', self.BZRPATH,] + args,
41
return self.runcmd(['python', self.BZRPATH,] + args,
45
class TestCommands(ExternalBase):
47
def test_help_commands(self):
50
self.runbzr('help commands')
51
self.runbzr('help help')
52
self.runbzr('commit -h')
54
def test_init_branch(self):
57
def test_whoami(self):
58
# this should always identify something, if only "john@localhost"
60
self.runbzr("whoami --email")
62
self.assertEquals(self.runbzr("whoami --email",
63
backtick=True).count('@'), 1)
65
def test_whoami_branch(self):
66
"""branch specific user identity works."""
68
f = file('.bzr/email', 'wt')
69
f.write('Branch Identity <branch@identi.ty>')
71
whoami = self.runbzr("whoami",backtick=True)
72
whoami_email = self.runbzr("whoami --email",backtick=True)
73
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
74
self.assertTrue(whoami_email.startswith('branch@identi.ty'))
76
def test_invalid_commands(self):
77
self.runbzr("pants", retcode=1)
78
self.runbzr("--pants off", retcode=1)
79
self.runbzr("diff --message foo", retcode=1)
81
def test_empty_commit(self):
83
self.build_tree(['hello.txt'])
84
self.runbzr("commit -m empty", retcode=1)
85
self.runbzr("add hello.txt")
86
self.runbzr("commit -m added")
88
def test_ignore_patterns(self):
89
from bzrlib.branch import Branch
91
b = Branch('.', init=True)
92
self.assertEquals(list(b.unknowns()), [])
94
file('foo.tmp', 'wt').write('tmp files are ignored')
95
self.assertEquals(list(b.unknowns()), [])
96
assert self.backtick('bzr unknowns') == ''
98
file('foo.c', 'wt').write('int main() {}')
99
self.assertEquals(list(b.unknowns()), ['foo.c'])
100
assert self.backtick('bzr unknowns') == 'foo.c\n'
102
self.runbzr(['add', 'foo.c'])
103
assert self.backtick('bzr unknowns') == ''
105
# 'ignore' works when creating the .bzignore file
106
file('foo.blah', 'wt').write('blah')
107
self.assertEquals(list(b.unknowns()), ['foo.blah'])
108
self.runbzr('ignore *.blah')
109
self.assertEquals(list(b.unknowns()), [])
110
assert file('.bzrignore', 'rb').read() == '*.blah\n'
112
# 'ignore' works when then .bzrignore file already exists
113
file('garh', 'wt').write('garh')
114
self.assertEquals(list(b.unknowns()), ['garh'])
115
assert self.backtick('bzr unknowns') == 'garh\n'
116
self.runbzr('ignore garh')
117
self.assertEquals(list(b.unknowns()), [])
118
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
120
def test_revert(self):
124
file('hello', 'wt').write('foo')
125
self.runbzr('add hello')
126
self.runbzr('commit -m setup hello')
128
file('goodbye', 'wt').write('baz')
129
self.runbzr('add goodbye')
130
self.runbzr('commit -m setup goodbye')
132
file('hello', 'wt').write('bar')
133
file('goodbye', 'wt').write('qux')
134
self.runbzr('revert hello')
135
self.check_file_contents('hello', 'foo')
136
self.check_file_contents('goodbye', 'qux')
137
self.runbzr('revert')
138
self.check_file_contents('goodbye', 'baz')
140
os.mkdir('revertdir')
141
self.runbzr('add revertdir')
142
self.runbzr('commit -m f')
143
os.rmdir('revertdir')
144
self.runbzr('revert')
146
def skipped_test_mv_modes(self):
147
"""Test two modes of operation for mv"""
148
from bzrlib.branch import Branch
149
b = Branch('.', init=True)
150
self.build_tree(['a', 'c', 'subdir/'])
151
self.run_bzr('mv', 'a', 'b')
152
self.run_bzr('mv', 'b', 'subdir')
153
self.run_bzr('mv', 'subdir/b', 'a')
154
self.run_bzr('mv', 'a', 'b', 'subdir')
155
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
157
def test_main_version(self):
158
"""Check output from version command and master option is reasonable"""
159
# output is intentionally passed through to stdout so that we
160
# can see the version being tested
161
output = self.runbzr('version', backtick=1)
162
self.log('bzr version output:')
164
self.assert_(output.startswith('bzr (bazaar-ng) '))
165
self.assertNotEqual(output.index('Canonical'), -1)
166
# make sure --version is consistent
167
tmp_output = self.runbzr('--version', backtick=1)
168
self.log('bzr --version output:')
170
self.assertEquals(output, tmp_output)
172
def example_branch(test):
174
file('hello', 'wt').write('foo')
175
test.runbzr('add hello')
176
test.runbzr('commit -m setup hello')
177
file('goodbye', 'wt').write('baz')
178
test.runbzr('add goodbye')
179
test.runbzr('commit -m setup goodbye')
181
def test_revert(self):
182
self.example_branch()
183
file('hello', 'wt').write('bar')
184
file('goodbye', 'wt').write('qux')
185
self.runbzr('revert hello')
186
self.check_file_contents('hello', 'foo')
187
self.check_file_contents('goodbye', 'qux')
188
self.runbzr('revert')
189
self.check_file_contents('goodbye', 'baz')
191
def test_merge(self):
192
from bzrlib.branch import Branch
198
self.example_branch()
200
self.runbzr('branch a b')
202
file('goodbye', 'wt').write('quux')
203
self.runbzr(['commit', '-m', "more u's are always good"])
206
file('hello', 'wt').write('quuux')
207
# We can't merge when there are in-tree changes
208
self.runbzr('merge ../b', retcode=1)
209
self.runbzr(['commit', '-m', "Like an epidemic of u's"])
210
self.runbzr('merge ../b')
211
self.check_file_contents('goodbye', 'quux')
212
# Merging a branch pulls its revision into the tree
215
a.get_revision_xml(b.last_patch())
217
self.log('pending merges: %s', a.pending_merges())
218
# assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
219
# % (a.pending_merges(), b.last_patch())
221
class OldTests(ExternalBase):
222
"""old tests moved from ./testbzr."""
225
from os import chdir, mkdir
226
from os.path import exists
230
backtick = self.backtick
233
progress("basic branch creation")
238
self.assertEquals(backtick('bzr root').rstrip(),
239
os.path.join(self.test_dir, 'branch1'))
241
progress("status of new file")
243
f = file('test.txt', 'wt')
244
f.write('hello world!\n')
247
out = backtick("bzr unknowns")
248
self.assertEquals(out, 'test.txt\n')
250
out = backtick("bzr status")
251
assert out == 'unknown:\n test.txt\n'
253
out = backtick("bzr status --all")
254
assert out == "unknown:\n test.txt\n"
256
out = backtick("bzr status test.txt --all")
257
assert out == "unknown:\n test.txt\n"
259
f = file('test2.txt', 'wt')
260
f.write('goodbye cruel world...\n')
263
out = backtick("bzr status test.txt")
264
assert out == "unknown:\n test.txt\n"
266
out = backtick("bzr status")
267
assert out == ("unknown:\n"
271
os.unlink('test2.txt')
273
progress("command aliases")
274
out = backtick("bzr st --all")
275
assert out == ("unknown:\n"
278
out = backtick("bzr stat")
279
assert out == ("unknown:\n"
282
progress("command help")
285
runbzr("help commands")
286
runbzr("help slartibartfast", 1)
288
out = backtick("bzr help ci")
289
out.index('aliases: ')
291
progress("can't rename unversioned file")
292
runbzr("rename test.txt new-test.txt", 1)
294
progress("adding a file")
296
runbzr("add test.txt")
297
assert backtick("bzr unknowns") == ''
298
assert backtick("bzr status --all") == ("added:\n"
301
progress("rename newly-added file")
302
runbzr("rename test.txt hello.txt")
303
assert os.path.exists("hello.txt")
304
assert not os.path.exists("test.txt")
306
assert backtick("bzr revno") == '0\n'
308
progress("add first revision")
309
runbzr(['commit', '-m', 'add first revision'])
311
progress("more complex renames")
313
runbzr("rename hello.txt sub1", 1)
314
runbzr("rename hello.txt sub1/hello.txt", 1)
315
runbzr("move hello.txt sub1", 1)
318
runbzr("rename sub1 sub2")
319
runbzr("move hello.txt sub2")
320
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
322
assert exists("sub2")
323
assert exists("sub2/hello.txt")
324
assert not exists("sub1")
325
assert not exists("hello.txt")
327
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
331
runbzr('move sub2/hello.txt sub1')
332
assert not exists('sub2/hello.txt')
333
assert exists('sub1/hello.txt')
334
runbzr('move sub2 sub1')
335
assert not exists('sub2')
336
assert exists('sub1/sub2')
338
runbzr(['commit', '-m', 'rename nested subdirectories'])
341
self.assertEquals(backtick('bzr root')[:-1],
342
os.path.join(self.test_dir, 'branch1'))
343
runbzr('move ../hello.txt .')
344
assert exists('./hello.txt')
345
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
346
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
347
runbzr(['commit', '-m', 'move to parent directory'])
349
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
351
runbzr('move sub2/hello.txt .')
352
assert exists('hello.txt')
354
f = file('hello.txt', 'wt')
355
f.write('some nice new content\n')
358
f = file('msg.tmp', 'wt')
359
f.write('this is my new commit\n')
362
runbzr('commit -F msg.tmp')
364
assert backtick('bzr revno') == '5\n'
365
runbzr('export -r 5 export-5.tmp')
366
runbzr('export export.tmp')
370
runbzr('log -v --forward')
371
runbzr('log -m', retcode=1)
372
log_out = backtick('bzr log -m commit')
373
assert "this is my new commit" in log_out
374
assert "rename nested" not in log_out
375
assert 'revision-id' not in log_out
376
assert 'revision-id' in backtick('bzr log --show-ids -m commit')
379
progress("file with spaces in name")
380
mkdir('sub directory')
381
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
384
runbzr('commit -m add-spaces')
388
runbzr('log --forward')