~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2005-07-04 12:26:02 UTC
  • Revision ID: mbp@sourcefrog.net-20050704122602-69901910521e62c3
- check command checks that all inventory-ids are the same as in the revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2007 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
"""UI tests for the test framework."""
18
 
 
19
 
import os
20
 
import re
21
 
import signal
22
 
import sys
23
 
 
24
 
import bzrlib
25
 
from bzrlib import (
26
 
    osutils,
27
 
    )
28
 
from bzrlib.errors import ParamikoNotPresent
29
 
from bzrlib.tests import (
30
 
                          TestCase,
31
 
                          TestCaseInTempDir,
32
 
                          TestCaseWithMemoryTransport,
33
 
                          TestCaseWithTransport,
34
 
                          TestUIFactory,
35
 
                          TestSkipped,
36
 
                          )
37
 
from bzrlib.symbol_versioning import (
38
 
    zero_eighteen,
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)
89
 
        finally:
90
 
            bzrlib.tests.default_transport = old_transport
91
 
            TestOptions.current_test = None
92
 
            TestCaseWithMemoryTransport.TEST_ROOT = old_root
93
 
 
94
 
 
95
 
class TestRunBzr(ExternalBase):
96
 
 
97
 
    def _run_bzr_core(self, argv, retcode=0, encoding=None, stdin=None,
98
 
                         working_dir=None):
99
 
        """Override _run_bzr_core to test how it is invoked by run_bzr.
100
 
 
101
 
        Attempts to run bzr from inside this class don't actually run it.
102
 
 
103
 
        We test how run_bzr actually invokes bzr in another location.
104
 
        Here we only need to test that it is run_bzr passes the right
105
 
        parameters to run_bzr.
106
 
        """
107
 
        self.argv = list(argv)
108
 
        self.retcode = retcode
109
 
        self.encoding = encoding
110
 
        self.stdin = stdin
111
 
        self.working_dir = working_dir
112
 
        return '', ''
113
 
 
114
 
    def test_args(self):
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.'],
118
 
                self.run_bzr,
119
 
                'arg1', 'arg2', 'arg3', retcode=1)
120
 
        self.assertEqual(['arg1', 'arg2', 'arg3'], self.argv)
121
 
 
122
 
    def test_encoding(self):
123
 
        """Test that run_bzr passes encoding to _run_bzr_core"""
124
 
        self.run_bzr('foo bar')
125
 
        self.assertEqual(None, self.encoding)
126
 
        self.assertEqual(['foo', 'bar'], self.argv)
127
 
 
128
 
        self.run_bzr('foo bar', encoding='baz')
129
 
        self.assertEqual('baz', self.encoding)
130
 
        self.assertEqual(['foo', 'bar'], self.argv)
131
 
 
132
 
    def test_retcode(self):
133
 
        """Test that run_bzr passes retcode to _run_bzr_core"""
134
 
        # Default is retcode == 0
135
 
        self.run_bzr('foo bar')
136
 
        self.assertEqual(0, self.retcode)
137
 
        self.assertEqual(['foo', 'bar'], self.argv)
138
 
 
139
 
        self.run_bzr('foo bar', retcode=1)
140
 
        self.assertEqual(1, self.retcode)
141
 
        self.assertEqual(['foo', 'bar'], self.argv)
142
 
 
143
 
        self.run_bzr('foo bar', retcode=None)
144
 
        self.assertEqual(None, self.retcode)
145
 
        self.assertEqual(['foo', 'bar'], self.argv)
146
 
 
147
 
        self.run_bzr(['foo', 'bar'], retcode=3)
148
 
        self.assertEqual(3, self.retcode)
149
 
        self.assertEqual(['foo', 'bar'], self.argv)
150
 
 
151
 
    def test_stdin(self):
152
 
        # test that the stdin keyword to run_bzr is passed through to
153
 
        # _run_bzr_core as-is. We do this by overriding
154
 
        # _run_bzr_core in this class, and then calling run_bzr,
155
 
        # which is a convenience function for _run_bzr_core, so 
156
 
        # should invoke it.
157
 
        self.run_bzr('foo bar', stdin='gam')
158
 
        self.assertEqual('gam', self.stdin)
159
 
        self.assertEqual(['foo', 'bar'], self.argv)
160
 
 
161
 
        self.run_bzr('foo bar', stdin='zippy')
162
 
        self.assertEqual('zippy', self.stdin)
163
 
        self.assertEqual(['foo', 'bar'], self.argv)
164
 
 
165
 
    def test_working_dir(self):
