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
34
class TestVersion(TestBase):
36
# output is intentionally passed through to stdout so that we
37
# can see the version being tested
38
self.runcmd(['bzr', 'version'])
42
class HelpCommands(TestBase):
44
self.runcmd('bzr --help')
45
self.runcmd('bzr help')
46
self.runcmd('bzr help commands')
47
self.runcmd('bzr help help')
48
self.runcmd('bzr commit -h')
51
class InitBranch(InTempDir):
54
self.runcmd(['bzr', 'init'])
58
class UserIdentity(InTempDir):
60
# this should always identify something, if only "john@localhost"
61
self.runcmd("bzr whoami")
62
self.runcmd("bzr whoami --email")
63
self.assertEquals(self.backtick("bzr whoami --email").count('@'),
67
class InvalidCommands(InTempDir):
69
self.runcmd("bzr pants", retcode=1)
70
self.runcmd("bzr --pants off", retcode=1)
71
self.runcmd("bzr diff --message foo", retcode=1)
75
class EmptyCommit(InTempDir):
77
self.runcmd("bzr init")
78
self.build_tree(['hello.txt'])
79
self.runcmd("bzr commit -m empty", retcode=1)
80
self.runcmd("bzr add hello.txt")
81
self.runcmd("bzr commit -m added")
85
class OldTests(InTempDir):
86
# old tests moved from ./testbzr
88
from os import chdir, mkdir
89
from os.path import exists
93
backtick = self.backtick
96
progress("basic branch creation")
97
runcmd(['mkdir', 'branch1'])
101
self.assertEquals(backtick('bzr root').rstrip(),
102
os.path.join(self.test_dir, 'branch1'))
104
progress("status of new file")
106
f = file('test.txt', 'wt')
107
f.write('hello world!\n')
110
out = backtick("bzr unknowns")
111
self.assertEquals(out, 'test.txt\n')
113
out = backtick("bzr status")
114
assert out == 'unknown:\n test.txt\n'
116
out = backtick("bzr status --all")
117
assert out == "unknown:\n test.txt\n"
119
out = backtick("bzr status test.txt --all")
120
assert out == "unknown:\n test.txt\n"
122
f = file('test2.txt', 'wt')
123
f.write('goodbye cruel world...\n')
126
out = backtick("bzr status test.txt")
127
assert out == "unknown:\n test.txt\n"
129
out = backtick("bzr status")
130
assert out == ("unknown:\n"
134
os.unlink('test2.txt')
136
progress("command aliases")
137
out = backtick("bzr st --all")
138
assert out == ("unknown:\n"
141
out = backtick("bzr stat")
142
assert out == ("unknown:\n"
145
progress("command help")
146
runcmd("bzr help st")
148
runcmd("bzr help commands")
149
runcmd("bzr help slartibartfast", 1)
151
out = backtick("bzr help ci")
152
out.index('aliases: ')
154
progress("can't rename unversioned file")
155
runcmd("bzr rename test.txt new-test.txt", 1)
157
progress("adding a file")
159
runcmd("bzr add test.txt")
160
assert backtick("bzr unknowns") == ''
161
assert backtick("bzr status --all") == ("added:\n"
164
progress("rename newly-added file")
165
runcmd("bzr rename test.txt hello.txt")
166
assert os.path.exists("hello.txt")
167
assert not os.path.exists("test.txt")
169
assert backtick("bzr revno") == '0\n'
171
progress("add first revision")
172
runcmd(["bzr", "commit", "-m", 'add first revision'])
174
progress("more complex renames")
176
runcmd("bzr rename hello.txt sub1", 1)
177
runcmd("bzr rename hello.txt sub1/hello.txt", 1)
178
runcmd("bzr move hello.txt sub1", 1)
180
runcmd("bzr add sub1")
181
runcmd("bzr rename sub1 sub2")
182
runcmd("bzr move hello.txt sub2")
183
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
185
assert exists("sub2")
186
assert exists("sub2/hello.txt")
187
assert not exists("sub1")
188
assert not exists("hello.txt")
190
runcmd(['bzr', 'commit', '-m', 'commit with some things moved to subdirs'])
193
runcmd('bzr add sub1')
194
runcmd('bzr move sub2/hello.txt sub1')
195
assert not exists('sub2/hello.txt')
196
assert exists('sub1/hello.txt')
197
runcmd('bzr move sub2 sub1')
198
assert not exists('sub2')
199
assert exists('sub1/sub2')
201
runcmd(['bzr', 'commit', '-m', 'rename nested subdirectories'])
204
self.assertEquals(backtick('bzr root')[:-1],
205
os.path.join(self.test_dir, 'branch1'))
206
runcmd('bzr move ../hello.txt .')
207
assert exists('./hello.txt')
208
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
209
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
210
runcmd(['bzr', 'commit', '-m', 'move to parent directory'])
212
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
214
runcmd('bzr move sub2/hello.txt .')
215
assert exists('hello.txt')
217
f = file('hello.txt', 'wt')
218
f.write('some nice new content\n')
221
f = file('msg.tmp', 'wt')
222
f.write('this is my new commit\n')
225
runcmd('bzr commit -F msg.tmp')
227
assert backtick('bzr revno') == '5\n'
228
runcmd('bzr export -r 5 export-5.tmp')
229
runcmd('bzr export export.tmp')
236
progress("file with spaces in name")
237
mkdir('sub directory')
238
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
241
runcmd('bzr commit -m add-spaces')
245
runcmd('bzr log --forward')
257
# Can't create a branch if it already exists
258
runcmd('bzr branch branch1', retcode=1)
259
# Can't create a branch if its parent doesn't exist
260
runcmd('bzr branch /unlikely/to/exist', retcode=1)
261
runcmd('bzr branch branch1 branch2')
265
runcmd('bzr pull', retcode=1)
266
runcmd('bzr pull ../branch2')
269
runcmd('bzr commit --unchanged -m empty')
271
chdir('../../branch2')
273
runcmd('bzr commit --unchanged -m empty')
275
runcmd('bzr commit --unchanged -m empty')
276
runcmd('bzr pull', retcode=1)
279
progress('status after remove')
280
mkdir('status-after-remove')
281
# see mail from William Dodé, 2005-05-25
282
# $ bzr init; touch a; bzr add a; bzr commit -m "add a"
283
# * looking for changes...
288
# bzr: local variable 'kind' referenced before assignment
289
# at /vrac/python/bazaar-ng/bzrlib/diff.py:286 in compare_trees()
290
# see ~/.bzr.log for debug information
291
chdir('status-after-remove')
293
file('a', 'w').write('foo')
295
runcmd(['bzr', 'commit', '-m', 'add a'])
296
runcmd('bzr remove a')
301
progress('ignore patterns')
302
mkdir('ignorebranch')
303
chdir('ignorebranch')
305
assert backtick('bzr unknowns') == ''
307
file('foo.tmp', 'wt').write('tmp files are ignored')
308
assert backtick('bzr unknowns') == ''
310
file('foo.c', 'wt').write('int main() {}')
311
assert backtick('bzr unknowns') == 'foo.c\n'
312
runcmd('bzr add foo.c')
313
assert backtick('bzr unknowns') == ''
315
# 'ignore' works when creating the .bzignore file
316
file('foo.blah', 'wt').write('blah')
317
assert backtick('bzr unknowns') == 'foo.blah\n'
318
runcmd('bzr ignore *.blah')
319
assert backtick('bzr unknowns') == ''
320
assert file('.bzrignore', 'rb').read() == '*.blah\n'
322
# 'ignore' works when then .bzrignore file already exists
323
file('garh', 'wt').write('garh')
324
assert backtick('bzr unknowns') == 'garh\n'
325
runcmd('bzr ignore garh')
326
assert backtick('bzr unknowns') == ''
327
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
334
progress("recursive and non-recursive add")
339
fp = os.path.join('foo', 'test.txt')
343
runcmd('bzr add --no-recurse foo')
344
runcmd('bzr file-id foo')
345
runcmd('bzr file-id ' + fp, 1) # not versioned yet
346
runcmd('bzr commit -m add-dir-only')
348
runcmd('bzr file-id ' + fp, 1) # still not versioned
350
runcmd('bzr add foo')
351
runcmd('bzr file-id ' + fp)
352
runcmd('bzr commit -m add-sub-file')
358
class RevertCommand(InTempDir):
360
self.runcmd('bzr init')
362
file('hello', 'wt').write('foo')
363
self.runcmd('bzr add hello')
364
self.runcmd('bzr commit -m setup hello')
366
file('hello', 'wt').write('bar')
367
self.runcmd('bzr revert hello')
368
self.check_file_contents('hello', 'foo')
374
# lists all tests from this module in the best order to run them. we
375
# do it this way rather than just discovering them all because it
376
# allows us to test more basic functions first where failures will be
377
# easiest to understand.
378
TEST_CLASSES = [TestVersion,