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.
31
from bzrlib.selftest import InTempDir, BzrTestBase
35
class ExternalBase(InTempDir):
36
def runbzr(self, args, retcode=0,backtick=False):
39
from subprocess import call
40
except ImportError, e:
44
if isinstance(args, basestring):
48
return self.backtick(['python', self.BZRPATH,] + args,
51
return self.runcmd(['python', self.BZRPATH,] + args,
56
class MvCommand(BzrTestBase):
58
"""Test two modes of operation for mv"""
59
b = Branch('.', init=True)
60
self.build_tree(['a', 'c', 'subdir/'])
61
self.run_bzr('mv', 'a', 'b')
62
self.run_bzr('mv', 'b', 'subdir')
63
self.run_bzr('mv', 'subdir/b', 'a')
64
self.run_bzr('mv', 'a', 'b', 'subdir')
65
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
69
class TestVersion(BzrTestBase):
70
"""Check output from version command and master option is reasonable"""
72
# output is intentionally passed through to stdout so that we
73
# can see the version being tested
74
from cStringIO import StringIO
77
sys.stdout = tmp_out = StringIO()
79
self.run_bzr('version')
83
output = tmp_out.getvalue()
84
self.log('bzr version output:')
87
self.assert_(output.startswith('bzr (bazaar-ng) '))
88
self.assertNotEqual(output.index('Canonical'), -1)
90
# make sure --version is consistent
92
sys.stdout = tmp_out = StringIO()
94
self.run_bzr('--version')
98
self.log('bzr --version output:')
99
self.log(tmp_out.getvalue())
101
self.assertEquals(output, tmp_out.getvalue())
107
class HelpCommands(ExternalBase):
109
self.runbzr('--help')
111
self.runbzr('help commands')
112
self.runbzr('help help')
113
self.runbzr('commit -h')
116
class InitBranch(ExternalBase):
119
self.runbzr(['init'])
122
class UserIdentity(ExternalBase):
124
# this should always identify something, if only "john@localhost"
125
self.runbzr("whoami")
126
self.runbzr("whoami --email")
128
self.assertEquals(self.runbzr("whoami --email",
129
backtick=True).count('@'), 1)
131
class UserIdentityBranch(ExternalBase):
133
# tests branch specific user identity
135
f = file('.bzr/email', 'wt')
136
f.write('Branch Identity <branch@identi.ty>')
138
whoami = self.runbzr("whoami",backtick=True)
139
whoami_email = self.runbzr("whoami --email",backtick=True)
140
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
141
self.assertTrue(whoami_email.startswith('branch@identi.ty'))
144
class InvalidCommands(ExternalBase):
146
self.runbzr("pants", retcode=1)
147
self.runbzr("--pants off", retcode=1)
148
self.runbzr("diff --message foo", retcode=1)
152
class EmptyCommit(ExternalBase):
155
self.build_tree(['hello.txt'])
156
self.runbzr("commit -m empty", retcode=1)
157
self.runbzr("add hello.txt")
158
self.runbzr("commit -m added")
162
class IgnorePatterns(ExternalBase):
164
from bzrlib.branch import Branch
166
b = Branch('.', init=True)
167
self.assertEquals(list(b.unknowns()), [])
169
file('foo.tmp', 'wt').write('tmp files are ignored')
170
self.assertEquals(list(b.unknowns()), [])
171
assert self.backtick('bzr unknowns') == ''
173
file('foo.c', 'wt').write('int main() {}')
174
self.assertEquals(list(b.unknowns()), ['foo.c'])
175
assert self.backtick('bzr unknowns') == 'foo.c\n'
177
self.runbzr(['add', 'foo.c'])
178
assert self.backtick('bzr unknowns') == ''
180
# 'ignore' works when creating the .bzignore file
181
file('foo.blah', 'wt').write('blah')
182
self.assertEquals(list(b.unknowns()), ['foo.blah'])
183
self.runbzr('ignore *.blah')
184
self.assertEquals(list(b.unknowns()), [])
185
assert file('.bzrignore', 'rb').read() == '*.blah\n'
187
# 'ignore' works when then .bzrignore file already exists
188
file('garh', 'wt').write('garh')
189
self.assertEquals(list(b.unknowns()), ['garh'])
190
assert self.backtick('bzr unknowns') == 'garh\n'
191
self.runbzr('ignore garh')
192
self.assertEquals(list(b.unknowns()), [])
193
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
198
class OldTests(ExternalBase):
199
# old tests moved from ./testbzr
201
from os import chdir, mkdir
202
from os.path import exists
206
backtick = self.backtick
209
progress("basic branch creation")
214
self.assertEquals(backtick('bzr root').rstrip(),
215
os.path.join(self.test_dir, 'branch1'))
217
progress("status of new file")
219
f = file('test.txt', 'wt')
220
f.write('hello world!\n')
223
out = backtick("bzr unknowns")
224
self.assertEquals(out, 'test.txt\n')
226
out = backtick("bzr status")
227
assert out == 'unknown:\n test.txt\n'
229
out = backtick("bzr status --all")
230
assert out == "unknown:\n test.txt\n"
232
out = backtick("bzr status test.txt --all")
233
assert out == "unknown:\n test.txt\n"
235
f = file('test2.txt', 'wt')
236
f.write('goodbye cruel world...\n')
239
out = backtick("bzr status test.txt")
240
assert out == "unknown:\n test.txt\n"
242
out = backtick("bzr status")
243
assert out == ("unknown:\n"
247
os.unlink('test2.txt')
249
progress("command aliases")
250
out = backtick("bzr st --all")
251
assert out == ("unknown:\n"
254
out = backtick("bzr stat")
255
assert out == ("unknown:\n"
258
progress("command help")
261
runbzr("help commands")
262
runbzr("help slartibartfast", 1)
264
out = backtick("bzr help ci")
265
out.index('aliases: ')
267
progress("can't rename unversioned file")
268
runbzr("rename test.txt new-test.txt", 1)
270
progress("adding a file")
272
runbzr("add test.txt")
273
assert backtick("bzr unknowns") == ''
274
assert backtick("bzr status --all") == ("added:\n"
277
progress("rename newly-added file")
278
runbzr("rename test.txt hello.txt")
279
assert os.path.exists("hello.txt")
280
assert not os.path.exists("test.txt")
282
assert backtick("bzr revno") == '0\n'
284
progress("add first revision")
285
runbzr(['commit', '-m', 'add first revision'])
287
progress("more complex renames")
289
runbzr("rename hello.txt sub1", 1)
290
runbzr("rename hello.txt sub1/hello.txt", 1)
291
runbzr("move hello.txt sub1", 1)
294
runbzr("rename sub1 sub2")
295
runbzr("move hello.txt sub2")
296
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
298
assert exists("sub2")
299
assert exists("sub2/hello.txt")
300
assert not exists("sub1")
301
assert not exists("hello.txt")
303
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
307
runbzr('move sub2/hello.txt sub1')
308
assert not exists('sub2/hello.txt')
309
assert exists('sub1/hello.txt')
310
runbzr('move sub2 sub1')
311
assert not exists('sub2')
312
assert exists('sub1/sub2')
314
runbzr(['commit', '-m', 'rename nested subdirectories'])
317
self.assertEquals(backtick('bzr root')[:-1],
318
os.path.join(self.test_dir, 'branch1'))
319
runbzr('move ../hello.txt .')
320
assert exists('./hello.txt')
321
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
322
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
323
runbzr(['commit', '-m', 'move to parent directory'])
325
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
327
runbzr('move sub2/hello.txt .')
328
assert exists('hello.txt')
330
f = file('hello.txt', 'wt')
331
f.write('some nice new content\n')
334
f = file('msg.tmp', 'wt')
335
f.write('this is my new commit\n')
338
runbzr('commit -F msg.tmp')
340
assert backtick('bzr revno') == '5\n'
341
runbzr('export -r 5 export-5.tmp')
342
runbzr('export export.tmp')
346
runbzr('log -v --forward')
347
runbzr('log -m', retcode=1)
348
log_out = backtick('bzr log -m commit')
349
assert "this is my new commit" in log_out
350
assert "rename nested" not in log_out
351
assert 'revision-id' not in log_out
352
assert 'revision-id' in backtick('bzr log --show-ids -m commit')
355
progress("file with spaces in name")
356
mkdir('sub directory')
357
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
360
runbzr('commit -m add-spaces')
364
runbzr('log --forward')
373
class RevertCommand(ExternalBase):
377
file('hello', 'wt').write('foo')
378
self.runbzr('add hello')
379
self.runbzr('commit -m setup hello')
381
file('goodbye', 'wt').write('baz')
382
self.runbzr('add goodbye')
383
self.runbzr('commit -m setup goodbye')
385
file('hello', 'wt').write('bar')
386
file('goodbye', 'wt').write('qux')
387
self.runbzr('revert hello')
388
self.check_file_contents('hello', 'foo')
389
self.check_file_contents('goodbye', 'qux')
390
self.runbzr('revert')
391
self.check_file_contents('goodbye', 'baz')