166
 
        """Test that run_bzr passes working_dir to _run_bzr_core"""
167
 
        self.run_bzr('foo bar')
168
 
        self.assertEqual(None, self.working_dir)
169
 
        self.assertEqual(['foo', 'bar'], self.argv)
170
 
 
171
 
        self.run_bzr('foo bar', working_dir='baz')
172
 
        self.assertEqual('baz', self.working_dir)
173
 
        self.assertEqual(['foo', 'bar'], self.argv)
174
 
 
175
 
    def test_reject_extra_keyword_arguments(self):
176
 
        self.assertRaises(TypeError, self.run_bzr, "foo bar",
177
 
                          error_regex=['error message'])
178
 
 
179
 
 
180
 
class TestBenchmarkTests(TestCaseWithTransport):
181
 
 
182
 
    def test_benchmark_runs_benchmark_tests(self):
183
 
        """bzr selftest --benchmark should not run the default test suite."""
184
 
        # We test this by passing a regression test name to --benchmark, which
185
 
        # should result in 0 rests run.
186
 
        old_root = TestCaseWithMemoryTransport.TEST_ROOT
187
 
        try:
188
 
            TestCaseWithMemoryTransport.TEST_ROOT = None
189
 
            out, err = self.run_bzr('selftest --benchmark'
190
 
                                    ' workingtree_implementations')
191
 
        finally:
192
 
            TestCaseWithMemoryTransport.TEST_ROOT = old_root
193
 
        self.assertContainsRe(out, 'Ran 0 tests.*\n\nOK')
194
 
        self.assertEqual(
195
 
            'tests passed\n',
196
 
            err)
197
 
        benchfile = open(".perf_history", "rt")
198
 
        try:
199
 
            lines = benchfile.readlines()
200
 
        finally:
201
 
            benchfile.close()
202
 
        self.assertEqual(1, len(lines))
203
 
        self.assertContainsRe(lines[0], "--date [0-9.]+")
204
 
 
205
 
 
206
 
class TestRunBzrCaptured(ExternalBase):
207
 
 
208
 
    def apply_redirected(self, stdin=None, stdout=None, stderr=None,
209
 
                         a_callable=None, *args, **kwargs):
210
 
        self.stdin = stdin
211
 
        self.factory_stdin = getattr(bzrlib.ui.ui_factory, "stdin", None)
212
 
        self.factory = bzrlib.ui.ui_factory
213
 
        self.working_dir = osutils.getcwd()
214
 
        stdout.write('foo\n')
215
 
        stderr.write('bar\n')
216
 
        return 0
217
 
 
218
 
    def test_stdin(self):
219
 
        # test that the stdin keyword to _run_bzr_core is passed through to
220
 
        # apply_redirected as a StringIO. We do this by overriding
221
 
        # apply_redirected in this class, and then calling _run_bzr_core,
222
 
        # which calls apply_redirected. 
223
 
        self.run_bzr(['foo', 'bar'], stdin='gam')
224
 
        self.assertEqual('gam', self.stdin.read())
225
 
        self.assertTrue(self.stdin is self.factory_stdin)
226
 
        self.run_bzr(['foo', 'bar'], stdin='zippy')
227
 
        self.assertEqual('zippy', self.stdin.read())
228
 
        self.assertTrue(self.stdin is self.factory_stdin)
229
 
 
230
 
    def test_ui_factory(self):
231
 
        # each invocation of self.run_bzr should get its
232
 
        # own UI factory, which is an instance of TestUIFactory,
233
 
        # with stdin, stdout and stderr attached to the stdin,
234
 
        # stdout and stderr of the invoked run_bzr
235
 
        current_factory = bzrlib.ui.ui_factory
236
 
        self.run_bzr(['foo'])
237
 
        self.failIf(current_factory is self.factory)
238
 
        self.assertNotEqual(sys.stdout, self.factory.stdout)
239
 
        self.assertNotEqual(sys.stderr, self.factory.stderr)
240
 
        self.assertEqual('foo\n', self.factory.stdout.getvalue())
241
 
        self.assertEqual('bar\n', self.factory.stderr.getvalue())
242
 
        self.assertIsInstance(self.factory, TestUIFactory)
243
 
 
244
 
    def test_working_dir(self):
245
 
        self.build_tree(['one/', 'two/'])
246
 
        cwd = osutils.getcwd()
247
 
 
248
 
        # Default is to work in the current directory
249
 
        self.run_bzr(['foo', 'bar'])
250
 
        self.assertEqual(cwd, self.working_dir)
251
 
 
252
 
        self.run_bzr(['foo', 'bar'], working_dir=None)
