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'
126
def skipped_test_mv_modes(self):
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
os.symlink('/unlikely/to/exist', 'symlink')
163
self.runbzr('add symlink')
164
self.runbzr('commit -m f')
166
self.runbzr('revert')
168
file('hello', 'wt').write('xyz')
169
self.runbzr('commit -m xyz hello')
170
self.runbzr('revert -r 1 hello')
171
self.check_file_contents('hello', 'foo')
172
self.runbzr('revert hello')
173
self.check_file_contents('hello', 'xyz')
174
os.chdir('revertdir')
175
self.runbzr('revert')
179
def test_mv_modes(self):
127
180
"""Test two modes of operation for mv"""
128
181
from bzrlib.branch import Branch
129
b = Branch('.', init=True)
182
b = Branch.initialize('.')
130
183
self.build_tree(['a', 'c', 'subdir/'])
131
self.run_bzr('mv', 'a', 'b')
132
self.run_bzr('mv', 'b', 'subdir')
133
self.run_bzr('mv', 'subdir/b', 'a')
134
self.run_bzr('mv', 'a', 'b', 'subdir')
135
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
184
self.run_bzr_captured(['add', self.test_dir])
185
self.run_bzr_captured(['mv', 'a', 'b'])
186
self.run_bzr_captured(['mv', 'b', 'subdir'])
187
self.run_bzr_captured(['mv', 'subdir/b', 'a'])
188
self.run_bzr_captured(['mv', 'a', 'c', 'subdir'])
189
self.run_bzr_captured(['mv', 'subdir/a', 'subdir/newa'])
137
192
def test_main_version(self):
138
193
"""Check output from version command and master option is reasonable"""
158
213
test.runbzr('add goodbye')
159
214
test.runbzr('commit -m setup goodbye')
161
def test_revert(self):
162
self.example_branch()
163
file('hello', 'wt').write('bar')
164
file('goodbye', 'wt').write('qux')
165
self.runbzr('revert hello')
166
self.check_file_contents('hello', 'foo')
167
self.check_file_contents('goodbye', 'qux')
168
self.runbzr('revert')
169
self.check_file_contents('goodbye', 'baz')
170
os.mkdir('revertdir')
171
self.runbzr('add revertdir')
172
self.runbzr('commit -m f')
173
os.rmdir('revertdir')
174
self.runbzr('revert')
175
os.symlink('/unlikely/to/exist', 'symlink')
176
self.runbzr('add symlink')
177
self.runbzr('commit -m f')
179
self.runbzr('revert')
217
self.example_branch()
218
file('hello', 'wt').write('hello world!')
219
self.runbzr('commit -m fixing hello')
220
output = self.runbzr('diff -r 2..3', backtick=1)
221
self.assert_('\n+hello world!' in output)
222
output = self.runbzr('diff -r last:3..last:1', backtick=1)
223
self.assert_('\n+baz' in output)
225
def test_branch(self):
226
"""Branch from one branch to another."""
229
self.example_branch()
231
self.runbzr('branch a b')
232
self.runbzr('branch a c -r 1')
181
234
def test_merge(self):
182
235
from bzrlib.branch import Branch
229
293
self.runbzr('merge ../b')
230
294
self.runbzr('commit -m blah4 --unchanged')
232
self.runbzr('pull ../a')
295
os.chdir('../b/subdir')
296
self.runbzr('pull ../../a')
233
297
assert a.revision_history()[-1] == b.revision_history()[-1]
235
299
def test_add_reports(self):
236
300
"""add command prints the names of added files."""
237
b = Branch('.', init=True)
301
b = Branch.initialize('.')
238
302
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
240
from cStringIO import StringIO
243
ret = self.apply_redirected(None, out, None,
246
self.assertEquals(ret, 0)
303
out = self.run_bzr_captured(['add'], retcode = 0)[0]
248
304
# the ordering is not defined at the moment
249
results = sorted(out.getvalue().rstrip('\n').split('\n'))
305
results = sorted(out.rstrip('\n').split('\n'))
250
306
self.assertEquals(['added dir',
307
'added dir'+os.sep+'sub.txt',
252
308
'added top.txt',],
311
def test_unknown_command(self):
312
"""Handling of unknown command."""
313
out, err = self.run_bzr_captured(['fluffy-badger'],
315
self.assertEquals(out, '')
316
err.index('unknown command')
256
320
def has_symlinks():
257
321
if hasattr(os, 'symlink'):
290
354
f.write('hello world!\n')
293
out = backtick("bzr unknowns")
294
self.assertEquals(out, 'test.txt\n')
357
self.assertEquals(capture('unknowns'), 'test.txt\n')
296
out = backtick("bzr status")
359
out = capture("status")
297
360
assert out == 'unknown:\n test.txt\n'
299
out = backtick("bzr status --all")
362
out = capture("status --all")
300
363
assert out == "unknown:\n test.txt\n"
302
out = backtick("bzr status test.txt --all")
365
out = capture("status test.txt --all")
303
366
assert out == "unknown:\n test.txt\n"
305
368
f = file('test2.txt', 'wt')
306
369
f.write('goodbye cruel world...\n')
309
out = backtick("bzr status test.txt")
372
out = capture("status test.txt")
310
373
assert out == "unknown:\n test.txt\n"
312
out = backtick("bzr status")
375
out = capture("status")
313
376
assert out == ("unknown:\n"
384
447
runbzr(['commit', '-m', 'rename nested subdirectories'])
386
449
chdir('sub1/sub2')
387
self.assertEquals(backtick('bzr root')[:-1],
450
self.assertEquals(capture('root')[:-1],
388
451
os.path.join(self.test_dir, 'branch1'))
389
452
runbzr('move ../hello.txt .')
390
453
assert exists('./hello.txt')
391
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
392
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
454
self.assertEquals(capture('relpath hello.txt'),
455
os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
456
assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
393
457
runbzr(['commit', '-m', 'move to parent directory'])
395
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
459
assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
397
461
runbzr('move sub2/hello.txt .')
398
462
assert exists('hello.txt')
443
507
os.symlink("NOWHERE1", "link1")
444
508
runbzr('add link1')
445
assert backtick('bzr unknowns') == ''
509
assert self.capture('unknowns') == ''
446
510
runbzr(['commit', '-m', '1: added symlink link1'])
450
assert backtick('bzr unknowns') == ''
514
assert self.capture('unknowns') == ''
451
515
os.symlink("NOWHERE2", "d1/link2")
452
assert backtick('bzr unknowns') == 'd1/link2\n'
516
assert self.capture('unknowns') == 'd1/link2\n'
453
517
# is d1/link2 found when adding d1
455
assert backtick('bzr unknowns') == ''
519
assert self.capture('unknowns') == ''
456
520
os.symlink("NOWHERE3", "d1/link3")
457
assert backtick('bzr unknowns') == 'd1/link3\n'
521
assert self.capture('unknowns') == 'd1/link3\n'
458
522
runbzr(['commit', '-m', '2: added dir, symlink'])
460
524
runbzr('rename d1 d2')