26
26
it's normally invoked.
29
from cStringIO import StringIO
33
32
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
34
33
from bzrlib.branch import Branch
34
from bzrlib.commands import run_bzr
37
37
class ExternalBase(TestCaseInTempDir):
39
def runbzr(self, args, retcode=0, backtick=False):
38
def runbzr(self, args, retcode=0,backtick=False):
40
39
if isinstance(args, basestring):
41
40
args = args.split()
44
return self.run_bzr_captured(args, retcode=retcode)[0]
43
return self.backtick(['python', self.BZRPATH,] + args,
46
return self.run_bzr_captured(args, retcode=retcode)
46
return self.runcmd(['python', self.BZRPATH,] + args,
49
50
class TestCommands(ExternalBase):
72
73
f = file('.bzr/email', 'wt')
73
74
f.write('Branch Identity <branch@identi.ty>')
75
bzr_email = os.environ.get('BZREMAIL')
76
if bzr_email is not None:
77
del os.environ['BZREMAIL']
78
76
whoami = self.runbzr("whoami",backtick=True)
79
77
whoami_email = self.runbzr("whoami --email",backtick=True)
80
78
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
81
79
self.assertTrue(whoami_email.startswith('branch@identi.ty'))
82
# Verify that the environment variable overrides the value
84
os.environ['BZREMAIL'] = 'Different ID <other@environ.ment>'
85
whoami = self.runbzr("whoami",backtick=True)
86
whoami_email = self.runbzr("whoami --email",backtick=True)
87
self.assertTrue(whoami.startswith('Different ID <other@environ.ment>'))
88
self.assertTrue(whoami_email.startswith('other@environ.ment'))
89
if bzr_email is not None:
90
os.environ['BZREMAIL'] = bzr_email
92
81
def test_invalid_commands(self):
93
82
self.runbzr("pants", retcode=1)
104
93
def test_ignore_patterns(self):
105
94
from bzrlib.branch import Branch
107
b = Branch.initialize('.')
96
b = Branch('.', init=True)
108
97
self.assertEquals(list(b.unknowns()), [])
110
99
file('foo.tmp', 'wt').write('tmp files are ignored')
111
100
self.assertEquals(list(b.unknowns()), [])
112
assert self.capture('unknowns') == ''
101
assert self.backtick('bzr unknowns') == ''
114
103
file('foo.c', 'wt').write('int main() {}')
115
104
self.assertEquals(list(b.unknowns()), ['foo.c'])
116
assert self.capture('unknowns') == 'foo.c\n'
105
assert self.backtick('bzr unknowns') == 'foo.c\n'
118
107
self.runbzr(['add', 'foo.c'])
119
assert self.capture('unknowns') == ''
108
assert self.backtick('bzr unknowns') == ''
121
110
# 'ignore' works when creating the .bzignore file
122
111
file('foo.blah', 'wt').write('blah')
128
117
# 'ignore' works when then .bzrignore file already exists
129
118
file('garh', 'wt').write('garh')
130
119
self.assertEquals(list(b.unknowns()), ['garh'])
131
assert self.capture('unknowns') == 'garh\n'
120
assert self.backtick('bzr unknowns') == 'garh\n'
132
121
self.runbzr('ignore garh')
133
122
self.assertEquals(list(b.unknowns()), [])
134
123
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
158
147
os.rmdir('revertdir')
159
148
self.runbzr('revert')
161
file('hello', 'wt').write('xyz')
162
self.runbzr('commit -m xyz hello')
163
self.runbzr('revert -r 1 hello')
164
self.check_file_contents('hello', 'foo')
165
self.runbzr('revert hello')
166
self.check_file_contents('hello', 'xyz')
168
150
def test_mv_modes(self):
169
151
"""Test two modes of operation for mv"""
170
152
from bzrlib.branch import Branch
171
b = Branch.initialize('.')
153
b = Branch('.', init=True)
172
154
self.build_tree(['a', 'c', 'subdir/'])
173
self.run_bzr_captured(['add', self.test_dir])
174
self.run_bzr_captured(['mv', 'a', 'b'])
175
self.run_bzr_captured(['mv', 'b', 'subdir'])
176
self.run_bzr_captured(['mv', 'subdir/b', 'a'])
177
self.run_bzr_captured(['mv', 'a', 'c', 'subdir'])
178
self.run_bzr_captured(['mv', 'subdir/a', 'subdir/newa'])
155
self.run_bzr('add', self.test_dir)
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', 'c', 'subdir')
160
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
181
163
def test_main_version(self):
202
184
test.runbzr('add goodbye')
203
185
test.runbzr('commit -m setup goodbye')
206
self.example_branch()
207
file('hello', 'wt').write('hello world!')
208
self.runbzr('commit -m fixing hello')
209
output = self.runbzr('diff -r 2..3', backtick=1)
210
self.assert_('\n+hello world!' in output)
211
output = self.runbzr('diff -r last:3..last:1', backtick=1)
212
self.assert_('\n+baz' in output)
215
self.example_branch()
216
file('hello', 'wt').write('hello world!')
217
self.runbzr('commit -m fixing hello')
218
output = self.runbzr('diff -r 2..3', backtick=1)
219
self.assert_('\n+hello world!' in output)
220
output = self.runbzr('diff -r last:3..last:1', backtick=1)
221
self.assert_('\n+baz' in output)
187
def test_revert(self):
188
self.example_branch()
189
file('hello', 'wt').write('bar')
190
file('goodbye', 'wt').write('qux')
191
self.runbzr('revert hello')
192
self.check_file_contents('hello', 'foo')
193
self.check_file_contents('goodbye', 'qux')
194
self.runbzr('revert')
195
self.check_file_contents('goodbye', 'baz')
223
197
def test_merge(self):
224
198
from bzrlib.branch import Branch
240
214
self.runbzr('merge ../b')
241
215
self.check_file_contents('goodbye', 'quux')
242
216
# Merging a branch pulls its revision into the tree
244
b = Branch.open('../b')
245
219
a.get_revision_xml(b.last_patch())
246
220
self.log('pending merges: %s', a.pending_merges())
247
221
# assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
255
229
self.example_branch()
256
self.runbzr('pull', retcode=1)
257
self.runbzr('missing', retcode=1)
258
self.runbzr('missing .')
259
self.runbzr('missing')
261
self.runbzr('pull /', retcode=1)
265
231
self.runbzr('branch a b')
268
233
self.runbzr('commit -m blah --unchanged')
271
b = Branch.open('../b')
272
237
assert a.revision_history() == b.revision_history()[:-1]
273
238
self.runbzr('pull ../b')
274
239
assert a.revision_history() == b.revision_history()
283
248
self.runbzr('pull ../a')
284
249
assert a.revision_history()[-1] == b.revision_history()[-1]
286
252
def test_add_reports(self):
287
253
"""add command prints the names of added files."""
288
b = Branch.initialize('.')
254
b = Branch('.', init=True)
289
255
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
290
out = self.run_bzr_captured(['add'], retcode = 0)[0]
257
from cStringIO import StringIO
260
ret = self.apply_redirected(None, out, None,
263
self.assertEquals(ret, 0)
291
265
# the ordering is not defined at the moment
292
results = sorted(out.rstrip('\n').split('\n'))
266
results = sorted(out.getvalue().rstrip('\n').split('\n'))
293
267
self.assertEquals(['added dir',
294
268
'added dir/sub.txt',
295
269
'added top.txt',],
298
def test_unknown_command(self):
299
"""Handling of unknown command."""
300
out, err = self.run_bzr_captured(['fluffy-badger'],
302
self.assertEquals(out, '')
303
err.index('unknown command')
307
273
class OldTests(ExternalBase):
308
274
"""old tests moved from ./testbzr."""
323
self.assertEquals(capture('root').rstrip(),
289
self.assertEquals(backtick('bzr root').rstrip(),
324
290
os.path.join(self.test_dir, 'branch1'))
326
292
progress("status of new file")
329
295
f.write('hello world!\n')
332
self.assertEquals(capture('unknowns'), 'test.txt\n')
298
out = backtick("bzr unknowns")
299
self.assertEquals(out, 'test.txt\n')
334
out = capture("status")
301
out = backtick("bzr status")
335
302
assert out == 'unknown:\n test.txt\n'
337
out = capture("status --all")
304
out = backtick("bzr status --all")
338
305
assert out == "unknown:\n test.txt\n"
340
out = capture("status test.txt --all")
307
out = backtick("bzr status test.txt --all")
341
308
assert out == "unknown:\n test.txt\n"
343
310
f = file('test2.txt', 'wt')
344
311
f.write('goodbye cruel world...\n')
347
out = capture("status test.txt")
314
out = backtick("bzr status test.txt")
348
315
assert out == "unknown:\n test.txt\n"
350
out = capture("status")
317
out = backtick("bzr status")
351
318
assert out == ("unknown:\n"
355
322
os.unlink('test2.txt')
357
324
progress("command aliases")
358
out = capture("st --all")
325
out = backtick("bzr st --all")
359
326
assert out == ("unknown:\n"
362
out = capture("stat")
329
out = backtick("bzr stat")
363
330
assert out == ("unknown:\n"
378
345
progress("adding a file")
380
347
runbzr("add test.txt")
381
assert capture("unknowns") == ''
382
assert capture("status --all") == ("added:\n"
348
assert backtick("bzr unknowns") == ''
349
assert backtick("bzr status --all") == ("added:\n"
385
352
progress("rename newly-added file")
387
354
assert os.path.exists("hello.txt")
388
355
assert not os.path.exists("test.txt")
390
assert capture("revno") == '0\n'
357
assert backtick("bzr revno") == '0\n'
392
359
progress("add first revision")
393
360
runbzr(['commit', '-m', 'add first revision'])
401
368
runbzr("add sub1")
402
369
runbzr("rename sub1 sub2")
403
370
runbzr("move hello.txt sub2")
404
assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
371
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
406
373
assert exists("sub2")
407
374
assert exists("sub2/hello.txt")
422
389
runbzr(['commit', '-m', 'rename nested subdirectories'])
424
391
chdir('sub1/sub2')
425
self.assertEquals(capture('root')[:-1],
392
self.assertEquals(backtick('bzr root')[:-1],
426
393
os.path.join(self.test_dir, 'branch1'))
427
394
runbzr('move ../hello.txt .')
428
395
assert exists('./hello.txt')
429
self.assertEquals(capture('relpath hello.txt'),
430
os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
431
assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
396
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
397
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
432
398
runbzr(['commit', '-m', 'move to parent directory'])
434
assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
400
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
436
402
runbzr('move sub2/hello.txt .')
437
403
assert exists('hello.txt')
447
413
runbzr('commit -F msg.tmp')
449
assert capture('revno') == '5\n'
415
assert backtick('bzr revno') == '5\n'
450
416
runbzr('export -r 5 export-5.tmp')
451
417
runbzr('export export.tmp')
455
421
runbzr('log -v --forward')
456
422
runbzr('log -m', retcode=1)
457
log_out = capture('log -m commit')
423
log_out = backtick('bzr log -m commit')
458
424
assert "this is my new commit" in log_out
459
425
assert "rename nested" not in log_out
460
426
assert 'revision-id' not in log_out
461
assert 'revision-id' in capture('log --show-ids -m commit')
427
assert 'revision-id' in backtick('bzr log --show-ids -m commit')
464
430
progress("file with spaces in name")