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):
34
def runbzr(self, args, retcode=0,backtick=False):
37
from subprocess import call
38
except ImportError, e:
42
if isinstance(args, basestring):
46
return self.backtick(['python', self.BZRPATH,] + args,
49
return self.runcmd(['python', self.BZRPATH,] + args,
52
class TestCommands(ExternalBase):
54
def test_help_commands(self):
57
self.runbzr('help commands')
58
self.runbzr('help help')
59
self.runbzr('commit -h')
61
def test_init_branch(self):
65
def test_whoami(self):
66
# this should always identify something, if only "john@localhost"
68
self.runbzr("whoami --email")
70
self.assertEquals(self.runbzr("whoami --email",
71
backtick=True).count('@'), 1)
73
def test_whoami_branch(self):
74
"""branch specific user identity works."""
76
f = file('.bzr/email', 'wt')
77
f.write('Branch Identity <branch@identi.ty>')
79
whoami = self.runbzr("whoami",backtick=True)
80
whoami_email = self.runbzr("whoami --email",backtick=True)
81
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
82
self.assertTrue(whoami_email.startswith('branch@identi.ty'))
84
def test_invalid_commands(self):
85
self.runbzr("pants", retcode=1)
86
self.runbzr("--pants off", retcode=1)
87
self.runbzr("diff --message foo", retcode=1)
89
def test_empty_commit(self):
91
self.build_tree(['hello.txt'])
92
self.runbzr("commit -m empty", retcode=1)
93
self.runbzr("add hello.txt")
94
self.runbzr("commit -m added")
96
def test_ignore_patterns(self):
97
from bzrlib.branch import Branch
99
b = Branch('.', init=True)
100
self.assertEquals(list(b.unknowns()), [])
102
file('foo.tmp', 'wt').write('tmp files are ignored')
103
self.assertEquals(list(b.unknowns()), [])
104
assert self.backtick('bzr unknowns') == ''
106
file('foo.c', 'wt').write('int main() {}')
107
self.assertEquals(list(b.unknowns()), ['foo.c'])
108
assert self.backtick('bzr unknowns') == 'foo.c\n'
110
self.runbzr(['add', 'foo.c'])
111
assert self.backtick('bzr unknowns') == ''
113
# 'ignore' works when creating the .bzignore file
114
file('foo.blah', 'wt').write('blah')
115
self.assertEquals(list(b.unknowns()), ['foo.blah'])
116
self.runbzr('ignore *.blah')
117
self.assertEquals(list(b.unknowns()), [])
118
assert file('.bzrignore', 'rb').read() == '*.blah\n'
120
# 'ignore' works when then .bzrignore file already exists
121
file('garh', 'wt').write('garh')
122
self.assertEquals(list(b.unknowns()), ['garh'])
123
assert self.backtick('bzr unknowns') == 'garh\n'
124
self.runbzr('ignore garh')
125
self.assertEquals(list(b.unknowns()), [])
126
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
128
def test_revert(self):
132
file('hello', 'wt').write('foo')
133
self.runbzr('add hello')
134
self.runbzr('commit -m setup hello')
136
file('goodbye', 'wt').write('baz')
137
self.runbzr('add goodbye')
138
self.runbzr('commit -m setup goodbye')
140
file('hello', 'wt').write('bar')
141
file('goodbye', 'wt').write('qux')
142
self.runbzr('revert hello')
143
self.check_file_contents('hello', 'foo')
144
self.check_file_contents('goodbye', 'qux')
145
self.runbzr('revert')
146
self.check_file_contents('goodbye', 'baz')
148
os.mkdir('revertdir')
149
self.runbzr('add revertdir')
150
self.runbzr('commit -m f')
151
os.rmdir('revertdir')
152
self.runbzr('revert')
154
def skipped_test_mv_modes(self):
155
"""Test two modes of operation for mv"""
156
from bzrlib.branch import Branch
157
b = Branch('.', init=True)
158
self.build_tree(['a', 'c', 'subdir/'])
159
self.run_bzr('mv', 'a', 'b')
160
self.run_bzr('mv', 'b', 'subdir')
161
self.run_bzr('mv', 'subdir/b', 'a')
162
self.run_bzr('mv', 'a', 'b', 'subdir')
163
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
165
def test_main_version(self):
166
"""Check output from version command and master option is reasonable"""
167
# output is intentionally passed through to stdout so that we
168
# can see the version being tested
169
output = self.runbzr('version', backtick=1)
170
self.log('bzr version output:')
172
self.assert_(output.startswith('bzr (bazaar-ng) '))
173
self.assertNotEqual(output.index('Canonical'), -1)
174
# make sure --version is consistent
175
tmp_output = self.runbzr('--version', backtick=1)
176
self.log('bzr --version output:')
178
self.assertEquals(output, tmp_output)
180
def example_branch(test):
182
file('hello', 'wt').write('foo')
183
test.runbzr('add hello')
184
test.runbzr('commit -m setup hello')
185
file('goodbye', 'wt').write('baz')
186
test.runbzr('add goodbye')
187
test.runbzr('commit -m setup goodbye')
189
def test_revert(self):
190
self.example_branch()
191
file('hello', 'wt').write('bar')
192
file('goodbye', 'wt').write('qux')
193
self.runbzr('revert hello')
194
self.check_file_contents('hello', 'foo')
195
self.check_file_contents('goodbye', 'qux')
196
self.runbzr('revert')
197
self.check_file_contents('goodbye', 'baz')
199
def test_merge(self):
200
from bzrlib.branch import Branch
201
from bzrlib.commands import run_bzr
207
self.example_branch()
209
self.runbzr('branch a b')
211
file('goodbye', 'wt').write('quux')
212
self.runbzr(['commit', '-m', "more u's are always good"])
215
file('hello', 'wt').write('quuux')
216
# We can't merge when there are in-tree changes
217
self.runbzr('merge ../b', retcode=1)
218
self.runbzr(['commit', '-m', "Like an epidemic of u's"])
219
self.runbzr('merge ../b')
220
self.check_file_contents('goodbye', 'quux')
221
# Merging a branch pulls its revision into the tree
224
a.get_revision_xml(b.last_patch())
226
self.log('pending merges: %s', a.pending_merges())
227
# assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
228
# % (a.pending_merges(), b.last_patch())
230
class OldTests(ExternalBase):
231
"""old tests moved from ./testbzr."""
234
from os import chdir, mkdir
235
from os.path import exists
239
backtick = self.backtick
242
progress("basic branch creation")
247
self.assertEquals(backtick('bzr root').rstrip(),
248
os.path.join(self.test_dir, 'branch1'))
250
progress("status of new file")
252
f = file('test.txt', 'wt')
253
f.write('hello world!\n')
256
out = backtick("bzr unknowns")
257
self.assertEquals(out, 'test.txt\n')
259
out = backtick("bzr status")
260
assert out == 'unknown:\n test.txt\n'
262
out = backtick("bzr status --all")
263
assert out == "unknown:\n test.txt\n"
265
out = backtick("bzr status test.txt --all")
266
assert out == "unknown:\n test.txt\n"
268
f = file('test2.txt', 'wt')
269
f.write('goodbye cruel world...\n')
272
out = backtick("bzr status test.txt")
273
assert out == "unknown:\n test.txt\n"
275
out = backtick("bzr status")
276
assert out == ("unknown:\n"
280
os.unlink('test2.txt')
282
progress("command aliases")
283
out = backtick("bzr st --all")
284
assert out == ("unknown:\n"
287
out = backtick("bzr stat")
288
assert out == ("unknown:\n"
291
progress("command help")
294
runbzr("help commands")
295
runbzr("help slartibartfast", 1)
297
out = backtick("bzr help ci")
298
out.index('aliases: ')
300
progress("can't rename unversioned file")
301
runbzr("rename test.txt new-test.txt", 1)
303
progress("adding a file")
305
runbzr("add test.txt")
306
assert backtick("bzr unknowns") == ''
307
assert backtick("bzr status --all") == ("added:\n"
310
progress("rename newly-added file")
311
runbzr("rename test.txt hello.txt")
312
assert os.path.exists("hello.txt")
313
assert not os.path.exists("test.txt")
315
assert backtick("bzr revno") == '0\n'
317
progress("add first revision")
318
runbzr(['commit', '-m', 'add first revision'])
320
progress("more complex renames")
322
runbzr("rename hello.txt sub1", 1)
323
runbzr("rename hello.txt sub1/hello.txt", 1)
324
runbzr("move hello.txt sub1", 1)
327
runbzr("rename sub1 sub2")
328
runbzr("move hello.txt sub2")
329
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
331
assert exists("sub2")
332
assert exists("sub2/hello.txt")
333
assert not exists("sub1")
334
assert not exists("hello.txt")
336
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
340
runbzr('move sub2/hello.txt sub1')
341
assert not exists('sub2/hello.txt')
342
assert exists('sub1/hello.txt')
343
runbzr('move sub2 sub1')
344
assert not exists('sub2')
345
assert exists('sub1/sub2')
347
runbzr(['commit', '-m', 'rename nested subdirectories'])
350
self.assertEquals(backtick('bzr root')[:-1],
351
os.path.join(self.test_dir, 'branch1'))
352
runbzr('move ../hello.txt .')
353
assert exists('./hello.txt')
354
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
355
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
356
runbzr(['commit', '-m', 'move to parent directory'])
358
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
360
runbzr('move sub2/hello.txt .')
361
assert exists('hello.txt')
363
f = file('hello.txt', 'wt')
364
f.write('some nice new content\n')
367
f = file('msg.tmp', 'wt')
368
f.write('this is my new commit\n')
371
runbzr('commit -F msg.tmp')
373
assert backtick('bzr revno') == '5\n'
374
runbzr('export -r 5 export-5.tmp')
375
runbzr('export export.tmp')
379
runbzr('log -v --forward')
380
runbzr('log -m', retcode=1)
381
log_out = backtick('bzr log -m commit')
382
assert "this is my new commit" in log_out
383
assert "rename nested" not in log_out
384
assert 'revision-id' not in log_out
385
assert 'revision-id' in backtick('bzr log --show-ids -m commit')
388
progress("file with spaces in name")
389
mkdir('sub directory')
390
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
393
runbzr('commit -m add-spaces')
397
runbzr('log --forward')