72
59
except ParamikoNotPresent:
73
60
raise TestSkipped("Paramiko not present")
74
61
old_transport = bzrlib.tests.default_transport
75
old_root = TestCaseWithMemoryTransport.TEST_ROOT
76
TestCaseWithMemoryTransport.TEST_ROOT = None
62
old_root = TestCaseInTempDir.TEST_ROOT
63
TestCaseInTempDir.TEST_ROOT = None
78
65
TestOptions.current_test = "test_transport_set_to_sftp"
79
stdout = self.run_bzr(
80
'selftest --transport=sftp test_transport_set_to_sftp')[0]
66
stdout = self.capture('selftest --transport=sftp test_transport_set_to_sftp')
81
68
self.assertContainsRe(stdout, 'Ran 1 test')
82
69
self.assertEqual(old_transport, bzrlib.tests.default_transport)
84
71
TestOptions.current_test = "test_transport_set_to_memory"
85
stdout = self.run_bzr(
86
'selftest --transport=memory test_transport_set_to_memory')[0]
72
stdout = self.capture('selftest --transport=memory test_transport_set_to_memory')
87
73
self.assertContainsRe(stdout, 'Ran 1 test')
88
74
self.assertEqual(old_transport, bzrlib.tests.default_transport)
90
76
bzrlib.tests.default_transport = old_transport
91
77
TestOptions.current_test = None
92
TestCaseWithMemoryTransport.TEST_ROOT = old_root
95
class TestRunBzr(ExternalBase):
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
111
self.working_dir = working_dir
115
"""Test that run_bzr passes args correctly to _run_bzr_core"""
117
['passing varargs to run_bzr was deprecated in version 0.18.'],
119
'arg1', 'arg2', 'arg3', retcode=1)
120
self.assertEqual(['arg1', 'arg2', 'arg3'], self.argv)
122
def test_encoding(self):
123
"""Test that run_bzr passes encoding to _run_bzr_core"""
124
self.run_bzr('foo bar')
125
self.assertEqual(None, self.encoding)
126
self.assertEqual(['foo', 'bar'], self.argv)
128
self.run_bzr('foo bar', encoding='baz')
129
self.assertEqual('baz', self.encoding)
130
self.assertEqual(['foo', 'bar'], self.argv)
132
def test_retcode(self):
133
"""Test that run_bzr passes retcode to _run_bzr_core"""
134
# Default is retcode == 0
135
self.run_bzr('foo bar')
136
self.assertEqual(0, self.retcode)
137
self.assertEqual(['foo', 'bar'], self.argv)
139
self.run_bzr('foo bar', retcode=1)
140
self.assertEqual(1, self.retcode)
141
self.assertEqual(['foo', 'bar'], self.argv)
143
self.run_bzr('foo bar', retcode=None)
144
self.assertEqual(None, self.retcode)
145
self.assertEqual(['foo', 'bar'], self.argv)
147
self.run_bzr(['foo', 'bar'], retcode=3)
148
self.assertEqual(3, self.retcode)
149
self.assertEqual(['foo', 'bar'], self.argv)
151
def test_stdin(self):
152
# test that the stdin keyword to run_bzr is passed through to
153
# _run_bzr_core as-is. We do this by overriding
154
# _run_bzr_core in this class, and then calling run_bzr,
155
# which is a convenience function for _run_bzr_core, so
157
self.run_bzr('foo bar', stdin='gam')
158
self.assertEqual('gam', self.stdin)
159
self.assertEqual(['foo', 'bar'], self.argv)
161
self.run_bzr('foo bar', stdin='zippy')
162
self.assertEqual('zippy', self.stdin)
163
self.assertEqual(['foo', 'bar'], self.argv)
165
def test_working_dir(self):
166
"""Test that run_bzr passes working_dir to _run_bzr_core"""
167
self.run_bzr('foo bar')
168
self.assertEqual(None, self.working_dir)
169
self.assertEqual(['foo', 'bar'], self.argv)
171
self.run_bzr('foo bar', working_dir='baz')
172
self.assertEqual('baz', self.working_dir)
173
self.assertEqual(['foo', 'bar'], self.argv)
176
class TestBenchmarkTests(TestCaseWithTransport):
78
TestCaseInTempDir.TEST_ROOT = old_root
178
80
def test_benchmark_runs_benchmark_tests(self):
179
81
"""bzr selftest --benchmark should not run the default test suite."""
180
82
# We test this by passing a regression test name to --benchmark, which
181
83
# should result in 0 rests run.
182
old_root = TestCaseWithMemoryTransport.TEST_ROOT
184
TestCaseWithMemoryTransport.TEST_ROOT = None
185
out, err = self.run_bzr('selftest --benchmark'
186
' workingtree_implementations')
188
TestCaseWithMemoryTransport.TEST_ROOT = old_root
84
out, err = self.run_bzr('selftest', '--benchmark', 'workingtree_implementations')
189
85
self.assertContainsRe(out, 'Ran 0 tests.*\n\nOK')
87
'running tests...\nRunning tests: .\nCleaning up: .\ntests passed\n',
193
benchfile = open(".perf_history", "rt")
195
lines = benchfile.readlines()
198
self.assertEqual(1, len(lines))
199
self.assertContainsRe(lines[0], "--date [0-9.]+")
91
class TestRunBzr(ExternalBase):
93
def run_bzr_captured(self, argv, retcode=0, stdin=None):
97
# test that the stdin keyword to run_bzr is passed through to
98
# run_bzr_captured as-is. We do this by overriding
99
# run_bzr_captured in this class, and then calling run_bzr,
100
# which is a convenience function for run_bzr_captured, so
102
self.run_bzr('foo', 'bar', stdin='gam')
103
self.assertEqual('gam', self.stdin)
104
self.run_bzr('foo', 'bar', stdin='zippy')
105
self.assertEqual('zippy', self.stdin)
202
108
class TestRunBzrCaptured(ExternalBase):
206
112
self.stdin = stdin
207
113
self.factory_stdin = getattr(bzrlib.ui.ui_factory, "stdin", None)
208
114
self.factory = bzrlib.ui.ui_factory
209
self.working_dir = osutils.getcwd()
210
115
stdout.write('foo\n')
211
116
stderr.write('bar\n')
214
119
def test_stdin(self):
215
# test that the stdin keyword to _run_bzr_core is passed through to
120
# test that the stdin keyword to run_bzr_captured is passed through to
216
121
# apply_redirected as a StringIO. We do this by overriding
217
# apply_redirected in this class, and then calling _run_bzr_core,
122
# apply_redirected in this class, and then calling run_bzr_captured,
218
123
# which calls apply_redirected.
219
self.run_bzr(['foo', 'bar'], stdin='gam')
124
self.run_bzr_captured(['foo', 'bar'], stdin='gam')
220
125
self.assertEqual('gam', self.stdin.read())
221
126
self.assertTrue(self.stdin is self.factory_stdin)
222
self.run_bzr(['foo', 'bar'], stdin='zippy')
127
self.run_bzr_captured(['foo', 'bar'], stdin='zippy')
223
128
self.assertEqual('zippy', self.stdin.read())
224
129
self.assertTrue(self.stdin is self.factory_stdin)
226
131
def test_ui_factory(self):
227
# each invocation of self.run_bzr should get its
228
# own UI factory, which is an instance of TestUIFactory,
229
# with stdin, stdout and stderr attached to the stdin,
230
# stdout and stderr of the invoked run_bzr
132
# each invocation of self.run_bzr_captured should get its own UI
133
# factory, which is an instance of TestUIFactory, with stdout and
134
# stderr attached to the stdout and stderr of the invoked
231
136
current_factory = bzrlib.ui.ui_factory
232
self.run_bzr(['foo'])
137
self.run_bzr_captured(['foo'])
233
138
self.failIf(current_factory is self.factory)
234
139
self.assertNotEqual(sys.stdout, self.factory.stdout)
235
140
self.assertNotEqual(sys.stderr, self.factory.stderr)
236
141
self.assertEqual('foo\n', self.factory.stdout.getvalue())
237
142
self.assertEqual('bar\n', self.factory.stderr.getvalue())
238
self.assertIsInstance(self.factory, TestUIFactory)
240
def test_working_dir(self):
241
self.build_tree(['one/', 'two/'])
242
cwd = osutils.getcwd()
244
# Default is to work in the current directory
245
self.run_bzr(['foo', 'bar'])
246
self.assertEqual(cwd, self.working_dir)
248
self.run_bzr(['foo', 'bar'], working_dir=None)
249
self.assertEqual(cwd, self.working_dir)
251
# The function should be run in the alternative directory
252
# but afterwards the current working dir shouldn't be changed
253
self.run_bzr(['foo', 'bar'], working_dir='one')
254
self.assertNotEqual(cwd, self.working_dir)
255
self.assertEndsWith(self.working_dir, 'one')
256
self.assertEqual(cwd, osutils.getcwd())
258
self.run_bzr(['foo', 'bar'], working_dir='two')
259
self.assertNotEqual(cwd, self.working_dir)
260
self.assertEndsWith(self.working_dir, 'two')
261
self.assertEqual(cwd, osutils.getcwd())
264
class TestRunBzrSubprocess(TestCaseWithTransport):
266
def test_run_bzr_subprocess(self):
267
"""The run_bzr_helper_external comand behaves nicely."""
268
result = self.run_bzr_subprocess('--version')
269
result = self.run_bzr_subprocess('--version', retcode=None)
270
self.assertContainsRe(result[0], 'is free software')
271
self.assertRaises(AssertionError, self.run_bzr_subprocess,
273
result = self.run_bzr_subprocess('--versionn', retcode=3)
274
result = self.run_bzr_subprocess('--versionn', retcode=None)
275
self.assertContainsRe(result[1], 'unknown command')
276
err = self.run_bzr_subprocess('merge', '--merge-type', 'magic merge',
278
self.assertContainsRe(err, 'Bad value "magic merge" for option'
281
def test_run_bzr_subprocess_env(self):
282
"""run_bzr_subprocess can set environment variables in the child only.
284
These changes should not change the running process, only the child.
286
# The test suite should unset this variable
287
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
288
out, err = self.run_bzr_subprocess('whoami', env_changes={
289
'BZR_EMAIL':'Joe Foo <joe@foo.com>'
290
}, universal_newlines=True)
291
self.assertEqual('', err)
292
self.assertEqual('Joe Foo <joe@foo.com>\n', out)
293
# And it should not be modified
294
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
296
# Do it again with a different address, just to make sure
297
# it is actually changing
298
out, err = self.run_bzr_subprocess('whoami', env_changes={
299
'BZR_EMAIL':'Barry <bar@foo.com>'
300
}, universal_newlines=True)
301
self.assertEqual('', err)
302
self.assertEqual('Barry <bar@foo.com>\n', out)
303
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
305
def test_run_bzr_subprocess_env_del(self):
306
"""run_bzr_subprocess can remove environment variables too."""
307
# Create a random email, so we are sure this won't collide
308
rand_bzr_email = 'John Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
309
rand_email = 'Jane Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
310
os.environ['BZR_EMAIL'] = rand_bzr_email
311
os.environ['EMAIL'] = rand_email
313
# By default, the child will inherit the current env setting
314
out, err = self.run_bzr_subprocess('whoami', universal_newlines=True)
315
self.assertEqual('', err)
316
self.assertEqual(rand_bzr_email + '\n', out)
318
# Now that BZR_EMAIL is not set, it should fall back to EMAIL
319
out, err = self.run_bzr_subprocess('whoami',
320
env_changes={'BZR_EMAIL':None},
321
universal_newlines=True)
322
self.assertEqual('', err)
323
self.assertEqual(rand_email + '\n', out)
325
# This switches back to the default email guessing logic
326
# Which shouldn't match either of the above addresses
327
out, err = self.run_bzr_subprocess('whoami',
328
env_changes={'BZR_EMAIL':None, 'EMAIL':None},
329
universal_newlines=True)
331
self.assertEqual('', err)
332
self.assertNotEqual(rand_bzr_email + '\n', out)
333
self.assertNotEqual(rand_email + '\n', out)
335
# TestCase cleans up BZR_EMAIL, and EMAIL at startup
336
del os.environ['BZR_EMAIL']
337
del os.environ['EMAIL']
339
def test_run_bzr_subprocess_env_del_missing(self):
340
"""run_bzr_subprocess won't fail if deleting a nonexistant env var"""
341
self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
342
out, err = self.run_bzr_subprocess('rocks',
343
env_changes={'NON_EXISTANT_ENV_VAR':None},
344
universal_newlines=True)
345
self.assertEqual('It sure does!\n', out)
346
self.assertEqual('', err)
348
def test_run_bzr_subprocess_working_dir(self):
349
"""Test that we can specify the working dir for the child"""
350
cwd = osutils.getcwd()
352
self.make_branch_and_tree('.')
353
self.make_branch_and_tree('one')
354
self.make_branch_and_tree('two')
356
def get_root(**kwargs):
357
"""Spawn a process to get the 'root' of the tree.
359
You can pass in arbitrary new arguments. This just makes
360
sure that the returned path doesn't have trailing whitespace.
362
return self.run_bzr_subprocess('root', **kwargs)[0].rstrip()
364
self.assertEqual(cwd, get_root())
365
self.assertEqual(cwd, get_root(working_dir=None))
366
# Has our path changed?
367
self.assertEqual(cwd, osutils.getcwd())
369
dir1 = get_root(working_dir='one')
370
self.assertEndsWith(dir1, 'one')
371
self.assertEqual(cwd, osutils.getcwd())
373
dir2 = get_root(working_dir='two')
374
self.assertEndsWith(dir2, 'two')
375
self.assertEqual(cwd, osutils.getcwd())
378
class _DontSpawnProcess(Exception):
379
"""A simple exception which just allows us to skip unnecessary steps"""
382
class TestRunBzrSubprocessCommands(TestCaseWithTransport):
384
def _popen(self, *args, **kwargs):
385
"""Record the command that is run, so that we can ensure it is correct"""
386
self._popen_args = args
387
self._popen_kwargs = kwargs
388
raise _DontSpawnProcess()
390
def test_run_bzr_subprocess_no_plugins(self):
391
self.assertRaises(_DontSpawnProcess, self.run_bzr_subprocess)
392
command = self._popen_args[0]
393
self.assertEqual(sys.executable, command[0])
394
self.assertEqual(self.get_bzr_path(), command[1])
395
self.assertEqual(['--no-plugins'], command[2:])
397
def test_allow_plugins(self):
398
self.assertRaises(_DontSpawnProcess,
399
self.run_bzr_subprocess, allow_plugins=True)
400
command = self._popen_args[0]
401
self.assertEqual([], command[2:])
404
class TestBzrSubprocess(TestCaseWithTransport):
406
def test_start_and_stop_bzr_subprocess(self):
407
"""We can start and perform other test actions while that process is
410
process = self.start_bzr_subprocess(['--version'])
411
result = self.finish_bzr_subprocess(process)
412
self.assertContainsRe(result[0], 'is free software')
413
self.assertEqual('', result[1])
415
def test_start_and_stop_bzr_subprocess_with_error(self):
416
"""finish_bzr_subprocess allows specification of the desired exit code.
418
process = self.start_bzr_subprocess(['--versionn'])
419
result = self.finish_bzr_subprocess(process, retcode=3)
420
self.assertEqual('', result[0])
421
self.assertContainsRe(result[1], 'unknown command')
423
def test_start_and_stop_bzr_subprocess_ignoring_retcode(self):
424
"""finish_bzr_subprocess allows the exit code to be ignored."""
425
process = self.start_bzr_subprocess(['--versionn'])
426
result = self.finish_bzr_subprocess(process, retcode=None)
427
self.assertEqual('', result[0])
428
self.assertContainsRe(result[1], 'unknown command')
430
def test_start_and_stop_bzr_subprocess_with_unexpected_retcode(self):
431
"""finish_bzr_subprocess raises self.failureException if the retcode is
432
not the expected one.
434
process = self.start_bzr_subprocess(['--versionn'])
435
self.assertRaises(self.failureException, self.finish_bzr_subprocess,
438
def test_start_and_stop_bzr_subprocess_send_signal(self):
439
"""finish_bzr_subprocess raises self.failureException if the retcode is
440
not the expected one.
442
process = self.start_bzr_subprocess(['wait-until-signalled'],
443
skip_if_plan_to_signal=True)
444
self.assertEqual('running\n', process.stdout.readline())
445
result = self.finish_bzr_subprocess(process, send_signal=signal.SIGINT,
447
self.assertEqual('', result[0])
448
self.assertEqual('bzr: interrupted\n', result[1])
450
def test_start_and_stop_working_dir(self):
451
cwd = osutils.getcwd()
453
self.make_branch_and_tree('one')
455
process = self.start_bzr_subprocess(['root'], working_dir='one')
456
result = self.finish_bzr_subprocess(process, universal_newlines=True)
457
self.assertEndsWith(result[0], 'one\n')
458
self.assertEqual('', result[1])
461
class TestRunBzrError(ExternalBase):
463
def test_run_bzr_error(self):
464
# retcode=0 is specially needed here because run_bzr_error expects
465
# an error (oddly enough) but we want to test the case of not
466
# actually getting one
467
out, err = self.run_bzr_error(['^$'], ['rocks'], retcode=0)
468
self.assertEqual(out, 'It sure does!\n')
469
# now test actually getting an error
470
out, err = self.run_bzr_error(
471
["bzr: ERROR: foobarbaz is not versioned"],
472
['file-id', 'foobarbaz'])
475
class TestSelftestListOnly(TestCase):
478
def _parse_test_list(lines, newlines_in_header=1):
479
"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
self.assertNotEqual(tests_all, tests_rand)
545
self.assertEqual(sorted(tests_all), sorted(tests_rand))
546
# Check that the seed can be reused to get the exact same order
547
seed_re = re.compile('Randomizing test order using seed (\w+)')
548
match_obj = seed_re.search(header_rand[-1])
549
seed = match_obj.group(1)
550
out_rand2,err_rand2 = self.run_bzr(['selftest', '--list-only',
551
'selftest', '--randomize', seed])
552
(header_rand2,tests_rand2,dummy) = self._parse_test_list(
553
out_rand2.splitlines(), 2)
554
self.assertEqual(tests_rand, tests_rand2)
143
self.assertIsInstance(self.factory, bzrlib.tests.blackbox.TestUIFactory)