~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: John Arbash Meinel
  • Date: 2009-03-27 22:29:55 UTC
  • mto: (3735.39.2 clean)
  • mto: This revision was merged to the branch mainline in revision 4280.
  • Revision ID: john@arbash-meinel.com-20090327222955-utifmfm888zerixt
Implement apply_delta_to_source which doesn't have to malloc another string.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""UI tests for the test framework."""
18
18
 
 
19
from cStringIO import StringIO
 
20
import os
 
21
import re
 
22
import signal
 
23
import sys
 
24
import unittest
 
25
 
 
26
import bzrlib
19
27
from bzrlib import (
20
 
    benchmarks,
21
 
    tests,
 
28
    osutils,
22
29
    )
 
30
from bzrlib.errors import ParamikoNotPresent
23
31
from bzrlib.tests import (
24
 
    features,
25
 
    )
26
 
from bzrlib.transport import memory
27
 
 
28
 
class SelfTestPatch:
29
 
 
30
 
    def get_params_passed_to_core(self, cmdline):
31
 
        params = []
32
 
        def selftest(*args, **kwargs):
33
 
            """Capture the arguments selftest was run with."""
34
 
            params.append((args, kwargs))
35
 
            return True
36
 
        # Yes this prevents using threads to run the test suite in parallel,
37
 
        # however we don't have a clean dependency injector for commands, 
38
 
        # and even if we did - we'd still be testing that the glue is wired
39
 
        # up correctly. XXX: TODO: Solve this testing problem.
40
 
        original_selftest = tests.selftest
41
 
        tests.selftest = selftest
42
 
        try:
43
 
            self.run_bzr(cmdline)
44
 
            return params[0]
 
32
                          SubUnitFeature,
 
33
                          TestCase,
 
34
                          TestCaseInTempDir,
 
35
                          TestCaseWithMemoryTransport,
 
36
                          TestCaseWithTransport,
 
37
                          TestUIFactory,
 
38
                          TestSkipped,
 
39
                          )
 
40
from bzrlib.tests.blackbox import ExternalBase
 
41
 
 
42
 
 
43
class TestOptions(TestCase):
 
44
 
 
45
    current_test = None
 
46
 
 
47
    def test_transport_set_to_sftp(self):
 
48
        # test the --transport option has taken effect from within the
 
49
        # test_transport test
 
50
        try:
 
51
            import bzrlib.transport.sftp
 
52
        except ParamikoNotPresent:
 
53
            raise TestSkipped("Paramiko not present")
 
54
        if TestOptions.current_test != "test_transport_set_to_sftp":
 
55
            return
 
56
        self.assertEqual(bzrlib.transport.sftp.SFTPAbsoluteServer,
 
57
                         bzrlib.tests.default_transport)
 
58
 
 
59
    def test_transport_set_to_memory(self):
 
60
        # test the --transport option has taken effect from within the
 
61
        # test_transport test
 
62
        import bzrlib.transport.memory
 
63
        if TestOptions.current_test != "test_transport_set_to_memory":
 
64
            return
 
65
        self.assertEqual(bzrlib.transport.memory.MemoryServer,
 
66
                         bzrlib.tests.default_transport)
 
67
 
 
68
    def test_transport(self):
 
69
        # test that --transport=sftp works
 
70
        try:
 
71
            import bzrlib.transport.sftp
 
72
        except ParamikoNotPresent:
 
73
            raise TestSkipped("Paramiko not present")
 
74
        old_transport = bzrlib.tests.default_transport
 
75
        old_root = TestCaseWithMemoryTransport.TEST_ROOT
 
76
        TestCaseWithMemoryTransport.TEST_ROOT = None
 
77
        try:
 
78
            TestOptions.current_test = "test_transport_set_to_sftp"
 
79
            stdout = self.run_bzr(
 
80
                'selftest --transport=sftp test_transport_set_to_sftp')[0]
 
81
            self.assertContainsRe(stdout, 'Ran 1 test')
 
82
            self.assertEqual(old_transport, bzrlib.tests.default_transport)
 
83
 
 
84
            TestOptions.current_test = "test_transport_set_to_memory"
 
85
            stdout = self.run_bzr(
 
86
                'selftest --transport=memory test_transport_set_to_memory')[0]
 
87
            self.assertContainsRe(stdout, 'Ran 1 test')
 
88
            self.assertEqual(old_transport, bzrlib.tests.default_transport)
