~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Vincent Ladeuil
  • Date: 2008-01-03 08:49:38 UTC
  • mfrom: (3111.1.31 175524)
  • mto: This revision was merged to the branch mainline in revision 3158.
  • Revision ID: v.ladeuil+lp@free.fr-20080103084938-7kvurk5uvde2ui54
Fix bug #175524, http test servers are 1.1 compliant

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 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
34
34
                          TestUIFactory,
35
35
                          TestSkipped,
36
36
                          )
 
37
from bzrlib.symbol_versioning import (
 
38
    zero_eighteen,
 
39
    )
37
40
from bzrlib.tests.blackbox import ExternalBase
38
41
 
39
42
 
73
76
        TestCaseWithMemoryTransport.TEST_ROOT = None
74
77
        try:
75
78
            TestOptions.current_test = "test_transport_set_to_sftp"
76
 
            stdout = self.capture('selftest --transport=sftp test_transport_set_to_sftp')
77
 
            
 
79
            stdout = self.run_bzr(
 
80
                'selftest --transport=sftp test_transport_set_to_sftp')[0]
78
81
            self.assertContainsRe(stdout, 'Ran 1 test')
79
82
            self.assertEqual(old_transport, bzrlib.tests.default_transport)
80
83
 
81
84
            TestOptions.current_test = "test_transport_set_to_memory"
82
 
            stdout = self.capture('selftest --transport=memory test_transport_set_to_memory')
 
85
            stdout = self.run_bzr(
 
86
                'selftest --transport=memory test_transport_set_to_memory')[0]
83
87
            self.assertContainsRe(stdout, 'Ran 1 test')
84
88
            self.assertEqual(old_transport, bzrlib.tests.default_transport)
85
89
        finally:
90
94
 
91
95
class TestRunBzr(ExternalBase):
92
96
 
93
 
    def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
 
97
    def _run_bzr_core(self, argv, retcode=0, encoding=None, stdin=None,
94
98
                         working_dir=None):
95
 
        """Override run_bzr_captured to test how it is invoked by run_bzr.
96
 
 
97
 
        We test how run_bzr_captured actually invokes bzr in another location.
 
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.
98
104
        Here we only need to test that it is run_bzr passes the right
99
 
        parameters to run_bzr_captured.
 
105
        parameters to run_bzr.
100
106
        """
101
 
        self.argv = argv
 
107
        self.argv = list(argv)
102
108
        self.retcode = retcode
103
109
        self.encoding = encoding
104
110
        self.stdin = stdin
105
111
        self.working_dir = working_dir
106
112
        return '', ''
107
113
 
108
 
    def test_args(self):
109
 
        """Test that run_bzr passes args correctly to run_bzr_captured"""
110
 
        self.run_bzr('arg1', 'arg2', 'arg3', retcode=1)
111
 
        self.assertEqual(('arg1', 'arg2', 'arg3'), self.argv)
112
 
 
113
114
    def test_encoding(self):
114
 
        """Test that run_bzr passes encoding to run_bzr_captured"""
115
 
        self.run_bzr('foo', 'bar')
 
115
        """Test that run_bzr passes encoding to _run_bzr_core"""
 
116
        self.run_bzr('foo bar')
116
117
        self.assertEqual(None, self.encoding)
117
 
        self.assertEqual(('foo', 'bar'), self.argv)
 
118
        self.assertEqual(['foo', 'bar'], self.argv)
118
119
 
119
 
        self.run_bzr('foo', 'bar', encoding='baz')
 
120
        self.run_bzr('foo bar', encoding='baz')
120
121
        self.assertEqual('baz', self.encoding)
121
 
        self.assertEqual(('foo', 'bar'), self.argv)
 
122
        self.assertEqual(['foo', 'bar'], self.argv)
122
123
 
123
124
    def test_retcode(self):
124
 
        """Test that run_bzr passes retcode to run_bzr_captured"""
 
125
        """Test that run_bzr passes retcode to _run_bzr_core"""
125
126
        # Default is retcode == 0
126
 
        self.run_bzr('foo', 'bar')
 
127
        self.run_bzr('foo bar')
127
128
        self.assertEqual(0, self.retcode)
128
 
        self.assertEqual(('foo', 'bar'), self.argv)
 
129
        self.assertEqual(['foo', 'bar'], self.argv)
129
130
 
130
 
        self.run_bzr('foo', 'bar', retcode=1)
 
