95
88
class TestRunBzr(ExternalBase):
97
def _run_bzr_core(self, argv, retcode=0, encoding=None, stdin=None,
90
def run_bzr_captured(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.
92
"""Override run_bzr_captured to test how it is invoked by run_bzr.
94
We test how run_bzr_captured actually invokes bzr in another location.
104
95
Here we only need to test that it is run_bzr passes the right
105
parameters to run_bzr.
96
parameters to run_bzr_captured.
107
self.argv = list(argv)
108
99
self.retcode = retcode
109
100
self.encoding = encoding
110
101
self.stdin = stdin
111
102
self.working_dir = working_dir
114
104
def test_args(self):
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)
105
"""Test that run_bzr passes args correctly to run_bzr_captured"""
106
self.run_bzr('arg1', 'arg2', 'arg3', retcode=1)
107
self.assertEqual(('arg1', 'arg2', 'arg3'), self.argv)
122
109
def test_encoding(self):
123
"""Test that run_bzr passes encoding to _run_bzr_core"""
124
self.run_bzr('foo bar')
110
"""Test that run_bzr passes encoding to run_bzr_captured"""
111
self.run_bzr('foo', 'bar')
125
112
self.assertEqual(None, self.encoding)
126
self.assertEqual(['foo', 'bar'], self.argv)
113
self.assertEqual(('foo', 'bar'), self.argv)
128
self.run_bzr('foo bar', encoding='baz')
115
self.run_bzr('foo', 'bar', encoding='baz')
129
116
self.assertEqual('baz', self.encoding)
130
self.assertEqual(['foo', 'bar'], self.argv)
117
self.assertEqual(('foo', 'bar'), self.argv)
132
119
def test_retcode(self):
133
"""Test that run_bzr passes retcode to _run_bzr_core"""
120
"""Test that run_bzr passes retcode to run_bzr_captured"""
134
121
# Default is retcode == 0
135
self.run_bzr('foo bar')
122
self.run_bzr('foo', 'bar')
136
123
self.assertEqual(0, self.retcode)
137
self.assertEqual(['foo', 'bar'], self.argv)
124
self.assertEqual(('foo', 'bar'), self.argv)
139
self.run_bzr('foo bar', retcode=1)
126
self.run_bzr('foo', 'bar', retcode=1)
140
127
self.assertEqual(1, self.retcode)
141
self.assertEqual(['foo', 'bar'], self.argv)
128
self.assertEqual(('foo', 'bar'), self.argv)
143
self.run_bzr('foo bar', retcode=None)
130
self.run_bzr('foo', 'bar', retcode=None)
144
131
self.assertEqual(None, self.retcode)
145
self.assertEqual(['foo', 'bar'], self.argv)
132
self.assertEqual(('foo', 'bar'), self.argv)
147
self.run_bzr(['foo', 'bar'], retcode=3)
134
self.run_bzr('foo', 'bar', retcode=3)
148
135
self.assertEqual(3, self.retcode)
149
self.assertEqual(['foo', 'bar'], self.argv)
136
self.assertEqual(('foo', 'bar'), self.argv)
151
138
def test_stdin(self):
152
139
# 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
140
# run_bzr_captured as-is. We do this by overriding
141
# run_bzr_captured in this class, and then calling run_bzr,
142
# which is a convenience function for run_bzr_captured, so
156
143
# should invoke it.
157
self.run_bzr('foo bar', stdin='gam')
144
self.run_bzr('foo', 'bar', stdin='gam')
158
145
self.assertEqual('gam', self.stdin)
159
self.assertEqual(['foo', 'bar'], self.argv)
146
self.assertEqual(('foo', 'bar'), self.argv)
161
self.run_bzr('foo bar', stdin='zippy')
148
self.run_bzr('foo', 'bar', stdin='zippy')
162
149
self.assertEqual('zippy', self.stdin)
163
self.assertEqual(['foo', 'bar'], self.argv)
150
self.assertEqual(('foo', 'bar'), self.argv)
165
152
def test_working_dir(self):
166
"""Test that run_bzr passes working_dir to _run_bzr_core"""
167
self.run_bzr('foo bar')
153
"""Test that run_bzr passes working_dir to run_bzr_captured"""
154
self.run_bzr('foo', 'bar')
168
155
self.assertEqual(None, self.working_dir)
169
self.assertEqual(['foo', 'bar'], self.argv)
156
self.assertEqual(('foo', 'bar'), self.argv)
171
self.run_bzr('foo bar', working_dir='baz')
158
self.run_bzr('foo', 'bar', working_dir='baz')
172
159
self.assertEqual('baz', self.working_dir)
173
self.assertEqual(['foo', 'bar'], self.argv)
160
self.assertEqual(('foo', 'bar'), self.argv)
176
163
class TestBenchmarkTests(TestCaseWithTransport):
214
200
def test_stdin(self):
215
# test that the stdin keyword to _run_bzr_core is passed through to
201
# test that the stdin keyword to run_bzr_captured is passed through to
216
202
# apply_redirected as a StringIO. We do this by overriding
217
# apply_redirected in this class, and then calling _run_bzr_core,
203
# apply_redirected in this class, and then calling run_bzr_captured,
218
204
# which calls apply_redirected.
219
self.run_bzr(['foo', 'bar'], stdin='gam')
205
self.run_bzr_captured(['foo', 'bar'], stdin='gam')
220
206
self.assertEqual('gam', self.stdin.read())
221
207
self.assertTrue(self.stdin is self.factory_stdin)
222
self.run_bzr(['foo', 'bar'], stdin='zippy')
208
self.run_bzr_captured(['foo', 'bar'], stdin='zippy')
223
209
self.assertEqual('zippy', self.stdin.read())
224
210
self.assertTrue(self.stdin is self.factory_stdin)
226
212
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
213
# each invocation of self.run_bzr_captured should get its own UI
214
# factory, which is an instance of TestUIFactory, with stdout and
215
# stderr attached to the stdout and stderr of the invoked
231
217
current_factory = bzrlib.ui.ui_factory
232
self.run_bzr(['foo'])
218
self.run_bzr_captured(['foo'])
233
219
self.failIf(current_factory is self.factory)
234
220
self.assertNotEqual(sys.stdout, self.factory.stdout)
235
221
self.assertNotEqual(sys.stderr, self.factory.stderr)
236
222
self.assertEqual('foo\n', self.factory.stdout.getvalue())
237
223
self.assertEqual('bar\n', self.factory.stderr.getvalue())
238
self.assertIsInstance(self.factory, TestUIFactory)
224
self.assertIsInstance(self.factory, bzrlib.tests.blackbox.TestUIFactory)
240
226
def test_working_dir(self):
241
227
self.build_tree(['one/', 'two/'])
242
228
cwd = osutils.getcwd()
244
230
# Default is to work in the current directory
245
self.run_bzr(['foo', 'bar'])
231
self.run_bzr_captured(['foo', 'bar'])
246
232
self.assertEqual(cwd, self.working_dir)
248
self.run_bzr(['foo', 'bar'], working_dir=None)
234
self.run_bzr_captured(['foo', 'bar'], working_dir=None)
249
235
self.assertEqual(cwd, self.working_dir)
251
237
# The function should be run in the alternative directory
252
238
# but afterwards the current working dir shouldn't be changed
253
self.run_bzr(['foo', 'bar'], working_dir='one')
239
self.run_bzr_captured(['foo', 'bar'], working_dir='one')
254
240
self.assertNotEqual(cwd, self.working_dir)
255
241
self.assertEndsWith(self.working_dir, 'one')
256
242
self.assertEqual(cwd, osutils.getcwd())
258
self.run_bzr(['foo', 'bar'], working_dir='two')
244
self.run_bzr_captured(['foo', 'bar'], working_dir='two')
259
245
self.assertNotEqual(cwd, self.working_dir)
260
246
self.assertEndsWith(self.working_dir, 'two')
261
247
self.assertEqual(cwd, osutils.getcwd())
453
438
self.make_branch_and_tree('one')
455
440
process = self.start_bzr_subprocess(['root'], working_dir='one')
456
result = self.finish_bzr_subprocess(process, universal_newlines=True)
441
result = self.finish_bzr_subprocess(process)
457
442
self.assertEndsWith(result[0], 'one\n')
458
443
self.assertEqual('', result[1])
461
446
class TestRunBzrError(ExternalBase):
463
448
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 TestSelftestCleanOutput(TestCaseInTempDir):
477
def test_clean_output(self):
478
# check that 'bzr selftest --clean-output' works correct
479
dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
480
files = ('bzr', 'setup.py', 'test9999.tmp')
485
f.write('content of ')
490
before = os.listdir(root)
492
self.assertEquals(['bzr','bzrlib','setup.py',
493
'test0000.tmp','test0001.tmp',
494
'test9999.tmp','tests'],
497
out, err = self.run_bzr('selftest --clean-output',
500
self.assertEquals(['delete directory: test0000.tmp',
501
'delete directory: test0001.tmp'],
502
sorted(out.splitlines()))
503
self.assertEquals('', err)
505
after = os.listdir(root)
507
self.assertEquals(['bzr','bzrlib','setup.py',
508
'test9999.tmp','tests'],
512
class TestSelftestListOnly(TestCase):
515
def _parse_test_list(lines, newlines_in_header=1):
516
"Parse a list of lines into a tuple of 3 lists (header,body,footer)."
523
header_newlines_found = 0
527
header_newlines_found += 1
528
if header_newlines_found >= newlines_in_header:
533
if line.startswith('-------'):
539
# If the last body line is blank, drop it off the list
540
if len(body) > 0 and body[-1] == '':
542
return (header,body,footer)
544
def test_list_only(self):
545
# check that bzr selftest --list-only works correctly
546
out,err = self.run_bzr('selftest selftest --list-only')
547
self.assertEndsWith(err, 'tests passed\n')
548
(header,body,footer) = self._parse_test_list(out.splitlines())
549
num_tests = len(body)
550
self.assertContainsRe(footer[0], 'Listed %s tests in' % num_tests)
552
def test_list_only_filtered(self):
553
# check that a filtered --list-only works, both include and exclude
554
out_all,err_all = self.run_bzr('selftest --list-only')
555
tests_all = self._parse_test_list(out_all.splitlines())[1]
556
out_incl,err_incl = self.run_bzr('selftest --list-only selftest')
557
tests_incl = self._parse_test_list(out_incl.splitlines())[1]
558
self.assertSubset(tests_incl, tests_all)
559
out_excl,err_excl = self.run_bzr(['selftest', '--list-only',
560
'--exclude', 'selftest'])
561
tests_excl = self._parse_test_list(out_excl.splitlines())[1]
562
self.assertSubset(tests_excl, tests_all)
563
set_incl = set(tests_incl)
564
set_excl = set(tests_excl)
565
intersection = set_incl.intersection(set_excl)
566
self.assertEquals(0, len(intersection))
567
self.assertEquals(len(tests_all), len(tests_incl) + len(tests_excl))
569
def test_list_only_random(self):
570
# check that --randomize works correctly
571
out_all,err_all = self.run_bzr('selftest --list-only selftest')
572
tests_all = self._parse_test_list(out_all.splitlines())[1]
573
# XXX: It looks like there are some orders for generating tests that
574
# fail as of 20070504 - maybe because of import order dependencies.
575
# So unfortunately this will rarely intermittently fail at the moment.
577
out_rand,err_rand = self.run_bzr(['selftest', '--list-only',
578
'selftest', '--randomize', 'now'])
579
(header_rand,tests_rand,dummy) = self._parse_test_list(
580
out_rand.splitlines(), 2)
581
self.assertNotEqual(tests_all, tests_rand)
582
self.assertEqual(sorted(tests_all), sorted(tests_rand))
583
# Check that the seed can be reused to get the exact same order
584
seed_re = re.compile('Randomizing test order using seed (\w+)')
585
match_obj = seed_re.search(header_rand[-1])
586
seed = match_obj.group(1)
587
out_rand2,err_rand2 = self.run_bzr(['selftest', '--list-only',
588
'selftest', '--randomize', seed])
589
(header_rand2,tests_rand2,dummy) = self._parse_test_list(
590
out_rand2.splitlines(), 2)
591
self.assertEqual(tests_rand, tests_rand2)
449
out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
450
self.assertEqual(out, 'it sure does!\n')
452
out, err = self.run_bzr_error(["bzr: ERROR: foobarbaz is not versioned"],
453
'file-id', 'foobarbaz')