45
89
        finally:
46
 
            tests.selftest = original_selftest
47
 
 
48
 
 
49
 
class TestOptionsWritingToDisk(tests.TestCaseInTempDir, SelfTestPatch):
 
90
            bzrlib.tests.default_transport = old_transport
 
91
            TestOptions.current_test = None
 
92
            TestCaseWithMemoryTransport.TEST_ROOT = old_root
 
93
 
 
94
    def test_subunit(self):
 
95
        """Passing --subunit results in subunit output."""
 
96
        self.requireFeature(SubUnitFeature)
 
97
        from subunit import ProtocolTestCase
 
98
        stdout = self.run_bzr(
 
99
            'selftest --subunit --no-plugins '
 
100
            'tests.test_selftest.SelftestTests.test_import_tests')[0]
 
101
        stream = StringIO(str(stdout))
 
102
        test = ProtocolTestCase(stream)
 
103
        result = unittest.TestResult()
 
104
        test.run(result)
 
105
        # 1 to deal with the 'test:' noise at the start, and 1 for the one we
 
106
        # ran.
 
107
        self.assertEqual(2, result.testsRun)
 
108
 
 
109
 
 
110
class TestRunBzr(ExternalBase):
 
111
 
 
112
    def _run_bzr_core(self, argv, retcode=0, encoding=None, stdin=None,
 
113
                         working_dir=None):
 
114
        """Override _run_bzr_core to test how it is invoked by run_bzr.
 
115
 
 
116
        Attempts to run bzr from inside this class don't actually run it.
 
117
 
 
118
        We test how run_bzr actually invokes bzr in another location.
 
119
        Here we only need to test that it is run_bzr passes the right
 
120
        parameters to run_bzr.
 
121
        """
 
122
        self.argv = list(argv)
 
123
        self.retcode = retcode
 
124
        self.encoding = encoding
 
125
        self.stdin = stdin
 
126
        self.working_dir = working_dir
 
127
        return '', ''
 
128
 
 
129
    def test_encoding(self):
 
130
        """Test that run_bzr passes encoding to _run_bzr_core"""
 
131
        self.run_bzr('foo bar')
 
132
        self.assertEqual(None, self.encoding)
 
133
        self.assertEqual(['foo', 'bar'], self.argv)
 
134
 
 
135
        self.run_bzr('foo bar', encoding='baz')
 
136
        self.assertEqual('baz', self.encoding)
 
137
        self.assertEqual(['foo', 'bar'], self.argv)
 
138
 
 
139
    def test_retcode(self):
 
140
        """Test that run_bzr passes retcode to _run_bzr_core"""
 
141
        # Default is retcode == 0
 
142
        self.run_bzr('foo bar')
 
143
        self.assertEqual(0, self.retcode)
 
144
        self.assertEqual(['foo', 'bar'], self.argv)
 
145
 
 
146
        self.run_bzr('foo bar', retcode=1)
 
147
        self.assertEqual(1, self.retcode)
 
148
        self.assertEqual(['foo', 'bar'], self.argv)
 
149
 
 
150
        self.run_bzr('foo bar', retcode=None)
 
151
        self.assertEqual(None, self.retcode)
 
152
        self.assertEqual(['foo', 'bar'], self.argv)
 
153
 
 
154
        self.run_bzr(['foo', 'bar'], retcode=3)
 
155
        self.assertEqual(3, self.retcode)
 
156
        self.assertEqual(['foo', 'bar'], self.argv)
 
157
 
 
158
    def test_stdin(self):
 
159
        # test that the stdin keyword to run_bzr is passed through to
 
160
        # _run_bzr_core as-is. We do this by overriding
 
161
        # _run_bzr_core in this class, and then calling run_bzr,
 
162
        # which is a convenience function for _run_bzr_core, so
 
163
        # should invoke it.
 
164
        self.run_bzr('foo bar', stdin='gam')
 
165
        self.assertEqual('gam', self.stdin)
 
166
        self.assertEqual(['foo', 'bar'], self.argv)
 
167
 
 
168
        self.run_bzr('foo bar', stdin='zippy')
 
169
        self.assertEqual('zippy', self.stdin)
 
170
        self.assertEqual(['foo', 'bar'], self.argv)
 
171
 
 
172
    def test_working_dir(self):
 
173
        """Test that run_bzr passes working_dir to _run_bzr_core"""
 