131
        self.run_bzr('foo bar', retcode=1)
131
132
        self.assertEqual(1, self.retcode)
132
 
        self.assertEqual(('foo', 'bar'), self.argv)
 
133
        self.assertEqual(['foo', 'bar'], self.argv)
133
134
 
134
 
        self.run_bzr('foo', 'bar', retcode=None)
 
135
        self.run_bzr('foo bar', retcode=None)
135
136
        self.assertEqual(None, self.retcode)
136
 
        self.assertEqual(('foo', 'bar'), self.argv)
 
137
        self.assertEqual(['foo', 'bar'], self.argv)
137
138
 
138
 
        self.run_bzr('foo', 'bar', retcode=3)
 
139
        self.run_bzr(['foo', 'bar'], retcode=3)
139
140
        self.assertEqual(3, self.retcode)
140
 
        self.assertEqual(('foo', 'bar'), self.argv)
 
141
        self.assertEqual(['foo', 'bar'], self.argv)
141
142
 
142
143
    def test_stdin(self):
143
144
        # test that the stdin keyword to run_bzr is passed through to
144
 
        # run_bzr_captured as-is. We do this by overriding
145
 
        # run_bzr_captured in this class, and then calling run_bzr,
146
 
        # which is a convenience function for run_bzr_captured, so 
 
145
        # _run_bzr_core as-is. We do this by overriding
 
146
        # _run_bzr_core in this class, and then calling run_bzr,
 
147
        # which is a convenience function for _run_bzr_core, so 
147
148
        # should invoke it.
148
 
        self.run_bzr('foo', 'bar', stdin='gam')
 
149
        self.run_bzr('foo bar', stdin='gam')
149
150
        self.assertEqual('gam', self.stdin)
150
 
        self.assertEqual(('foo', 'bar'), self.argv)
 
151
        self.assertEqual(['foo', 'bar'], self.argv)
151
152
 
152
 
        self.run_bzr('foo', 'bar', stdin='zippy')
 
153
        self.run_bzr('foo bar', stdin='zippy')
153
154
        self.assertEqual('zippy', self.stdin)
154
 
        self.assertEqual(('foo', 'bar'), self.argv)
 
155
        self.assertEqual(['foo', 'bar'], self.argv)
155
156
 
156
157
    def test_working_dir(self):
157
 
        """Test that run_bzr passes working_dir to run_bzr_captured"""
158
 
        self.run_bzr('foo', 'bar')
 
158
        """Test that run_bzr passes working_dir to _run_bzr_core"""
 
159
        self.run_bzr('foo bar')
159
160
        self.assertEqual(None, self.working_dir)
160
 
        self.assertEqual(('foo', 'bar'), self.argv)
 
161
        self.assertEqual(['foo', 'bar'], self.argv)
161
162
 
162
 
        self.run_bzr('foo', 'bar', working_dir='baz')
 
163
        self.run_bzr('foo bar', working_dir='baz')
163
164
        self.assertEqual('baz', self.working_dir)
164
 
        self.assertEqual(('foo', 'bar'), self.argv)
 
165
        self.assertEqual(['foo', 'bar'], self.argv)
 
166
 
 
167
    def test_reject_extra_keyword_arguments(self):
 
168
        self.assertRaises(TypeError, self.run_bzr, "foo bar",
 
169
                          error_regex=['error message'])
165
170
 
166
171
 
167
172
class TestBenchmarkTests(TestCaseWithTransport):
169
174
    def test_benchmark_runs_benchmark_tests(self):
170
175
        """bzr selftest --benchmark should not run the default test suite."""
171
176
        # We test this by passing a regression test name to --benchmark, which
172
 
        # should result in 0 rests run.
 
177
        # should result in 0 tests run.
173
178
        old_root = TestCaseWithMemoryTransport.TEST_ROOT
174
179
        try:
175
180
            TestCaseWithMemoryTransport.TEST_ROOT = None
176
 
            out, err = self.run_bzr('selftest', '--benchmark', 'workingtree_implementations')
 
181
            out, err = self.run_bzr('selftest --benchmark'
 
182
                                    ' workingtree_implementations')
177
183
        finally:
178
184
            TestCaseWithMemoryTransport.TEST_ROOT = old_root
179
185
        self.assertContainsRe(out, 'Ran 0 tests.*\n\nOK')
202
208
        return 0
203
209
 
204
210
    def test_stdin(self):
