~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Robert Collins
  • Date: 2007-03-08 04:06:06 UTC
  • mfrom: (2323.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 2442.
  • Revision ID: robertc@robertcollins.net-20070308040606-84gsniv56huiyjt4
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License version 2 as published by
5
 
# the Free Software Foundation.
 
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.
6
7
#
7
8
# This program is distributed in the hope that it will be useful,
8
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
16
 
16
17
"""UI tests for the test framework."""
17
18
 
 
19
import os
 
20
import signal
18
21
import sys
19
22
 
20
23
import bzrlib
 
24
from bzrlib import (
 
25
    osutils,
 
26
    )
21
27
from bzrlib.errors import ParamikoNotPresent
22
28
from bzrlib.tests import (
23
29
                          TestCase,
24
30
                          TestCaseInTempDir,
 
31
                          TestCaseWithMemoryTransport,
 
32
                          TestCaseWithTransport,
25
33
                          TestSkipped,
26
34
                          )
27
35
from bzrlib.tests.blackbox import ExternalBase
59
67
        except ParamikoNotPresent:
60
68
            raise TestSkipped("Paramiko not present")
61
69
        old_transport = bzrlib.tests.default_transport
62
 
        old_root = TestCaseInTempDir.TEST_ROOT
63
 
        TestCaseInTempDir.TEST_ROOT = None
 
70
        old_root = TestCaseWithMemoryTransport.TEST_ROOT
 
71
        TestCaseWithMemoryTransport.TEST_ROOT = None
64
72
        try:
65
73
            TestOptions.current_test = "test_transport_set_to_sftp"
66
74
            stdout = self.capture('selftest --transport=sftp test_transport_set_to_sftp')
75
83
        finally:
76
84
            bzrlib.tests.default_transport = old_transport
77
85
            TestOptions.current_test = None
78
 
            TestCaseInTempDir.TEST_ROOT = old_root
 
86
            TestCaseWithMemoryTransport.TEST_ROOT = old_root
79
87
 
80
 
    def test_benchmark_runs_benchmark_tests(self):
81
 
        """bzr selftest --benchmark should not run the default test suite."""
82
 
        # We test this by passing a regression test name to --benchmark, which
83
 
        # should result in 0 rests run.
84
 
        out, err = self.run_bzr('selftest', '--benchmark', 'workingtree_implementations')
85
 
        self.assertContainsRe(out, 'Ran 0 tests.*\n\nOK')
86
 
        self.assertEqual(
87
 
            'running tests...\ntests passed\n',
88
 
            err)
89
 
        
90
88
 
91
89
class TestRunBzr(ExternalBase):
92
90
 
93
 
    def run_bzr_captured(self, argv, retcode=0, stdin=None):
 
91
    def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
 
92
                         working_dir=None):
 
93
        """Override run_bzr_captured to test how it is invoked by run_bzr.
 
94
 
 
95
        We test how run_bzr_captured actually invokes bzr in another location.
 
96
        Here we only need to test that it is run_bzr passes the right
 
97
        parameters to run_bzr_captured.
 
98
        """
 
99
        self.argv = argv
 
100
        self.retcode = retcode
 
101
        self.encoding = encoding
94
102
        self.stdin = stdin
 
103
        self.working_dir = working_dir
 
104
 
 
105
    def test_args(self):
 
106
        """Test that run_bzr passes args correctly to run_bzr_captured"""
 
107
        self.run_bzr('arg1', 'arg2', 'arg3', retcode=1)
 
108
        self.assertEqual(('arg1', 'arg2', 'arg3'), self.argv)
 
109
 
 
110
    def test_encoding(self):
 
111
        """Test that run_bzr passes encoding to run_bzr_captured"""
 
112
        self.run_bzr('foo', 'bar')
 
113
        self.assertEqual(None, self.encoding)
 
114
        self.assertEqual(('foo', 'bar'), self.argv)
 
115
 
 
116
        self.run_bzr('foo', 'bar', encoding='baz')
 
117
        self.assertEqual('baz', self.encoding)
 
118
        self.assertEqual(('foo', 'bar'), self.argv)
 
119
 
 
120
    def test_retcode(self):
 
121
        """Test that run_bzr passes retcode to run_bzr_captured"""
 
122
        # Default is retcode == 0
 
123
        self.run_bzr('foo', 'bar')
 
