89
92
class TestRunBzr(ExternalBase):
91
def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
94
def _run_bzr_core(self, argv, retcode=0, encoding=None, stdin=None,
93
"""Override run_bzr_captured to test how it is invoked by run_bzr.
95
We test how run_bzr_captured actually invokes bzr in another location.
96
"""Override _run_bzr_core to test how it is invoked by run_bzr.
98
Attempts to run bzr from inside this class don't actually run it.
100
We test how run_bzr actually invokes bzr in another location.
96
101
Here we only need to test that it is run_bzr passes the right
97
parameters to run_bzr_captured.
102
parameters to run_bzr.
104
self.argv = list(argv)
100
105
self.retcode = retcode
101
106
self.encoding = encoding
102
107
self.stdin = stdin
103
108
self.working_dir = working_dir
106
"""Test that run_bzr passes args correctly to run_bzr_captured"""
107
self.run_bzr('arg1', 'arg2', 'arg3', retcode=1)
108
self.assertEqual(('arg1', 'arg2', 'arg3'), self.argv)
110
111
def test_encoding(self):
111
"""Test that run_bzr passes encoding to run_bzr_captured"""
112
self.run_bzr('foo', 'bar')
112
"""Test that run_bzr passes encoding to _run_bzr_core"""
113
self.run_bzr('foo bar')
113
114
self.assertEqual(None, self.encoding)
114
self.assertEqual(('foo', 'bar'), self.argv)
115
self.assertEqual(['foo', 'bar'], self.argv)
116
self.run_bzr('foo', 'bar', encoding='baz')
117
self.run_bzr('foo bar', encoding='baz')
117
118
self.assertEqual('baz', self.encoding)
118
self.assertEqual(('foo', 'bar'), self.argv)
119
self.assertEqual(['foo', 'bar'], self.argv)
120
121
def test_retcode(self):
121
"""Test that run_bzr passes retcode to run_bzr_captured"""
122
"""Test that run_bzr passes retcode to _run_bzr_core"""
122
123
# Default is retcode == 0
123
self.run_bzr('foo', 'bar')
124
self.run_bzr('foo bar')
124
125
self.assertEqual(0, self.retcode)
125
self.assertEqual(('foo', 'bar'), self.argv)
126
self.assertEqual(['foo', 'bar'], self.argv)
127
self.run_bzr('foo', 'bar', retcode=1)
128
self.run_bzr('foo bar', retcode=1)
128
129
self.assertEqual(1, self.retcode)
129
self.assertEqual(('foo', 'bar'), self.argv)
130
self.assertEqual(['foo', 'bar'], self.argv)
131
self.run_bzr('foo', 'bar', retcode=None)
132
self.run_bzr('foo bar', retcode=None)
132
133
self.assertEqual(None, self.retcode)
133
self.assertEqual(('foo', 'bar'), self.argv)
134
self.assertEqual(['foo', 'bar'], self.argv)
135
self.run_bzr('foo', 'bar', retcode=3)
136
self.run_bzr(['foo', 'bar'], retcode=3)
136
137
self.assertEqual(3, self.retcode)
137
self.assertEqual(('foo', 'bar'), self.argv)
138
self.assertEqual(['foo', 'bar'], self.argv)
139
140
def test_stdin(self):
140
141
# test that the stdin keyword to run_bzr is passed through to
141
# run_bzr_captured as-is. We do this by overriding
142
# run_bzr_captured in this class, and then calling run_bzr,
143
# which is a convenience function for run_bzr_captured, so
142
# _run_bzr_core as-is. We do this by overriding
143
# _run_bzr_core in this class, and then calling run_bzr,
144
# which is a convenience function for _run_bzr_core, so
144
145
# should invoke it.
145
self.run_bzr('foo', 'bar', stdin='gam')
146
self.run_bzr('foo bar', stdin='gam')
146
147
self.assertEqual('gam', self.stdin)
147
self.assertEqual(('foo', 'bar'), self.argv)
148
self.assertEqual(['foo', 'bar'], self.argv)
149
self.run_bzr('foo', 'bar', stdin='zippy')
150
self.run_bzr('foo bar', stdin='zippy')
150
151
self.assertEqual('zippy', self.stdin)
151
self.assertEqual(('foo', 'bar'), self.argv)
152
self.assertEqual(['foo', 'bar'], self.argv)
153
154
def test_working_dir(self):
154
"""Test that run_bzr passes working_dir to run_bzr_captured"""
155
self.run_bzr('foo', 'bar')
155
"""Test that run_bzr passes working_dir to _run_bzr_core"""
156
self.run_bzr('foo bar')
156
157
self.assertEqual(None, self.working_dir)
157
self.assertEqual(('foo', 'bar'), self.argv)
158
self.assertEqual(['foo', 'bar'], self.argv)
159
self.run_bzr('foo', 'bar', working_dir='baz')
160
self.run_bzr('foo bar', working_dir='baz')
160
161
self.assertEqual('baz', self.working_dir)
161
self.assertEqual(('foo', 'bar'), self.argv)
162
self.assertEqual(['foo', 'bar'], self.argv)
164
def test_reject_extra_keyword_arguments(self):
165
self.assertRaises(TypeError, self.run_bzr, "foo bar",
166
error_regex=['error message'])
164
169
class TestBenchmarkTests(TestCaseWithTransport):
201
207
def test_stdin(self):
202
# test that the stdin keyword to run_bzr_captured is passed through to
208
# test that the stdin keyword to _run_bzr_core is passed through to
203
209
# apply_redirected as a StringIO. We do this by overriding
204
# apply_redirected in this class, and then calling run_bzr_captured,
210
# apply_redirected in this class, and then calling _run_bzr_core,
205
211
# which calls apply_redirected.
206
self.run_bzr_captured(['foo', 'bar'], stdin='gam')
212
self.run_bzr(['foo', 'bar'], stdin='gam')
207
213
self.assertEqual('gam', self.stdin.read())
208
214
self.assertTrue(self.stdin is self.factory_stdin)
209
self.run_bzr_captured(['foo', 'bar'], stdin='zippy')
215
self.run_bzr(['foo', 'bar'], stdin='zippy')
210
216
self.assertEqual('zippy', self.stdin.read())
211
217
self.assertTrue(self.stdin is self.factory_stdin)
213
219
def test_ui_factory(self):
214
# each invocation of self.run_bzr_captured should get its own UI
215
# factory, which is an instance of TestUIFactory, with stdout and
216
# stderr attached to the stdout and stderr of the invoked
220
# each invocation of self.run_bzr should get its
221
# own UI factory, which is an instance of TestUIFactory,
222
# with stdin, stdout and stderr attached to the stdin,
223
# stdout and stderr of the invoked run_bzr
218
224
current_factory = bzrlib.ui.ui_factory
219
self.run_bzr_captured(['foo'])
225
self.run_bzr(['foo'])
220
226
self.failIf(current_factory is self.factory)
221
227
self.assertNotEqual(sys.stdout, self.factory.stdout)
222
228
self.assertNotEqual(sys.stderr, self.factory.stderr)
223
229
self.assertEqual('foo\n', self.factory.stdout.getvalue())
224
230
self.assertEqual('bar\n', self.factory.stderr.getvalue())
225
self.assertIsInstance(self.factory, bzrlib.tests.blackbox.TestUIFactory)
231
self.assertIsInstance(self.factory, TestUIFactory)
227
233
def test_working_dir(self):
228
234
self.build_tree(['one/', 'two/'])
229
235
cwd = osutils.getcwd()
231
237
# Default is to work in the current directory
232
self.run_bzr_captured(['foo', 'bar'])
238
self.run_bzr(['foo', 'bar'])
233
239
self.assertEqual(cwd, self.working_dir)
235
self.run_bzr_captured(['foo', 'bar'], working_dir=None)
241
self.run_bzr(['foo', 'bar'], working_dir=None)
236
242
self.assertEqual(cwd, self.working_dir)
238
244
# The function should be run in the alternative directory
239
245
# but afterwards the current working dir shouldn't be changed
240
self.run_bzr_captured(['foo', 'bar'], working_dir='one')
246
self.run_bzr(['foo', 'bar'], working_dir='one')
241
247
self.assertNotEqual(cwd, self.working_dir)
242
248
self.assertEndsWith(self.working_dir, 'one')
243
249
self.assertEqual(cwd, osutils.getcwd())
245
self.run_bzr_captured(['foo', 'bar'], working_dir='two')
251
self.run_bzr(['foo', 'bar'], working_dir='two')
246
252
self.assertNotEqual(cwd, self.working_dir)
247
253
self.assertEndsWith(self.working_dir, 'two')
248
254
self.assertEqual(cwd, osutils.getcwd())
448
455
class TestRunBzrError(ExternalBase):
450
457
def test_run_bzr_error(self):
451
out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
452
self.assertEqual(out, 'it sure does!\n')
454
out, err = self.run_bzr_error(["bzr: ERROR: foobarbaz is not versioned"],
455
'file-id', 'foobarbaz')
458
class TestSelftestCleanOutput(TestCaseInTempDir):
460
def test_clean_output(self):
461
# check that 'bzr selftest --clean-output' works correct
462
dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
463
files = ('bzr', 'setup.py', 'test9999.tmp')
468
f.write('content of ')
473
before = os.listdir(root)
475
self.assertEquals(['bzr','bzrlib','setup.py',
476
'test0000.tmp','test0001.tmp',
477
'test9999.tmp','tests'],
480
out,err = self.run_bzr_captured(['selftest','--clean-output'],
483
self.assertEquals(['delete directory: test0000.tmp',
484
'delete directory: test0001.tmp'],
485
sorted(out.splitlines()))
486
self.assertEquals('', err)
488
after = os.listdir(root)
490
self.assertEquals(['bzr','bzrlib','setup.py',
491
'test9999.tmp','tests'],
458
# retcode=0 is specially needed here because run_bzr_error expects
459
# an error (oddly enough) but we want to test the case of not
460
# actually getting one
461
out, err = self.run_bzr_error(['^$'], ['rocks'], retcode=0)
462
self.assertEqual(out, 'It sure does!\n')
463
# now test actually getting an error
464
out, err = self.run_bzr_error(
465
["bzr: ERROR: foobarbaz is not versioned"],
466
['file-id', 'foobarbaz'])
469
class TestSelftestListOnly(TestCase):
472
def _parse_test_list(lines, newlines_in_header=1):
473
"Parse a list of lines into a tuple of 3 lists (header,body,footer)."
479
header_newlines_found = 0
483
header_newlines_found += 1
484
if header_newlines_found >= newlines_in_header:
489
if line.startswith('-------'):
495
# If the last body line is blank, drop it off the list
496
if len(body) > 0 and body[-1] == '':
498
return (header,body,footer)
500
def test_list_only(self):
501
# check that bzr selftest --list-only works correctly
502
out,err = self.run_bzr('selftest selftest --list-only')
503
self.assertEndsWith(err, 'tests passed\n')
504
(header,body,footer) = self._parse_test_list(out.splitlines())
505
num_tests = len(body)
506
self.assertContainsRe(footer[0], 'Listed %s tests in' % num_tests)
508
def test_list_only_filtered(self):
509
# check that a filtered --list-only works, both include and exclude
510
out_all,err_all = self.run_bzr('selftest --list-only')
511
tests_all = self._parse_test_list(out_all.splitlines())[1]
512
out_incl,err_incl = self.run_bzr('selftest --list-only selftest')
513
tests_incl = self._parse_test_list(out_incl.splitlines())[1]
514
self.assertSubset(tests_incl, tests_all)
515
out_excl,err_excl = self.run_bzr(['selftest', '--list-only',
516
'--exclude', 'selftest'])
517
tests_excl = self._parse_test_list(out_excl.splitlines())[1]
518
self.assertSubset(tests_excl, tests_all)
519
set_incl = set(tests_incl)
520
set_excl = set(tests_excl)
521
intersection = set_incl.intersection(set_excl)
522
self.assertEquals(0, len(intersection))
523
self.assertEquals(len(tests_all), len(tests_incl) + len(tests_excl))
525
def test_list_only_random(self):
526
# check that --randomize works correctly
527
out_all,err_all = self.run_bzr('selftest --list-only selftest')
528
tests_all = self._parse_test_list(out_all.splitlines())[1]
529
# XXX: It looks like there are some orders for generating tests that
530
# fail as of 20070504 - maybe because of import order dependencies.
531
# So unfortunately this will rarely intermittently fail at the moment.
533
out_rand,err_rand = self.run_bzr(['selftest', '--list-only',
534
'selftest', '--randomize', 'now'])
535
(header_rand,tests_rand,dummy) = self._parse_test_list(
536
out_rand.splitlines(), 2)
537
# XXX: The following line asserts that the randomized order is not the
538
# same as the default order. It is just possible that they'll get
539
# randomized into the same order and this will falsely fail, but
540
# that's very unlikely in practice because there are thousands of
542
self.assertNotEqual(tests_all, tests_rand)
543
self.assertEqual(sorted(tests_all), sorted(tests_rand))
544
# Check that the seed can be reused to get the exact same order
545
seed_re = re.compile('Randomizing test order using seed (\w+)')
546
match_obj = seed_re.search(header_rand[-1])
547
seed = match_obj.group(1)
548
out_rand2,err_rand2 = self.run_bzr(['selftest', '--list-only',
549
'selftest', '--randomize', seed])
550
(header_rand2,tests_rand2,dummy) = self._parse_test_list(
551
out_rand2.splitlines(), 2)
552
self.assertEqual(tests_rand, tests_rand2)
555
class TestSelftestWithIdList(TestCaseInTempDir):
557
def test_load_list(self):
558
# We don't want to call selftest for the whole suite, so we start with
560
test_list_fname = 'test.list'
561
fl = open(test_list_fname, 'wt')
562
fl.write('%s\n' % self.id())
564
out, err = self.run_bzr(
565
['selftest', '--load-list', test_list_fname, '--list'])
566
self.assertContainsRe(out, "Listed 1 test in")
568
def test_load_unknown(self):
569
out, err = self.run_bzr('selftest --load-list I_do_not_exist ',
573
class TestSelftestStartingWith(TestCase):
575
def test_starting_with_single_argument(self):
576
out, err = self.run_bzr(
577
['selftest', '--starting-with', self.id(), '--list'])
578
self.assertContainsRe(out, "Listed 1 test in")
579
self.assertContainsRe(out, self.id())
581
def test_starting_with_multiple_argument(self):
582
out, err = self.run_bzr(
584
'--starting-with', self.id(),
585
'--starting-with', 'bzrlib.tests.test_sampler',
587
self.assertContainsRe(out, "Listed 2 tests in")
588
self.assertContainsRe(out, self.id())
589
self.assertContainsRe(out, 'bzrlib.tests.test_sampler')