205
 
        # test that the stdin keyword to run_bzr_captured is passed through to
 
211
        # test that the stdin keyword to _run_bzr_core is passed through to
206
212
        # apply_redirected as a StringIO. We do this by overriding
207
 
        # apply_redirected in this class, and then calling run_bzr_captured,
 
213
        # apply_redirected in this class, and then calling _run_bzr_core,
208
214
        # which calls apply_redirected. 
209
 
        self.run_bzr_captured(['foo', 'bar'], stdin='gam')
 
215
        self.run_bzr(['foo', 'bar'], stdin='gam')
210
216
        self.assertEqual('gam', self.stdin.read())
211
217
        self.assertTrue(self.stdin is self.factory_stdin)
212
 
        self.run_bzr_captured(['foo', 'bar'], stdin='zippy')
 
218
        self.run_bzr(['foo', 'bar'], stdin='zippy')
213
219
        self.assertEqual('zippy', self.stdin.read())
214
220
        self.assertTrue(self.stdin is self.factory_stdin)
215
221
 
216
222
    def test_ui_factory(self):
217
 
        # each invocation of self.run_bzr_captured should get its
 
223
        # each invocation of self.run_bzr should get its
218
224
        # own UI factory, which is an instance of TestUIFactory,
219
225
        # with stdin, stdout and stderr attached to the stdin,
220
 
        # stdout and stderr of the invoked run_bzr_captured
 
226
        # stdout and stderr of the invoked run_bzr
221
227
        current_factory = bzrlib.ui.ui_factory
222
 
        self.run_bzr_captured(['foo'])
 
228
        self.run_bzr(['foo'])
223
229
        self.failIf(current_factory is self.factory)
224
230
        self.assertNotEqual(sys.stdout, self.factory.stdout)
225
231
        self.assertNotEqual(sys.stderr, self.factory.stderr)
232
238
        cwd = osutils.getcwd()
233
239
 
234
240
        # Default is to work in the current directory
235
 
        self.run_bzr_captured(['foo', 'bar'])
 
241
        self.run_bzr(['foo', 'bar'])
236
242
        self.assertEqual(cwd, self.working_dir)
237
243
 
238
 
        self.run_bzr_captured(['foo', 'bar'], working_dir=None)
 
244
        self.run_bzr(['foo', 'bar'], working_dir=None)
239
245
        self.assertEqual(cwd, self.working_dir)
240
246
 
241
247
        # The function should be run in the alternative directory
242
248
        # but afterwards the current working dir shouldn't be changed
243
 
        self.run_bzr_captured(['foo', 'bar'], working_dir='one')
 
249
        self.run_bzr(['foo', 'bar'], working_dir='one')
244
250
        self.assertNotEqual(cwd, self.working_dir)
245
251
        self.assertEndsWith(self.working_dir, 'one')
246
252
        self.assertEqual(cwd, osutils.getcwd())
247
253
 
248
 
        self.run_bzr_captured(['foo', 'bar'], working_dir='two')
 
254
        self.run_bzr(['foo', 'bar'], working_dir='two')
249
255
        self.assertNotEqual(cwd, self.working_dir)
250
256
        self.assertEndsWith(self.working_dir, 'two')
251
257
        self.assertEqual(cwd, osutils.getcwd())
256
262
    def test_run_bzr_subprocess(self):
257
263
        """The run_bzr_helper_external comand behaves nicely."""
258
264
        result = self.run_bzr_subprocess('--version')
 
265
        result = self.run_bzr_subprocess(['--version'])
259
266
        result = self.run_bzr_subprocess('--version', retcode=None)
260
267
        self.assertContainsRe(result[0], 'is free software')