124
        self.assertEqual(0, self.retcode)
 
125
        self.assertEqual(('foo', 'bar'), self.argv)
 
126
 
 
127
        self.run_bzr('foo', 'bar', retcode=1)
 
128
        self.assertEqual(1, self.retcode)
 
129
        self.assertEqual(('foo', 'bar'), self.argv)
 
130
 
 
131
        self.run_bzr('foo', 'bar', retcode=None)
 
132
        self.assertEqual(None, self.retcode)
 
133
        self.assertEqual(('foo', 'bar'), self.argv)
 
134
 
 
135
        self.run_bzr('foo', 'bar', retcode=3)
 
136
        self.assertEqual(3, self.retcode)
 
137
        self.assertEqual(('foo', 'bar'), self.argv)
95
138
 
96
139
    def test_stdin(self):
97
140
        # test that the stdin keyword to run_bzr is passed through to
101
144
        # should invoke it.
102
145
        self.run_bzr('foo', 'bar', stdin='gam')
103
146
        self.assertEqual('gam', self.stdin)
 
147
        self.assertEqual(('foo', 'bar'), self.argv)
 
148
 
104
149
        self.run_bzr('foo', 'bar', stdin='zippy')
105
150
        self.assertEqual('zippy', self.stdin)
 
151
        self.assertEqual(('foo', 'bar'), self.argv)
 
152
 
 
153
    def test_working_dir(self):
 
154
        """Test that run_bzr passes working_dir to run_bzr_captured"""
 
155
        self.run_bzr('foo', 'bar')
 
156
        self.assertEqual(None, self.working_dir)
 
157
        self.assertEqual(('foo', 'bar'), self.argv)
 
158
 
 
159
        self.run_bzr('foo', 'bar', working_dir='baz')
 
160
        self.assertEqual('baz', self.working_dir)
 
161
        self.assertEqual(('foo', 'bar'), self.argv)
 
162
 
 
163
 
 
164
class TestBenchmarkTests(TestCaseWithTransport):
 
165
 
 
166
    def test_benchmark_runs_benchmark_tests(self):
 
167
        """bzr selftest --benchmark should not run the default test suite."""
 
168
        # We test this by passing a regression test name to --benchmark, which
 
169
        # should result in 0 rests run.
 
170
        old_root = TestCaseWithMemoryTransport.TEST_ROOT
 
171
        try:
 
172
            TestCaseWithMemoryTransport.TEST_ROOT = None
 
173
            out, err = self.run_bzr('selftest', '--benchmark', 'workingtree_implementations')
 
174
        finally:
 
175
            TestCaseWithMemoryTransport.TEST_ROOT = old_root
 
176
        self.assertContainsRe(out, 'Ran 0 tests.*\n\nOK')
 
177
        self.assertEqual(
 
178
            'tests passed\n',
 
179
            err)
 
180
        benchfile = open(".perf_history", "rt")
 
181
        try:
 
182
            lines = benchfile.readlines()
 
183
        finally:
 
184
            benchfile.close()
 
185
        self.assertEqual(1, len(lines))
 
186
        self.assertContainsRe(lines[0], "--date [0-9.]+")
106
187
 
107
188
 
108
189
class TestRunBzrCaptured(ExternalBase):
112
193
        self.stdin = stdin
113
194
        self.factory_stdin = getattr(bzrlib.ui.ui_factory, "stdin", None)
114
195
        self.factory = bzrlib.ui.ui_factory
 
196
        self.working_dir = osutils.getcwd()
115
197
        stdout.write('foo\n')
116
198
        stderr.write('bar\n')
117
199
        return 0
141
223
        self.assertEqual('foo\n', self.factory.stdout.getvalue())
142
224
        self.assertEqual('bar\n', self.factory.stderr.getvalue())
143
225
        self.assertIsInstance(self.factory, bzrlib.tests.blackbox.TestUIFactory)
 
226
 
 
227
    def test_working_dir(self):
 
228
        self.build_tree(['one/', 'two/'])
 
229
        cwd = osutils.getcwd()
 
230
 
 
231
        # Default is to work in the current directory
 
232
        self.run_bzr_captured(['foo', 'bar'])
 
233
        self.assertEqual(cwd, self.working_dir)
 
234
 
 
235
        self.run_bzr_captured(['foo', 'bar'], working_dir=None)
 
