~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_selftest.py

Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
"""UI tests for the test framework."""
17
17
 
18
18
import os
 
19
import signal
19
20
import sys
20
21
 
21
22
import bzrlib
25
26
from bzrlib.errors import ParamikoNotPresent
26
27
from bzrlib.tests import (
27
28
                          TestCase,
28
 
                          TestCaseInTempDir,
 
29
                          TestCaseWithMemoryTransport,
29
30
                          TestCaseWithTransport,
30
31
                          TestSkipped,
31
32
                          )
64
65
        except ParamikoNotPresent:
65
66
            raise TestSkipped("Paramiko not present")
66
67
        old_transport = bzrlib.tests.default_transport
67
 
        old_root = TestCaseInTempDir.TEST_ROOT
68
 
        TestCaseInTempDir.TEST_ROOT = None
 
68
        old_root = TestCaseWithMemoryTransport.TEST_ROOT
 
69
        TestCaseWithMemoryTransport.TEST_ROOT = None
69
70
        try:
70
71
            TestOptions.current_test = "test_transport_set_to_sftp"
71
72
            stdout = self.capture('selftest --transport=sftp test_transport_set_to_sftp')
80
81
        finally:
81
82
            bzrlib.tests.default_transport = old_transport
82
83
            TestOptions.current_test = None
83
 
            TestCaseInTempDir.TEST_ROOT = old_root
 
84
            TestCaseWithMemoryTransport.TEST_ROOT = old_root
84
85
 
85
86
 
86
87
class TestRunBzr(ExternalBase):
87
88
 
88
 
    def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None):
 
89
    def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
 
90
                         working_dir=None):
 
91
        """Override run_bzr_captured to test how it is invoked by run_bzr.
 
92
 
 
93
        We test how run_bzr_captured actually invokes bzr in another location.
 
94
        Here we only need to test that it is run_bzr passes the right
 
95
        parameters to run_bzr_captured.
 
96
        """
 
97
        self.argv = argv
 
98
        self.retcode = retcode
 
99
        self.encoding = encoding
89
100
        self.stdin = stdin
 
101
        self.working_dir = working_dir
 
102
 
 
103
    def test_args(self):
 
104
        """Test that run_bzr passes args correctly to run_bzr_captured"""
 
105
        self.run_bzr('arg1', 'arg2', 'arg3', retcode=1)
 
106
        self.assertEqual(('arg1', 'arg2', 'arg3'), self.argv)
 
107
 
 
108
    def test_encoding(self):
 
109
        """Test that run_bzr passes encoding to run_bzr_captured"""
 
110
        self.run_bzr('foo', 'bar')
 
111
        self.assertEqual(None, self.encoding)
 
112
        self.assertEqual(('foo', 'bar'), self.argv)
 
113
 
 
114
        self.run_bzr('foo', 'bar', encoding='baz')
 
115
        self.assertEqual('baz', self.encoding)
 
116
        self.assertEqual(('foo', 'bar'), self.argv)
 
117
 
 
118
    def test_retcode(self):
 
119
        """Test that run_bzr passes retcode to run_bzr_captured"""
 
120
        # Default is retcode == 0
 
121
        self.run_bzr('foo', 'bar')
 
122
        self.assertEqual(0, self.retcode)
 
123
        self.assertEqual(('foo', 'bar'), self.argv)
 
124
 
 
125
        self.run_bzr('foo', 'bar', retcode=1)
 
126
        self.assertEqual(1, self.retcode)
 
127
        self.assertEqual(('foo', 'bar'), self.argv)
 
128
 
 
129
        self.run_bzr('foo', 'bar', retcode=None)
 
130
        self.assertEqual(None, self.retcode)
 
131
        self.assertEqual(('foo', 'bar'), self.argv)
 
132
 
 
133
        self.run_bzr('foo', 'bar', retcode=3)
 
134
        self.assertEqual(3, self.retcode)
 
135
        self.assertEqual(('foo', 'bar'), self.argv)
90
136
 
91
137
    def test_stdin(self):
92
138
        # test that the stdin keyword to run_bzr is passed through to
96
142
        # should invoke it.
97
143
        self.run_bzr('foo', 'bar', stdin='gam')