174
        self.run_bzr('foo bar')
 
175
        self.assertEqual(None, self.working_dir)
 
176
        self.assertEqual(['foo', 'bar'], self.argv)
 
177
 
 
178
        self.run_bzr('foo bar', working_dir='baz')
 
179
        self.assertEqual('baz', self.working_dir)
 
180
        self.assertEqual(['foo', 'bar'], self.argv)
 
181
 
 
182
    def test_reject_extra_keyword_arguments(self):
 
183
        self.assertRaises(TypeError, self.run_bzr, "foo bar",
 
184
                          error_regex=['error message'])
 
185
 
 
186
 
 
187
class TestBenchmarkTests(TestCaseWithTransport):
50
188
 
51
189
    def test_benchmark_runs_benchmark_tests(self):
52
 
        """selftest --benchmark should change the suite factory."""
53
 
        params = self.get_params_passed_to_core('selftest --benchmark')
54
 
        self.assertEqual(benchmarks.test_suite,
55
 
            params[1]['test_suite_factory'])
56
 
        self.assertNotEqual(None, params[1]['bench_history'])
 
190
        """bzr selftest --benchmark should not run the default test suite."""
 
191
        # We test this by passing a regression test name to --benchmark, which
 
192
        # should result in 0 tests run.
 
193
        old_root = TestCaseWithMemoryTransport.TEST_ROOT
 
194
        try:
 
195
            TestCaseWithMemoryTransport.TEST_ROOT = None
 
196
            out, err = self.run_bzr('selftest --benchmark'
 
197
                                    ' workingtree_implementations')
 
198
        finally:
 
199
            TestCaseWithMemoryTransport.TEST_ROOT = old_root
 
200
        self.assertContainsRe(out, 'Ran 0 tests.*\n\nOK')
 
201
        self.assertEqual(
 
202
            'tests passed\n',
 
203
            err)
57
204
        benchfile = open(".perf_history", "rt")
58
205
        try:
59
206
            lines = benchfile.readlines()
60
207
        finally:
61
208
            benchfile.close()
62
 
        # Because we don't run the actual test code no output is made to the
63
 
        # file.
64
 
        self.assertEqual(0, len(lines))
65
 
 
66
 
 
67
 
class TestOptions(tests.TestCase, SelfTestPatch):
68
 
 
69
 
    def test_load_list(self):
70
 
        params = self.get_params_passed_to_core('selftest --load-list foo')
71
 
        self.assertEqual('foo', params[1]['load_list'])
72
 
 
73
 
    def test_transport_set_to_sftp(self):
74
 
        # Test that we can pass a transport to the selftest core - sftp
75
 
        # version.
76
 
        self.requireFeature(features.paramiko)
77
 
        from bzrlib.tests import stub_sftp
78
 
        params = self.get_params_passed_to_core('selftest --transport=sftp')
79
 
        self.assertEqual(stub_sftp.SFTPAbsoluteServer,
80
 
            params[1]["transport"])
81
 
 
82
 
    def test_transport_set_to_memory(self):
83
 
        # Test that we can pass a transport to the selftest core - memory
84
 
        # version.
85
 
        params = self.get_params_passed_to_core('selftest --transport=memory')
86
 
        self.assertEqual(memory.MemoryServer, params[1]["transport"])
87
 
 
88
 
    def test_parameters_passed_to_core(self):
89
 
        params = self.get_params_passed_to_core('selftest --list-only')
90
 
        self.assertTrue("list_only" in params[1])
91
 
        params = self.get_params_passed_to_core('selftest --list-only selftest')
92
 
        self.assertTrue("list_only" in params[1])
93
 
        params = self.get_params_passed_to_core(['selftest', '--list-only',
94
 
            '--exclude', 'selftest'])
95
 
        self.assertTrue("list_only" in params[1])
96
 
        params = self.get_params_passed_to_core(['selftest', '--list-only',
97
 
            'selftest', '--randomize', 'now'])
98
 
        self.assertSubset(["list_only", "random_seed"], params[1])
99
 
 
100
 
    def test_starting_with(self):
101
 
        params = self.get_params_passed_to_core('selftest --starting-with foo')
102
 
        self.assertEqual(['foo'], params[1]['starting_with'])
103
 
 
104
 
    def test_starting_with_multiple_argument(self):
105
 
        params = self.get_params_passed_to_core(
106
 
            'selftest --starting-with foo --starting-with bar')