236
        self.assertEqual(cwd, self.working_dir)
 
237
 
 
238
        # The function should be run in the alternative directory
 
239
        # but afterwards the current working dir shouldn't be changed
 
240
        self.run_bzr_captured(['foo', 'bar'], working_dir='one')
 
241
        self.assertNotEqual(cwd, self.working_dir)
 
242
        self.assertEndsWith(self.working_dir, 'one')
 
243
        self.assertEqual(cwd, osutils.getcwd())
 
244
 
 
245
        self.run_bzr_captured(['foo', 'bar'], working_dir='two')
 
246
        self.assertNotEqual(cwd, self.working_dir)
 
247
        self.assertEndsWith(self.working_dir, 'two')
 
248
        self.assertEqual(cwd, osutils.getcwd())
 
249
 
 
250
 
 
251
class TestRunBzrSubprocess(TestCaseWithTransport):
 
252
 
 
253
    def test_run_bzr_subprocess(self):
 
254
        """The run_bzr_helper_external comand behaves nicely."""
 
255
        result = self.run_bzr_subprocess('--version')
 
256
        result = self.run_bzr_subprocess('--version', retcode=None)
 
257
        self.assertContainsRe(result[0], 'is free software')
 
258
        self.assertRaises(AssertionError, self.run_bzr_subprocess, 
 
259
                          '--versionn')
 
260
        result = self.run_bzr_subprocess('--versionn', retcode=3)
 
261
        result = self.run_bzr_subprocess('--versionn', retcode=None)
 
262
        self.assertContainsRe(result[1], 'unknown command')
 
263
        err = self.run_bzr_subprocess('merge', '--merge-type', 'magic merge', 
 
264
                                      retcode=3)[1]
 
265
        self.assertContainsRe(err, 'Bad value "magic merge" for option'
 
266
                              ' "merge-type"')
 
267
 
 
268
    def test_run_bzr_subprocess_env(self):
 
269
        """run_bzr_subprocess can set environment variables in the child only.
 
270
 
 
271
        These changes should not change the running process, only the child.
 
272
        """
 
273
        # The test suite should unset this variable
 
274
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
 
275
        out, err = self.run_bzr_subprocess('whoami', env_changes={
 
276
                                            'BZR_EMAIL':'Joe Foo <joe@foo.com>'
 
277
                                          }, universal_newlines=True)
 
278
        self.assertEqual('', err)
 
279
        self.assertEqual('Joe Foo <joe@foo.com>\n', out)
 
280
        # And it should not be modified
 
281
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
 
282
 
 
283
        # Do it again with a different address, just to make sure
 
284
        # it is actually changing
 
285
        out, err = self.run_bzr_subprocess('whoami', env_changes={
 
286
                                            'BZR_EMAIL':'Barry <bar@foo.com>'
 
287
                                          }, universal_newlines=True)
 
288
        self.assertEqual('', err)
 
289
        self.assertEqual('Barry <bar@foo.com>\n', out)
 
290
        self.assertEqual(None, os.environ.get('BZR_EMAIL'))
 
291
 
 
292
    def test_run_bzr_subprocess_env_del(self):
 
293
        """run_bzr_subprocess can remove environment variables too."""
 
294
        # Create a random email, so we are sure this won't collide
 
295
        rand_bzr_email = 'John Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
 
296
        rand_email = 'Jane Doe <jdoe@%s.com>' % (osutils.rand_chars(20),)
 
297
        os.environ['BZR_EMAIL'] = rand_bzr_email
 
298
        os.environ['EMAIL'] = rand_email
 
299
        try:
 
300
            # By default, the child will inherit the current env setting
 
301
            out, err = self.run_bzr_subprocess('whoami', universal_newlines=True)
 
302
            self.assertEqual('', err)
 
303
            self.assertEqual(rand_bzr_email + '\n', out)
 
304
 
 
305
            # Now that BZR_EMAIL is not set, it should fall back to EMAIL
 
306
            out, err = self.run_bzr_subprocess('whoami',
 
307
                                               env_changes={'BZR_EMAIL':None},
 
308
                                               universal_newlines=True)
 
309
            self.assertEqual('', err)
 
310
            self.assertEqual(rand_email + '\n', out)
 
311
 
 
312
            # This switches back to the default email guessing logic
 
313
            # Which shouldn't match either of the above addresses
 
