~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Matthew Fuller
  • Date: 2009-08-18 08:10:44 UTC
  • mto: (4772.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4773.
  • Revision ID: fullermd@over-yonder.net-20090818081044-2due6ius01c4pwjl
Fix up some doctests to handle things ending up as RevisionSpec_dwim's
instead of RS_revno, and ending up as _dwim's (which may error
eventually, but won't until we try to evaluate them) instead of
insta-errors.

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