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.
29
# this code was previously in testbzr
31
from unittest import TestCase
32
from bzrlib.selftest import TestBase, InTempDir
36
class ExternalBase(TestBase):
37
def runbzr(self, args):
40
from subprocess import call
41
except ImportError, e:
45
if isinstance(args, basestring):
48
return self.runcmd(['python', self.BZRPATH,] + args)
52
class TestVersion(TestBase):
54
# output is intentionally passed through to stdout so that we
55
# can see the version being tested
56
self.runbzr(['version'])
60
class HelpCommands(TestBase):
64
self.runbzr('help commands')
65
self.runbzr('help help')
66
self.runbzr('commit -h')
69
class InitBranch(InTempDir):
76
class UserIdentity(InTempDir):
78
# this should always identify something, if only "john@localhost"
80
self.runbzr("whoami --email")
81
self.assertEquals(self.backtick("bzr whoami --email").count('@'),
85
class InvalidCommands(InTempDir):
87
self.runbzr("pants", retcode=1)
88
self.runbzr("--pants off", retcode=1)
89
self.runbzr("diff --message foo", retcode=1)
93
class EmptyCommit(InTempDir):
96
self.build_tree(['hello.txt'])
97
self.runbzr("commit -m empty", retcode=1)
98
self.runbzr("add hello.txt")
99
self.runbzr("commit -m added")
103
class OldTests(InTempDir):
104
# old tests moved from ./testbzr
106
from os import chdir, mkdir
107
from os.path import exists
111
backtick = self.backtick
114
progress("basic branch creation")
115
runcmd(['mkdir', 'branch1'])
119
self.assertEquals(backtick('bzr root').rstrip(),
120
os.path.join(self.test_dir, 'branch1'))
122
progress("status of new file")
124
f = file('test.txt', 'wt')
125
f.write('hello world!\n')
128
out = backtick("bzr unknowns")
129
self.assertEquals(out, 'test.txt\n')
131
out = backtick("bzr status")
132
assert out == 'unknown:\n test.txt\n'
134
out = backtick("bzr status --all")
135
assert out == "unknown:\n test.txt\n"
137
out = backtick("bzr status test.txt --all")
138
assert out == "unknown:\n test.txt\n"
140
f = file('test2.txt', 'wt')
141
f.write('goodbye cruel world...\n')
144
out = backtick("bzr status test.txt")
145
assert out == "unknown:\n test.txt\n"
147
out = backtick("bzr status")
148
assert out == ("unknown:\n"
152
os.unlink('test2.txt')
154
progress("command aliases")
155
out = backtick("bzr st --all")
156
assert out == ("unknown:\n"
159
out = backtick("bzr stat")
160
assert out == ("unknown:\n"
163
progress("command help")
166
runbzr("help commands")
167
runbzr("help slartibartfast", 1)
169
out = backtick("bzr help ci")
170
out.index('aliases: ')
172
progress("can't rename unversioned file")
173
runbzr("rename test.txt new-test.txt", 1)
175
progress("adding a file")
177
runbzr("add test.txt")
178
assert backtick("bzr unknowns") == ''
179
assert backtick("bzr status --all") == ("added:\n"
182
progress("rename newly-added file")
183
runbzr("rename test.txt hello.txt")
184
assert os.path.exists("hello.txt")
185
assert not os.path.exists("test.txt")
187
assert backtick("bzr revno") == '0\n'
189
progress("add first revision")
190
runcmd(["bzr", "commit", "-m", 'add first revision'])
192
progress("more complex renames")
194
runbzr("rename hello.txt sub1", 1)
195
runbzr("rename hello.txt sub1/hello.txt", 1)
196
runbzr("move hello.txt sub1", 1)
199
runbzr("rename sub1 sub2")
200
runbzr("move hello.txt sub2")
201
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
203
assert exists("sub2")
204
assert exists("sub2/hello.txt")
205
assert not exists("sub1")
206
assert not exists("hello.txt")
208
runbzr(['commit', '-m', 'commit with some things moved to subdirs'])
212
runbzr('move sub2/hello.txt sub1')
213
assert not exists('sub2/hello.txt')
214
assert exists('sub1/hello.txt')
215
runbzr('move sub2 sub1')
216
assert not exists('sub2')
217
assert exists('sub1/sub2')
219
runbzr(['commit', '-m', 'rename nested subdirectories'])
222
self.assertEquals(backtick('bzr root')[:-1],
223
os.path.join(self.test_dir, 'branch1'))
224
runbzr('move ../hello.txt .')
225
assert exists('./hello.txt')
226
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
227
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
228
runbzr(['commit', '-m', 'move to parent directory'])
230
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
232
runbzr('move sub2/hello.txt .')
233
assert exists('hello.txt')
235
f = file('hello.txt', 'wt')
236
f.write('some nice new content\n')
239
f = file('msg.tmp', 'wt')
240
f.write('this is my new commit\n')
243
runbzr('commit -F msg.tmp')
245
assert backtick('bzr revno') == '5\n'
246
runbzr('export -r 5 export-5.tmp')
247
runbzr('export export.tmp')
254
progress("file with spaces in name")
255
mkdir('sub directory')
256
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
259
runbzr('commit -m add-spaces')
263
runbzr('log --forward')
275
# Can't create a branch if it already exists
276
runbzr('branch branch1', retcode=1)
277
# Can't create a branch if its parent doesn't exist
278
runbzr('branch /unlikely/to/exist', retcode=1)
279
runbzr('branch branch1 branch2')
283
runbzr('pull', retcode=1)
284
runbzr('pull ../branch2')
287
runbzr('commit --unchanged -m empty')
289
chdir('../../branch2')
291
runbzr('commit --unchanged -m empty')
293
runbzr('commit --unchanged -m empty')
294
runbzr('pull', retcode=1)
297
progress('status after remove')
298
mkdir('status-after-remove')
299
# see mail from William Dodé, 2005-05-25
300
# $ bzr init; touch a; bzr add a; bzr commit -m "add a"
301
# * looking for changes...
306
# bzr: local variable 'kind' referenced before assignment
307
# at /vrac/python/bazaar-ng/bzrlib/diff.py:286 in compare_trees()
308
# see ~/.bzr.log for debug information
309
chdir('status-after-remove')
311
file('a', 'w').write('foo')
313
runbzr(['commit', '-m', 'add a'])
319
progress('ignore patterns')
320
mkdir('ignorebranch')
321
chdir('ignorebranch')
323
assert backtick('bzr unknowns') == ''
325
file('foo.tmp', 'wt').write('tmp files are ignored')
326
assert backtick('bzr unknowns') == ''
328
file('foo.c', 'wt').write('int main() {}')
329
assert backtick('bzr unknowns') == 'foo.c\n'
331
assert backtick('bzr unknowns') == ''
333
# 'ignore' works when creating the .bzignore file
334
file('foo.blah', 'wt').write('blah')
335
assert backtick('bzr unknowns') == 'foo.blah\n'
336
runbzr('ignore *.blah')
337
assert backtick('bzr unknowns') == ''
338
assert file('.bzrignore', 'rb').read() == '*.blah\n'
340
# 'ignore' works when then .bzrignore file already exists
341
file('garh', 'wt').write('garh')
342
assert backtick('bzr unknowns') == 'garh\n'
343
runbzr('ignore garh')
344
assert backtick('bzr unknowns') == ''
345
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
352
progress("recursive and non-recursive add")
357
fp = os.path.join('foo', 'test.txt')
361
runbzr('add --no-recurse foo')
362
runbzr('file-id foo')
363
runbzr('file-id ' + fp, 1) # not versioned yet
364
runbzr('commit -m add-dir-only')
366
runbzr('file-id ' + fp, 1) # still not versioned
369
runbzr('file-id ' + fp)
370
runbzr('commit -m add-sub-file')
376
class RevertCommand(InTempDir):
380
file('hello', 'wt').write('foo')
381
self.runbzr('add hello')
382
self.runbzr('commit -m setup hello')
384
file('hello', 'wt').write('bar')
385
self.runbzr('revert hello')
386
self.check_file_contents('hello', 'foo')
392
# lists all tests from this module in the best order to run them. we
393
# do it this way rather than just discovering them all because it
394
# allows us to test more basic functions first where failures will be
395
# easiest to understand.
396
TEST_CLASSES = [TestVersion,