107
 
        self.assertEqual(['foo', 'bar'], params[1]['starting_with'])
108
 
 
109
 
    def test_subunit(self):
110
 
        self.requireFeature(features.subunit)
111
 
        params = self.get_params_passed_to_core('selftest --subunit')
112
 
        self.assertEqual(tests.SubUnitBzrRunner, params[1]['runner_class'])
113
 
 
114
 
    def _parse_test_list(self, lines, newlines_in_header=0):
 
209
        self.assertEqual(1, len(lines))
 
210
        self.assertContainsRe(lines[0], "--date [0-9.]+")
 
211
 
 
212
 
 
213
class TestRunBzrCaptured(ExternalBase):
 
214
 
 
215
    def apply_redirected(self, stdin=None, stdout=None, stderr=None,
 
216
                         a_callable=None, *args, **kwargs):
 
217
        self.stdin = stdin
 
218
        self.factory_stdin = getattr(bzrlib.ui.ui_factory, "stdin", None)
 
219
        self.factory = bzrlib.ui.ui_factory
 
220
        self.working_dir = osutils.getcwd()
 
221
        stdout.write('foo\n')
 
222
        stderr.write('bar\n')
 
223
        return 0
 
224
 
 
225
    def test_stdin(self):
 
226
        # test that the stdin keyword to _run_bzr_core is passed through to
 
227
        # apply_redirected as a StringIO. We do this by overriding
 
228
        # apply_redirected in this class, and then calling _run_bzr_core,
 
229
        # which calls apply_redirected.
 
230
        self.run_bzr(['foo', 'bar'], stdin='gam')
 
231
        self.assertEqual('gam', self.stdin.read())
 
232
        self.assertTrue(self.stdin is self.factory_stdin)
 
233
        self.run_bzr(['foo', 'bar'], stdin='zippy')
 
234
        self.assertEqual('zippy', self.stdin.read())
 
235
        self.assertTrue(self.stdin is self.factory_stdin)
 
236
 
 
237
    def test_ui_factory(self):
 
238
        # each invocation of self.run_bzr should get its
 
239
        # own UI factory, which is an instance of TestUIFactory,
 
240
        # with stdin, stdout and stderr attached to the stdin,
 
241
        # stdout and stderr of the invoked run_bzr
 
242
        current_factory = bzrlib.ui.ui_factory
 
243
        self.run_bzr(['foo'])
 
244
        self.failIf(current_factory is self.factory)
 
245
        self.assertNotEqual(sys.stdout, self.factory.stdout)
 
246
        self.assertNotEqual(sys.stderr, self.factory.stderr)
 
247
        self.assertEqual('foo\n', self.factory.stdout.getvalue())
 
248
        self.assertEqual('bar\n', self.factory.stderr.getvalue())
 
249
        self.assertIsInstance(self.factory, TestUIFactory)
 
250
 
 
251
    def test_working_dir(self):
 
252
        self.build_tree(['one/', 'two/'])
 
253
        cwd = osutils.getcwd()
 
254
 
 
255
        # Default is to work in the current directory
 
256
        self.run_bzr(['foo', 'bar'])
 
257
        self.assertEqual(cwd, self.working_dir)
 
258
 
 
259
        self.run_bzr(['foo', 'bar'], working_dir=None)
 
260
        self.assertEqual(cwd, self.working_dir)
 
261
 
 
262
        # The function should be run in the alternative directory
 
263
        # but afterwards the current working dir shouldn't be changed
 
264
        self.run_bzr(['foo', 'bar'], working_dir='one')
 
265
        self.assertNotEqual(cwd, self.working_dir)
 
266
        self.assertEndsWith(self.working_dir, 'one')
 
267
        self.assertEqual(cwd, osutils.getcwd())
 
268
 
 
269
        self.run_bzr(['foo', 'bar'], working_dir='two')
 
270
        self.assertNotEqual(cwd, self.working_dir)
 
271
        self.assertEndsWith(self.working_dir, 'two')
 
272
        self.assertEqual(cwd, osutils.getcwd())
 
273
 
 
274
 
 
275
class TestRunBzrSubprocess(TestCaseWithTransport):
 
276
 
 
277
    def test_run_bzr_subprocess(self):
 
278
        """The run_bzr_helper_external command behaves nicely."""
 
279
        result = self.run_bzr_subprocess('--version')
 
280
        result = self.run_bzr_subprocess(['--version'])
 