314
            out, err = self.run_bzr_subprocess('whoami',
 
315
                           env_changes={'BZR_EMAIL':None, 'EMAIL':None},
 
316
                           universal_newlines=True)
 
317
 
 
318
            self.assertEqual('', err)
 
319
            self.assertNotEqual(rand_bzr_email + '\n', out)
 
320
            self.assertNotEqual(rand_email + '\n', out)
 
321
        finally:
 
322
            # TestCase cleans up BZR_EMAIL, and EMAIL at startup
 
323
            del os.environ['BZR_EMAIL']
 
324
            del os.environ['EMAIL']
 
325
 
 
326
    def test_run_bzr_subprocess_env_del_missing(self):
 
327
        """run_bzr_subprocess won't fail if deleting a nonexistant env var"""
 
328
        self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
 
329
        out, err = self.run_bzr_subprocess('rocks',
 
330
                        env_changes={'NON_EXISTANT_ENV_VAR':None},
 
331
                        universal_newlines=True)
 
332
        self.assertEqual('It sure does!\n', out)
 
333
        self.assertEqual('', err)
 
334
 
 
335
    def test_run_bzr_subprocess_working_dir(self):
 
336
        """Test that we can specify the working dir for the child"""
 
337
        cwd = osutils.getcwd()
 
338
 
 
339
        self.make_branch_and_tree('.')
 
340
        self.make_branch_and_tree('one')
 
341
        self.make_branch_and_tree('two')
 
342
 
 
343
        def get_root(**kwargs):
 
344
            """Spawn a process to get the 'root' of the tree.
 
345
 
 
346
            You can pass in arbitrary new arguments. This just makes
 
347
            sure that the returned path doesn't have trailing whitespace.
 
348
            """
 
349
            return self.run_bzr_subprocess('root', **kwargs)[0].rstrip()
 
350
 
 
351
        self.assertEqual(cwd, get_root())
 
352
        self.assertEqual(cwd, get_root(working_dir=None))
 
353
        # Has our path changed?
 
354
        self.assertEqual(cwd, osutils.getcwd())
 
355
 
 
356
        dir1 = get_root(working_dir='one')
 
357
        self.assertEndsWith(dir1, 'one')
 
358
        self.assertEqual(cwd, osutils.getcwd())
 
359
 
 
360
        dir2 = get_root(working_dir='two')
 
361
        self.assertEndsWith(dir2, 'two')
 
362
        self.assertEqual(cwd, osutils.getcwd())
 
363
 
 
364
 
 
365
class _DontSpawnProcess(Exception):
 
366
    """A simple exception which just allows us to skip unnecessary steps"""
 
367
 
 
368
 
 
369
class TestRunBzrSubprocessCommands(TestCaseWithTransport):
 
370
 
 
371
    def _popen(self, *args, **kwargs):
 
372
        """Record the command that is run, so that we can ensure it is correct"""
 
373
        self._popen_args = args
 
374
        self._popen_kwargs = kwargs
 
375
        raise _DontSpawnProcess()
 
376
 
 
377
    def test_run_bzr_subprocess_no_plugins(self):
 
378
        self.assertRaises(_DontSpawnProcess, self.run_bzr_subprocess)
 
379
        command = self._popen_args[0]
 
380
        self.assertEqual(sys.executable, command[0])
 
381
        self.assertEqual(self.get_bzr_path(), command[1])
 
382
        self.assertEqual(['--no-plugins'], command[2:])
 
383
 
 
384
    def test_allow_plugins(self):
 
385
        self.assertRaises(_DontSpawnProcess,
 
386
                          self.run_bzr_subprocess, allow_plugins=True)
 
387
        command = self._popen_args[0]
 
388
        self.assertEqual([], command[2:])
 
389
 
 
390
 
 
391
class TestBzrSubprocess(TestCaseWithTransport):
 
392
 
 
393
    def test_start_and_stop_bzr_subprocess(self):
 
394
        """We can start and perform other test actions while that process is
 
395
        still alive.
 
396
        """
 
397
        process = self.start_bzr_subprocess(['--version'])
 
398
        result = self.finish_bzr_subprocess(process)
 
399
        self.assertContainsRe(result[0], 'is free software')
 
400
        self.assertEqual('', result[1])
 
401
 
 
402
    def test_start_and_stop_bzr_subprocess_with_error(self):
 