98
144
        self.assertEqual('gam', self.stdin)
 
145
        self.assertEqual(('foo', 'bar'), self.argv)
 
146
 
99
147
        self.run_bzr('foo', 'bar', stdin='zippy')
100
148
        self.assertEqual('zippy', self.stdin)
 
149
        self.assertEqual(('foo', 'bar'), self.argv)
 
150
 
 
151
    def test_working_dir(self):
 
152
        """Test that run_bzr passes working_dir to run_bzr_captured"""
 
153
        self.run_bzr('foo', 'bar')
 
154
        self.assertEqual(None, self.working_dir)
 
155
        self.assertEqual(('foo', 'bar'), self.argv)
 
156
 
 
157
        self.run_bzr('foo', 'bar', working_dir='baz')
 
158
        self.assertEqual('baz', self.working_dir)
 
159
        self.assertEqual(('foo', 'bar'), self.argv)
101
160
 
102
161
 
103
162
class TestBenchmarkTests(TestCaseWithTransport):
106
165
        """bzr selftest --benchmark should not run the default test suite."""
107
166
        # We test this by passing a regression test name to --benchmark, which
108
167
        # should result in 0 rests run.
109
 
        old_root = TestCaseInTempDir.TEST_ROOT
 
168
        old_root = TestCaseWithMemoryTransport.TEST_ROOT
110
169
        try:
111
 
            TestCaseInTempDir.TEST_ROOT = None
 
170
            TestCaseWithMemoryTransport.TEST_ROOT = None
112
171
            out, err = self.run_bzr('selftest', '--benchmark', 'workingtree_implementations')
113
172
        finally:
114
 
            TestCaseInTempDir.TEST_ROOT = old_root
 
173
            TestCaseWithMemoryTransport.TEST_ROOT = old_root
115
174
        self.assertContainsRe(out, 'Ran 0 tests.*\n\nOK')
116
175
        self.assertEqual(
117
176
            'running tests...\ntests passed\n',
132
191
        self.stdin = stdin
133
192
        self.factory_stdin = getattr(bzrlib.ui.ui_factory, "stdin", None)
134
193
        self.factory = bzrlib.ui.ui_factory
 
194
        self.working_dir = osutils.getcwd()
135
195
        stdout.write('foo\n')
136
196
        stderr.write('bar\n')
137
197
        return 0
162
222
        self.assertEqual('bar\n', self.factory.stderr.getvalue())
163
223
        self.assertIsInstance(self.factory, bzrlib.tests.blackbox.TestUIFactory)
164
224
 
 
225
    def test_working_dir(self):
 
226
        self.build_tree(['one/', 'two/'])
 
227
        cwd = osutils.getcwd()
 
228
 
 
229
        # Default is to work in the current directory
 
230
        self.run_bzr_captured(['foo', 'bar'])
 
231
        self.assertEqual(cwd, self.working_dir)
 
232
 
 
233
        self.run_bzr_captured(['foo', 'bar'], working_dir=None)
 
234
        self.assertEqual(cwd, self.working_dir)
 
235
 
 
236
        # The function should be run in the alternative directory
 
237
        # but afterwards the current working dir shouldn't be changed
 
238
        self.run_bzr_captured(['foo', 'bar'], working_dir='one')
 
239
        self.assertNotEqual(cwd, self.working_dir)
 
240
        self.assertEndsWith(self.working_dir, 'one')
 
241
        self.assertEqual(cwd, osutils.getcwd())
 
242
 
 
243
        self.run_bzr_captured(['foo', 'bar'], working_dir='two')
 
244
        self.assertNotEqual(cwd, self.working_dir)
 
245
        self.assertEndsWith(self.working_dir, 'two')
 
246
        self.assertEqual(cwd, osutils.getcwd())
 
247
 
 
248
 
 
249
class TestRunBzrSubprocess(TestCaseWithTransport):
 
250
 
165
251
    def test_run_bzr_subprocess(self):
166
252
        """The run_bzr_helper_external comand behaves nicely."""
167
253
        result = self.run_bzr_subprocess('--version')
185
271
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
186
272
        out, err = self.run_bzr_subprocess('whoami', env_changes={
187
273
                                            'BZR_EMAIL':'Joe Foo <joe@foo.com>'
188
 
                                          })
 
274
                                          }, universal_newlines=True)