281
        result = self.run_bzr_subprocess('--version', retcode=None)
 
282
        self.assertContainsRe(result[0], 'is free software')
 
283
        self.assertRaises(AssertionError, self.run_bzr_subprocess,
 
284
                          '--versionn')
 
285
        result = self.run_bzr_subprocess('--versionn', retcode=3)
 
286
        result = self.run_bzr_subprocess('--versionn', retcode=None)
 
287
        self.assertContainsRe(result[1], 'unknown command')
 
288
        err = self.run_bzr_subprocess(['merge', '--merge-type',
 
289
                                      'magic merge'], retcode=3)[1]
 
290
        self.assertContainsRe(err, 'Bad value "magic merge" for option'
 
291
                              ' "merge-type"')
 
292
 
 
293
    def test_run_bzr_subprocess_env(self):
 
294
        """run_bzr_subprocess can set environment variables in the child only.
 
295
 
 
296
        These changes should not change the running process, only the child.
 
297
        """
 
298
        # The test suite should unset this variable
 
299
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
 
300
        out, err = self.run_bzr_subprocess('whoami', env_changes={
 
301
                                            'BZR_EMAIL':'Joe Foo <joe@foo.com>'
 
302
                                          }, universal_newlines=True)
 
303
        self.assertEqual('', err)
 
304
        self.assertEqual('Joe Foo <joe@foo.com>\n', out)
 
305
        # And it should not be modified
 
306
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
 
307
 
 
308
        # Do it again with a different address, just to make sure
 
309
        # it is actually changing
 
310
        out, err = self.run_bzr_subprocess('whoami', env_changes={
 
311
                                            'BZR_EMAIL':'Barry <bar@foo.com>'
 
312
                                          }, universal_newlines=True)
 
313
        self.assertEqual('', err)
 
314
        self.assertEqual('Barry <bar@foo.com>\n', out)
 
315
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
 
316
 
 
317
    def test_run_bzr_subprocess_env_del(self):
 
318
        """run_bzr_subprocess can remove environment variables too."""
 
319
        # Create a random email, so we are sure this won't collide
 
320
        rand_bzr_email = 'John Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
 
321
        rand_email = 'Jane Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
 
322
        os.environ['BZR_EMAIL'] = rand_bzr_email
 
323
        os.environ['EMAIL'] = rand_email
 
324
        try:
 
325
            # By default, the child will inherit the current env setting
 
326
            out, err = self.run_bzr_subprocess('whoami', universal_newlines=True)
 
327
            self.assertEqual('', err)
 
328
            self.assertEqual(rand_bzr_email + '\n', out)
 
329
 
 
330
            # Now that BZR_EMAIL is not set, it should fall back to EMAIL
 
331
            out, err = self.run_bzr_subprocess('whoami',
 
332
                                               env_changes={'BZR_EMAIL':None},
 
333
                                               universal_newlines=True)
 
334
            self.assertEqual('', err)
 
335
            self.assertEqual(rand_email + '\n', out)
 
336
 
 
337
            # This switches back to the default email guessing logic
 
338
            # Which shouldn't match either of the above addresses
 
339
            out, err = self.run_bzr_subprocess('whoami',
 
340
                           env_changes={'BZR_EMAIL':None, 'EMAIL':None},
 
341
                           universal_newlines=True)
 
342
 
 
343
            self.assertEqual('', err)
 
344
            self.assertNotEqual(rand_bzr_email + '\n', out)
 
345
            self.assertNotEqual(rand_email + '\n', out)
 
346
        finally:
 
347
            # TestCase cleans up BZR_EMAIL, and EMAIL at startup
 
348
            del os.environ['BZR_EMAIL']
 
349
            del os.environ['EMAIL']
 
350
 
 
351
    def test_run_bzr_subprocess_env_del_missing(self):
 
352
        """run_bzr_subprocess won't fail if deleting a nonexistant env var"""
 
353
        self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
 
354
        out, err = self.run_bzr_subprocess('rocks',
 
355
                        env_changes={'NON_EXISTANT_ENV_VAR':None},
 
356
                        universal_newlines=True)
 
357
        self.assertEqual('It sure does!\n', out)
 
358
        self.assertEqual('', err)
 
359
 
 
360
    def test_run_bzr_subprocess_working_dir(self):
 
361
        """Test that we can specify the working dir for the child"""
 
362
        cwd = osutils.getcwd()
 