261
268
        self.assertRaises(AssertionError, self.run_bzr_subprocess, 
263
270
        result = self.run_bzr_subprocess('--versionn', retcode=3)
264
271
        result = self.run_bzr_subprocess('--versionn', retcode=None)
265
272
        self.assertContainsRe(result[1], 'unknown command')
266
 
        err = self.run_bzr_subprocess('merge', '--merge-type', 'magic merge', 
267
 
                                      retcode=3)[1]
 
273
        err = self.run_bzr_subprocess(['merge', '--merge-type',
 
274
                                      'magic merge'], retcode=3)[1]
268
275
        self.assertContainsRe(err, 'Bad value "magic merge" for option'
269
276
                              ' "merge-type"')
 
277
        self.callDeprecated(['passing varargs to run_bzr_subprocess was'
 
278
                             ' deprecated in version 0.91.'],
 
279
                            self.run_bzr_subprocess,
 
280
                            'arg1', 'arg2', 'arg3', retcode=3)
270
281
 
271
282
    def test_run_bzr_subprocess_env(self):
272
283
        """run_bzr_subprocess can set environment variables in the child only.
378
389
        raise _DontSpawnProcess()
379
390
 
380
391
    def test_run_bzr_subprocess_no_plugins(self):
381
 
        self.assertRaises(_DontSpawnProcess, self.run_bzr_subprocess)
 
392
        self.assertRaises(_DontSpawnProcess, self.run_bzr_subprocess, '')
382
393
        command = self._popen_args[0]
383
394
        self.assertEqual(sys.executable, command[0])
384
395
        self.assertEqual(self.get_bzr_path(), command[1])
386
397
 
387
398
    def test_allow_plugins(self):
388
399
        self.assertRaises(_DontSpawnProcess,
389
 
                          self.run_bzr_subprocess, allow_plugins=True)
 
400
                          self.run_bzr_subprocess, '', allow_plugins=True)
390
401
        command = self._popen_args[0]
391
402
        self.assertEqual([], command[2:])
392
403
 
423
434
        """
424
435
        process = self.start_bzr_subprocess(['--versionn'])
425
436
        self.assertRaises(self.failureException, self.finish_bzr_subprocess,
426
 
                          process, retcode=0)
 
437
                          process)
427
438
        
428
439
    def test_start_and_stop_bzr_subprocess_send_signal(self):
