95
91
class TestRunBzr(ExternalBase):
97
def _run_bzr_core(self, argv, retcode=0, encoding=None, stdin=None,
93
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.
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.
104
98
Here we only need to test that it is run_bzr passes the right
105
parameters to run_bzr.
99
parameters to run_bzr_captured.
107
self.argv = list(argv)
108
102
self.retcode = retcode
109
103
self.encoding = encoding
110
104
self.stdin = stdin
111
105
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)
114
113
def test_encoding(self):
115
"""Test that run_bzr passes encoding to _run_bzr_core"""
116
self.run_bzr('foo bar')
114
"""Test that run_bzr passes encoding to run_bzr_captured"""
115
self.run_bzr('foo', 'bar')
117
116
self.assertEqual(None, self.encoding)
118
self.assertEqual(['foo', 'bar'], self.argv)
117
self.assertEqual(('foo', 'bar'), self.argv)
120
self.run_bzr('foo bar', encoding='baz')
119
self.run_bzr('foo', 'bar', encoding='baz')
121
120
self.assertEqual('baz', self.encoding)
122
self.assertEqual(['foo', 'bar'], self.argv)
121
self.assertEqual(('foo', 'bar'), self.argv)
124
123
def test_retcode(self):
125
"""Test that run_bzr passes retcode to _run_bzr_core"""
124
"""Test that run_bzr passes retcode to run_bzr_captured"""
126
125
# Default is retcode == 0
127
self.run_bzr('foo bar')
126
self.run_bzr('foo', 'bar')
128
127
self.assertEqual(0, self.retcode)
129
self.assertEqual(['foo', 'bar'], self.argv)
128
self.assertEqual(('foo', 'bar'), self.argv)
131
self.run_bzr('foo bar', retcode=1)
130
self.run_bzr('foo', 'bar', retcode=1)
132
131
self.assertEqual(1, self.retcode)
133
self.assertEqual(['foo', 'bar'], self.argv)
132
self.assertEqual(('foo', 'bar'), self.argv)
135
self.run_bzr('foo bar', retcode=None)
134
self.run_bzr('foo', 'bar', retcode=None)
136
135
self.assertEqual(None, self.retcode)
137
self.assertEqual(['foo', 'bar'], self.argv)
136
self.assertEqual(('foo', 'bar'), self.argv)
139
self.run_bzr(['foo', 'bar'], retcode=3)
138
self.run_bzr('foo', 'bar', retcode=3)
140
139
self.assertEqual(3, self.retcode)
141
self.assertEqual(['foo', 'bar'], self.argv)
140
self.assertEqual(('foo', 'bar'), self.argv)
143
142
def test_stdin(self):
144
143
# test that the stdin keyword to run_bzr is passed through to
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
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
148
147
# should invoke it.
149
self.run_bzr('foo bar', stdin='gam')
148
self.run_bzr('foo', 'bar', stdin='gam')
150
149
self.assertEqual('gam', self.stdin)
151
self.assertEqual(['foo', 'bar'], self.argv)
150
self.assertEqual(('foo', 'bar'), self.argv)
153
self.run_bzr('foo bar', stdin='zippy')
152
self.run_bzr('foo', 'bar', stdin='zippy')
154
153
self.assertEqual('zippy', self.stdin)
155
self.assertEqual(['foo', 'bar'], self.argv)
154
self.assertEqual(('foo', 'bar'), self.argv)
157
156
def test_working_dir(self):
158
"""Test that run_bzr passes working_dir to _run_bzr_core"""
159
self.run_bzr('foo bar')
157
"""Test that run_bzr passes working_dir to run_bzr_captured"""
158
self.run_bzr('foo', 'bar')
160
159
self.assertEqual(None, self.working_dir)
161
self.assertEqual(['foo', 'bar'], self.argv)
160
self.assertEqual(('foo', 'bar'), self.argv)
163
self.run_bzr('foo bar', working_dir='baz')
162
self.run_bzr('foo', 'bar', working_dir='baz')
164
163
self.assertEqual('baz', self.working_dir)
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'])
164
self.assertEqual(('foo', 'bar'), self.argv)
172
167
class TestBenchmarkTests(TestCaseWithTransport):
210
204
def test_stdin(self):
211
# test that the stdin keyword to _run_bzr_core is passed through to
205
# test that the stdin keyword to run_bzr_captured is passed through to
212
206
# apply_redirected as a StringIO. We do this by overriding
213
# apply_redirected in this class, and then calling _run_bzr_core,
207
# apply_redirected in this class, and then calling run_bzr_captured,
214
208
# which calls apply_redirected.
215
self.run_bzr(['foo', 'bar'], stdin='gam')
209
self.run_bzr_captured(['foo', 'bar'], stdin='gam')
216
210
self.assertEqual('gam', self.stdin.read())
217
211
self.assertTrue(self.stdin is self.factory_stdin)
218
self.run_bzr(['foo', 'bar'], stdin='zippy')
212
self.run_bzr_captured(['foo', 'bar'], stdin='zippy')
219
213
self.assertEqual('zippy', self.stdin.read())
220
214
self.assertTrue(self.stdin is self.factory_stdin)
222
216
def test_ui_factory(self):
223
# each invocation of self.run_bzr should get its
217
# each invocation of self.run_bzr_captured should get its
224
218
# own UI factory, which is an instance of TestUIFactory,
225
219
# with stdin, stdout and stderr attached to the stdin,
226
# stdout and stderr of the invoked run_bzr
220
# stdout and stderr of the invoked run_bzr_captured
227
221
current_factory = bzrlib.ui.ui_factory
228
self.run_bzr(['foo'])
222
self.run_bzr_captured(['foo'])
229
223
self.failIf(current_factory is self.factory)
230
224
self.assertNotEqual(sys.stdout, self.factory.stdout)
231
225
self.assertNotEqual(sys.stderr, self.factory.stderr)
238
232
cwd = osutils.getcwd()
240
234
# Default is to work in the current directory
241
self.run_bzr(['foo', 'bar'])
235
self.run_bzr_captured(['foo', 'bar'])
242
236
self.assertEqual(cwd, self.working_dir)
244
self.run_bzr(['foo', 'bar'], working_dir=None)
238
self.run_bzr_captured(['foo', 'bar'], working_dir=None)
245
239
self.assertEqual(cwd, self.working_dir)
247
241
# The function should be run in the alternative directory
248
242
# but afterwards the current working dir shouldn't be changed
249
self.run_bzr(['foo', 'bar'], working_dir='one')
243
self.run_bzr_captured(['foo', 'bar'], working_dir='one')
250
244
self.assertNotEqual(cwd, self.working_dir)
251
245
self.assertEndsWith(self.working_dir, 'one')
252
246
self.assertEqual(cwd, osutils.getcwd())
254
self.run_bzr(['foo', 'bar'], working_dir='two')
248
self.run_bzr_captured(['foo', 'bar'], working_dir='two')
255
249
self.assertNotEqual(cwd, self.working_dir)
256
250
self.assertEndsWith(self.working_dir, 'two')
257
251
self.assertEqual(cwd, osutils.getcwd())
462
451
class TestRunBzrError(ExternalBase):
464
453
def test_run_bzr_error(self):
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)
454
out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
469
455
self.assertEqual(out, 'It sure does!\n')
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'])
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'],
476
498
class TestSelftestListOnly(TestCase):
515
539
def test_list_only_filtered(self):
516
540
# check that a filtered --list-only works, both include and exclude
517
out_all,err_all = self.run_bzr('selftest --list-only')
541
out_all,err_all = self.run_bzr_captured(['selftest', '--list-only'])
518
542
tests_all = self._parse_test_list(out_all.splitlines())[1]
519
out_incl,err_incl = self.run_bzr('selftest --list-only selftest')
543
out_incl,err_incl = self.run_bzr_captured(['selftest', '--list-only',
520
545
tests_incl = self._parse_test_list(out_incl.splitlines())[1]
521
546
self.assertSubset(tests_incl, tests_all)
522
out_excl,err_excl = self.run_bzr(['selftest', '--list-only',
523
'--exclude', 'selftest'])
547
out_excl,err_excl = self.run_bzr_captured(['selftest', '--list-only',
548
'--exclude', 'selftest'])
524
549
tests_excl = self._parse_test_list(out_excl.splitlines())[1]
525
550
self.assertSubset(tests_excl, tests_all)
526
551
set_incl = set(tests_incl)
532
557
def test_list_only_random(self):
533
558
# check that --randomize works correctly
534
out_all,err_all = self.run_bzr('selftest --list-only selftest')
559
out_all,err_all = self.run_bzr_captured(['selftest', '--list-only',
535
561
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'])
562
out_rand,err_rand = self.run_bzr_captured(['selftest', '--list-only',
563
'selftest', '--randomize', 'now'])
542
564
(header_rand,tests_rand,dummy) = self._parse_test_list(
543
565
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
549
566
self.assertNotEqual(tests_all, tests_rand)
550
567
self.assertEqual(sorted(tests_all), sorted(tests_rand))
551
568
# Check that the seed can be reused to get the exact same order
552
569
seed_re = re.compile('Randomizing test order using seed (\w+)')
553
570
match_obj = seed_re.search(header_rand[-1])
554
571
seed = match_obj.group(1)
555
out_rand2,err_rand2 = self.run_bzr(['selftest', '--list-only',
556
'selftest', '--randomize', seed])
572
out_rand2,err_rand2 = self.run_bzr_captured(['selftest', '--list-only',
573
'selftest', '--randomize', seed])
557
574
(header_rand2,tests_rand2,dummy) = self._parse_test_list(
558
575
out_rand2.splitlines(), 2)
559
576
self.assertEqual(tests_rand, tests_rand2)
562
class TestSelftestWithIdList(TestCaseInTempDir):
564
def test_load_list(self):
565
# We don't want to call selftest for the whole suite, so we start with
567
test_list_fname = 'test.list'
568
fl = open(test_list_fname, 'wt')
569
fl.write('%s\n' % self.id())
571
out, err = self.run_bzr(
572
['selftest', '--load-list', test_list_fname, '--list'])
573
self.assertContainsRe(out, "Listed 1 test in")
575
def test_load_unknown(self):
576
out, err = self.run_bzr('selftest --load-list I_do_not_exist ',