363
 
 
364
        self.make_branch_and_tree('.')
 
365
        self.make_branch_and_tree('one')
 
366
        self.make_branch_and_tree('two')
 
367
 
 
368
        def get_root(**kwargs):
 
369
            """Spawn a process to get the 'root' of the tree.
 
370
 
 
371
            You can pass in arbitrary new arguments. This just makes
 
372
            sure that the returned path doesn't have trailing whitespace.
 
373
            """
 
374
            return self.run_bzr_subprocess('root', **kwargs)[0].rstrip()
 
375
 
 
376
        self.assertEqual(cwd, get_root())
 
377
        self.assertEqual(cwd, get_root(working_dir=None))
 
378
        # Has our path changed?
 
379
        self.assertEqual(cwd, osutils.getcwd())
 
380
 
 
381
        dir1 = get_root(working_dir='one')
 
382
        self.assertEndsWith(dir1, 'one')
 
383
        self.assertEqual(cwd, osutils.getcwd())
 
384
 
 
385
        dir2 = get_root(working_dir='two')
 
386
        self.assertEndsWith(dir2, 'two')
 
387
        self.assertEqual(cwd, osutils.getcwd())
 
388
 
 
389
 
 
390
class _DontSpawnProcess(Exception):
 
391
    """A simple exception which just allows us to skip unnecessary steps"""
 
392
 
 
393
 
 
394
class TestRunBzrSubprocessCommands(TestCaseWithTransport):
 
395
 
 
396
    def _popen(self, *args, **kwargs):
 
397
        """Record the command that is run, so that we can ensure it is correct"""
 
398
        self._popen_args = args
 
399
        self._popen_kwargs = kwargs
 
400
        raise _DontSpawnProcess()
 
401
 
 
402
    def test_run_bzr_subprocess_no_plugins(self):
 
403
        self.assertRaises(_DontSpawnProcess, self.run_bzr_subprocess, '')
 
404
        command = self._popen_args[0]
 
405
        self.assertEqual(sys.executable, command[0])
 
406
        self.assertEqual(self.get_bzr_path(), command[1])
 
407
        self.assertEqual(['--no-plugins'], command[2:])
 
408
 
 
409
    def test_allow_plugins(self):
 
410
        self.assertRaises(_DontSpawnProcess,
 
411
                          self.run_bzr_subprocess, '', allow_plugins=True)
 
412
        command = self._popen_args[0]
 
413
        self.assertEqual([], command[2:])
 
414
 
 
415
 
 
416
class TestBzrSubprocess(TestCaseWithTransport):
 
417
 
 
418
    def test_start_and_stop_bzr_subprocess(self):
 
419
        """We can start and perform other test actions while that process is
 
420
        still alive.
 
421
        """
 
422
        process = self.start_bzr_subprocess(['--version'])
 
423
        result = self.finish_bzr_subprocess(process)
 
424
        self.assertContainsRe(result[0], 'is free software')
 
425
        self.assertEqual('', result[1])
 
426
 
 
427
    def test_start_and_stop_bzr_subprocess_with_error(self):
 
428
        """finish_bzr_subprocess allows specification of the desired exit code.
 
429
        """
 
430
        process = self.start_bzr_subprocess(['--versionn'])
 
431
        result = self.finish_bzr_subprocess(process, retcode=3)
 
432
        self.assertEqual('', result[0])
 
433
        self.assertContainsRe(result[1], 'unknown command')
 
434
 
 
435
    def test_start_and_stop_bzr_subprocess_ignoring_retcode(self):
 
436
        """finish_bzr_subprocess allows the exit code to be ignored."""
 
437
        process = self.start_bzr_subprocess(['--versionn'])
 
438
        result = self.finish_bzr_subprocess(process, retcode=None)
 
439
        self.assertEqual('', result[0])
 
440
        self.assertContainsRe(result[1], 'unknown command')
 
441
 
 
442
    def test_start_and_stop_bzr_subprocess_with_unexpected_retcode(self):
 
443
        """finish_bzr_subprocess raises self.failureException if the retcode is
 
444
        not the expected one.
 
445
        """
 
446
        process = self.start_bzr_subprocess(['--versionn'])
 
447
        self.assertRaises(self.failureException, self.finish_bzr_subprocess,
 
448
                          process)
 
449
 
 
450
    def test_start_and_stop_bzr_subprocess_send_signal(self):
 