429
440
        """finish_bzr_subprocess raises self.failureException if the retcode is
451
462
class TestRunBzrError(ExternalBase):
452
463
 
453
464
    def test_run_bzr_error(self):
454
 
        out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
 
465
        # retcode=0 is specially needed here because run_bzr_error expects
 
466
        # an error (oddly enough) but we want to test the case of not
 
467
        # actually getting one
 
468
        out, err = self.run_bzr_error(['^$'], ['rocks'], retcode=0)
455
469
        self.assertEqual(out, 'It sure does!\n')
456
 
 
457
 
        out, err = self.run_bzr_error(["bzr: ERROR: foobarbaz is not versioned"],
458
 
                                      'file-id', 'foobarbaz')
459
 
 
460
 
 
461
 
class TestSelftestCleanOutput(TestCaseInTempDir):
462
 
 
463
 
    def test_clean_output(self):
464
 
        # check that 'bzr selftest --clean-output' works correct
465
 
        dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
466
 
        files = ('bzr', 'setup.py', 'test9999.tmp')
467
 
        for i in dirs:
468
 
            os.mkdir(i)
469
 
        for i in files:
470
 
            f = file(i, 'wb')
471
 
            f.write('content of ')
472
 
            f.write(i)
473
 
            f.close()
474
 
 
475
 
        root = os.getcwdu()
476
 
        before = os.listdir(root)
477
 
        before.sort()
478
 
        self.assertEquals(['bzr','bzrlib','setup.py',
479
 
                           'test0000.tmp','test0001.tmp',
480
 
                           'test9999.tmp','tests'],
481
 
                           before)
482
 
 
483
 
        out,err = self.run_bzr_captured(['selftest','--clean-output'],
484
 
                                        working_dir=root)
485
 
 
486
 
        self.assertEquals(['delete directory: test0000.tmp',
487
 
                          'delete directory: test0001.tmp'],
488
 
                          sorted(out.splitlines()))
489
 
        self.assertEquals('', err)
490
 
 
491
 
        after = os.listdir(root)
492
 
        after.sort()
493
 
        self.assertEquals(['bzr','bzrlib','setup.py',
494
 
                           'test9999.tmp','tests'],
495
 
                           after)
 
470
        # now test actually getting an error
 
471
        out, err = self.run_bzr_error(
 
472
                ["bzr: ERROR: foobarbaz is not versioned"],
 
473
                ['file-id', 'foobarbaz'])
496
474
 
497
475
 
498
476
class TestSelftestListOnly(TestCase):
500
478
    @staticmethod
501
479
    def _parse_test_list(lines, newlines_in_header=1):
502
480
        "Parse a list of lines into a tuple of 3 lists (header,body,footer)."
503
 
 
504
481
        in_header = True
505
482
        in_footer = False
506
483
        header = []
507
484
        body = []
508
485
        footer = []
509
 
        header_newlines_found = 0 
 
486
        header_newlines_found = 0
510
487
        for line in lines:
511
488
            if in_header:
512
489
                if line == '':
524
501
                footer.append(line)
525
502
        # If the last body line is blank, drop it off the list
526
503
        if len(body) > 0 and body[-1] == '':
527
 
            body.pop()                
 
504
            body.pop()
528
505
        return (header,body,footer)
529
506
 
530
507
    def test_list_only(self):
531
508
        # check that bzr selftest --list-only works correctly
532
 
        out,err = self.run_bzr_captured(['selftest', 'selftest',
533
 
            '--list-only'])
 
509
        out,err = self.run_bzr('selftest selftest --list-only')
534
510
        self.assertEndsWith(err, 'tests passed\n')
535
511
        (header,body,footer) = self._parse_test_list(out.splitlines())
536
512
        num_tests = len(body)
538
514
 
539
515
    def test_list_only_filtered(self):
540
516
        # check that a filtered --list-only works, both include and exclude
541
 
        out_all,err_all = self.run_bzr_captured(['selftest', '--list-only'])
 
517
        out_all,err_all = self.run_bzr('selftest --list-only')
542
518
        tests_all = self._parse_test_list(out_all.splitlines())[1]
543
 
        out_incl,err_incl = self.run_bzr_captured(['selftest', '--list-only',
544
 
          'selftest'])
 
519
        out_incl,err_incl = self.run_bzr('selftest --list-only selftest')
545
520
        tests_incl = self._parse_test_list(out_incl.splitlines())[1]
546
521
        self.assertSubset(tests_incl, tests_all)
547
 
        out_excl,err_excl = self.run_bzr_captured(['selftest', '--list-only',
548
 
          '--exclude', 'selftest'])
 
522
        out_excl,err_excl = self.run_bzr(['selftest', '--list-only',
 
523
                                          '--exclude', 'selftest'])
549
524
        tests_excl = self._parse_test_list(out_excl.splitlines())[1]
550
525
        self.assertSubset(tests_excl, tests_all)
551
526
        set_incl = set(tests_incl)
556
531
 
557
532
    def test_list_only_random(self):
558
533
        # check that --randomize works correctly
559
 
        out_all,err_all = self.run_bzr_captured(['selftest', '--list-only',
560
 
            'selftest'])
 
534
        out_all,err_all = self.run_bzr('selftest --list-only selftest')
561
535
        tests_all = self._parse_test_list(out_all.splitlines())[1]
562
536
        # XXX: It looks like there are some orders for generating tests that
563
537
        # fail as of 20070504 - maybe because of import order dependencies.
564
538
        # So unfortunately this will rarely intermittently fail at the moment.
565
539
        # -- mbp 20070504
566
 
        out_rand,err_rand = self.run_bzr_captured(['selftest', '--list-only',
567
 
            'selftest', '--randomize', 'now'])
 
540
        out_rand,err_rand = self.run_bzr(['selftest', '--list-only',
 
541
                                          'selftest', '--randomize', 'now'])
568
542
        (header_rand,tests_rand,dummy) = self._parse_test_list(
569
543
            out_rand.splitlines(), 2)
 
544
        # XXX: The following line asserts that the randomized order is not the
 
545
        # same as the default order.  It is just possible that they'll get
 
546
        # randomized into the same order and this will falsely fail, but
 
547
        # that's very unlikely in practice because there are thousands of
 
548
        # tests.
570
549
        self.assertNotEqual(tests_all, tests_rand)
571
550
        self.assertEqual(sorted(tests_all), sorted(tests_rand))
572
551
        # Check that the seed can be reused to get the exact same order
573
552
        seed_re = re.compile('Randomizing test order using seed (\w+)')
574
553
        match_obj = seed_re.search(header_rand[-1])
575
554
        seed = match_obj.group(1)
576
 
        out_rand2,err_rand2 = self.run_bzr_captured(['selftest', '--list-only',
577
 
            'selftest', '--randomize', seed])
 
555
        out_rand2,err_rand2 = self.run_bzr(['selftest', '--list-only',
 
556
                                            'selftest', '--randomize', seed])
578
557
        (header_rand2,tests_rand2,dummy) = self._parse_test_list(
579
558
            out_rand2.splitlines(), 2)
580
559
        self.assertEqual(tests_rand, tests_rand2)