60
72
except ParamikoNotPresent:
61
73
raise TestSkipped("Paramiko not present")
62
74
old_transport = bzrlib.tests.default_transport
63
old_root = TestCaseInTempDir.TEST_ROOT
64
TestCaseInTempDir.TEST_ROOT = None
75
old_root = TestCaseWithMemoryTransport.TEST_ROOT
76
TestCaseWithMemoryTransport.TEST_ROOT = None
66
78
TestOptions.current_test = "test_transport_set_to_sftp"
67
stdout = self.capture('selftest --transport=sftp test_transport_set_to_sftp')
79
stdout = self.run_bzr(
80
'selftest --transport=sftp test_transport_set_to_sftp')[0]
69
81
self.assertContainsRe(stdout, 'Ran 1 test')
70
82
self.assertEqual(old_transport, bzrlib.tests.default_transport)
72
84
TestOptions.current_test = "test_transport_set_to_memory"
73
stdout = self.capture('selftest --transport=memory test_transport_set_to_memory')
85
stdout = self.run_bzr(
86
'selftest --transport=memory test_transport_set_to_memory')[0]
74
87
self.assertContainsRe(stdout, 'Ran 1 test')
75
88
self.assertEqual(old_transport, bzrlib.tests.default_transport)
77
90
bzrlib.tests.default_transport = old_transport
78
91
TestOptions.current_test = None
79
TestCaseInTempDir.TEST_ROOT = old_root
92
TestCaseWithMemoryTransport.TEST_ROOT = old_root
82
95
class TestRunBzr(ExternalBase):
84
def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None):
97
def _run_bzr_core(self, argv, retcode=0, encoding=None, stdin=None,
99
"""Override _run_bzr_core to test how it is invoked by run_bzr.
101
Attempts to run bzr from inside this class don't actually run it.
103
We test how run_bzr actually invokes bzr in another location.
104
Here we only need to test that it is run_bzr passes the right
105
parameters to run_bzr.
107
self.argv = list(argv)
108
self.retcode = retcode
109
self.encoding = encoding
85
110
self.stdin = stdin
111
self.working_dir = working_dir
114
def test_encoding(self):
115
"""Test that run_bzr passes encoding to _run_bzr_core"""
116
self.run_bzr('foo bar')
117
self.assertEqual(None, self.encoding)
118
self.assertEqual(['foo', 'bar'], self.argv)
120
self.run_bzr('foo bar', encoding='baz')
121
self.assertEqual('baz', self.encoding)
122
self.assertEqual(['foo', 'bar'], self.argv)
124
def test_retcode(self):
125
"""Test that run_bzr passes retcode to _run_bzr_core"""
126
# Default is retcode == 0
127
self.run_bzr('foo bar')
128
self.assertEqual(0, self.retcode)
129
self.assertEqual(['foo', 'bar'], self.argv)
131
self.run_bzr('foo bar', retcode=1)
132
self.assertEqual(1, self.retcode)
133
self.assertEqual(['foo', 'bar'], self.argv)
135
self.run_bzr('foo bar', retcode=None)
136
self.assertEqual(None, self.retcode)
137
self.assertEqual(['foo', 'bar'], self.argv)
139
self.run_bzr(['foo', 'bar'], retcode=3)
140
self.assertEqual(3, self.retcode)
141
self.assertEqual(['foo', 'bar'], self.argv)
87
143
def test_stdin(self):
88
144
# test that the stdin keyword to run_bzr is passed through to
89
# run_bzr_captured as-is. We do this by overriding
90
# run_bzr_captured in this class, and then calling run_bzr,
91
# which is a convenience function for run_bzr_captured, so
145
# _run_bzr_core as-is. We do this by overriding
146
# _run_bzr_core in this class, and then calling run_bzr,
147
# which is a convenience function for _run_bzr_core, so
92
148
# should invoke it.
93
self.run_bzr('foo', 'bar', stdin='gam')
149
self.run_bzr('foo bar', stdin='gam')
94
150
self.assertEqual('gam', self.stdin)
95
self.run_bzr('foo', 'bar', stdin='zippy')
151
self.assertEqual(['foo', 'bar'], self.argv)
153
self.run_bzr('foo bar', stdin='zippy')
96
154
self.assertEqual('zippy', self.stdin)
155
self.assertEqual(['foo', 'bar'], self.argv)
157
def test_working_dir(self):
158
"""Test that run_bzr passes working_dir to _run_bzr_core"""
159
self.run_bzr('foo bar')
160
self.assertEqual(None, self.working_dir)
161
self.assertEqual(['foo', 'bar'], self.argv)
163
self.run_bzr('foo bar', working_dir='baz')
164
self.assertEqual('baz', self.working_dir)
165
self.assertEqual(['foo', 'bar'], self.argv)
167
def test_reject_extra_keyword_arguments(self):
168
self.assertRaises(TypeError, self.run_bzr, "foo bar",
169
error_regex=['error message'])
99
172
class TestBenchmarkTests(TestCaseWithTransport):
128
202
self.stdin = stdin
129
203
self.factory_stdin = getattr(bzrlib.ui.ui_factory, "stdin", None)
130
204
self.factory = bzrlib.ui.ui_factory
205
self.working_dir = osutils.getcwd()
131
206
stdout.write('foo\n')
132
207
stderr.write('bar\n')
135
210
def test_stdin(self):
136
# test that the stdin keyword to run_bzr_captured is passed through to
211
# test that the stdin keyword to _run_bzr_core is passed through to
137
212
# apply_redirected as a StringIO. We do this by overriding
138
# apply_redirected in this class, and then calling run_bzr_captured,
213
# apply_redirected in this class, and then calling _run_bzr_core,
139
214
# which calls apply_redirected.
140
self.run_bzr_captured(['foo', 'bar'], stdin='gam')
215
self.run_bzr(['foo', 'bar'], stdin='gam')
141
216
self.assertEqual('gam', self.stdin.read())
142
217
self.assertTrue(self.stdin is self.factory_stdin)
143
self.run_bzr_captured(['foo', 'bar'], stdin='zippy')
218
self.run_bzr(['foo', 'bar'], stdin='zippy')
144
219
self.assertEqual('zippy', self.stdin.read())
145
220
self.assertTrue(self.stdin is self.factory_stdin)
147
222
def test_ui_factory(self):
148
# each invocation of self.run_bzr_captured should get its own UI
149
# factory, which is an instance of TestUIFactory, with stdout and
150
# stderr attached to the stdout and stderr of the invoked
223
# each invocation of self.run_bzr should get its
224
# own UI factory, which is an instance of TestUIFactory,
225
# with stdin, stdout and stderr attached to the stdin,
226
# stdout and stderr of the invoked run_bzr
152
227
current_factory = bzrlib.ui.ui_factory
153
self.run_bzr_captured(['foo'])
228
self.run_bzr(['foo'])
154
229
self.failIf(current_factory is self.factory)
155
230
self.assertNotEqual(sys.stdout, self.factory.stdout)
156
231
self.assertNotEqual(sys.stderr, self.factory.stderr)
157
232
self.assertEqual('foo\n', self.factory.stdout.getvalue())
158
233
self.assertEqual('bar\n', self.factory.stderr.getvalue())
159
self.assertIsInstance(self.factory, bzrlib.tests.blackbox.TestUIFactory)
234
self.assertIsInstance(self.factory, TestUIFactory)
236
def test_working_dir(self):
237
self.build_tree(['one/', 'two/'])
238
cwd = osutils.getcwd()
240
# Default is to work in the current directory
241
self.run_bzr(['foo', 'bar'])
242
self.assertEqual(cwd, self.working_dir)
244
self.run_bzr(['foo', 'bar'], working_dir=None)
245
self.assertEqual(cwd, self.working_dir)
247
# The function should be run in the alternative directory
248
# but afterwards the current working dir shouldn't be changed
249
self.run_bzr(['foo', 'bar'], working_dir='one')
250
self.assertNotEqual(cwd, self.working_dir)
251
self.assertEndsWith(self.working_dir, 'one')
252
self.assertEqual(cwd, osutils.getcwd())
254
self.run_bzr(['foo', 'bar'], working_dir='two')
255
self.assertNotEqual(cwd, self.working_dir)
256
self.assertEndsWith(self.working_dir, 'two')
257
self.assertEqual(cwd, osutils.getcwd())
260
class TestRunBzrSubprocess(TestCaseWithTransport):
161
262
def test_run_bzr_subprocess(self):
162
263
"""The run_bzr_helper_external comand behaves nicely."""
163
264
result = self.run_bzr_subprocess('--version')
265
result = self.run_bzr_subprocess(['--version'])
164
266
result = self.run_bzr_subprocess('--version', retcode=None)
165
267
self.assertContainsRe(result[0], 'is free software')
166
268
self.assertRaises(AssertionError, self.run_bzr_subprocess,
168
270
result = self.run_bzr_subprocess('--versionn', retcode=3)
169
271
result = self.run_bzr_subprocess('--versionn', retcode=None)
170
272
self.assertContainsRe(result[1], 'unknown command')
171
err = self.run_bzr_subprocess('merge', '--merge-type', 'magic merge',
173
self.assertContainsRe(err, 'No known merge type magic merge')
273
err = self.run_bzr_subprocess(['merge', '--merge-type',
274
'magic merge'], retcode=3)[1]
275
self.assertContainsRe(err, 'Bad value "magic merge" for option'
277
self.callDeprecated(['passing varargs to run_bzr_subprocess was'
278
' deprecated in version 0.91.'],
279
self.run_bzr_subprocess,
280
'arg1', 'arg2', 'arg3', retcode=3)
282
def test_run_bzr_subprocess_env(self):
283
"""run_bzr_subprocess can set environment variables in the child only.
285
These changes should not change the running process, only the child.
287
# The test suite should unset this variable
288
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
289
out, err = self.run_bzr_subprocess('whoami', env_changes={
290
'BZR_EMAIL':'Joe Foo <joe@foo.com>'
291
}, universal_newlines=True)
292
self.assertEqual('', err)
293
self.assertEqual('Joe Foo <joe@foo.com>\n', out)
294
# And it should not be modified
295
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
297
# Do it again with a different address, just to make sure
298
# it is actually changing
299
out, err = self.run_bzr_subprocess('whoami', env_changes={
300
'BZR_EMAIL':'Barry <bar@foo.com>'
301
}, universal_newlines=True)
302
self.assertEqual('', err)
303
self.assertEqual('Barry <bar@foo.com>\n', out)
304
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
306
def test_run_bzr_subprocess_env_del(self):
307
"""run_bzr_subprocess can remove environment variables too."""
308
# Create a random email, so we are sure this won't collide
309
rand_bzr_email = 'John Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
310
rand_email = 'Jane Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
311
os.environ['BZR_EMAIL'] = rand_bzr_email
312
os.environ['EMAIL'] = rand_email
314
# By default, the child will inherit the current env setting
315
out, err = self.run_bzr_subprocess('whoami', universal_newlines=True)
316
self.assertEqual('', err)
317
self.assertEqual(rand_bzr_email + '\n', out)
319
# Now that BZR_EMAIL is not set, it should fall back to EMAIL
320
out, err = self.run_bzr_subprocess('whoami',
321
env_changes={'BZR_EMAIL':None},
322
universal_newlines=True)
323
self.assertEqual('', err)
324
self.assertEqual(rand_email + '\n', out)
326
# This switches back to the default email guessing logic
327
# Which shouldn't match either of the above addresses
328
out, err = self.run_bzr_subprocess('whoami',
329
env_changes={'BZR_EMAIL':None, 'EMAIL':None},
330
universal_newlines=True)
332
self.assertEqual('', err)
333
self.assertNotEqual(rand_bzr_email + '\n', out)
334
self.assertNotEqual(rand_email + '\n', out)
336
# TestCase cleans up BZR_EMAIL, and EMAIL at startup
337
del os.environ['BZR_EMAIL']
338
del os.environ['EMAIL']
340
def test_run_bzr_subprocess_env_del_missing(self):
341
"""run_bzr_subprocess won't fail if deleting a nonexistant env var"""
342
self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
343
out, err = self.run_bzr_subprocess('rocks',
344
env_changes={'NON_EXISTANT_ENV_VAR':None},
345
universal_newlines=True)
346
self.assertEqual('It sure does!\n', out)
347
self.assertEqual('', err)
349
def test_run_bzr_subprocess_working_dir(self):
350
"""Test that we can specify the working dir for the child"""
351
cwd = osutils.getcwd()
353
self.make_branch_and_tree('.')
354
self.make_branch_and_tree('one')
355
self.make_branch_and_tree('two')
357
def get_root(**kwargs):
358
"""Spawn a process to get the 'root' of the tree.
360
You can pass in arbitrary new arguments. This just makes
361
sure that the returned path doesn't have trailing whitespace.
363
return self.run_bzr_subprocess('root', **kwargs)[0].rstrip()
365
self.assertEqual(cwd, get_root())
366
self.assertEqual(cwd, get_root(working_dir=None))
367
# Has our path changed?
368
self.assertEqual(cwd, osutils.getcwd())
370
dir1 = get_root(working_dir='one')
371
self.assertEndsWith(dir1, 'one')
372
self.assertEqual(cwd, osutils.getcwd())
374
dir2 = get_root(working_dir='two')
375
self.assertEndsWith(dir2, 'two')
376
self.assertEqual(cwd, osutils.getcwd())
379
class _DontSpawnProcess(Exception):
380
"""A simple exception which just allows us to skip unnecessary steps"""
383
class TestRunBzrSubprocessCommands(TestCaseWithTransport):
385
def _popen(self, *args, **kwargs):
386
"""Record the command that is run, so that we can ensure it is correct"""
387
self._popen_args = args
388
self._popen_kwargs = kwargs
389
raise _DontSpawnProcess()
391
def test_run_bzr_subprocess_no_plugins(self):
392
self.assertRaises(_DontSpawnProcess, self.run_bzr_subprocess, '')
393
command = self._popen_args[0]
394
self.assertEqual(sys.executable, command[0])
395
self.assertEqual(self.get_bzr_path(), command[1])
396
self.assertEqual(['--no-plugins'], command[2:])
398
def test_allow_plugins(self):
399
self.assertRaises(_DontSpawnProcess,
400
self.run_bzr_subprocess, '', allow_plugins=True)
401
command = self._popen_args[0]
402
self.assertEqual([], command[2:])
405
class TestBzrSubprocess(TestCaseWithTransport):
407
def test_start_and_stop_bzr_subprocess(self):
408
"""We can start and perform other test actions while that process is
411
process = self.start_bzr_subprocess(['--version'])
412
result = self.finish_bzr_subprocess(process)
413
self.assertContainsRe(result[0], 'is free software')
414
self.assertEqual('', result[1])
416
def test_start_and_stop_bzr_subprocess_with_error(self):
417
"""finish_bzr_subprocess allows specification of the desired exit code.
419
process = self.start_bzr_subprocess(['--versionn'])
420
result = self.finish_bzr_subprocess(process, retcode=3)
421
self.assertEqual('', result[0])
422
self.assertContainsRe(result[1], 'unknown command')
424
def test_start_and_stop_bzr_subprocess_ignoring_retcode(self):
425
"""finish_bzr_subprocess allows the exit code to be ignored."""
426
process = self.start_bzr_subprocess(['--versionn'])
427
result = self.finish_bzr_subprocess(process, retcode=None)
428
self.assertEqual('', result[0])
429
self.assertContainsRe(result[1], 'unknown command')
431
def test_start_and_stop_bzr_subprocess_with_unexpected_retcode(self):
432
"""finish_bzr_subprocess raises self.failureException if the retcode is
433
not the expected one.
435
process = self.start_bzr_subprocess(['--versionn'])
436
self.assertRaises(self.failureException, self.finish_bzr_subprocess,
439
def test_start_and_stop_bzr_subprocess_send_signal(self):
440
"""finish_bzr_subprocess raises self.failureException if the retcode is
441
not the expected one.
443
process = self.start_bzr_subprocess(['wait-until-signalled'],
444
skip_if_plan_to_signal=True)
445
self.assertEqual('running\n', process.stdout.readline())
446
result = self.finish_bzr_subprocess(process, send_signal=signal.SIGINT,
448
self.assertEqual('', result[0])
449
self.assertEqual('bzr: interrupted\n', result[1])
451
def test_start_and_stop_working_dir(self):
452
cwd = osutils.getcwd()
454
self.make_branch_and_tree('one')
456
process = self.start_bzr_subprocess(['root'], working_dir='one')
457
result = self.finish_bzr_subprocess(process, universal_newlines=True)
458
self.assertEndsWith(result[0], 'one\n')
459
self.assertEqual('', result[1])
176
462
class TestRunBzrError(ExternalBase):
178
464
def test_run_bzr_error(self):
179
out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
180
self.assertEqual(out, 'it sure does!\n')
182
out, err = self.run_bzr_error(["'foobarbaz' is not a versioned file"],
183
'file-id', 'foobarbaz')
465
# retcode=0 is specially needed here because run_bzr_error expects
466
# an error (oddly enough) but we want to test the case of not
467
# actually getting one
468
out, err = self.run_bzr_error(['^$'], ['rocks'], retcode=0)
469
self.assertEqual(out, 'It sure does!\n')
470
# now test actually getting an error
471
out, err = self.run_bzr_error(
472
["bzr: ERROR: foobarbaz is not versioned"],
473
['file-id', 'foobarbaz'])
476
class TestSelftestListOnly(TestCase):
479
def _parse_test_list(lines, newlines_in_header=1):
480
"Parse a list of lines into a tuple of 3 lists (header,body,footer)."
486
header_newlines_found = 0
490
header_newlines_found += 1
491
if header_newlines_found >= newlines_in_header:
496
if line.startswith('-------'):
502
# If the last body line is blank, drop it off the list
503
if len(body) > 0 and body[-1] == '':
505
return (header,body,footer)
507
def test_list_only(self):
508
# check that bzr selftest --list-only works correctly
509
out,err = self.run_bzr('selftest selftest --list-only')
510
self.assertEndsWith(err, 'tests passed\n')
511
(header,body,footer) = self._parse_test_list(out.splitlines())
512
num_tests = len(body)
513
self.assertContainsRe(footer[0], 'Listed %s tests in' % num_tests)
515
def test_list_only_filtered(self):
516
# check that a filtered --list-only works, both include and exclude
517
out_all,err_all = self.run_bzr('selftest --list-only')
518
tests_all = self._parse_test_list(out_all.splitlines())[1]
519
out_incl,err_incl = self.run_bzr('selftest --list-only selftest')
520
tests_incl = self._parse_test_list(out_incl.splitlines())[1]
521
self.assertSubset(tests_incl, tests_all)
522
out_excl,err_excl = self.run_bzr(['selftest', '--list-only',
523
'--exclude', 'selftest'])
524
tests_excl = self._parse_test_list(out_excl.splitlines())[1]
525
self.assertSubset(tests_excl, tests_all)
526
set_incl = set(tests_incl)
527
set_excl = set(tests_excl)
528
intersection = set_incl.intersection(set_excl)
529
self.assertEquals(0, len(intersection))
530
self.assertEquals(len(tests_all), len(tests_incl) + len(tests_excl))
532
def test_list_only_random(self):
533
# check that --randomize works correctly
534
out_all,err_all = self.run_bzr('selftest --list-only selftest')
535
tests_all = self._parse_test_list(out_all.splitlines())[1]
536
# XXX: It looks like there are some orders for generating tests that
537
# fail as of 20070504 - maybe because of import order dependencies.
538
# So unfortunately this will rarely intermittently fail at the moment.
540
out_rand,err_rand = self.run_bzr(['selftest', '--list-only',
541
'selftest', '--randomize', 'now'])
542
(header_rand,tests_rand,dummy) = self._parse_test_list(
543
out_rand.splitlines(), 2)
544
# XXX: The following line asserts that the randomized order is not the
545
# same as the default order. It is just possible that they'll get
546
# randomized into the same order and this will falsely fail, but
547
# that's very unlikely in practice because there are thousands of
549
self.assertNotEqual(tests_all, tests_rand)
550
self.assertEqual(sorted(tests_all), sorted(tests_rand))
551
# Check that the seed can be reused to get the exact same order
552
seed_re = re.compile('Randomizing test order using seed (\w+)')
553
match_obj = seed_re.search(header_rand[-1])
554
seed = match_obj.group(1)
555
out_rand2,err_rand2 = self.run_bzr(['selftest', '--list-only',
556
'selftest', '--randomize', seed])
557
(header_rand2,tests_rand2,dummy) = self._parse_test_list(
558
out_rand2.splitlines(), 2)
559
self.assertEqual(tests_rand, tests_rand2)