451
        """finish_bzr_subprocess raises self.failureException if the retcode is
 
452
        not the expected one.
 
453
        """
 
454
        process = self.start_bzr_subprocess(['wait-until-signalled'],
 
455
                                            skip_if_plan_to_signal=True)
 
456
        self.assertEqual('running\n', process.stdout.readline())
 
457
        result = self.finish_bzr_subprocess(process, send_signal=signal.SIGINT,
 
458
                                            retcode=3)
 
459
        self.assertEqual('', result[0])
 
460
        self.assertEqual('bzr: interrupted\n', result[1])
 
461
 
 
462
    def test_start_and_stop_working_dir(self):
 
463
        cwd = osutils.getcwd()
 
464
 
 
465
        self.make_branch_and_tree('one')
 
466
 
 
467
        process = self.start_bzr_subprocess(['root'], working_dir='one')
 
468
        result = self.finish_bzr_subprocess(process, universal_newlines=True)
 
469
        self.assertEndsWith(result[0], 'one\n')
 
470
        self.assertEqual('', result[1])
 
471
 
 
472
 
 
473
class TestRunBzrError(ExternalBase):
 
474
 
 
475
    def test_run_bzr_error(self):
 
476
        # retcode=0 is specially needed here because run_bzr_error expects
 
477
        # an error (oddly enough) but we want to test the case of not
 
478
        # actually getting one
 
479
        out, err = self.run_bzr_error(['^$'], ['rocks'], retcode=0)
 
480
        self.assertEqual(out, 'It sure does!\n')
 
481
        # now test actually getting an error
 
482
        out, err = self.run_bzr_error(
 
483
                ["bzr: ERROR: foobarbaz is not versioned"],
 
484
                ['file-id', 'foobarbaz'])
 
485
 
 
486
 
 
487
class TestSelftestListOnly(TestCase):
 
488
 
 
489
    @staticmethod
 
490
    def _parse_test_list(lines, newlines_in_header=1):
115
491
        "Parse a list of lines into a tuple of 3 lists (header,body,footer)."
116
 
        in_header = newlines_in_header != 0
 
492
        in_header = True
117
493
        in_footer = False
118
494
        header = []
119
495
        body = []
140
516
        return (header,body,footer)
141
517
 
142
518
    def test_list_only(self):
143
 
        # check that bzr selftest --list-only outputs no ui noise
144
 
        def selftest(*args, **kwargs):
145
 
            """Capture the arguments selftest was run with."""
146
 
            return True
147
 
        def outputs_nothing(cmdline):
148
 
            out,err = self.run_bzr(cmdline)
149
 
            (header,body,footer) = self._parse_test_list(out.splitlines())
150
 
            num_tests = len(body)
151
 
            self.assertLength(0, header)
152
 
            self.assertLength(0, footer)
153
 
            self.assertEqual('', err)
154
 
        # Yes this prevents using threads to run the test suite in parallel,
155
 
        # however we don't have a clean dependency injector for commands, 
156
 
        # and even if we did - we'd still be testing that the glue is wired
157
 
        # up correctly. XXX: TODO: Solve this testing problem.
158
 
        original_selftest = tests.selftest
159
 
        tests.selftest = selftest
160
 
        try:
161
 
            outputs_nothing('selftest --list-only')
162
 
            outputs_nothing('selftest --list-only selftest')
163
 
            outputs_nothing(['selftest', '--list-only', '--exclude', 'selftest'])
164
 
        finally:
165
 
            tests.selftest = original_selftest
166
 
 
167
 
    def test_lsprof_tests(self):
168
 
        params = self.get_params_passed_to_core('selftest --lsprof-tests')
169
 
        self.assertEqual(True, params[1]["lsprof_tests"])
 
519
        # check that bzr selftest --list-only works correctly
 
520
        out,err = self.run_bzr('selftest selftest --list-only')
 
521
        self.assertEndsWith(err, 'tests passed\n')
 
522
        (header,body,footer) = self._parse_test_list(out.splitlines())
 
523
        num_tests = len(body)
 
524
        self.assertContainsRe(footer[0], 'Listed %s tests in' % num_tests)
 
525
 
 
526
    def test_list_only_filtered(self):
 
527
        # check that a filtered --list-only works, both include and exclude
 
528
        out_all,err_all = self.run_bzr('selftest --list-only')
 
529
        tests_all = self._parse_test_list(out_all.splitlines())[1]
 