403
        """finish_bzr_subprocess allows specification of the desired exit code.
 
404
        """
 
405
        process = self.start_bzr_subprocess(['--versionn'])
 
406
        result = self.finish_bzr_subprocess(process, retcode=3)
 
407
        self.assertEqual('', result[0])
 
408
        self.assertContainsRe(result[1], 'unknown command')
 
409
 
 
410
    def test_start_and_stop_bzr_subprocess_ignoring_retcode(self):
 
411
        """finish_bzr_subprocess allows the exit code to be ignored."""
 
412
        process = self.start_bzr_subprocess(['--versionn'])
 
413
        result = self.finish_bzr_subprocess(process, retcode=None)
 
414
        self.assertEqual('', result[0])
 
415
        self.assertContainsRe(result[1], 'unknown command')
 
416
 
 
417
    def test_start_and_stop_bzr_subprocess_with_unexpected_retcode(self):
 
418
        """finish_bzr_subprocess raises self.failureException if the retcode is
 
419
        not the expected one.
 
420
        """
 
421
        process = self.start_bzr_subprocess(['--versionn'])
 
422
        self.assertRaises(self.failureException, self.finish_bzr_subprocess,
 
423
                          process, retcode=0)
 
424
        
 
425
    def test_start_and_stop_bzr_subprocess_send_signal(self):
 
426
        """finish_bzr_subprocess raises self.failureException if the retcode is
 
427
        not the expected one.
 
428
        """
 
429
        process = self.start_bzr_subprocess(['wait-until-signalled'],
 
430
                                            skip_if_plan_to_signal=True)
 
431
        self.assertEqual('running\n', process.stdout.readline())
 
432
        result = self.finish_bzr_subprocess(process, send_signal=signal.SIGINT,
 
433
                                            retcode=3)
 
434
        self.assertEqual('', result[0])
 
435
        self.assertEqual('bzr: interrupted\n', result[1])
 
436
 
 
437
    def test_start_and_stop_working_dir(self):
 
438
        cwd = osutils.getcwd()
 
439
 
 
440
        self.make_branch_and_tree('one')
 
441
 
 
442
        process = self.start_bzr_subprocess(['root'], working_dir='one')
 
443
        result = self.finish_bzr_subprocess(process, universal_newlines=True)
 
444
        self.assertEndsWith(result[0], 'one\n')
 
445
        self.assertEqual('', result[1])
 
446
 
 
447
 
 
448
class TestRunBzrError(ExternalBase):
 
449
 
 
450
    def test_run_bzr_error(self):
 
451
        out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
 
452
        self.assertEqual(out, 'It sure does!\n')
 
453
 
 
454
        out, err = self.run_bzr_error(["bzr: ERROR: foobarbaz is not versioned"],
 
455
                                      'file-id', 'foobarbaz')
 
456
 
 
457
 
 
458
class TestSelftestCleanOutput(TestCaseInTempDir):
 
459
 
 
460
    def test_clean_output(self):
 
461
        # check that 'bzr selftest --clean-output' works correct
 
462
        dirs = ('test0000.tmp', 'test0001.tmp', 'bzrlib', 'tests')
 
463
        files = ('bzr', 'setup.py', 'test9999.tmp')
 
464
        for i in dirs:
 
465
            os.mkdir(i)
 
466
        for i in files:
 
467
            f = file(i, 'wb')
 
468
            f.write('content of ')
 
469
            f.write(i)
 
470
            f.close()
 
471
 
 
472
        root = os.getcwdu()
 
473
        before = os.listdir(root)
 
474
        before.sort()
 
475
        self.assertEquals(['bzr','bzrlib','setup.py',
 
476
                           'test0000.tmp','test0001.tmp',
 
477
                           'test9999.tmp','tests'],
 
478
                           before)
 
479
 
 
480
        out,err = self.run_bzr_captured(['selftest','--clean-output'],
 
481
                                        working_dir=root)
 
482
 
 
483
        self.assertEquals(['delete directory: test0000.tmp',
 
484
                          'delete directory: test0001.tmp'],
 
485
                          sorted(out.splitlines()))
 
486
        self.assertEquals('', err)
 
487
 
 
488
        after = os.listdir(root)
 
489
        after.sort()
 
490
        self.assertEquals(['bzr','bzrlib','setup.py',
 
491
                           'test9999.tmp','tests'],
 
492
                           after)