65
72
except ParamikoNotPresent:
66
73
raise TestSkipped("Paramiko not present")
67
74
old_transport = bzrlib.tests.default_transport
68
old_root = TestCaseInTempDir.TEST_ROOT
69
TestCaseInTempDir.TEST_ROOT = None
75
old_root = TestCaseWithMemoryTransport.TEST_ROOT
76
TestCaseWithMemoryTransport.TEST_ROOT = None
71
78
TestOptions.current_test = "test_transport_set_to_sftp"
72
stdout = self.capture('selftest --transport=sftp test_transport_set_to_sftp')
79
stdout = self.run_bzr(
80
'selftest --transport=sftp test_transport_set_to_sftp')[0]
74
81
self.assertContainsRe(stdout, 'Ran 1 test')
75
82
self.assertEqual(old_transport, bzrlib.tests.default_transport)
77
84
TestOptions.current_test = "test_transport_set_to_memory"
78
stdout = self.capture('selftest --transport=memory test_transport_set_to_memory')
85
stdout = self.run_bzr(
86
'selftest --transport=memory test_transport_set_to_memory')[0]
79
87
self.assertContainsRe(stdout, 'Ran 1 test')
80
88
self.assertEqual(old_transport, bzrlib.tests.default_transport)
82
90
bzrlib.tests.default_transport = old_transport
83
91
TestOptions.current_test = None
84
TestCaseInTempDir.TEST_ROOT = old_root
92
TestCaseWithMemoryTransport.TEST_ROOT = old_root
87
95
class TestRunBzr(ExternalBase):
89
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,
91
"""Override run_bzr_captured to test how it is invoked by run_bzr.
93
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.
94
104
Here we only need to test that it is run_bzr passes the right
95
parameters to run_bzr_captured.
105
parameters to run_bzr.
107
self.argv = list(argv)
98
108
self.retcode = retcode
99
109
self.encoding = encoding
100
110
self.stdin = stdin
101
111
self.working_dir = working_dir
103
114
def test_args(self):
104
"""Test that run_bzr passes args correctly to run_bzr_captured"""
115
"""Test that run_bzr passes args correctly to _run_bzr_core"""
116
## self.callDeprecated(
117
## ['passing varargs to run_bzr was deprecated in version 0.18.'],
119
## 'arg1', 'arg2', 'arg3', retcode=1)
105
120
self.run_bzr('arg1', 'arg2', 'arg3', retcode=1)
106
self.assertEqual(('arg1', 'arg2', 'arg3'), self.argv)
121
self.assertEqual(['arg1', 'arg2', 'arg3'], self.argv)
108
123
def test_encoding(self):
109
"""Test that run_bzr passes encoding to run_bzr_captured"""
110
self.run_bzr('foo', 'bar')
124
"""Test that run_bzr passes encoding to _run_bzr_core"""
125
self.run_bzr('foo bar')
111
126
self.assertEqual(None, self.encoding)
112
self.assertEqual(('foo', 'bar'), self.argv)
127
self.assertEqual(['foo', 'bar'], self.argv)
114
self.run_bzr('foo', 'bar', encoding='baz')
129
self.run_bzr('foo bar', encoding='baz')
115
130
self.assertEqual('baz', self.encoding)
116
self.assertEqual(('foo', 'bar'), self.argv)
131
self.assertEqual(['foo', 'bar'], self.argv)
118
133
def test_retcode(self):
119
"""Test that run_bzr passes retcode to run_bzr_captured"""
134
"""Test that run_bzr passes retcode to _run_bzr_core"""
120
135
# Default is retcode == 0
121
self.run_bzr('foo', 'bar')
136
self.run_bzr('foo bar')
122
137
self.assertEqual(0, self.retcode)
123
self.assertEqual(('foo', 'bar'), self.argv)
138
self.assertEqual(['foo', 'bar'], self.argv)
125
self.run_bzr('foo', 'bar', retcode=1)
140
self.run_bzr('foo bar', retcode=1)
126
141
self.assertEqual(1, self.retcode)
127
self.assertEqual(('foo', 'bar'), self.argv)
142
self.assertEqual(['foo', 'bar'], self.argv)
129
self.run_bzr('foo', 'bar', retcode=None)
144
self.run_bzr('foo bar', retcode=None)
130
145
self.assertEqual(None, self.retcode)
131
self.assertEqual(('foo', 'bar'), self.argv)
146
self.assertEqual(['foo', 'bar'], self.argv)
133
self.run_bzr('foo', 'bar', retcode=3)
148
self.run_bzr(['foo', 'bar'], retcode=3)
134
149
self.assertEqual(3, self.retcode)
135
self.assertEqual(('foo', 'bar'), self.argv)
150
self.assertEqual(['foo', 'bar'], self.argv)
137
152
def test_stdin(self):
138
153
# test that the stdin keyword to run_bzr is passed through to
139
# run_bzr_captured as-is. We do this by overriding
140
# run_bzr_captured in this class, and then calling run_bzr,
141
# which is a convenience function for run_bzr_captured, so
154
# _run_bzr_core as-is. We do this by overriding
155
# _run_bzr_core in this class, and then calling run_bzr,
156
# which is a convenience function for _run_bzr_core, so
142
157
# should invoke it.
143
self.run_bzr('foo', 'bar', stdin='gam')
158
self.run_bzr('foo bar', stdin='gam')
144
159
self.assertEqual('gam', self.stdin)
145
self.assertEqual(('foo', 'bar'), self.argv)
160
self.assertEqual(['foo', 'bar'], self.argv)
147
self.run_bzr('foo', 'bar', stdin='zippy')
162
self.run_bzr('foo bar', stdin='zippy')
148
163
self.assertEqual('zippy', self.stdin)
149
self.assertEqual(('foo', 'bar'), self.argv)
164
self.assertEqual(['foo', 'bar'], self.argv)
151
166
def test_working_dir(self):
152
"""Test that run_bzr passes working_dir to run_bzr_captured"""
153
self.run_bzr('foo', 'bar')
167
"""Test that run_bzr passes working_dir to _run_bzr_core"""
168
self.run_bzr('foo bar')
154
169
self.assertEqual(None, self.working_dir)
155
self.assertEqual(('foo', 'bar'), self.argv)
170
self.assertEqual(['foo', 'bar'], self.argv)
157
self.run_bzr('foo', 'bar', working_dir='baz')
172
self.run_bzr('foo bar', working_dir='baz')
158
173
self.assertEqual('baz', self.working_dir)
159
self.assertEqual(('foo', 'bar'), self.argv)
174
self.assertEqual(['foo', 'bar'], self.argv)
162
177
class TestBenchmarkTests(TestCaseWithTransport):
199
215
def test_stdin(self):
200
# test that the stdin keyword to run_bzr_captured is passed through to
216
# test that the stdin keyword to _run_bzr_core is passed through to
201
217
# apply_redirected as a StringIO. We do this by overriding
202
# apply_redirected in this class, and then calling run_bzr_captured,
218
# apply_redirected in this class, and then calling _run_bzr_core,
203
219
# which calls apply_redirected.
204
self.run_bzr_captured(['foo', 'bar'], stdin='gam')
220
self.run_bzr(['foo', 'bar'], stdin='gam')
205
221
self.assertEqual('gam', self.stdin.read())
206
222
self.assertTrue(self.stdin is self.factory_stdin)
207
self.run_bzr_captured(['foo', 'bar'], stdin='zippy')
223
self.run_bzr(['foo', 'bar'], stdin='zippy')
208
224
self.assertEqual('zippy', self.stdin.read())
209
225
self.assertTrue(self.stdin is self.factory_stdin)
211
227
def test_ui_factory(self):
212
# each invocation of self.run_bzr_captured should get its own UI
213
# factory, which is an instance of TestUIFactory, with stdout and
214
# stderr attached to the stdout and stderr of the invoked
228
# each invocation of self.run_bzr should get its
229
# own UI factory, which is an instance of TestUIFactory,
230
# with stdin, stdout and stderr attached to the stdin,
231
# stdout and stderr of the invoked run_bzr
216
232
current_factory = bzrlib.ui.ui_factory
217
self.run_bzr_captured(['foo'])
233
self.run_bzr(['foo'])
218
234
self.failIf(current_factory is self.factory)
219
235
self.assertNotEqual(sys.stdout, self.factory.stdout)
220
236
self.assertNotEqual(sys.stderr, self.factory.stderr)
221
237
self.assertEqual('foo\n', self.factory.stdout.getvalue())
222
238
self.assertEqual('bar\n', self.factory.stderr.getvalue())
223
self.assertIsInstance(self.factory, bzrlib.tests.blackbox.TestUIFactory)
239
self.assertIsInstance(self.factory, TestUIFactory)
225
241
def test_working_dir(self):
226
242
self.build_tree(['one/', 'two/'])
227
243
cwd = osutils.getcwd()
229
245
# Default is to work in the current directory
230
self.run_bzr_captured(['foo', 'bar'])
246
self.run_bzr(['foo', 'bar'])
231
247
self.assertEqual(cwd, self.working_dir)
233
self.run_bzr_captured(['foo', 'bar'], working_dir=None)
249
self.run_bzr(['foo', 'bar'], working_dir=None)
234
250
self.assertEqual(cwd, self.working_dir)
236
252
# The function should be run in the alternative directory
237
253
# but afterwards the current working dir shouldn't be changed
238
self.run_bzr_captured(['foo', 'bar'], working_dir='one')
254
self.run_bzr(['foo', 'bar'], working_dir='one')
239
255
self.assertNotEqual(cwd, self.working_dir)
240
256
self.assertEndsWith(self.working_dir, 'one')
241
257
self.assertEqual(cwd, osutils.getcwd())
243
self.run_bzr_captured(['foo', 'bar'], working_dir='two')
259
self.run_bzr(['foo', 'bar'], working_dir='two')
244
260
self.assertNotEqual(cwd, self.working_dir)
245
261
self.assertEndsWith(self.working_dir, 'two')
246
262
self.assertEqual(cwd, osutils.getcwd())
411
454
self.make_branch_and_tree('one')
413
456
process = self.start_bzr_subprocess(['root'], working_dir='one')
414
result = self.finish_bzr_subprocess(process)
457
result = self.finish_bzr_subprocess(process, universal_newlines=True)
415
458
self.assertEndsWith(result[0], 'one\n')
416
459
self.assertEqual('', result[1])
419
462
class TestRunBzrError(ExternalBase):
421
464
def test_run_bzr_error(self):
422
out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
423
self.assertEqual(out, 'it sure does!\n')
425
out, err = self.run_bzr_error(["'foobarbaz' is not a versioned file"],
426
'file-id', 'foobarbaz')
465
out, err = self.run_bzr_error(['^$'], ['rocks'], retcode=0)
466
self.assertEqual(out, 'It sure does!\n')
468
out, err = self.run_bzr_error(["bzr: ERROR: foobarbaz is not versioned"],
469
['file-id', 'foobarbaz'])
472
class TestSelftestCleanOutput(TestCaseInTempDir):
474
def test_clean_output(self):
475
# check that 'bzr selftest --clean-output' works correct
476
dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
477
files = ('bzr', 'setup.py', 'test9999.tmp')
482
f.write('content of ')
487
before = os.listdir(root)
489
self.assertEquals(['bzr','bzrlib','setup.py',
490
'test0000.tmp','test0001.tmp',
491
'test9999.tmp','tests'],
494
out, err = self.run_bzr(['selftest','--clean-output'],
497
self.assertEquals(['delete directory: test0000.tmp',
498
'delete directory: test0001.tmp'],
499
sorted(out.splitlines()))
500
self.assertEquals('', err)
502
after = os.listdir(root)
504
self.assertEquals(['bzr','bzrlib','setup.py',
505
'test9999.tmp','tests'],
509
class TestSelftestListOnly(TestCase):
512
def _parse_test_list(lines, newlines_in_header=1):
513
"Parse a list of lines into a tuple of 3 lists (header,body,footer)."
520
header_newlines_found = 0
524
header_newlines_found += 1
525
if header_newlines_found >= newlines_in_header:
530
if line.startswith('-------'):
536
# If the last body line is blank, drop it off the list
537
if len(body) > 0 and body[-1] == '':
539
return (header,body,footer)
541
def test_list_only(self):
542
# check that bzr selftest --list-only works correctly
543
out,err = self.run_bzr(['selftest', 'selftest',
545
self.assertEndsWith(err, 'tests passed\n')
546
(header,body,footer) = self._parse_test_list(out.splitlines())
547
num_tests = len(body)
548
self.assertContainsRe(footer[0], 'Listed %s tests in' % num_tests)
550
def test_list_only_filtered(self):
551
# check that a filtered --list-only works, both include and exclude
552
out_all,err_all = self.run_bzr(['selftest', '--list-only'])
553
tests_all = self._parse_test_list(out_all.splitlines())[1]
554
out_incl,err_incl = self.run_bzr(['selftest', '--list-only',
556
tests_incl = self._parse_test_list(out_incl.splitlines())[1]
557
self.assertSubset(tests_incl, tests_all)
558
out_excl,err_excl = self.run_bzr(['selftest', '--list-only',
559
'--exclude', 'selftest'])
560
tests_excl = self._parse_test_list(out_excl.splitlines())[1]
561
self.assertSubset(tests_excl, tests_all)
562
set_incl = set(tests_incl)
563
set_excl = set(tests_excl)
564
intersection = set_incl.intersection(set_excl)
565
self.assertEquals(0, len(intersection))
566
self.assertEquals(len(tests_all), len(tests_incl) + len(tests_excl))
568
def test_list_only_random(self):
569
# check that --randomize works correctly
570
out_all,err_all = self.run_bzr(['selftest', '--list-only',
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)