105
101
self.assertEqual('zippy', self.stdin)
104
class TestBenchmarkTests(TestCaseWithTransport):
106
def test_benchmark_runs_benchmark_tests(self):
107
"""bzr selftest --benchmark should not run the default test suite."""
108
# We test this by passing a regression test name to --benchmark, which
109
# should result in 0 rests run.
110
old_root = TestCaseInTempDir.TEST_ROOT
112
TestCaseInTempDir.TEST_ROOT = None
113
out, err = self.run_bzr('selftest', '--benchmark', 'workingtree_implementations')
115
TestCaseInTempDir.TEST_ROOT = old_root
116
self.assertContainsRe(out, 'Ran 0 tests.*\n\nOK')
118
'running tests...\ntests passed\n',
120
benchfile = open(".perf_history", "rt")
122
lines = benchfile.readlines()
125
self.assertEqual(1, len(lines))
126
self.assertContainsRe(lines[0], "--date [0-9.]+")
108
129
class TestRunBzrCaptured(ExternalBase):
110
131
def apply_redirected(self, stdin=None, stdout=None, stderr=None,
141
162
self.assertEqual('foo\n', self.factory.stdout.getvalue())
142
163
self.assertEqual('bar\n', self.factory.stderr.getvalue())
143
164
self.assertIsInstance(self.factory, bzrlib.tests.blackbox.TestUIFactory)
166
def test_run_bzr_subprocess(self):
167
"""The run_bzr_helper_external comand behaves nicely."""
168
result = self.run_bzr_subprocess('--version')
169
result = self.run_bzr_subprocess('--version', retcode=None)
170
self.assertContainsRe(result[0], 'is free software')
171
self.assertRaises(AssertionError, self.run_bzr_subprocess,
173
result = self.run_bzr_subprocess('--versionn', retcode=3)
174
result = self.run_bzr_subprocess('--versionn', retcode=None)
175
self.assertContainsRe(result[1], 'unknown command')
176
err = self.run_bzr_subprocess('merge', '--merge-type', 'magic merge',
178
self.assertContainsRe(err, 'No known merge type magic merge')
180
def test_run_bzr_subprocess_env(self):
181
"""run_bzr_subprocess can set environment variables in the child only.
183
These changes should not change the running process, only the child.
185
# The test suite should unset this variable
186
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
187
out, err = self.run_bzr_subprocess('whoami', env_changes={
188
'BZR_EMAIL':'Joe Foo <joe@foo.com>'
189
}, universal_newlines=True)
190
self.assertEqual('', err)
191
self.assertEqual('Joe Foo <joe@foo.com>\n', out)
192
# And it should not be modified
193
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
195
# Do it again with a different address, just to make sure
196
# it is actually changing
197
out, err = self.run_bzr_subprocess('whoami', env_changes={
198
'BZR_EMAIL':'Barry <bar@foo.com>'
199
}, universal_newlines=True)
200
self.assertEqual('', err)
201
self.assertEqual('Barry <bar@foo.com>\n', out)
202
self.assertEqual(None, os.environ.get('BZR_EMAIL'))
204
def test_run_bzr_subprocess_env_del(self):
205
"""run_bzr_subprocess can remove environment variables too."""
206
# Create a random email, so we are sure this won't collide
207
rand_bzr_email = 'John Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
208
rand_email = 'Jane Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
209
os.environ['BZR_EMAIL'] = rand_bzr_email
210
os.environ['EMAIL'] = rand_email
212
# By default, the child will inherit the current env setting
213
out, err = self.run_bzr_subprocess('whoami', universal_newlines=True)
214
self.assertEqual('', err)
215
self.assertEqual(rand_bzr_email + '\n', out)
217
# Now that BZR_EMAIL is not set, it should fall back to EMAIL
218
out, err = self.run_bzr_subprocess('whoami',
219
env_changes={'BZR_EMAIL':None},
220
universal_newlines=True)
221
self.assertEqual('', err)
222
self.assertEqual(rand_email + '\n', out)
224
# This switches back to the default email guessing logic
225
# Which shouldn't match either of the above addresses
226
out, err = self.run_bzr_subprocess('whoami',
227
env_changes={'BZR_EMAIL':None, 'EMAIL':None},
228
universal_newlines=True)
230
self.assertEqual('', err)
231
self.assertNotEqual(rand_bzr_email + '\n', out)
232
self.assertNotEqual(rand_email + '\n', out)
234
# TestCase cleans up BZR_EMAIL, and EMAIL at startup
235
del os.environ['BZR_EMAIL']
236
del os.environ['EMAIL']
238
def test_run_bzr_subprocess_env_del_missing(self):
239
"""run_bzr_subprocess won't fail if deleting a nonexistant env var"""
240
self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
241
out, err = self.run_bzr_subprocess('rocks',
242
env_changes={'NON_EXISTANT_ENV_VAR':None},
243
universal_newlines=True)
244
self.assertEqual('it sure does!\n', out)
245
self.assertEqual('', err)
247
def test_start_and_stop_bzr_subprocess(self):
248
"""We can start and perform other test actions while that process is
251
process = self.start_bzr_subprocess(['--version'])
252
result = self.finish_bzr_subprocess(process)
253
self.assertContainsRe(result[0], 'is free software')
254
self.assertEqual('', result[1])
256
def test_start_and_stop_bzr_subprocess_with_error(self):
257
"""finish_bzr_subprocess allows specification of the desired exit code.
259
process = self.start_bzr_subprocess(['--versionn'])
260
result = self.finish_bzr_subprocess(process, retcode=3)
261
self.assertEqual('', result[0])
262
self.assertContainsRe(result[1], 'unknown command')
264
def test_start_and_stop_bzr_subprocess_ignoring_retcode(self):
265
"""finish_bzr_subprocess allows the exit code to be ignored."""
266
process = self.start_bzr_subprocess(['--versionn'])
267
result = self.finish_bzr_subprocess(process, retcode=None)
268
self.assertEqual('', result[0])
269
self.assertContainsRe(result[1], 'unknown command')
271
def test_start_and_stop_bzr_subprocess_with_unexpected_retcode(self):
272
"""finish_bzr_subprocess raises self.failureException if the retcode is
273
not the expected one.
275
process = self.start_bzr_subprocess(['--versionn'])
276
self.assertRaises(self.failureException, self.finish_bzr_subprocess,
279
def test_start_and_stop_bzr_subprocess_send_signal(self):
280
"""finish_bzr_subprocess raises self.failureException if the retcode is
281
not the expected one.
283
process = self.start_bzr_subprocess(['wait-until-signalled'],
284
skip_if_plan_to_signal=True)
285
self.assertEqual('running\n', process.stdout.readline())
286
result = self.finish_bzr_subprocess(process, send_signal=signal.SIGINT,
288
self.assertEqual('', result[0])
289
self.assertEqual('bzr: interrupted\n', result[1])
292
class TestRunBzrError(ExternalBase):
294
def test_run_bzr_error(self):
295
out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
296
self.assertEqual(out, 'it sure does!\n')
298
out, err = self.run_bzr_error(["'foobarbaz' is not a versioned file"],
299
'file-id', 'foobarbaz')