26
26
it's normally invoked.
29
from cStringIO import StringIO
30
33
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
34
from bzrlib.branch import Branch
32
37
class ExternalBase(TestCaseInTempDir):
34
def runbzr(self, args, retcode=0,backtick=False):
37
from subprocess import call
38
except ImportError, e:
39
def runbzr(self, args, retcode=0, backtick=False):
42
40
if isinstance(args, basestring):
43
41
args = args.split()
46
return self.backtick(['python', self.BZRPATH,] + args,
44
return self.run_bzr_captured(args, retcode=retcode)[0]
49
return self.runcmd(['python', self.BZRPATH,] + args,
46
return self.run_bzr_captured(args, retcode=retcode)
52
49
class TestCommands(ExternalBase):
76
72
f = file('.bzr/email', 'wt')
77
73
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']
79
78
whoami = self.runbzr("whoami",backtick=True)
80
79
whoami_email = self.runbzr("whoami --email",backtick=True)
81
80
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
82
81
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
84
92
def test_invalid_commands(self):
85
93
self.runbzr("pants", retcode=1)
96
104
def test_ignore_patterns(self):
97
105
from bzrlib.branch import Branch
99
b = Branch('.', init=True)
107
b = Branch.initialize('.')
100
108
self.assertEquals(list(b.unknowns()), [])
102
110
file('foo.tmp', 'wt').write('tmp files are ignored')
103
111
self.assertEquals(list(b.unknowns()), [])
104
assert self.backtick('bzr unknowns') == ''
112
assert self.capture('unknowns') == ''
106
114
file('foo.c', 'wt').write('int main() {}')
107
115
self.assertEquals(list(b.unknowns()), ['foo.c'])
108
assert self.backtick('bzr unknowns') == 'foo.c\n'
116
assert self.capture('unknowns') == 'foo.c\n'
110
118
self.runbzr(['add', 'foo.c'])
111
assert self.backtick('bzr unknowns') == ''
119
assert self.capture('unknowns') == ''
113
121
# 'ignore' works when creating the .bzignore file
114
122
file('foo.blah', 'wt').write('blah')
120
128
# 'ignore' works when then .bzrignore file already exists
121
129
file('garh', 'wt').write('garh')
122
130
self.assertEquals(list(b.unknowns()), ['garh'])
123
assert self.backtick('bzr unknowns') == 'garh\n'
131
assert self.capture('unknowns') == 'garh\n'
124
132
self.runbzr('ignore garh')
125
133
self.assertEquals(list(b.unknowns()), [])
126
134
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
128
136
def test_revert(self):
130
137
self.runbzr('init')
132
139
file('hello', 'wt').write('foo')
151
158
os.rmdir('revertdir')
152
159
self.runbzr('revert')
154
def skipped_test_mv_modes(self):
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
def test_mv_modes(self):
155
169
"""Test two modes of operation for mv"""
156
170
from bzrlib.branch import Branch
157
b = Branch('.', init=True)
171
b = Branch.initialize('.')
158
172
self.build_tree(['a', 'c', 'subdir/'])
159
self.run_bzr('mv', 'a', 'b')
160
self.run_bzr('mv', 'b', 'subdir')
161
self.run_bzr('mv', 'subdir/b', 'a')
162
self.run_bzr('mv', 'a', 'b', 'subdir')
163
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
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'])
165
181
def test_main_version(self):
166
182
"""Check output from version command and master option is reasonable"""
186
202
test.runbzr('add goodbye')
187
203
test.runbzr('commit -m setup goodbye')
189
def test_revert(self):
190
self.example_branch()
191
file('hello', 'wt').write('bar')
192
file('goodbye', 'wt').write('qux')
193
self.runbzr('revert hello')
194
self.check_file_contents('hello', 'foo')
195
self.check_file_contents('goodbye', 'qux')
196
self.runbzr('revert')
197
self.check_file_contents('goodbye', 'baz')
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)
199
223
def test_merge(self):
200
224
from bzrlib.branch import Branch
201
from bzrlib.commands import run_bzr
207
228
self.example_branch()
209
230
self.runbzr('branch a b')
219
240
self.runbzr('merge ../b')
220
241
self.check_file_contents('goodbye', 'quux')
221
242
# Merging a branch pulls its revision into the tree
244
b = Branch.open('../b')
224
245
a.get_revision_xml(b.last_patch())
226
246
self.log('pending merges: %s', a.pending_merges())
227
# assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
228
# % (a.pending_merges(), b.last_patch())
247
# assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
248
# % (a.pending_merges(), b.last_patch())
251
"""Pull changes from one branch to another."""
255
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
self.runbzr('branch a b')
268
self.runbzr('commit -m blah --unchanged')
271
b = Branch.open('../b')
272
assert a.revision_history() == b.revision_history()[:-1]
273
self.runbzr('pull ../b')
274
assert a.revision_history() == b.revision_history()
275
self.runbzr('commit -m blah2 --unchanged')
277
self.runbzr('commit -m blah3 --unchanged')
278
self.runbzr('pull ../a', retcode=1)
280
self.runbzr('merge ../b')
281
self.runbzr('commit -m blah4 --unchanged')
283
self.runbzr('pull ../a')
284
assert a.revision_history()[-1] == b.revision_history()[-1]
286
def test_add_reports(self):
287
"""add command prints the names of added files."""
288
b = Branch.initialize('.')
289
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
290
out = self.run_bzr_captured(['add'], retcode = 0)[0]
291
# the ordering is not defined at the moment
292
results = sorted(out.rstrip('\n').split('\n'))
293
self.assertEquals(['added dir',
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')
230
307
class OldTests(ExternalBase):
231
308
"""old tests moved from ./testbzr."""
253
329
f.write('hello world!\n')
256
out = backtick("bzr unknowns")
257
self.assertEquals(out, 'test.txt\n')
332
self.assertEquals(capture('unknowns'), 'test.txt\n')
259
out = backtick("bzr status")
334
out = capture("status")
260
335
assert out == 'unknown:\n test.txt\n'
262
out = backtick("bzr status --all")
337
out = capture("status --all")
263
338
assert out == "unknown:\n test.txt\n"
265
out = backtick("bzr status test.txt --all")
340
out = capture("status test.txt --all")
266
341
assert out == "unknown:\n test.txt\n"
268
343
f = file('test2.txt', 'wt')
269
344
f.write('goodbye cruel world...\n')
272
out = backtick("bzr status test.txt")
347
out = capture("status test.txt")
273
348
assert out == "unknown:\n test.txt\n"
275
out = backtick("bzr status")
350
out = capture("status")
276
351
assert out == ("unknown:\n"
303
378
progress("adding a file")
305
380
runbzr("add test.txt")
306
assert backtick("bzr unknowns") == ''
307
assert backtick("bzr status --all") == ("added:\n"
381
assert capture("unknowns") == ''
382
assert capture("status --all") == ("added:\n"
310
385
progress("rename newly-added file")
312
387
assert os.path.exists("hello.txt")
313
388
assert not os.path.exists("test.txt")
315
assert backtick("bzr revno") == '0\n'
390
assert capture("revno") == '0\n'
317
392
progress("add first revision")
318
393
runbzr(['commit', '-m', 'add first revision'])
326
401
runbzr("add sub1")
327
402
runbzr("rename sub1 sub2")
328
403
runbzr("move hello.txt sub2")
329
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
404
assert capture("relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
331
406
assert exists("sub2")
332
407
assert exists("sub2/hello.txt")
347
422
runbzr(['commit', '-m', 'rename nested subdirectories'])
349
424
chdir('sub1/sub2')
350
self.assertEquals(backtick('bzr root')[:-1],
425
self.assertEquals(capture('root')[:-1],
351
426
os.path.join(self.test_dir, 'branch1'))
352
427
runbzr('move ../hello.txt .')
353
428
assert exists('./hello.txt')
354
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
355
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
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')
356
432
runbzr(['commit', '-m', 'move to parent directory'])
358
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
434
assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
360
436
runbzr('move sub2/hello.txt .')
361
437
assert exists('hello.txt')
379
455
runbzr('log -v --forward')
380
456
runbzr('log -m', retcode=1)
381
log_out = backtick('bzr log -m commit')
457
log_out = capture('log -m commit')
382
458
assert "this is my new commit" in log_out
383
459
assert "rename nested" not in log_out
384
460
assert 'revision-id' not in log_out
385
assert 'revision-id' in backtick('bzr log --show-ids -m commit')
461
assert 'revision-id' in capture('log --show-ids -m commit')
388
464
progress("file with spaces in name")