253
 
        self.assertEqual(cwd, self.working_dir)
254
 
 
255
 
        # The function should be run in the alternative directory
256
 
        # but afterwards the current working dir shouldn't be changed
257
 
        self.run_bzr(['foo', 'bar'], working_dir='one')
258
 
        self.assertNotEqual(cwd, self.working_dir)
259
 
        self.assertEndsWith(self.working_dir, 'one')
260
 
        self.assertEqual(cwd, osutils.getcwd())
261
 
 
262
 
        self.run_bzr(['foo', 'bar'], working_dir='two')
263
 
        self.assertNotEqual(cwd, self.working_dir)
264
 
        self.assertEndsWith(self.working_dir, 'two')
265
 
        self.assertEqual(cwd, osutils.getcwd())
266
 
 
267
 
 
268
 
class TestRunBzrSubprocess(TestCaseWithTransport):
269
 
 
270
 
    def test_run_bzr_subprocess(self):
271
 
        """The run_bzr_helper_external comand behaves nicely."""
272
 
        result = self.run_bzr_subprocess('--version')
273
 
        result = self.run_bzr_subprocess(['--version'])
274
 
        result = self.run_bzr_subprocess('--version', retcode=None)
275
 
        self.assertContainsRe(result[0], 'is free software')
276
 
        self.assertRaises(AssertionError, self.run_bzr_subprocess, 
277
 
                          '--versionn')
278
 
        result = self.run_bzr_subprocess('--versionn', retcode=3)
279
 
        result = self.run_bzr_subprocess('--versionn', retcode=None)
280
 
        self.assertContainsRe(result[1], 'unknown command')
281
 
        err = self.run_bzr_subprocess(['merge', '--merge-type',
282
 
                                      'magic merge'], retcode=3)[1]
283
 
        self.assertContainsRe(err, 'Bad value "magic merge" for option'
284
 
                              ' "merge-type"')
285
 
        self.callDeprecated(['passing varargs to run_bzr_subprocess was'
286
 
                             ' deprecated in version 0.91.'],
287
 
                            self.run_bzr_subprocess,
288
 
                            'arg1', 'arg2', 'arg3', retcode=3)
289
 
 
290
 
    def test_run_bzr_subprocess_env(self):
291
 
        """run_bzr_subprocess can set environment variables in the child only.
292
 
 
293
 
        These changes should not change the running process, only the child.
294
 
        """
295
 
        # The test suite should unset this variable
296
 
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
297
 
        out, err = self.run_bzr_subprocess('whoami', env_changes={
298
 
                                            'BZR_EMAIL':'Joe Foo <joe@foo.com>'
299
 
                                          }, universal_newlines=True)
300
 
        self.assertEqual('', err)
301
 
        self.assertEqual('Joe Foo <joe@foo.com>\n', out)
302
 
        # And it should not be modified
303
 
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
304
 
 
305
 
        # Do it again with a different address, just to make sure
306
 
        # it is actually changing
307
 
        out, err = self.run_bzr_subprocess('whoami', env_changes={
308
 
                                            'BZR_EMAIL':'Barry <bar@foo.com>'
309
 
                                          }, universal_newlines=True)
310
 
        self.assertEqual('', err)
311
 
        self.assertEqual('Barry <bar@foo.com>\n', out)
312
 
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
313
 
 
314
 
    def test_run_bzr_subprocess_env_del(self):
315
 
        """run_bzr_subprocess can remove environment variables too."""
316
 
        # Create a random email, so we are sure this won't collide
317
 
        rand_bzr_email = 'John Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
318
 
        rand_email = 'Jane Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
319
 
        os.environ['BZR_EMAIL'] = rand_bzr_email
320
 
        os.environ['EMAIL'] = rand_email
321
 
        try:
322
 
            # By default, the child will inherit the current env setting
323
 
            out, err = self.run_bzr_subprocess('whoami', universal_newlines=True)
324
 
            self.assertEqual('', err)
325
 
            self.assertEqual(rand_bzr_email + '\n', out)
326
 
 
327
 
            # Now that BZR_EMAIL is not set, it should fall back to EMAIL
328
 
            out, err = self.run_bzr_subprocess('whoami',
329
 
                                               env_changes={'BZR_EMAIL':None},
330
 
                                               universal_newlines=True)
331
 
            self.assertEqual('', err)
332
 
            self.assertEqual(rand_email + '\n', out)
333
 
 
334
 
            # This switches back to the default email guessing logic
335
 
            # Which shouldn't match either of the above addresses
