26
26
it's normally invoked.
29
from cStringIO import StringIO
33
34
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
34
35
from bzrlib.branch import Branch
35
from bzrlib.commands import run_bzr
38
38
class ExternalBase(TestCaseInTempDir):
39
def runbzr(self, args, retcode=0,backtick=False):
40
def runbzr(self, args, retcode=0, backtick=False):
40
41
if isinstance(args, basestring):
41
42
args = args.split()
44
return self.backtick(['python', self.BZRPATH,] + args,
45
return self.run_bzr_captured(args, retcode=retcode)[0]
47
return self.runcmd(['python', self.BZRPATH,] + args,
47
return self.run_bzr_captured(args, retcode=retcode)
51
50
class TestCommands(ExternalBase):
74
73
f = file('.bzr/email', 'wt')
75
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']
77
79
whoami = self.runbzr("whoami",backtick=True)
78
80
whoami_email = self.runbzr("whoami --email",backtick=True)
79
81
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
80
82
self.assertTrue(whoami_email.startswith('branch@identi.ty'))
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
82
93
def test_invalid_commands(self):
83
94
self.runbzr("pants", retcode=1)
94
105
def test_ignore_patterns(self):
95
106
from bzrlib.branch import Branch
97
b = Branch('.', init=True)
108
b = Branch.initialize('.')
98
109
self.assertEquals(list(b.unknowns()), [])
100
111
file('foo.tmp', 'wt').write('tmp files are ignored')
101
112
self.assertEquals(list(b.unknowns()), [])
102
assert self.backtick('bzr unknowns') == ''
113
assert self.capture('unknowns') == ''
104
115
file('foo.c', 'wt').write('int main() {}')
105
116
self.assertEquals(list(b.unknowns()), ['foo.c'])
106
assert self.backtick('bzr unknowns') == 'foo.c\n'
117
assert self.capture('unknowns') == 'foo.c\n'
108
119
self.runbzr(['add', 'foo.c'])
109
assert self.backtick('bzr unknowns') == ''
120
assert self.capture('unknowns') == ''
111
122
# 'ignore' works when creating the .bzignore file
112
123
file('foo.blah', 'wt').write('blah')
113
124
self.assertEquals(list(b.unknowns()), ['foo.blah'])
114
125
self.runbzr('ignore *.blah')
115
126
self.assertEquals(list(b.unknowns()), [])
116
assert file('.bzrignore', 'rb').read() == '*.blah\n'
127
assert file('.bzrignore', 'rU').read() == '*.blah\n'
118
129
# 'ignore' works when then .bzrignore file already exists
119
130
file('garh', 'wt').write('garh')
120
131
self.assertEquals(list(b.unknowns()), ['garh'])
121
assert self.backtick('bzr unknowns') == 'garh\n'
132
assert self.capture('unknowns') == 'garh\n'
122
133
self.runbzr('ignore garh')
123
134
self.assertEquals(list(b.unknowns()), [])
124
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
135
assert file('.bzrignore', 'rU').read() == '*.blah\ngarh\n'
126
137
def test_revert(self):
127
138
self.runbzr('init')
148
159
os.rmdir('revertdir')
149
160
self.runbzr('revert')
151
def skipped_test_mv_modes(self):
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):
152
174
"""Test two modes of operation for mv"""
153
175
from bzrlib.branch import Branch
154
b = Branch('.', init=True)
176
b = Branch.initialize('.')
155
177
self.build_tree(['a', 'c', 'subdir/'])
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', 'b', 'subdir')
160
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
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'])
162
186
def test_main_version(self):
163
187
"""Check output from version command and master option is reasonable"""
183
207
test.runbzr('add goodbye')
184
208
test.runbzr('commit -m setup goodbye')
186
def test_revert(self):
187
self.example_branch()
188
file('hello', 'wt').write('bar')
189
file('goodbye', 'wt').write('qux')
190
self.runbzr('revert hello')
191
self.check_file_contents('hello', 'foo')
192
self.check_file_contents('goodbye', 'qux')
193
self.runbzr('revert')
194
self.check_file_contents('goodbye', 'baz')
211
self.example_branch()
212
file('hello', 'wt').write('hello world!')
213
self.runbzr('commit -m fixing hello')
214
output = self.runbzr('diff -r 2..3', backtick=1)
215
self.assert_('\n+hello world!' in output)
216
output = self.runbzr('diff -r last:3..last:1', backtick=1)
217
self.assert_('\n+baz' in output)
219
def test_branch(self):
220
"""Branch from one branch to another."""
223
self.example_branch()
225
self.runbzr('branch a b')
226
self.runbzr('branch a c -r 1')
196
228
def test_merge(self):
197
229
from bzrlib.branch import Branch
213
245
self.runbzr('merge ../b')
214
246
self.check_file_contents('goodbye', 'quux')
215
247
# Merging a branch pulls its revision into the tree
249
b = Branch.open('../b')
218
250
a.get_revision_xml(b.last_patch())
219
251
self.log('pending merges: %s', a.pending_merges())
220
252
# assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
228
260
self.example_branch()
261
self.runbzr('pull', retcode=1)
262
self.runbzr('missing', retcode=1)
263
self.runbzr('missing .')
264
self.runbzr('missing')
266
self.runbzr('pull /', retcode=1)
230
270
self.runbzr('branch a b')
274
self.runbzr('add subdir')
232
275
self.runbzr('commit -m blah --unchanged')
278
b = Branch.open('../b')
236
279
assert a.revision_history() == b.revision_history()[:-1]
237
280
self.runbzr('pull ../b')
238
281
assert a.revision_history() == b.revision_history()
244
287
self.runbzr('merge ../b')
245
288
self.runbzr('commit -m blah4 --unchanged')
247
self.runbzr('pull ../a')
289
os.chdir('../b/subdir')
290
self.runbzr('pull ../../a')
248
291
assert a.revision_history()[-1] == b.revision_history()[-1]
251
293
def test_add_reports(self):
252
294
"""add command prints the names of added files."""
253
b = Branch('.', init=True)
295
b = Branch.initialize('.')
254
296
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
256
from cStringIO import StringIO
259
ret = self.apply_redirected(None, out, None,
262
self.assertEquals(ret, 0)
297
out = self.run_bzr_captured(['add'], retcode = 0)[0]
264
298
# the ordering is not defined at the moment
265
results = sorted(out.getvalue().rstrip('\n').split('\n'))
299
results = sorted(out.rstrip('\n').split('\n'))
266
300
self.assertEquals(['added dir',
301
'added dir'+os.sep+'sub.txt',
268
302
'added top.txt',],
305
def test_unknown_command(self):
306
"""Handling of unknown command."""
307
out, err = self.run_bzr_captured(['fluffy-badger'],
309
self.assertEquals(out, '')
310
err.index('unknown command')
272
314
def has_symlinks():
273
315
if hasattr(os, 'symlink'):
300
self.assertEquals(backtick('bzr root').rstrip(),
342
self.assertEquals(capture('root').rstrip(),
301
343
os.path.join(self.test_dir, 'branch1'))
303
345
progress("status of new file")
306
348
f.write('hello world!\n')
309
out = backtick("bzr unknowns")
310
self.assertEquals(out, 'test.txt\n')
351
self.assertEquals(capture('unknowns'), 'test.txt\n')
312
out = backtick("bzr status")
353
out = capture("status")
313
354
assert out == 'unknown:\n test.txt\n'
315
out = backtick("bzr status --all")
356
out = capture("status --all")
316
357
assert out == "unknown:\n test.txt\n"
318
out = backtick("bzr status test.txt --all")
359
out = capture("status test.txt --all")
319
360
assert out == "unknown:\n test.txt\n"
321
362
f = file('test2.txt', 'wt')
322
363
f.write('goodbye cruel world...\n')
325
out = backtick("bzr status test.txt")
366
out = capture("status test.txt")
326
367
assert out == "unknown:\n test.txt\n"
328
out = backtick("bzr status")
369
out = capture("status")
329
370
assert out == ("unknown:\n"
333
374
os.unlink('test2.txt')
335
376
progress("command aliases")
336
out = backtick("bzr st --all")
377
out = capture("st --all")
337
378
assert out == ("unknown:\n"
340
out = backtick("bzr stat")
381
out = capture("stat")
341
382
assert out == ("unknown:\n"
356
397
progress("adding a file")
358
399
runbzr("add test.txt")
359
assert backtick("bzr unknowns") == ''
360
assert backtick("bzr status --all") == ("added:\n"
400
assert capture("unknowns") == ''
401
assert capture("status --all") == ("added:\n"
363
404
progress("rename newly-added file")
365
406
assert os.path.exists("hello.txt")
366
407
assert not os.path.exists("test.txt")
368
assert backtick("bzr revno") == '0\n'
409
assert capture("revno") == '0\n'
370
411
progress("add first revision")
371
412
runbzr(['commit', '-m', 'add first revision'])
379
420
runbzr("add sub1")
380
421
runbzr("rename sub1 sub2")
381
422
runbzr("move hello.txt sub2")
382
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
423
assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
384
425
assert exists("sub2")
385
426
assert exists("sub2/hello.txt")
400
441
runbzr(['commit', '-m', 'rename nested subdirectories'])
402
443
chdir('sub1/sub2')
403
self.assertEquals(backtick('bzr root')[:-1],
444
self.assertEquals(capture('root')[:-1],
404
445
os.path.join(self.test_dir, 'branch1'))
405
446
runbzr('move ../hello.txt .')
406
447
assert exists('./hello.txt')
407
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
408
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
448
self.assertEquals(capture('relpath hello.txt'),
449
os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
450
assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
409
451
runbzr(['commit', '-m', 'move to parent directory'])
411
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
453
assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
413
455
runbzr('move sub2/hello.txt .')
414
456
assert exists('hello.txt')
424
466
runbzr('commit -F msg.tmp')
426
assert backtick('bzr revno') == '5\n'
468
assert capture('revno') == '5\n'
427
469
runbzr('export -r 5 export-5.tmp')
428
470
runbzr('export export.tmp')
432
474
runbzr('log -v --forward')
433
475
runbzr('log -m', retcode=1)
434
log_out = backtick('bzr log -m commit')
476
log_out = capture('log -m commit')
435
477
assert "this is my new commit" in log_out
436
478
assert "rename nested" not in log_out
437
479
assert 'revision-id' not in log_out
438
assert 'revision-id' in backtick('bzr log --show-ids -m commit')
480
assert 'revision-id' in capture('log --show-ids -m commit')
441
483
progress("file with spaces in name")
459
501
os.symlink("NOWHERE1", "link1")
460
502
runbzr('add link1')
461
assert backtick('bzr unknowns') == ''
503
assert self.capture('unknowns') == ''
462
504
runbzr(['commit', '-m', '1: added symlink link1'])
466
assert backtick('bzr unknowns') == ''
508
assert self.capture('unknowns') == ''
467
509
os.symlink("NOWHERE2", "d1/link2")
468
assert backtick('bzr unknowns') == 'd1/link2\n'
510
assert self.capture('unknowns') == 'd1/link2\n'
469
511
# is d1/link2 found when adding d1
471
assert backtick('bzr unknowns') == ''
513
assert self.capture('unknowns') == ''
472
514
os.symlink("NOWHERE3", "d1/link3")
473
assert backtick('bzr unknowns') == 'd1/link3\n'
515
assert self.capture('unknowns') == 'd1/link3\n'
474
516
runbzr(['commit', '-m', '2: added dir, symlink'])
476
518
runbzr('rename d1 d2')
487
529
os.unlink("d2/link1")
488
530
os.symlink("TARGET 1", "d2/link1")
490
assert backtick("bzr relpath d2/link1") == "d2/link1\n"
532
assert self.capture("relpath d2/link1") == "d2/link1\n"
491
533
runbzr(['commit', '-m', '4: retarget of two links'])
493
535
runbzr('remove d2/link1')
494
assert backtick('bzr unknowns') == 'd2/link1\n'
536
assert self.capture('unknowns') == 'd2/link1\n'
495
537
runbzr(['commit', '--unchanged', '-m', '5: remove d2/link1'])
496
538
print ("commit --uchanged is needed to delete a file with no other"
497
539
" changes. this is a bug.")
501
543
runbzr('rename d2/link3 d1/link3new')
502
assert backtick('bzr unknowns') == 'd2/link1\n'
544
assert self.capture('unknowns') == 'd2/link1\n'
503
545
runbzr(['commit', '-m', '6: remove d2/link1, move/rename link3'])
505
547
runbzr(['check'])