530
        out_incl,err_incl = self.run_bzr('selftest --list-only selftest')
 
531
        tests_incl = self._parse_test_list(out_incl.splitlines())[1]
 
532
        self.assertSubset(tests_incl, tests_all)
 
533
        out_excl,err_excl = self.run_bzr(['selftest', '--list-only',
 
534
                                          '--exclude', 'selftest'])
 
535
        tests_excl = self._parse_test_list(out_excl.splitlines())[1]
 
536
        self.assertSubset(tests_excl, tests_all)
 
537
        set_incl = set(tests_incl)
 
538
        set_excl = set(tests_excl)
 
539
        intersection = set_incl.intersection(set_excl)
 
540
        self.assertEquals(0, len(intersection))
 
541
        self.assertEquals(len(tests_all), len(tests_incl) + len(tests_excl))
 
542
 
 
543
    def test_list_only_random(self):
 
544
        # check that --randomize works correctly
 
545
        out_all,err_all = self.run_bzr('selftest --list-only selftest')
 
546
        tests_all = self._parse_test_list(out_all.splitlines())[1]
 
547
        # XXX: It looks like there are some orders for generating tests that
 
548
        # fail as of 20070504 - maybe because of import order dependencies.
 
549
        # So unfortunately this will rarely intermittently fail at the moment.
 
550
        # -- mbp 20070504
 
551
        out_rand,err_rand = self.run_bzr(['selftest', '--list-only',
 
552
                                          'selftest', '--randomize', 'now'])
 
553
        (header_rand,tests_rand,dummy) = self._parse_test_list(
 
554
            out_rand.splitlines(), 2)
 
555
        # XXX: The following line asserts that the randomized order is not the
 
556
        # same as the default order.  It is just possible that they'll get
 
557
        # randomized into the same order and this will falsely fail, but
 
558
        # that's very unlikely in practice because there are thousands of
 
559
        # tests.
 
560
        self.assertNotEqual(tests_all, tests_rand)
 
561
        self.assertEqual(sorted(tests_all), sorted(tests_rand))
 
562
        # Check that the seed can be reused to get the exact same order
 
563
        seed_re = re.compile('Randomizing test order using seed (\w+)')
 
564
        match_obj = seed_re.search(header_rand[-1])
 
565
        seed = match_obj.group(1)
 
566
        out_rand2,err_rand2 = self.run_bzr(['selftest', '--list-only',
 
567
                                            'selftest', '--randomize', seed])
 
568
        (header_rand2,tests_rand2,dummy) = self._parse_test_list(
 
569
            out_rand2.splitlines(), 2)
 
570
        self.assertEqual(tests_rand, tests_rand2)
 
571
 
 
572
 
 
573
class TestSelftestWithIdList(TestCaseInTempDir):
 
574
 
 
575
    def test_load_list(self):
 
576
        # We don't want to call selftest for the whole suite, so we start with
 
577
        # a reduced list.
 
578
        test_list_fname = 'test.list'
 
579
        fl = open(test_list_fname, 'wt')
 
580
        fl.write('%s\n' % self.id())
 
581
        fl.close()
 
582
        out, err = self.run_bzr(
 
583
            ['selftest', '--load-list', test_list_fname, '--list'])
 
584
        self.assertContainsRe(out, "Listed 1 test in")
 
585
 
 
586
    def test_load_unknown(self):
 
587
        out, err = self.run_bzr('selftest --load-list I_do_not_exist ',
 
588
                                retcode=3)
 
589
 
 
590
 
 
591
class TestSelftestStartingWith(TestCase):
 
592
 
 
593
    def test_starting_with_single_argument(self):
 
594
        out, err = self.run_bzr(
 
595
            ['selftest', '--starting-with', self.id(), '--list'])
 
596
        self.assertContainsRe(out, "Listed 1 test in")
 
597
        self.assertContainsRe(out, self.id())
 
598
 
 
599
    def test_starting_with_multiple_argument(self):
 
600
        out, err = self.run_bzr(
 
601
            ['selftest',
 
602
             '--starting-with', self.id(),
 
603
             '--starting-with', 'bzrlib.tests.test_sampler',
 
604
             '--list'])
 
605
        self.assertContainsRe(out, "Listed 2 tests in")
 
606
        self.assertContainsRe(out, self.id())
 
607
        self.assertContainsRe(out, 'bzrlib.tests.test_sampler')