336
 
            out, err = self.run_bzr_subprocess('whoami',
337
 
                           env_changes={'BZR_EMAIL':None, 'EMAIL':None},
338
 
                           universal_newlines=True)
339
 
 
340
 
            self.assertEqual('', err)
341
 
            self.assertNotEqual(rand_bzr_email + '\n', out)
342
 
            self.assertNotEqual(rand_email + '\n', out)
343
 
        finally:
344
 
            # TestCase cleans up BZR_EMAIL, and EMAIL at startup
345
 
            del os.environ['BZR_EMAIL']
346
 
            del os.environ['EMAIL']
347
 
 
348
 
    def test_run_bzr_subprocess_env_del_missing(self):
349
 
        """run_bzr_subprocess won't fail if deleting a nonexistant env var"""
350
 
        self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
351
 
        out, err = self.run_bzr_subprocess('rocks',
352
 
                        env_changes={'NON_EXISTANT_ENV_VAR':None},
353
 
                        universal_newlines=True)
354
 
        self.assertEqual('It sure does!\n', out)
355
 
        self.assertEqual('', err)
356
 
 
357
 
    def test_run_bzr_subprocess_working_dir(self):
358
 
        """Test that we can specify the working dir for the child"""
359
 
        cwd = osutils.getcwd()
360
 
 
361
 
        self.make_branch_and_tree('.')
362
 
        self.make_branch_and_tree('one')
363
 
        self.make_branch_and_tree('two')
364
 
 
365
 
        def get_root(**kwargs):
366
 
            """Spawn a process to get the 'root' of the tree.
367
 
 
368
 
            You can pass in arbitrary new arguments. This just makes
369
 
            sure that the returned path doesn't have trailing whitespace.
370
 
            """
371
 
            return self.run_bzr_subprocess('root', **kwargs)[0].rstrip()
372
 
 
373
 
        self.assertEqual(cwd, get_root())
374
 
        self.assertEqual(cwd, get_root(working_dir=None))
375
 
        # Has our path changed?
376
 
        self.assertEqual(cwd, osutils.getcwd())
377
 
 
378
 
        dir1 = get_root(working_dir='one')
379
 
        self.assertEndsWith(dir1, 'one')
380
 
        self.assertEqual(cwd, osutils.getcwd())
381
 
 
382
 
        dir2 = get_root(working_dir='two')
383
 
        self.assertEndsWith(dir2, 'two')
384
 
        self.assertEqual(cwd, osutils.getcwd())
385
 
 
386
 
 
387
 
class _DontSpawnProcess(Exception):
388
 
    """A simple exception which just allows us to skip unnecessary steps"""
389
 
 
390
 
 
391
 
class TestRunBzrSubprocessCommands(TestCaseWithTransport):
392
 
 
393
 
    def _popen(self, *args, **kwargs):
394
 
        """Record the command that is run, so that we can ensure it is correct"""
395
 
        self._popen_args = args
396
 
        self._popen_kwargs = kwargs
397
 
        raise _DontSpawnProcess()
398
 
 
399
 
    def test_run_bzr_subprocess_no_plugins(self):
400
 
        self.assertRaises(_DontSpawnProcess, self.run_bzr_subprocess, '')
401
 
        command = self._popen_args[0]
402
 
        self.assertEqual(sys.executable, command[0])
403
 
        self.assertEqual(self.get_bzr_path(), command[1])
404
 
        self.assertEqual(['--no-plugins'], command[2:])
405
 
 
406
 
    def test_allow_plugins(self):
407
 
        self.assertRaises(_DontSpawnProcess,
408
 
                          self.run_bzr_subprocess, '', allow_plugins=True)
409
 
        command = self._popen_args[0]
410
 
        self.assertEqual([], command[2:])
411
 
 
412
 
 
413
 
class TestBzrSubprocess(TestCaseWithTransport):
414
 
 
415
 
    def test_start_and_stop_bzr_subprocess(self):
416
 
        """We can start and perform other test actions while that process is
417
 
        still alive.
418
 
        """
419
 
        process = self.start_bzr_subprocess(['--version'])
420
 
        result = self.finish_bzr_subprocess(process)
421
 
        self.assertContainsRe(result[0], 'is free software')
422
 
        self.assertEqual('', result[1])
423
 
 
424
 
    def test_start_and_stop_bzr_subprocess_with_error(self):
425
 
        """finish_bzr_subprocess allows specification of the desired exit code.
426
 
        """
427
 
        process = self.start_bzr_subprocess(['--versionn'])
428
 
        result = self.finish_bzr_subprocess(process, retcode=3)
