31
from bzrlib.selftest import TestBase, InTempDir, BzrTestBase
35
class ExternalBase(InTempDir):
36
def runbzr(self, args, retcode=0):
39
from subprocess import call
40
except ImportError, e:
32
from bzrlib.selftest import TestCaseInTempDir, BzrTestBase
33
from bzrlib.branch import Branch
34
from bzrlib.commands import run_bzr
37
class ExternalBase(TestCaseInTempDir):
38
def runbzr(self, args, retcode=0, backtick=False):
44
39
if isinstance(args, basestring):
45
40
args = args.split()
47
return self.runcmd(['python', self.BZRPATH,] + args,
52
class MvCommand(BzrTestBase):
54
"""Test two modes of operation for mv"""
55
b = Branch('.', init=True)
56
self.build_tree(['a', 'c', 'subdir/'])
57
self.run_bzr('mv', 'a', 'b')
58
self.run_bzr('mv', 'b', 'subdir')
59
self.run_bzr('mv', 'subdir/b', 'a')
60
self.run_bzr('mv', 'a', 'b', 'subdir')
61
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
65
class TestVersion(BzrTestBase):
66
"""Check output from version command and master option is reasonable"""
68
# output is intentionally passed through to stdout so that we
69
# can see the version being tested
70
from cStringIO import StringIO
73
sys.stdout = tmp_out = StringIO()
75
self.run_bzr('version')
79
output = tmp_out.getvalue()
80
self.log('bzr version output:')
83
self.assert_(output.startswith('bzr (bazaar-ng) '))
84
self.assertNotEqual(output.index('Canonical'), -1)
86
# make sure --version is consistent
88
sys.stdout = tmp_out = StringIO()
90
self.run_bzr('--version')
94
self.log('bzr --version output:')
95
self.log(tmp_out.getvalue())
97
self.assertEquals(output, tmp_out.getvalue())
103
class HelpCommands(ExternalBase):
43
return self.run_bzr_captured(args, retcode=retcode)[0]
45
return self.run_bzr_captured(args, retcode=retcode)
48
class TestCommands(ExternalBase):
50
def test_help_commands(self):
105
51
self.runbzr('--help')
106
52
self.runbzr('help')
107
53
self.runbzr('help commands')
108
54
self.runbzr('help help')
109
55
self.runbzr('commit -h')
112
class InitBranch(ExternalBase):
57
def test_init_branch(self):
115
58
self.runbzr(['init'])
119
class UserIdentity(ExternalBase):
60
def test_whoami(self):
121
61
# this should always identify something, if only "john@localhost"
122
62
self.runbzr("whoami")
123
63
self.runbzr("whoami --email")
124
self.assertEquals(self.backtick("bzr whoami --email").count('@'),
128
class InvalidCommands(ExternalBase):
65
self.assertEquals(self.runbzr("whoami --email",
66
backtick=True).count('@'), 1)
68
def test_whoami_branch(self):
69
"""branch specific user identity works."""
71
f = file('.bzr/email', 'wt')
72
f.write('Branch Identity <branch@identi.ty>')
74
whoami = self.runbzr("whoami",backtick=True)
75
whoami_email = self.runbzr("whoami --email",backtick=True)
76
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
77
self.assertTrue(whoami_email.startswith('branch@identi.ty'))
79
def test_invalid_commands(self):
130
80
self.runbzr("pants", retcode=1)
131
81
self.runbzr("--pants off", retcode=1)
132
82
self.runbzr("diff --message foo", retcode=1)
136
class EmptyCommit(ExternalBase):
84
def test_empty_commit(self):
138
85
self.runbzr("init")
139
86
self.build_tree(['hello.txt'])
140
87
self.runbzr("commit -m empty", retcode=1)
141
88
self.runbzr("add hello.txt")
142
89
self.runbzr("commit -m added")
146
class IgnorePatterns(ExternalBase):
91
def test_ignore_patterns(self):
148
92
from bzrlib.branch import Branch
150
b = Branch('.', init=True)
94
b = Branch.initialize('.')
151
95
self.assertEquals(list(b.unknowns()), [])
153
97
file('foo.tmp', 'wt').write('tmp files are ignored')
154
98
self.assertEquals(list(b.unknowns()), [])
155
assert self.backtick('bzr unknowns') == ''
99
assert self.capture('unknowns') == ''
157
101
file('foo.c', 'wt').write('int main() {}')
158
102
self.assertEquals(list(b.unknowns()), ['foo.c'])
159
assert self.backtick('bzr unknowns') == 'foo.c\n'
103
assert self.capture('unknowns') == 'foo.c\n'
161
105
self.runbzr(['add', 'foo.c'])
162
assert self.backtick('bzr unknowns') == ''
106
assert self.capture('unknowns') == ''
164
108
# 'ignore' works when creating the .bzignore file
165
109
file('foo.blah', 'wt').write('blah')
171
115
# 'ignore' works when then .bzrignore file already exists
172
116
file('garh', 'wt').write('garh')
173
117
self.assertEquals(list(b.unknowns()), ['garh'])
174
assert self.backtick('bzr unknowns') == 'garh\n'
118
assert self.capture('unknowns') == 'garh\n'
175
119
self.runbzr('ignore garh')
176
120
self.assertEquals(list(b.unknowns()), [])
177
121
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
123
def test_revert(self):
126
file('hello', 'wt').write('foo')
127
self.runbzr('add hello')
128
self.runbzr('commit -m setup hello')
130
file('goodbye', 'wt').write('baz')
131
self.runbzr('add goodbye')
132
self.runbzr('commit -m setup goodbye')
134
file('hello', 'wt').write('bar')
135
file('goodbye', 'wt').write('qux')
136
self.runbzr('revert hello')
137
self.check_file_contents('hello', 'foo')
138
self.check_file_contents('goodbye', 'qux')
139
self.runbzr('revert')
140
self.check_file_contents('goodbye', 'baz')
142
os.mkdir('revertdir')
143
self.runbzr('add revertdir')
144
self.runbzr('commit -m f')
145
os.rmdir('revertdir')
146
self.runbzr('revert')
148
def test_mv_modes(self):
149
"""Test two modes of operation for mv"""
150
from bzrlib.branch import Branch
151
b = Branch.initialize('.')
152
self.build_tree(['a', 'c', 'subdir/'])
153
self.run_bzr('add', self.test_dir)
154
self.run_bzr('mv', 'a', 'b')
155
self.run_bzr('mv', 'b', 'subdir')
156
self.run_bzr('mv', 'subdir/b', 'a')
157
self.run_bzr('mv', 'a', 'c', 'subdir')
158
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
161
def test_main_version(self):
162
"""Check output from version command and master option is reasonable"""
163
# output is intentionally passed through to stdout so that we
164
# can see the version being tested
165
output = self.runbzr('version', backtick=1)
166
self.log('bzr version output:')
168
self.assert_(output.startswith('bzr (bazaar-ng) '))
169
self.assertNotEqual(output.index('Canonical'), -1)
170
# make sure --version is consistent
171
tmp_output = self.runbzr('--version', backtick=1)
172
self.log('bzr --version output:')
174
self.assertEquals(output, tmp_output)
176
def example_branch(test):
178
file('hello', 'wt').write('foo')
179
test.runbzr('add hello')
180
test.runbzr('commit -m setup hello')
181
file('goodbye', 'wt').write('baz')
182
test.runbzr('add goodbye')
183
test.runbzr('commit -m setup goodbye')
185
def test_revert(self):
186
self.example_branch()
187
file('hello', 'wt').write('bar')
188
file('goodbye', 'wt').write('qux')
189
self.runbzr('revert hello')
190
self.check_file_contents('hello', 'foo')
191
self.check_file_contents('goodbye', 'qux')
192
self.runbzr('revert')
193
self.check_file_contents('goodbye', 'baz')
195
def test_merge(self):
196
from bzrlib.branch import Branch
200
self.example_branch()
202
self.runbzr('branch a b')
204
file('goodbye', 'wt').write('quux')
205
self.runbzr(['commit', '-m', "more u's are always good"])
208
file('hello', 'wt').write('quuux')
209
# We can't merge when there are in-tree changes
210
self.runbzr('merge ../b', retcode=1)
211
self.runbzr(['commit', '-m', "Like an epidemic of u's"])
212
self.runbzr('merge ../b')
213
self.check_file_contents('goodbye', 'quux')
214
# Merging a branch pulls its revision into the tree
216
b = Branch.open('../b')
217
a.get_revision_xml(b.last_patch())
218
self.log('pending merges: %s', a.pending_merges())
219
# assert a.pending_merges() == [b.last_patch()], "Assertion %s %s" \
220
# % (a.pending_merges(), b.last_patch())
223
"""Pull changes from one branch to another."""
227
self.example_branch()
228
self.runbzr('pull', retcode=1)
229
self.runbzr('missing', retcode=1)
230
self.runbzr('missing .')
231
self.runbzr('missing')
233
self.runbzr('pull /', retcode=1)
237
self.runbzr('branch a b')
240
self.runbzr('commit -m blah --unchanged')
243
b = Branch.open('../b')
244
assert a.revision_history() == b.revision_history()[:-1]
245
self.runbzr('pull ../b')
246
assert a.revision_history() == b.revision_history()
247
self.runbzr('commit -m blah2 --unchanged')
249
self.runbzr('commit -m blah3 --unchanged')
250
self.runbzr('pull ../a', retcode=1)
252
self.runbzr('merge ../b')
253
self.runbzr('commit -m blah4 --unchanged')
255
self.runbzr('pull ../a')
256
assert a.revision_history()[-1] == b.revision_history()[-1]
259
def test_add_reports(self):
260
"""add command prints the names of added files."""
261
b = Branch.initialize('.')
262
self.build_tree(['top.txt', 'dir/', 'dir/sub.txt'])
264
from cStringIO import StringIO
267
ret = self.apply_redirected(None, out, None,
270
self.assertEquals(ret, 0)
272
# the ordering is not defined at the moment
273
results = sorted(out.getvalue().rstrip('\n').split('\n'))
274
self.assertEquals(['added dir',
279
def test_unknown_command(self):
280
"""Handling of unknown command."""
281
out, err = self.run_bzr_captured(['fluffy-badger'],
283
self.assertEquals(out, '')
284
err.index('unknown command')
182
288
class OldTests(ExternalBase):
183
# old tests moved from ./testbzr
289
"""old tests moved from ./testbzr."""
185
292
from os import chdir, mkdir
186
293
from os.path import exists
189
295
runbzr = self.runbzr
190
backtick = self.backtick
296
capture = self.capture
191
297
progress = self.log
193
299
progress("basic branch creation")
298
403
runbzr(['commit', '-m', 'rename nested subdirectories'])
300
405
chdir('sub1/sub2')
301
self.assertEquals(backtick('bzr root')[:-1],
406
self.assertEquals(capture('root')[:-1],
302
407
os.path.join(self.test_dir, 'branch1'))
303
408
runbzr('move ../hello.txt .')
304
409
assert exists('./hello.txt')
305
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
306
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
410
self.assertEquals(capture('relpath hello.txt'),
411
os.path.join('sub1', 'sub2', 'hello.txt') + '\n')
412
assert capture('relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
307
413
runbzr(['commit', '-m', 'move to parent directory'])
309
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
415
assert capture('relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
311
417
runbzr('move sub2/hello.txt .')
312
418
assert exists('hello.txt')