1
# Copyright (C) 2005 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""UI tests for the test framework."""
28
from bzrlib.errors import ParamikoNotPresent
29
from bzrlib.tests import (
32
TestCaseWithMemoryTransport,
33
TestCaseWithTransport,
37
from bzrlib.tests.blackbox import ExternalBase
40
class TestOptions(TestCase):
44
def test_transport_set_to_sftp(self):
45
# test the --transport option has taken effect from within the
48
import bzrlib.transport.sftp
49
except ParamikoNotPresent:
50
raise TestSkipped("Paramiko not present")
51
if TestOptions.current_test != "test_transport_set_to_sftp":
53
self.assertEqual(bzrlib.transport.sftp.SFTPAbsoluteServer,
54
bzrlib.tests.default_transport)
56
def test_transport_set_to_memory(self):
57
# test the --transport option has taken effect from within the
59
import bzrlib.transport.memory
60
if TestOptions.current_test != "test_transport_set_to_memory":
62
self.assertEqual(bzrlib.transport.memory.MemoryServer,
63
bzrlib.tests.default_transport)
65
def test_transport(self):
66
# test that --transport=sftp works
68
import bzrlib.transport.sftp
69
except ParamikoNotPresent:
70
raise TestSkipped("Paramiko not present")
71
old_transport = bzrlib.tests.default_transport
72
old_root = TestCaseWithMemoryTransport.TEST_ROOT
73
TestCaseWithMemoryTransport.TEST_ROOT = None
75
TestOptions.current_test = "test_transport_set_to_sftp"
76
stdout = self.capture('selftest --transport=sftp test_transport_set_to_sftp')
78
self.assertContainsRe(stdout, 'Ran 1 test')
79
self.assertEqual(old_transport, bzrlib.tests.default_transport)
81
TestOptions.current_test = "test_transport_set_to_memory"
82
stdout = self.capture('selftest --transport=memory test_transport_set_to_memory')
83
self.assertContainsRe(stdout, 'Ran 1 test')
84
self.assertEqual(old_transport, bzrlib.tests.default_transport)
86
bzrlib.tests.default_transport = old_transport
87
TestOptions.current_test = None
88
TestCaseWithMemoryTransport.TEST_ROOT = old_root
91
class TestRunBzr(ExternalBase):
93
def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
95
"""Override run_bzr_captured to test how it is invoked by run_bzr.
97
We test how run_bzr_captured actually invokes bzr in another location.
98
Here we only need to test that it is run_bzr passes the right
99
parameters to run_bzr_captured.
102
self.retcode = retcode
103
self.encoding = encoding
105
self.working_dir = working_dir
109
"""Test that run_bzr passes args correctly to run_bzr_captured"""
110
self.run_bzr('arg1', 'arg2', 'arg3', retcode=1)
111
self.assertEqual(('arg1', 'arg2', 'arg3'), self.argv)
113
def test_encoding(self):
114
"""Test that run_bzr passes encoding to run_bzr_captured"""
115
self.run_bzr('foo', 'bar')
116
self.assertEqual(None, self.encoding)
117
self.assertEqual(('foo', 'bar'), self.argv)
119
self.run_bzr('foo', 'bar', encoding='baz')
120
self.assertEqual('baz', self.encoding)
121
self.assertEqual(('foo', 'bar'), self.argv)
123
def test_retcode(self):
124
"""Test that run_bzr passes retcode to run_bzr_captured"""
125
# Default is retcode == 0
126
self.run_bzr('foo', 'bar')
127
self.assertEqual(0, self.retcode)
128
self.assertEqual(('foo', 'bar'), self.argv)
130
self.run_bzr('foo', 'bar', retcode=1)
131
self.assertEqual(1, self.retcode)
132
self.assertEqual(('foo', 'bar'), self.argv)
134
self.run_bzr('foo', 'bar', retcode=None)
135
self.assertEqual(None, self.retcode)
136
self.assertEqual(('foo', 'bar'), self.argv)
138
self.run_bzr('foo', 'bar', retcode=3)
139
self.assertEqual(3, self.retcode)
140
self.assertEqual(('foo', 'bar'), self.argv)
142
def test_stdin(self):
143
# test that the stdin keyword to run_bzr is passed through to
144
# run_bzr_captured as-is. We do this by overriding
145
# run_bzr_captured in this class, and then calling run_bzr,
146
# which is a convenience function for run_bzr_captured, so
148
self.run_bzr('foo', 'bar', stdin='gam')
149
self.assertEqual('gam', self.stdin)
150
self.assertEqual(('foo', 'bar'), self.argv)
152
self.run_bzr('foo', 'bar', stdin='zippy')
153
self.assertEqual('zippy', self.stdin)
154
self.assertEqual(('foo', 'bar'), self.argv)
156
def test_working_dir(self):
157
"""Test that run_bzr passes working_dir to run_bzr_captured"""
158
self.run_bzr('foo', 'bar')
159
self.assertEqual(None, self.working_dir)
160
self.assertEqual(('foo', 'bar'), self.argv)
162
self.run_bzr('foo', 'bar', working_dir='baz')
163
self.assertEqual('baz', self.working_dir)
164
self.assertEqual(('foo', 'bar'), self.argv)
167
class TestBenchmarkTests(TestCaseWithTransport):
169
def test_benchmark_runs_benchmark_tests(self):
170
"""bzr selftest --benchmark should not run the default test suite."""
171
# We test this by passing a regression test name to --benchmark, which
172
# should result in 0 rests run.
173
old_root = TestCaseWithMemoryTransport.TEST_ROOT
175
TestCaseWithMemoryTransport.TEST_ROOT = None
176
out, err = self.run_bzr('selftest', '--benchmark', 'workingtree_implementations')
178
TestCaseWithMemoryTransport.TEST_ROOT = old_root
179
self.assertContainsRe(out, 'Ran 0 tests.*\n\nOK')
183
benchfile = open(".perf_history", "rt")
185
lines = benchfile.readlines()
188
self.assertEqual(1, len(lines))
189
self.assertContainsRe(lines[0], "--date [0-9.]+")
192
class TestRunBzrCaptured(ExternalBase):
194
def apply_redirected(self, stdin=None, stdout=None, stderr=None,
195
a_callable=None, *args, **kwargs):
197
self.factory_stdin = getattr(bzrlib.ui.ui_factory, "stdin", None)
198
self.factory = bzrlib.ui.ui_factory
199
self.working_dir = osutils.getcwd()
200
stdout.write('foo\n')
201
stderr.write('bar\n')
204
def test_stdin(self):
205
# test that the stdin keyword to run_bzr_captured is passed through to
206
# apply_redirected as a StringIO. We do this by overriding
207
# apply_redirected in this class, and then calling run_bzr_captured,
208
# which calls apply_redirected.
209
self.run_bzr_captured(['foo', 'bar'], stdin='gam')
210
self.assertEqual('gam', self.stdin.read())
211
self.assertTrue(self.stdin is self.factory_stdin)
212
self.run_bzr_captured(['foo', 'bar'], stdin='zippy')
213
self.assertEqual('zippy', self.stdin.read())
214
self.assertTrue(self.stdin is self.factory_stdin)
216
def test_ui_factory(self):
217
# each invocation of self.run_bzr_captured should get its
218
# own UI factory, which is an instance of TestUIFactory,
219
# with stdin, stdout and stderr attached to the stdin,
220
# stdout and stderr of the invoked run_bzr_captured
221
current_factory = bzrlib.ui.ui_factory
222
self.run_bzr_captured(['foo'])
223
self.failIf(current_factory is self.factory)
224
self.assertNotEqual(sys.stdout, self.factory.stdout)
225
self.assertNotEqual(sys.stderr, self.factory.stderr)
226
self.assertEqual('foo\n', self.factory.stdout.getvalue())
227
self.assertEqual('bar\n', self.factory.stderr.getvalue())
228
self.assertIsInstance(self.factory, TestUIFactory)
230
def test_working_dir(self):
231
self.build_tree(['one/', 'two/'])
232
cwd = osutils.getcwd()
234
# Default is to work in the current directory
235
self.run_bzr_captured(['foo', 'bar'])
236
self.assertEqual(cwd, self.working_dir)
238
self.run_bzr_captured(['foo', 'bar'], working_dir=None)
239
self.assertEqual(cwd, self.working_dir)
241
# The function should be run in the alternative directory
242
# but afterwards the current working dir shouldn't be changed
243
self.run_bzr_captured(['foo', 'bar'], working_dir='one')
244
self.assertNotEqual(cwd, self.working_dir)
245
self.assertEndsWith(self.working_dir, 'one')
246
self.assertEqual(cwd, osutils.getcwd())
248
self.run_bzr_captured(['foo', 'bar'], working_dir='two')
249
self.assertNotEqual(cwd, self.working_dir)
250
self.assertEndsWith(self.working_dir, 'two')
251
self.assertEqual(cwd, osutils.getcwd())
254
class TestRunBzrSubprocess(TestCaseWithTransport):
256
def test_run_bzr_subprocess(self):
257
"""The run_bzr_helper_external comand behaves nicely."""
258
result = self.run_bzr_subprocess('--version')
259
result = self.run_bzr_subprocess('--version', retcode=None)
260
self.assertContainsRe(result[0], 'is free software')
261
self.assertRaises(AssertionError, self.run_bzr_subprocess,
263
result = self.run_bzr_subprocess('--versionn', retcode=3)
264
result = self.run_bzr_subprocess('--versionn', retcode=None)
265
self.assertContainsRe(result[1], 'unknown command')
266
err = self.run_bzr_subprocess('merge', '--merge-type', 'magic merge',
268
self.assertContainsRe(err, 'Bad value "magic merge" for option'
271
def test_run_bzr_subprocess_env(self):
272
"""run_bzr_subprocess can set environment variables in the child only.
274
These changes should not change the running process, only the child.
276
# The test suite should unset this variable
277
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
278
out, err = self.run_bzr_subprocess('whoami', env_changes={
279
'BZR_EMAIL':'Joe Foo <joe@foo.com>'
280
}, universal_newlines=True)
281
self.assertEqual('', err)
282
self.assertEqual('Joe Foo <joe@foo.com>\n', out)
283
# And it should not be modified
284
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
286
# Do it again with a different address, just to make sure
287
# it is actually changing
288
out, err = self.run_bzr_subprocess('whoami', env_changes={
289
'BZR_EMAIL':'Barry <bar@foo.com>'
290
}, universal_newlines=True)
291
self.assertEqual('', err)
292
self.assertEqual('Barry <bar@foo.com>\n', out)
293
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
295
def test_run_bzr_subprocess_env_del(self):
296
"""run_bzr_subprocess can remove environment variables too."""
297
# Create a random email, so we are sure this won't collide
298
rand_bzr_email = 'John Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
299
rand_email = 'Jane Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
300
os.environ['BZR_EMAIL'] = rand_bzr_email
301
os.environ['EMAIL'] = rand_email
303
# By default, the child will inherit the current env setting
304
out, err = self.run_bzr_subprocess('whoami', universal_newlines=True)
305
self.assertEqual('', err)
306
self.assertEqual(rand_bzr_email + '\n', out)
308
# Now that BZR_EMAIL is not set, it should fall back to EMAIL
309
out, err = self.run_bzr_subprocess('whoami',
310
env_changes={'BZR_EMAIL':None},
311
universal_newlines=True)
312
self.assertEqual('', err)
313
self.assertEqual(rand_email + '\n', out)
315
# This switches back to the default email guessing logic
316
# Which shouldn't match either of the above addresses
317
out, err = self.run_bzr_subprocess('whoami',
318
env_changes={'BZR_EMAIL':None, 'EMAIL':None},
319
universal_newlines=True)
321
self.assertEqual('', err)
322
self.assertNotEqual(rand_bzr_email + '\n', out)
323
self.assertNotEqual(rand_email + '\n', out)
325
# TestCase cleans up BZR_EMAIL, and EMAIL at startup
326
del os.environ['BZR_EMAIL']
327
del os.environ['EMAIL']
329
def test_run_bzr_subprocess_env_del_missing(self):
330
"""run_bzr_subprocess won't fail if deleting a nonexistant env var"""
331
self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
332
out, err = self.run_bzr_subprocess('rocks',
333
env_changes={'NON_EXISTANT_ENV_VAR':None},
334
universal_newlines=True)
335
self.assertEqual('It sure does!\n', out)
336
self.assertEqual('', err)
338
def test_run_bzr_subprocess_working_dir(self):
339
"""Test that we can specify the working dir for the child"""
340
cwd = osutils.getcwd()
342
self.make_branch_and_tree('.')
343
self.make_branch_and_tree('one')
344
self.make_branch_and_tree('two')
346
def get_root(**kwargs):
347
"""Spawn a process to get the 'root' of the tree.
349
You can pass in arbitrary new arguments. This just makes
350
sure that the returned path doesn't have trailing whitespace.
352
return self.run_bzr_subprocess('root', **kwargs)[0].rstrip()
354
self.assertEqual(cwd, get_root())
355
self.assertEqual(cwd, get_root(working_dir=None))
356
# Has our path changed?
357
self.assertEqual(cwd, osutils.getcwd())
359
dir1 = get_root(working_dir='one')
360
self.assertEndsWith(dir1, 'one')
361
self.assertEqual(cwd, osutils.getcwd())
363
dir2 = get_root(working_dir='two')
364
self.assertEndsWith(dir2, 'two')
365
self.assertEqual(cwd, osutils.getcwd())
368
class _DontSpawnProcess(Exception):
369
"""A simple exception which just allows us to skip unnecessary steps"""
372
class TestRunBzrSubprocessCommands(TestCaseWithTransport):
374
def _popen(self, *args, **kwargs):
375
"""Record the command that is run, so that we can ensure it is correct"""
376
self._popen_args = args
377
self._popen_kwargs = kwargs
378
raise _DontSpawnProcess()
380
def test_run_bzr_subprocess_no_plugins(self):
381
self.assertRaises(_DontSpawnProcess, self.run_bzr_subprocess)
382
command = self._popen_args[0]
383
self.assertEqual(sys.executable, command[0])
384
self.assertEqual(self.get_bzr_path(), command[1])
385
self.assertEqual(['--no-plugins'], command[2:])
387
def test_allow_plugins(self):
388
self.assertRaises(_DontSpawnProcess,
389
self.run_bzr_subprocess, allow_plugins=True)
390
command = self._popen_args[0]
391
self.assertEqual([], command[2:])
394
class TestBzrSubprocess(TestCaseWithTransport):
396
def test_start_and_stop_bzr_subprocess(self):
397
"""We can start and perform other test actions while that process is
400
process = self.start_bzr_subprocess(['--version'])
401
result = self.finish_bzr_subprocess(process)
402
self.assertContainsRe(result[0], 'is free software')
403
self.assertEqual('', result[1])
405
def test_start_and_stop_bzr_subprocess_with_error(self):
406
"""finish_bzr_subprocess allows specification of the desired exit code.
408
process = self.start_bzr_subprocess(['--versionn'])
409
result = self.finish_bzr_subprocess(process, retcode=3)
410
self.assertEqual('', result[0])
411
self.assertContainsRe(result[1], 'unknown command')
413
def test_start_and_stop_bzr_subprocess_ignoring_retcode(self):
414
"""finish_bzr_subprocess allows the exit code to be ignored."""
415
process = self.start_bzr_subprocess(['--versionn'])
416
result = self.finish_bzr_subprocess(process, retcode=None)
417
self.assertEqual('', result[0])
418
self.assertContainsRe(result[1], 'unknown command')
420
def test_start_and_stop_bzr_subprocess_with_unexpected_retcode(self):
421
"""finish_bzr_subprocess raises self.failureException if the retcode is
422
not the expected one.
424
process = self.start_bzr_subprocess(['--versionn'])
425
self.assertRaises(self.failureException, self.finish_bzr_subprocess,
428
def test_start_and_stop_bzr_subprocess_send_signal(self):
429
"""finish_bzr_subprocess raises self.failureException if the retcode is
430
not the expected one.
432
process = self.start_bzr_subprocess(['wait-until-signalled'],
433
skip_if_plan_to_signal=True)
434
self.assertEqual('running\n', process.stdout.readline())
435
result = self.finish_bzr_subprocess(process, send_signal=signal.SIGINT,
437
self.assertEqual('', result[0])
438
self.assertEqual('bzr: interrupted\n', result[1])
440
def test_start_and_stop_working_dir(self):
441
cwd = osutils.getcwd()
443
self.make_branch_and_tree('one')
445
process = self.start_bzr_subprocess(['root'], working_dir='one')
446
result = self.finish_bzr_subprocess(process, universal_newlines=True)
447
self.assertEndsWith(result[0], 'one\n')
448
self.assertEqual('', result[1])
451
class TestRunBzrError(ExternalBase):
453
def test_run_bzr_error(self):
454
out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
455
self.assertEqual(out, 'It sure does!\n')
457
out, err = self.run_bzr_error(["bzr: ERROR: foobarbaz is not versioned"],
458
'file-id', 'foobarbaz')
461
class TestSelftestCleanOutput(TestCaseInTempDir):
463
def test_clean_output(self):
464
# check that 'bzr selftest --clean-output' works correct
465
dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
466
files = ('bzr', 'setup.py', 'test9999.tmp')
471
f.write('content of ')
476
before = os.listdir(root)
478
self.assertEquals(['bzr','bzrlib','setup.py',
479
'test0000.tmp','test0001.tmp',
480
'test9999.tmp','tests'],
483
out,err = self.run_bzr_captured(['selftest','--clean-output'],
486
self.assertEquals(['delete directory: test0000.tmp',
487
'delete directory: test0001.tmp'],
488
sorted(out.splitlines()))
489
self.assertEquals('', err)
491
after = os.listdir(root)
493
self.assertEquals(['bzr','bzrlib','setup.py',
494
'test9999.tmp','tests'],
498
class TestSelftestListOnly(TestCase):
501
def _parse_test_list(lines, newlines_in_header=1):
502
"Parse a list of lines into a tuple of 3 lists (header,body,footer)."
509
header_newlines_found = 0
513
header_newlines_found += 1
514
if header_newlines_found >= newlines_in_header:
519
if line.startswith('-------'):
525
# If the last body line is blank, drop it off the list
526
if len(body) > 0 and body[-1] == '':
528
return (header,body,footer)
530
def test_list_only(self):
531
# check that bzr selftest --list-only works correctly
532
out,err = self.run_bzr_captured(['selftest', 'selftest',
534
self.assertEndsWith(err, 'tests passed\n')
535
(header,body,footer) = self._parse_test_list(out.splitlines())
536
num_tests = len(body)
537
self.assertContainsRe(footer[0], 'Listed %s tests in' % num_tests)
539
def test_list_only_filtered(self):
540
# check that a filtered --list-only works, both include and exclude
541
out_all,err_all = self.run_bzr_captured(['selftest', '--list-only'])
542
tests_all = self._parse_test_list(out_all.splitlines())[1]
543
out_incl,err_incl = self.run_bzr_captured(['selftest', '--list-only',
545
tests_incl = self._parse_test_list(out_incl.splitlines())[1]
546
self.assertSubset(tests_incl, tests_all)
547
out_excl,err_excl = self.run_bzr_captured(['selftest', '--list-only',
548
'--exclude', 'selftest'])
549
tests_excl = self._parse_test_list(out_excl.splitlines())[1]
550
self.assertSubset(tests_excl, tests_all)
551
set_incl = set(tests_incl)
552
set_excl = set(tests_excl)
553
intersection = set_incl.intersection(set_excl)
554
self.assertEquals(0, len(intersection))
555
self.assertEquals(len(tests_all), len(tests_incl) + len(tests_excl))
557
def test_list_only_random(self):
558
# check that --randomize works correctly
559
out_all,err_all = self.run_bzr_captured(['selftest', '--list-only',
561
tests_all = self._parse_test_list(out_all.splitlines())[1]
562
# XXX: It looks like there are some orders for generating tests that
563
# fail as of 20070504 - maybe because of import order dependencies.
564
# So unfortunately this will rarely intermittently fail at the moment.
566
out_rand,err_rand = self.run_bzr_captured(['selftest', '--list-only',
567
'selftest', '--randomize', 'now'])
568
(header_rand,tests_rand,dummy) = self._parse_test_list(
569
out_rand.splitlines(), 2)
570
self.assertNotEqual(tests_all, tests_rand)
571
self.assertEqual(sorted(tests_all), sorted(tests_rand))
572
# Check that the seed can be reused to get the exact same order
573
seed_re = re.compile('Randomizing test order using seed (\w+)')
574
match_obj = seed_re.search(header_rand[-1])
575
seed = match_obj.group(1)
576
out_rand2,err_rand2 = self.run_bzr_captured(['selftest', '--list-only',
577
'selftest', '--randomize', seed])
578
(header_rand2,tests_rand2,dummy) = self._parse_test_list(
579
out_rand2.splitlines(), 2)
580
self.assertEqual(tests_rand, tests_rand2)