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