91
95
class TestRunBzr(ExternalBase):
93
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,
95
"""Override run_bzr_captured to test how it is invoked by run_bzr.
97
We test how run_bzr_captured actually invokes bzr in another location.
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.
98
104
Here we only need to test that it is run_bzr passes the right
99
parameters to run_bzr_captured.
105
parameters to run_bzr.
107
self.argv = list(argv)
102
108
self.retcode = retcode
103
109
self.encoding = encoding
104
110
self.stdin = stdin
105
111
self.working_dir = working_dir
109
"""Test that run_bzr passes args correctly to run_bzr_captured"""
110
self.run_bzr('arg1', 'arg2', 'arg3', retcode=1)
111
self.assertEqual(('arg1', 'arg2', 'arg3'), self.argv)
113
114
def test_encoding(self):
114
"""Test that run_bzr passes encoding to run_bzr_captured"""
115
self.run_bzr('foo', 'bar')
115
"""Test that run_bzr passes encoding to _run_bzr_core"""
116
self.run_bzr('foo bar')
116
117
self.assertEqual(None, self.encoding)
117
self.assertEqual(('foo', 'bar'), self.argv)
118
self.assertEqual(['foo', 'bar'], self.argv)
119
self.run_bzr('foo', 'bar', encoding='baz')
120
self.run_bzr('foo bar', encoding='baz')
120
121
self.assertEqual('baz', self.encoding)
121
self.assertEqual(('foo', 'bar'), self.argv)
122
self.assertEqual(['foo', 'bar'], self.argv)
123
124
def test_retcode(self):
124
"""Test that run_bzr passes retcode to run_bzr_captured"""
125
"""Test that run_bzr passes retcode to _run_bzr_core"""
125
126
# Default is retcode == 0
126
self.run_bzr('foo', 'bar')
127
self.run_bzr('foo bar')
127
128
self.assertEqual(0, self.retcode)
128
self.assertEqual(('foo', 'bar'), self.argv)
129
self.assertEqual(['foo', 'bar'], self.argv)
130
self.run_bzr('foo', 'bar', retcode=1)
131
self.run_bzr('foo bar', retcode=1)
131
132
self.assertEqual(1, self.retcode)
132
self.assertEqual(('foo', 'bar'), self.argv)
133
self.assertEqual(['foo', 'bar'], self.argv)
134
self.run_bzr('foo', 'bar', retcode=None)
135
self.run_bzr('foo bar', retcode=None)
135
136
self.assertEqual(None, self.retcode)
136
self.assertEqual(('foo', 'bar'), self.argv)
137
self.assertEqual(['foo', 'bar'], self.argv)
138
self.run_bzr('foo', 'bar', retcode=3)
139
self.run_bzr(['foo', 'bar'], retcode=3)
139
140
self.assertEqual(3, self.retcode)
140
self.assertEqual(('foo', 'bar'), self.argv)
141
self.assertEqual(['foo', 'bar'], self.argv)
142
143
def test_stdin(self):
143
144
# test that the stdin keyword to run_bzr is passed through to
144
# run_bzr_captured as-is. We do this by overriding
145
# run_bzr_captured in this class, and then calling run_bzr,
146
# 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
147
148
# should invoke it.
148
self.run_bzr('foo', 'bar', stdin='gam')
149
self.run_bzr('foo bar', stdin='gam')
149
150
self.assertEqual('gam', self.stdin)
150
self.assertEqual(('foo', 'bar'), self.argv)
151
self.assertEqual(['foo', 'bar'], self.argv)
152
self.run_bzr('foo', 'bar', stdin='zippy')
153
self.run_bzr('foo bar', stdin='zippy')
153
154
self.assertEqual('zippy', self.stdin)
154
self.assertEqual(('foo', 'bar'), self.argv)
155
self.assertEqual(['foo', 'bar'], self.argv)
156
157
def test_working_dir(self):
157
"""Test that run_bzr passes working_dir to run_bzr_captured"""
158
self.run_bzr('foo', 'bar')
158
"""Test that run_bzr passes working_dir to _run_bzr_core"""
159
self.run_bzr('foo bar')
159
160
self.assertEqual(None, self.working_dir)
160
self.assertEqual(('foo', 'bar'), self.argv)
161
self.assertEqual(['foo', 'bar'], self.argv)
162
self.run_bzr('foo', 'bar', working_dir='baz')
163
self.run_bzr('foo bar', working_dir='baz')
163
164
self.assertEqual('baz', self.working_dir)
164
self.assertEqual(('foo', 'bar'), self.argv)
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'])
167
172
class TestBenchmarkTests(TestCaseWithTransport):
204
210
def test_stdin(self):
205
# 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
206
212
# apply_redirected as a StringIO. We do this by overriding
207
# apply_redirected in this class, and then calling run_bzr_captured,
213
# apply_redirected in this class, and then calling _run_bzr_core,
208
214
# which calls apply_redirected.
209
self.run_bzr_captured(['foo', 'bar'], stdin='gam')
215
self.run_bzr(['foo', 'bar'], stdin='gam')
210
216
self.assertEqual('gam', self.stdin.read())
211
217
self.assertTrue(self.stdin is self.factory_stdin)
212
self.run_bzr_captured(['foo', 'bar'], stdin='zippy')
218
self.run_bzr(['foo', 'bar'], stdin='zippy')
213
219
self.assertEqual('zippy', self.stdin.read())
214
220
self.assertTrue(self.stdin is self.factory_stdin)
216
222
def test_ui_factory(self):
217
# each invocation of self.run_bzr_captured should get its
223
# each invocation of self.run_bzr should get its
218
224
# own UI factory, which is an instance of TestUIFactory,
219
225
# with stdin, stdout and stderr attached to the stdin,
220
# stdout and stderr of the invoked run_bzr_captured
226
# stdout and stderr of the invoked run_bzr
221
227
current_factory = bzrlib.ui.ui_factory
222
self.run_bzr_captured(['foo'])
228
self.run_bzr(['foo'])
223
229
self.failIf(current_factory is self.factory)
224
230
self.assertNotEqual(sys.stdout, self.factory.stdout)
225
231
self.assertNotEqual(sys.stderr, self.factory.stderr)
232
238
cwd = osutils.getcwd()
234
240
# Default is to work in the current directory
235
self.run_bzr_captured(['foo', 'bar'])
241
self.run_bzr(['foo', 'bar'])
236
242
self.assertEqual(cwd, self.working_dir)
238
self.run_bzr_captured(['foo', 'bar'], working_dir=None)
244
self.run_bzr(['foo', 'bar'], working_dir=None)
239
245
self.assertEqual(cwd, self.working_dir)
241
247
# The function should be run in the alternative directory
242
248
# but afterwards the current working dir shouldn't be changed
243
self.run_bzr_captured(['foo', 'bar'], working_dir='one')
249
self.run_bzr(['foo', 'bar'], working_dir='one')
244
250
self.assertNotEqual(cwd, self.working_dir)
245
251
self.assertEndsWith(self.working_dir, 'one')
246
252
self.assertEqual(cwd, osutils.getcwd())
248
self.run_bzr_captured(['foo', 'bar'], working_dir='two')
254
self.run_bzr(['foo', 'bar'], working_dir='two')
249
255
self.assertNotEqual(cwd, self.working_dir)
250
256
self.assertEndsWith(self.working_dir, 'two')
251
257
self.assertEqual(cwd, osutils.getcwd())
451
462
class TestRunBzrError(ExternalBase):
453
464
def test_run_bzr_error(self):
454
out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
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)
455
469
self.assertEqual(out, 'It sure does!\n')
457
out, err = self.run_bzr_error(["bzr: ERROR: foobarbaz is not versioned"],
458
'file-id', 'foobarbaz')
461
class TestSelftestCleanOutput(TestCaseInTempDir):
463
def test_clean_output(self):
464
# check that 'bzr selftest --clean-output' works correct
465
dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
466
files = ('bzr', 'setup.py', 'test9999.tmp')
471
f.write('content of ')
476
before = os.listdir(root)
478
self.assertEquals(['bzr','bzrlib','setup.py',
479
'test0000.tmp','test0001.tmp',
480
'test9999.tmp','tests'],
483
out,err = self.run_bzr_captured(['selftest','--clean-output'],
486
self.assertEquals(['delete directory: test0000.tmp',
487
'delete directory: test0001.tmp'],
488
sorted(out.splitlines()))
489
self.assertEquals('', err)
491
after = os.listdir(root)
493
self.assertEquals(['bzr','bzrlib','setup.py',
494
'test9999.tmp','tests'],
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'])
498
476
class TestSelftestListOnly(TestCase):
539
515
def test_list_only_filtered(self):
540
516
# check that a filtered --list-only works, both include and exclude
541
out_all,err_all = self.run_bzr_captured(['selftest', '--list-only'])
517
out_all,err_all = self.run_bzr('selftest --list-only')
542
518
tests_all = self._parse_test_list(out_all.splitlines())[1]
543
out_incl,err_incl = self.run_bzr_captured(['selftest', '--list-only',
519
out_incl,err_incl = self.run_bzr('selftest --list-only selftest')
545
520
tests_incl = self._parse_test_list(out_incl.splitlines())[1]
546
521
self.assertSubset(tests_incl, tests_all)
547
out_excl,err_excl = self.run_bzr_captured(['selftest', '--list-only',
548
'--exclude', 'selftest'])
522
out_excl,err_excl = self.run_bzr(['selftest', '--list-only',
523
'--exclude', 'selftest'])
549
524
tests_excl = self._parse_test_list(out_excl.splitlines())[1]
550
525
self.assertSubset(tests_excl, tests_all)
551
526
set_incl = set(tests_incl)
557
532
def test_list_only_random(self):
558
533
# check that --randomize works correctly
559
out_all,err_all = self.run_bzr_captured(['selftest', '--list-only',
534
out_all,err_all = self.run_bzr('selftest --list-only selftest')
561
535
tests_all = self._parse_test_list(out_all.splitlines())[1]
562
536
# XXX: It looks like there are some orders for generating tests that
563
537
# fail as of 20070504 - maybe because of import order dependencies.
564
538
# So unfortunately this will rarely intermittently fail at the moment.
565
539
# -- mbp 20070504
566
out_rand,err_rand = self.run_bzr_captured(['selftest', '--list-only',
567
'selftest', '--randomize', 'now'])
540
out_rand,err_rand = self.run_bzr(['selftest', '--list-only',
541
'selftest', '--randomize', 'now'])
568
542
(header_rand,tests_rand,dummy) = self._parse_test_list(
569
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
570
549
self.assertNotEqual(tests_all, tests_rand)
571
550
self.assertEqual(sorted(tests_all), sorted(tests_rand))
572
551
# Check that the seed can be reused to get the exact same order
573
552
seed_re = re.compile('Randomizing test order using seed (\w+)')
574
553
match_obj = seed_re.search(header_rand[-1])
575
554
seed = match_obj.group(1)
576
out_rand2,err_rand2 = self.run_bzr_captured(['selftest', '--list-only',
577
'selftest', '--randomize', seed])
555
out_rand2,err_rand2 = self.run_bzr(['selftest', '--list-only',
556
'selftest', '--randomize', seed])
578
557
(header_rand2,tests_rand2,dummy) = self._parse_test_list(
579
558
out_rand2.splitlines(), 2)
580
559
self.assertEqual(tests_rand, tests_rand2)