26
26
it's normally invoked.
29
from cStringIO import StringIO
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:
34
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
35
from bzrlib.branch import Branch
38
class ExternalBase(TestCaseInTempDir):
40
def runbzr(self, args, retcode=0, backtick=False):
44
41
if isinstance(args, basestring):
45
42
args = args.split()
48
return self.backtick(['python', self.BZRPATH,] + args,
45
return self.run_bzr_captured(args, retcode=retcode)[0]
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):
47
return self.run_bzr_captured(args, retcode=retcode)
50
class TestCommands(ExternalBase):
52
def test_help_commands(self):
109
53
self.runbzr('--help')
110
54
self.runbzr('help')
111
55
self.runbzr('help commands')
112
56
self.runbzr('help help')
113
57
self.runbzr('commit -h')
116
class InitBranch(ExternalBase):
59
def test_init_branch(self):
119
60
self.runbzr(['init'])
122
class UserIdentity(ExternalBase):
62
def test_whoami(self):
124
63
# this should always identify something, if only "john@localhost"
125
64
self.runbzr("whoami")
126
65
self.runbzr("whoami --email")
128
67
self.assertEquals(self.runbzr("whoami --email",
129
68
backtick=True).count('@'), 1)
131
class UserIdentityBranch(ExternalBase):
133
# tests branch specific user identity
70
def test_whoami_branch(self):
71
"""branch specific user identity works."""
134
72
self.runbzr('init')
135
73
f = file('.bzr/email', 'wt')
136
74
f.write('Branch Identity <branch@identi.ty>')
76
bzr_email = os.environ.get('BZREMAIL')
77
if bzr_email is not None:
78
del os.environ['BZREMAIL']
138
79
whoami = self.runbzr("whoami",backtick=True)
139
80
whoami_email = self.runbzr("whoami --email",backtick=True)
140
81
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
141
82
self.assertTrue(whoami_email.startswith('branch@identi.ty'))
144
class InvalidCommands(ExternalBase):
83
# Verify that the environment variable overrides the value
85
os.environ['BZREMAIL'] = 'Different ID <other@environ.ment>'
86
whoami = self.runbzr("whoami",backtick=True)
87
whoami_email = self.runbzr("whoami --email",backtick=True)
88
self.assertTrue(whoami.startswith('Different ID <other@environ.ment>'))
89
self.assertTrue(whoami_email.startswith('other@environ.ment'))
90
if bzr_email is not None:
91
os.environ['BZREMAIL'] = bzr_email
93
def test_invalid_commands(self):
146
94
self.runbzr("pants", retcode=1)
147
95
self.runbzr("--pants off", retcode=1)
148
96
self.runbzr("diff --message foo", retcode=1)
152
class EmptyCommit(ExternalBase):
98
def test_empty_commit(self):
154
99
self.runbzr("init")
155
100
self.build_tree(['hello.txt'])
156
101
self.runbzr("commit -m empty", retcode=1)
157
102
self.runbzr("add hello.txt")
158
103
self.runbzr("commit -m added")
162
class IgnorePatterns(ExternalBase):
105
def test_ignore_patterns(self):
164
106
from bzrlib.branch import Branch
166
b = Branch('.', init=True)
108
b = Branch.initialize('.')
167
109
self.assertEquals(list(b.unknowns()), [])
169
111
file('foo.tmp', 'wt').write('tmp files are ignored')
170
112
self.assertEquals(list(b.unknowns()), [])
171
assert self.backtick('bzr unknowns') == ''
113
assert self.capture('unknowns') == ''
173
115
file('foo.c', 'wt').write('int main() {}')
174
116
self.assertEquals(list(b.unknowns()), ['foo.c'])
175
assert self.backtick('bzr unknowns') == 'foo.c\n'
117
assert self.capture('unknowns') == 'foo.c\n'
177
119
self.runbzr(['add', 'foo.c'])
178
assert self.backtick('bzr unknowns') == ''
120
assert self.capture('unknowns') == ''
180
122
# 'ignore' works when creating the .bzignore file
181
123
file('foo.blah', 'wt').write('blah')
182
124
self.assertEquals(list(b.unknowns()), ['foo.blah'])
183
125
self.runbzr('ignore *.blah')
184
126
self.assertEquals(list(b.unknowns()), [])
185
assert file('.bzrignore', 'rb').read() == '*.blah\n'
127
assert file('.bzrignore', 'rU').read() == '*.blah\n'
187
129
# 'ignore' works when then .bzrignore file already exists
188
130
file('garh', 'wt').write('garh')
189
131
self.assertEquals(list(b.unknowns()), ['garh'])
190
assert self.backtick('bzr unknowns') == 'garh\n'
132
assert self.capture('unknowns') == 'garh\n'
191
133
self.runbzr('ignore garh')
192
134
self.assertEquals(list(b.unknowns()), [])
193
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
135
assert file('.bzrignore', 'rU').read() == '*.blah\ngarh\n'
137
def test_revert(self):
140
file('hello', 'wt').write('foo')
141
self.runbzr('add hello')
142
self.runbzr('commit -m setup hello')
144
file('goodbye', 'wt').write('baz')
145
self.runbzr('add goodbye')
146
self.runbzr('commit -m setup goodbye')
148
file('hello', 'wt').write('bar')
149
file('goodbye', 'wt').write('qux')
150
self.runbzr('revert hello')
151
self.check_file_contents('hello', 'foo')
152
self.check_file_contents('goodbye', 'qux')
153
self.runbzr('revert')
154
self.check_file_contents('goodbye', 'baz')
156
os.mkdir('revertdir')
157
self.runbzr('add revertdir')
158
self.runbzr('commit -m f')
159
os.rmdir('revertdir')
160
self.runbzr('revert')
162
file('hello', 'wt').write('xyz')
163
self.runbzr('commit -m xyz hello')
164
self.runbzr('revert -r 1 hello')
165
self.check_file_contents('hello', 'foo')
166
self.runbzr('revert hello')
167
self.check_file_contents('hello', 'xyz')
168
os.chdir('revertdir')
169
self.runbzr('revert')
173
def test_mv_modes(self):
174
"""Test two modes of operation for mv"""
175
from bzrlib.branch import Branch
176
b = Branch.initialize('.')
177
self.build_tree(['a', 'c', 'subdir/'])
178
self.run_bzr_captured(['add', self.test_dir])
179
self.run_bzr_captured(['mv', 'a', 'b'])
180
self.run_bzr_captured(['mv', 'b', 'subdir'])
181
self.run_bzr_captured(['mv', 'subdir/b', 'a'])
182
self.run_bzr_captured(['mv', 'a', 'c', 'subdir'])
183
self.run_bzr_captured(['mv', 'subdir/a', 'subdir/newa'])
186
def test_main_version(self):
187
"""Check output from version command and master option is reasonable"""
188
# output is intentionally passed through to stdout so that we
189
# can see the version being tested
190
output = self.runbzr('version', backtick=1)
191
self.log('bzr version output:')
193
self.assert_(output.startswith('bzr (bazaar-ng) '))
194
self.assertNotEqual(output.index('Canonical'), -1)
195
# make sure --version is consistent
196
tmp_output = self.runbzr('--version', backtick=1)
197
self.log('bzr --version output:')
199
self.assertEquals(output, tmp_output)
201
def example_branch(test):
203
file('hello', 'wt').write('foo')
204
test.runbzr('add hello')
205
test.runbzr('commit -m setup hello')
206
file('goodbye', 'wt').write('baz')
207
test.runbzr('add goodbye')
208
test.runbzr('commit -m setup goodbye')
210
def test_export(self):
213
self.example_branch()
214
self.runbzr('export ../latest')
215
self.assertEqual(file('../latest/goodbye', 'rt').read(), 'baz')
216
self.runbzr('export ../first -r 1')
217
assert not os.path.exists('../first/goodbye')
218
self.assertEqual(file('../first/hello', 'rt').read(), 'foo')
219
self.runbzr('export ../first.gz -r 1')
220
self.assertEqual(file('../first.gz/hello', 'rt').read(), 'foo')
221
self.runbzr('export ../first.bz2 -r 1')
222
self.assertEqual(file('../first.bz2/hello', 'rt').read(), 'foo')
223
self.runbzr('export ../first.tar -r 1')
224
assert os.path.isfile('../first.tar')
225
from tarfile import TarFile
226
tf = TarFile('../first.tar')
227
assert 'first/hello' in tf.getnames(), tf.getnames()
228
self.assertEqual(tf.extractfile('first/hello').read(), 'foo')
229
self.runbzr('export ../first.tar.gz -r 1')
230
assert os.path.isfile('../first.tar.gz')
231
self.runbzr('export ../first.tbz2 -r 1')
232
assert os.path.isfile('../first.tbz2')
233
self.runbzr('export ../first.tar.bz2 -r 1')
234
assert os.path.isfile('../first.tar.bz2')
235
self.runbzr('export ../first.tar.tbz2 -r 1')
236
assert os.path.isfile('../first.tar.tbz2')
237
from bz2 import BZ2File
238
tf = TarFile('../first.tar.tbz2',
239
fileobj=BZ2File('../first.tar.tbz2', 'r'))
240
assert 'first.tar/hello' in tf.getnames(), tf.getnames()
241
self.assertEqual(tf.extractfile('first.tar/hello').read(), 'foo')
242
self.runbzr('export ../first2.tar -r 1 --root pizza')
243
tf = TarFile('../first2.tar')
244
assert 'pizza/hello' in tf.getnames(), tf.getnames()
247
self.example_branch()
248
file('hello', 'wt').write('hello world!')
249
self.runbzr('commit -m fixing hello')
250
output = self.runbzr('diff -r 2..3', backtick=1)
251
self.assert_('\n+hello world!' in output)
252
output = self.runbzr('diff -r last:3..last:1', backtick=1)
253
self.assert_('\n+baz' in output)
255
def test_branch(self):
256
"""Branch from one branch to another."""
259
self.example_branch()
261
self.runbzr('branch a b')
262
self.runbzr('branch a c -r 1')
264
self.runbzr('commit -m foo --unchanged')
266
# naughty - abstraction violations RBC 20050928
267
print "test_branch used to delete the stores, how is this meant to work ?"
268
#shutil.rmtree('a/.bzr/revision-store')
269
#shutil.rmtree('a/.bzr/inventory-store', ignore_errors=True)
270
#shutil.rmtree('a/.bzr/text-store', ignore_errors=True)
271
self.runbzr('branch a d --basis b')
273
def test_merge(self):
274
from bzrlib.branch import Branch
278
self.example_branch()
280
self.runbzr('branch a b')
282
file('goodbye', 'wt').write('quux')
283
self.runbzr(['commit', '-m', "more u's are always good"])
286
file('hello', 'wt').write('quuux')
287
# We can't merge when there are in-tree changes
288
self.runbzr('merge ../b', retcode=1)
289
self.runbzr(['commit', '-m', "Like an epidemic of u's"])
290
self.runbzr('merge ../b')
291
self.check_file_contents('goodbye', 'quux')
292
# Merging a branch pulls its revision into the tree
294
b = Branch.open('../b')
295
a.get_revision_xml(b.last_revision())
296
self.log('pending merges: %s', a.pending_merges())
297
# assert a.pending_merges() == [b.last_revision()], "Assertion %s %s" \
298
# % (a.pending_merges(), b.last_patch())
300
def test_merge_with_missing_file(self):
301
"""Merge handles missing file conflicts"""
305
print >> file('sub/a.txt', 'wb'), "hello"
308
self.runbzr(('commit', '-m', 'added a'))
309
self.runbzr('branch . ../b')
310
print >> file('sub/a.txt', 'ab'), "there"
311
self.runbzr(('commit', '-m', 'Added there'))
312
os.unlink('sub/a.txt')
314
self.runbzr(('commit', '-m', 'Removed a.txt'))
316
print >> file('sub/a.txt', 'ab'), "something"
317
self.runbzr(('commit', '-m', 'Modified a.txt'))
318
self.runbzr('merge ../a/')
319
assert os.path.exists('sub/a.txt.THIS')
320
assert os.path.exists('sub/a.txt.BASE')
323
"""Pull changes from one branch to another."""
327
self.example_branch()
328
self.runbzr('pull', retcode=1)
329
self.runbzr('missing', retcode=1)
330
self.runbzr('missing .')
331
self.runbzr('missing')
333
self.runbzr('pull /', retcode=1)
337
self.runbzr('branch a b')
341
self.runbzr('add subdir')
342
self.runbzr('commit -m blah --unchanged')
345
b = Branch.open('../b')
346
assert a.revision_history() == b.revision_history()[:-1]
347
self.runbzr('pull ../b')
348
assert a.revision_history() == b.revision_history()
349
self.runbzr('commit -m blah2 --unchanged')
351
self.runbzr('commit -m blah3 --unchanged')
352
self.runbzr('pull ../a', retcode=1)
353
print "DECIDE IF PULL CAN CONVERGE, blackbox.py"
356
self.runbzr('merge ../b')
357
self.runbzr('commit -m blah4 --unchanged')
358
os.chdir('../b/subdir')
359
self.runbzr('pull ../../a')
360
assert a.revision_history()[-1] == b.revision_history()[-1]
361
self.runbzr('commit -m blah5 --unchanged')
362
self.runbzr('commit -m blah6 --unchanged')
364
self.runbzr('pull ../a')
366
self.runbzr('commit -m blah7 --unchanged')
367
self.runbzr('merge ../b')
368
self.runbzr('commit -m blah8 --unchanged')
369
self.runbzr('pull ../b')
370
self.runbzr('pull ../b')
372
def test_add_reports(self):
373
"""add command prints the names of added files."""
374
b = Branch.initialize('.')
375
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
376
out = self.run_bzr_captured(['add'], retcode = 0)[0]
377
# the ordering is not defined at the moment
378
results = sorted(out.rstrip('\n').split('\n'))
379
self.assertEquals(['added dir',
380
'added dir'+os.sep+'sub.txt',
384
def test_unknown_command(self):
385
"""Handling of unknown command."""
386
out, err = self.run_bzr_captured(['fluffy-badger'],
388
self.assertEquals(out, '')
389
err.index('unknown command')
198
393
class OldTests(ExternalBase):
199
# old tests moved from ./testbzr
394
"""old tests moved from ./testbzr."""
201
397
from os import chdir, mkdir
202
398
from os.path import exists
205
400
runbzr = self.runbzr
206
backtick = self.backtick
401
capture = self.capture
207
402
progress = self.log
209
404
progress("basic branch creation")