1
# Copyright (C) 2005 by Canonical Ltd
2
# -*- coding: utf-8 -*-
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Black-box tests for bzr.
21
These check that it behaves properly when it's invoked through the regular
22
command-line interface.
24
This always reinvokes bzr through a new Python interpreter, which is a
25
bit inefficient but arguably tests in a way more representative of how
26
it's normally invoked.
30
from bzrlib.selftest import InTempDir, BzrTestBase
33
class ExternalBase(InTempDir):
35
def runbzr(self, args, retcode=0,backtick=False):
38
from subprocess import call
39
except ImportError, e:
43
if isinstance(args, basestring):
47
return self.backtick(['python', self.BZRPATH,] + args,
50
return self.runcmd(['python', self.BZRPATH,] + args,
53
class TestCommands(ExternalBase):
55
def test_help_commands(self):
58
self.runbzr('help commands')
59
self.runbzr('help help')
60
self.runbzr('commit -h')
62
def test_init_branch(self):
66
def test_whoami(self):
67
# this should always identify something, if only "john@localhost"
69
self.runbzr("whoami --email")
71
self.assertEquals(self.runbzr("whoami --email",
72
backtick=True).count('@'), 1)
74
def test_whoami_branch(self):
75
"""branch specific user identity works."""
77
f = file('.bzr/email', 'wt')
78
f.write('Branch Identity <branch@identi.ty>')
80
whoami = self.runbzr("whoami",backtick=True)
81
whoami_email = self.runbzr("whoami --email",backtick=True)
82
self.assertTrue(whoami.startswith('Branch Identity <branch@identi.ty>'))
83
self.assertTrue(whoami_email.startswith('branch@identi.ty'))
85
def test_invalid_commands(self):
86
self.runbzr("pants", retcode=1)
87
self.runbzr("--pants off", retcode=1)
88
self.runbzr("diff --message foo", retcode=1)
90
def test_empty_commit(self):
92
self.build_tree(['hello.txt'])
93
self.runbzr("commit -m empty", retcode=1)
94
self.runbzr("add hello.txt")
95
self.runbzr("commit -m added")
97
def test_ignore_patterns(self):
98
from bzrlib.branch import Branch
100
b = Branch('.', init=True)
101
self.assertEquals(list(b.unknowns()), [])
103
file('foo.tmp', 'wt').write('tmp files are ignored')
104
self.assertEquals(list(b.unknowns()), [])
105
assert self.backtick('bzr unknowns') == ''
107
file('foo.c', 'wt').write('int main() {}')
108
self.assertEquals(list(b.unknowns()), ['foo.c'])
109
assert self.backtick('bzr unknowns') == 'foo.c\n'
111
self.runbzr(['add', 'foo.c'])
112
assert self.backtick('bzr unknowns') == ''
114
# 'ignore' works when creating the .bzignore file
115
file('foo.blah', 'wt').write('blah')
116
self.assertEquals(list(b.unknowns()), ['foo.blah'])
117
self.runbzr('ignore *.blah')
118
self.assertEquals(list(b.unknowns()), [])
119
assert file('.bzrignore', 'rb').read() == '*.blah\n'
121
# 'ignore' works when then .bzrignore file already exists
122
file('garh', 'wt').write('garh')
123
self.assertEquals(list(b.unknowns()), ['garh'])
124
assert self.backtick('bzr unknowns') == 'garh\n'
125
self.runbzr('ignore garh')
126
self.assertEquals(list(b.unknowns()), [])
127
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
129
def test_revert(self):
132
file('hello', 'wt').write('foo')
133
self.runbzr('add hello')
134
self.runbzr('commit -m setup hello')
136
file('goodbye', 'wt').write('baz')
137
self.runbzr('add goodbye')
138
self.runbzr('commit -m setup goodbye')
140
file('hello', 'wt').write('bar')
141
file('goodbye', 'wt').write('qux')
142
self.runbzr('revert hello')
143
self.check_file_contents('hello', 'foo')
144
self.check_file_contents('goodbye', 'qux')
145
self.runbzr('revert')
146
self.check_file_contents('goodbye', 'baz')
148
def skipped_test_mv_modes(self):
149
"""Test two modes of operation for mv"""
150
from bzrlib.branch import Branch
151
b = Branch('.', init=True)
152
self.build_tree(['a', 'c', 'subdir/'])
153
self.run_bzr('mv', 'a', 'b')
154
self.run_bzr('mv', 'b', 'subdir')
155
self.run_bzr('mv', 'subdir/b', 'a')
156
self.run_bzr('mv', 'a', 'b', 'subdir')
157
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
159
def test_main_version(self):
160
"""Check output from version command and master option is reasonable"""
161
# output is intentionally passed through to stdout so that we
162
# can see the version being tested
163
output = self.runbzr('version', backtick=1)
164
self.log('bzr version output:')
166
self.assert_(output.startswith('bzr (bazaar-ng) '))
167
self.assertNotEqual(output.index('Canonical'), -1)
168
# make sure --version is consistent
169
tmp_output = self.runbzr('--version', backtick=1)
170
self.log('bzr --version output:')
172
self.assertEquals(output, tmp_output)
174
class OldTests(ExternalBase):
175
# old tests moved from ./testbzr
177
from os import chdir, mkdir
178
from os.path import exists
182
backtick = self.backtick
185
progress("basic branch creation")
190
self.assertEquals(backtick('bzr root').rstrip(),
191
os.path.join(self.test_dir, 'branch1'))
193
progress("status of new file")
195
f = file('test.txt', 'wt')
196
f.write('hello world!\n')
199
out = backtick("bzr unknowns")
200
self.assertEquals(out, 'test.txt\n')
202
out = backtick("bzr status")
203
assert out == 'unknown:\n test.txt\n'
205
out = backtick("bzr status --all")
206
assert out == "unknown:\n test.txt\n"
208
out = backtick("bzr status test.txt --all")
209
assert out == "unknown:\n test.txt\n"
211
f = file('test2.txt', 'wt')
212
f.write('goodbye cruel world...\n')
215
out = backtick("bzr status test.txt")
216
assert out == "unknown:\n test.txt\n"
218
out = backtick("bzr status")
219
assert out == ("unknown:\n"
223
os.unlink('test2.txt')
225
progress("command aliases")
226
out = backtick("bzr st --all")
227
assert out == ("unknown:\n"
230
out = backtick("bzr stat")
231
assert out == ("unknown:\n"
234
progress("command help")
237
runbzr("help commands")
238
runbzr("help slartibartfast", 1)
240
out = backtick("bzr help ci")
241
out.index('aliases: ')
243
progress("can't rename unversioned file")
244
runbzr("rename test.txt new-test.txt", 1)
246
progress("adding a file")
248
runbzr("add test.txt")
249
assert backtick("bzr unknowns") == ''
250
assert backtick("bzr status --all") == ("added:\n"
253
progress("rename newly-added file")
254
runbzr("rename test.txt hello.txt")
255
assert os.path.exists("hello.txt")
256
assert not os.path.exists("test.txt")
258
assert backtick("bzr revno") == '0\n'
260
progress("add first revision")
261
runbzr(['commit', '-m', 'add first revision'])
263
progress("more complex renames")
265
runbzr("rename hello.txt sub1", 1)
266
runbzr("rename hello.txt sub1/hello.txt", 1)
267
runbzr("move hello.txt sub1", 1)
270
runbzr("rename sub1 sub2")
271
runbzr("move hello.txt sub2")
272
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
274
assert exists("sub2")
275
assert exists("sub2/hello.txt")
276
assert not exists("sub1")
277
assert not exists("hello.txt")
279
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
283
runbzr('move sub2/hello.txt sub1')
284
assert not exists('sub2/hello.txt')
285
assert exists('sub1/hello.txt')
286
runbzr('move sub2 sub1')
287
assert not exists('sub2')
288
assert exists('sub1/sub2')
290
runbzr(['commit', '-m', 'rename nested subdirectories'])
293
self.assertEquals(backtick('bzr root')[:-1],
294
os.path.join(self.test_dir, 'branch1'))
295
runbzr('move ../hello.txt .')
296
assert exists('./hello.txt')
297
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
298
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
299
runbzr(['commit', '-m', 'move to parent directory'])
301
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
303
runbzr('move sub2/hello.txt .')
304
assert exists('hello.txt')
306
f = file('hello.txt', 'wt')
307
f.write('some nice new content\n')
310
f = file('msg.tmp', 'wt')
311
f.write('this is my new commit\n')
314
runbzr('commit -F msg.tmp')
316
assert backtick('bzr revno') == '5\n'
317
runbzr('export -r 5 export-5.tmp')
318
runbzr('export export.tmp')
322
runbzr('log -v --forward')
323
runbzr('log -m', retcode=1)
324
log_out = backtick('bzr log -m commit')
325
assert "this is my new commit" in log_out
326
assert "rename nested" not in log_out
327
assert 'revision-id' not in log_out
328
assert 'revision-id' in backtick('bzr log --show-ids -m commit')
331
progress("file with spaces in name")
332
mkdir('sub directory')
333
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
336
runbzr('commit -m add-spaces')
340
runbzr('log --forward')