429
 
        self.assertEqual('', result[0])
430
 
        self.assertContainsRe(result[1], 'unknown command')
431
 
 
432
 
    def test_start_and_stop_bzr_subprocess_ignoring_retcode(self):
433
 
        """finish_bzr_subprocess allows the exit code to be ignored."""
434
 
        process = self.start_bzr_subprocess(['--versionn'])
435
 
        result = self.finish_bzr_subprocess(process, retcode=None)
436
 
        self.assertEqual('', result[0])
437
 
        self.assertContainsRe(result[1], 'unknown command')
438
 
 
439
 
    def test_start_and_stop_bzr_subprocess_with_unexpected_retcode(self):
440
 
        """finish_bzr_subprocess raises self.failureException if the retcode is
441
 
        not the expected one.
442
 
        """
443
 
        process = self.start_bzr_subprocess(['--versionn'])
444
 
        self.assertRaises(self.failureException, self.finish_bzr_subprocess,
445
 
                          process)
446
 
        
447
 
    def test_start_and_stop_bzr_subprocess_send_signal(self):
448
 
        """finish_bzr_subprocess raises self.failureException if the retcode is
449
 
        not the expected one.
450
 
        """
451
 
        process = self.start_bzr_subprocess(['wait-until-signalled'],
452
 
                                            skip_if_plan_to_signal=True)
453
 
        self.assertEqual('running\n', process.stdout.readline())
454
 
        result = self.finish_bzr_subprocess(process, send_signal=signal.SIGINT,
455
 
                                            retcode=3)
456
 
        self.assertEqual('', result[0])
457
 
        self.assertEqual('bzr: interrupted\n', result[1])
458
 
 
459
 
    def test_start_and_stop_working_dir(self):
460
 
        cwd = osutils.getcwd()
461
 
 
462
 
        self.make_branch_and_tree('one')
463
 
 
464
 
        process = self.start_bzr_subprocess(['root'], working_dir='one')
465
 
        result = self.finish_bzr_subprocess(process, universal_newlines=True)
466
 
        self.assertEndsWith(result[0], 'one\n')
467
 
        self.assertEqual('', result[1])
468
 
 
469
 
 
470
 
class TestRunBzrError(ExternalBase):
471
 
 
472
 
    def test_run_bzr_error(self):
473
 
        # retcode=0 is specially needed here because run_bzr_error expects
474
 
        # an error (oddly enough) but we want to test the case of not
475
 
        # actually getting one
476
 
        out, err = self.run_bzr_error(['^$'], ['rocks'], retcode=0)
477
 
        self.assertEqual(out, 'It sure does!\n')
478
 
        # now test actually getting an error
479
 
        out, err = self.run_bzr_error(
480
 
                ["bzr: ERROR: foobarbaz is not versioned"],
481
 
                ['file-id', 'foobarbaz'])
482
 
 
483
 
 
484
 
class TestSelftestListOnly(TestCase):
485
 
 
486
 
    @staticmethod
487
 
    def _parse_test_list(lines, newlines_in_header=1):
488
 
        "Parse a list of lines into a tuple of 3 lists (header,body,footer)."
489
 
        in_header = True
490
 
        in_footer = False
491
 
        header = []
492
 
        body = []
493
 
        footer = []
494
 
        header_newlines_found = 0
495
 
        for line in lines:
496
 
            if in_header:
497
 
                if line == '':
498
 
                    header_newlines_found += 1
499
 
                    if header_newlines_found >= newlines_in_header:
500
 
                        in_header = False
501
 
                        continue
502
 
                header.append(line)
503
 
            elif not in_footer:
504
 
                if line.startswith('-------'):
505
 
                    in_footer = True
506
 
                else:
507
 
                    body.append(line)
508
 
            else:
509
 
                footer.append(line)
510
 
        # If the last body line is blank, drop it off the list
511
 
        if len(body) > 0 and body[-1] == '':
512
 
            body.pop()
513
 
        return (header,body,footer)
514
 
 
515
 
    def test_list_only(self):
516
 
        # check that bzr selftest --list-only works correctly
517
 
        out,err = self.run_bzr('selftest selftest --list-only')
518
 
        self.assertEndsWith(err, 'tests passed\n')
519
 
        (header,body,footer) = self._parse_test_list(out.splitlines())
520
 
        num_tests = len(body)
521
 
        self.assertContainsRe(footer[0], 'Listed %s tests in' % num_tests)
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(), 2)
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(), 2)
567
 
        self.assertEqual(tests_rand, tests_rand2)
568