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 OldTests(InTempDir):
76
# old tests moved from ./testbzr
78
from os import chdir, mkdir
79
from os.path import exists
83
backtick = self.backtick
86
progress("basic branch creation")
87
runcmd(['mkdir', 'branch1'])
91
self.assertEquals(backtick('bzr root').rstrip(),
92
os.path.join(self.test_dir, 'branch1'))
94
progress("status of new file")
96
f = file('test.txt', 'wt')
97
f.write('hello world!\n')
100
out = backtick("bzr unknowns")
101
self.assertEquals(out, 'test.txt\n')
103
out = backtick("bzr status")
104
assert out == 'unknown:\n test.txt\n'
106
out = backtick("bzr status --all")
107
assert out == "unknown:\n test.txt\n"
109
out = backtick("bzr status test.txt --all")
110
assert out == "unknown:\n test.txt\n"
112
f = file('test2.txt', 'wt')
113
f.write('goodbye cruel world...\n')
116
out = backtick("bzr status test.txt")
117
assert out == "unknown:\n test.txt\n"
119
out = backtick("bzr status")
120
assert out == ("unknown:\n"
124
os.unlink('test2.txt')
126
progress("command aliases")
127
out = backtick("bzr st --all")
128
assert out == ("unknown:\n"
131
out = backtick("bzr stat")
132
assert out == ("unknown:\n"
135
progress("command help")
136
runcmd("bzr help st")
138
runcmd("bzr help commands")
139
runcmd("bzr help slartibartfast", 1)
141
out = backtick("bzr help ci")
142
out.index('aliases: ')
144
progress("can't rename unversioned file")
145
runcmd("bzr rename test.txt new-test.txt", 1)
147
progress("adding a file")
149
runcmd("bzr add test.txt")
150
assert backtick("bzr unknowns") == ''
151
assert backtick("bzr status --all") == ("added:\n"
154
progress("rename newly-added file")
155
runcmd("bzr rename test.txt hello.txt")
156
assert os.path.exists("hello.txt")
157
assert not os.path.exists("test.txt")
159
assert backtick("bzr revno") == '0\n'
161
progress("add first revision")
162
runcmd(["bzr", "commit", "-m", 'add first revision'])
164
progress("more complex renames")
166
runcmd("bzr rename hello.txt sub1", 1)
167
runcmd("bzr rename hello.txt sub1/hello.txt", 1)
168
runcmd("bzr move hello.txt sub1", 1)
170
runcmd("bzr add sub1")
171
runcmd("bzr rename sub1 sub2")
172
runcmd("bzr move hello.txt sub2")
173
assert backtick("bzr relpath sub2/hello.txt") == os.path.join("sub2", "hello.txt\n")
175
assert exists("sub2")
176
assert exists("sub2/hello.txt")
177
assert not exists("sub1")
178
assert not exists("hello.txt")
180
runcmd(['bzr', 'commit', '-m', 'commit with some things moved to subdirs'])
183
runcmd('bzr add sub1')
184
runcmd('bzr move sub2/hello.txt sub1')
185
assert not exists('sub2/hello.txt')
186
assert exists('sub1/hello.txt')
187
runcmd('bzr move sub2 sub1')
188
assert not exists('sub2')
189
assert exists('sub1/sub2')
191
runcmd(['bzr', 'commit', '-m', 'rename nested subdirectories'])
194
self.assertEquals(backtick('bzr root')[:-1],
195
os.path.join(self.test_dir, 'branch1'))
196
runcmd('bzr move ../hello.txt .')
197
assert exists('./hello.txt')
198
assert backtick('bzr relpath hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
199
assert backtick('bzr relpath ../../sub1/sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
200
runcmd(['bzr', 'commit', '-m', 'move to parent directory'])
202
assert backtick('bzr relpath sub2/hello.txt') == os.path.join('sub1', 'sub2', 'hello.txt\n')
204
runcmd('bzr move sub2/hello.txt .')
205
assert exists('hello.txt')
207
f = file('hello.txt', 'wt')
208
f.write('some nice new content\n')
211
f = file('msg.tmp', 'wt')
212
f.write('this is my new commit\n')
215
runcmd('bzr commit -F msg.tmp')
217
assert backtick('bzr revno') == '5\n'
218
runcmd('bzr export -r 5 export-5.tmp')
219
runcmd('bzr export export.tmp')
226
progress("file with spaces in name")
227
mkdir('sub directory')
228
file('sub directory/file with spaces ', 'wt').write('see how this works\n')
231
runcmd('bzr commit -m add-spaces')
235
runcmd('bzr log --forward')
247
# Can't create a branch if it already exists
248
runcmd('bzr branch branch1', retcode=1)
249
# Can't create a branch if its parent doesn't exist
250
runcmd('bzr branch /unlikely/to/exist', retcode=1)
251
runcmd('bzr branch branch1 branch2')
255
runcmd('bzr pull', retcode=1)
256
runcmd('bzr pull ../branch2')
259
runcmd('bzr commit -m empty')
261
chdir('../../branch2')
263
runcmd('bzr commit -m empty')
265
runcmd('bzr commit -m empty')
266
runcmd('bzr pull', retcode=1)
269
progress('status after remove')
270
mkdir('status-after-remove')
271
# see mail from William Dodé, 2005-05-25
272
# $ bzr init; touch a; bzr add a; bzr commit -m "add a"
273
# * looking for changes...
278
# bzr: local variable 'kind' referenced before assignment
279
# at /vrac/python/bazaar-ng/bzrlib/diff.py:286 in compare_trees()
280
# see ~/.bzr.log for debug information
281
chdir('status-after-remove')
283
file('a', 'w').write('foo')
285
runcmd(['bzr', 'commit', '-m', 'add a'])
286
runcmd('bzr remove a')
291
progress('ignore patterns')
292
mkdir('ignorebranch')
293
chdir('ignorebranch')
295
assert backtick('bzr unknowns') == ''
297
file('foo.tmp', 'wt').write('tmp files are ignored')
298
assert backtick('bzr unknowns') == ''
300
file('foo.c', 'wt').write('int main() {}')
301
assert backtick('bzr unknowns') == 'foo.c\n'
302
runcmd('bzr add foo.c')
303
assert backtick('bzr unknowns') == ''
305
# 'ignore' works when creating the .bzignore file
306
file('foo.blah', 'wt').write('blah')
307
assert backtick('bzr unknowns') == 'foo.blah\n'
308
runcmd('bzr ignore *.blah')
309
assert backtick('bzr unknowns') == ''
310
assert file('.bzrignore', 'rb').read() == '*.blah\n'
312
# 'ignore' works when then .bzrignore file already exists
313
file('garh', 'wt').write('garh')
314
assert backtick('bzr unknowns') == 'garh\n'
315
runcmd('bzr ignore garh')
316
assert backtick('bzr unknowns') == ''
317
assert file('.bzrignore', 'rb').read() == '*.blah\ngarh\n'
324
progress("recursive and non-recursive add")
329
fp = os.path.join('foo', 'test.txt')
333
runcmd('bzr add --no-recurse foo')
334
runcmd('bzr file-id foo')
335
runcmd('bzr file-id ' + fp, 1) # not versioned yet
336
runcmd('bzr commit -m add-dir-only')
338
runcmd('bzr file-id ' + fp, 1) # still not versioned
340
runcmd('bzr add foo')
341
runcmd('bzr file-id ' + fp)
342
runcmd('bzr commit -m add-sub-file')
348
class RevertCommand(InTempDir):
350
self.runcmd('bzr init')
352
file('hello', 'wt').write('foo')
353
self.runcmd('bzr add hello')
354
self.runcmd('bzr commit -m setup hello')
356
file('hello', 'wt').write('bar')
357
self.runcmd('bzr revert hello')
358
self.check_file_contents('hello', 'foo')
364
# lists all tests from this module in the best order to run them. we
365
# do it this way rather than just discovering them all because it
366
# allows us to test more basic functions first where failures will be
367
# easiest to understand.
370
from unittest import TestSuite
372
s.addTests([TestVersion(),