189
275
        self.assertEqual('', err)
190
276
        self.assertEqual('Joe Foo <joe@foo.com>\n', out)
191
277
        # And it should not be modified
195
281
        # it is actually changing
196
282
        out, err = self.run_bzr_subprocess('whoami', env_changes={
197
283
                                            'BZR_EMAIL':'Barry <bar@foo.com>'
198
 
                                          })
 
284
                                          }, universal_newlines=True)
199
285
        self.assertEqual('', err)
200
286
        self.assertEqual('Barry <bar@foo.com>\n', out)
201
287
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
209
295
        os.environ['EMAIL'] = rand_email
210
296
        try:
211
297
            # By default, the child will inherit the current env setting
212
 
            out, err = self.run_bzr_subprocess('whoami')
 
298
            out, err = self.run_bzr_subprocess('whoami', universal_newlines=True)
213
299
            self.assertEqual('', err)
214
300
            self.assertEqual(rand_bzr_email + '\n', out)
215
301
 
216
302
            # Now that BZR_EMAIL is not set, it should fall back to EMAIL
217
303
            out, err = self.run_bzr_subprocess('whoami',
218
 
                                               env_changes={'BZR_EMAIL':None})
 
304
                                               env_changes={'BZR_EMAIL':None},
 
305
                                               universal_newlines=True)
219
306
            self.assertEqual('', err)
220
307
            self.assertEqual(rand_email + '\n', out)
221
308
 
222
309
            # This switches back to the default email guessing logic
223
310
            # Which shouldn't match either of the above addresses
224
311
            out, err = self.run_bzr_subprocess('whoami',
225
 
                           env_changes={'BZR_EMAIL':None, 'EMAIL':None})
 
312
                           env_changes={'BZR_EMAIL':None, 'EMAIL':None},
 
313
                           universal_newlines=True)
226
314
 
227
315
            self.assertEqual('', err)
228
316
            self.assertNotEqual(rand_bzr_email + '\n', out)
236
324
        """run_bzr_subprocess won't fail if deleting a nonexistant env var"""
237
325
        self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
238
326
        out, err = self.run_bzr_subprocess('rocks',
239
 
                        env_changes={'NON_EXISTANT_ENV_VAR':None})
240
 
        self.assertEquals('it sure does!\n', out)
241
 
 
 
327
                        env_changes={'NON_EXISTANT_ENV_VAR':None},
 
328
                        universal_newlines=True)
 
329
        self.assertEqual('it sure does!\n', out)
 
330
        self.assertEqual('', err)
 
331
 
 
332
    def test_run_bzr_subprocess_working_dir(self):
 
333
        """Test that we can specify the working dir for the child"""
 
334
        cwd = osutils.getcwd()
 
335
 
 
336
        self.make_branch_and_tree('.')
 
337
        self.make_branch_and_tree('one')
 
338
        self.make_branch_and_tree('two')
 
339
 
 
340
        def get_root(**kwargs):
 
341
            """Spawn a process to get the 'root' of the tree.
 
342
 
 
343
            You can pass in arbitrary new arguments. This just makes
 
344
            sure that the returned path doesn't have trailing whitespace.
 
345
            """
 
346
            return self.run_bzr_subprocess('root', **kwargs)[0].rstrip()
 
347
 
 
348
        self.assertEqual(cwd, get_root())
 
349
        self.assertEqual(cwd, get_root(working_dir=None))
 
350
        # Has our path changed?
 
351
        self.assertEqual(cwd, osutils.getcwd())
 
352
 
 
353
        dir1 = get_root(working_dir='one')
 
354
        self.assertEndsWith(dir1, 'one')
 
355
        self.assertEqual(cwd, osutils.getcwd())
 
356
 
 
357
        dir2 = get_root(working_dir='two')
 
358
        self.assertEndsWith(dir2, 'two')
 
359
        self.assertEqual(cwd, osutils.getcwd())
 
360
 
 
361
 
 
362
class _DontSpawnProcess(Exception):
 
363
    """A simple exception which just allows us to skip unnecessary steps"""
 
364
 
 
365
 
 
366
class TestRunBzrSubprocessCommands(TestCaseWithTransport):
 
367
 
 
368
    def _popen(self, *args, **kwargs):
 
369
        """Record the command that is run, so that we can ensure it is correct"""
 
370
        self._popen_args = args
 
371
        self._popen_kwargs = kwargs
 
372
        raise _DontSpawnProcess()
 
373
 
 
374
    def test_run_bzr_subprocess_no_plugins(self):
 
375
        self.assertRaises(_DontSpawnProcess, self.run_bzr_subprocess)
 
376
        command = self._popen_args[0]
 
377
        self.assertEqual(sys.executable, command[0])
 
378
        self.assertEqual(self.get_bzr_path(), command[1])
 
379
        self.assertEqual(['--no-plugins'], command[2:])
 
380
 
 
381
    def test_allow_plugins(self):
 
382
        self.assertRaises(_DontSpawnProcess,
 
383
                          self.run_bzr_subprocess, allow_plugins=True)
 
384
        command = self._popen_args[0]
 
385
        self.assertEqual([], command[2:])
 
386
 
 
387
 
 
388
class TestBzrSubprocess(TestCaseWithTransport):
 
389
 
 
390
    def test_start_and_stop_bzr_subprocess(self):
 
391
        """We can start and perform other test actions while that process is
 
392
        still alive.
 
393
        """
 
394
        process = self.start_bzr_subprocess(['--version'])
 
395
        result = self.finish_bzr_subprocess(process)
 
396
        self.assertContainsRe(result[0], 'is free software')
 
397
        self.assertEqual('', result[1])
 
398
 
 
399
    def test_start_and_stop_bzr_subprocess_with_error(self):
 
400
        """finish_bzr_subprocess allows specification of the desired exit code.
 
401
        """
 
402
        process = self.start_bzr_subprocess(['--versionn'])
 
403
        result = self.finish_bzr_subprocess(process, retcode=3)
 
404
        self.assertEqual('', result[0])
 
405
        self.assertContainsRe(result[1], 'unknown command')
 
406
 
 
407
    def test_start_and_stop_bzr_subprocess_ignoring_retcode(self):
 
408
        """finish_bzr_subprocess allows the exit code to be ignored."""
 
409
        process = self.start_bzr_subprocess(['--versionn'])
 
410
        result = self.finish_bzr_subprocess(process, retcode=None)
 
411
        self.assertEqual('', result[0])
 
412
        self.assertContainsRe(result[1], 'unknown command')
 
413
 
 
414
    def test_start_and_stop_bzr_subprocess_with_unexpected_retcode(self):
 
415
        """finish_bzr_subprocess raises self.failureException if the retcode is
 
416
        not the expected one.
 
417
        """
 
418
        process = self.start_bzr_subprocess(['--versionn'])
 
419
        self.assertRaises(self.failureException, self.finish_bzr_subprocess,
 
420
                          process, retcode=0)
 
421
        
 
422
    def test_start_and_stop_bzr_subprocess_send_signal(self):
 
423
        """finish_bzr_subprocess raises self.failureException if the retcode is
 
424
        not the expected one.
 
425
        """
 
426
        process = self.start_bzr_subprocess(['wait-until-signalled'],
 
427
                                            skip_if_plan_to_signal=True)
 
428
        self.assertEqual('running\n', process.stdout.readline())
 
429
        result = self.finish_bzr_subprocess(process, send_signal=signal.SIGINT,
 
430
                                            retcode=3)
 
431
        self.assertEqual('', result[0])
 
432
        self.assertEqual('bzr: interrupted\n', result[1])
 
433
 
 
434
    def test_start_and_stop_working_dir(self):
 
435
        cwd = osutils.getcwd()
 
436
 
 
437
        self.make_branch_and_tree('one')
 
438
 
 
439
        process = self.start_bzr_subprocess(['root'], working_dir='one')
 
440
        result = self.finish_bzr_subprocess(process)
 
441
        self.assertEndsWith(result[0], 'one\n')
 
442
        self.assertEqual('', result[1])
 
443
        
242
444
 
243
445
class TestRunBzrError(ExternalBase):
244
446