/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

  • Committer: Martin Pool
  • Date: 2005-08-30 01:44:02 UTC
  • Revision ID: mbp@sourcefrog.net-20050830014401-028847129b413b22
- remove dead code from blackbox tests (pychecker)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by 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
 
 
18
import logging
 
19
import unittest
 
20
import tempfile
 
21
import os
 
22
import sys
 
23
 
 
24
from testsweet import run_suite
 
25
import bzrlib.commands
 
26
 
 
27
import bzrlib.trace
 
28
import bzrlib.fetch
 
29
 
 
30
MODULES_TO_TEST = []
 
31
MODULES_TO_DOCTEST = []
 
32
 
 
33
from logging import debug, warning, error
 
34
 
 
35
 
 
36
class TestCase(unittest.TestCase):
 
37
    """Base class for bzr unit tests.
 
38
    
 
39
    Tests that need access to disk resources should subclass 
 
40
    TestCaseInTempDir not TestCase.
 
41
 
 
42
    Error and debug log messages are redirected from their usual
 
43
    location into a temporary file, the contents of which can be
 
44
    retrieved by _get_log().
 
45
       
 
46
    There are also convenience functions to invoke bzr's command-line
 
47
    routine, and to build and check bzr trees."""
 
48
 
 
49
    BZRPATH = 'bzr'
 
50
 
 
51
    def setUp(self):
 
52
        # this replaces the default testsweet.TestCase; we don't want logging changed
 
53
        unittest.TestCase.setUp(self)
 
54
        bzrlib.trace.disable_default_logging()
 
55
        self._enable_file_logging()
 
56
 
 
57
 
 
58
    def _enable_file_logging(self):
 
59
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
 
60
 
 
61
        self._log_file = os.fdopen(fileno, 'w+')
 
62
 
 
63
        hdlr = logging.StreamHandler(self._log_file)
 
64
        hdlr.setLevel(logging.DEBUG)
 
65
        hdlr.setFormatter(logging.Formatter('%(levelname)4.4s  %(message)s'))
 
66
        logging.getLogger('').addHandler(hdlr)
 
67
        logging.getLogger('').setLevel(logging.DEBUG)
 
68
        self._log_hdlr = hdlr
 
69
        debug('opened log file %s', name)
 
70
        
 
71
        self._log_file_name = name
 
72
 
 
73
        
 
74
    def tearDown(self):
 
75
        logging.getLogger('').removeHandler(self._log_hdlr)
 
76
        bzrlib.trace.enable_default_logging()
 
77
        logging.debug('%s teardown', self.id())
 
78
        self._log_file.close()
 
79
        unittest.TestCase.tearDown(self)
 
80
 
 
81
 
 
82
    def log(self, *args):
 
83
        logging.debug(*args)
 
84
 
 
85
    def _get_log(self):
 
86
        """Return as a string the log for this test"""
 
87
        return open(self._log_file_name).read()
 
88
 
 
89
    def run_bzr(self, *args, **kwargs):
 
90
        """Invoke bzr, as if it were run from the command line.
 
91
 
 
92
        This should be the main method for tests that want to exercise the
 
93
        overall behavior of the bzr application (rather than a unit test
 
94
        or a functional test of the library.)
 
95
 
 
96
        Much of the old code runs bzr by forking a new copy of Python, but
 
97
        that is slower, harder to debug, and generally not necessary.
 
98
        """
 
99
        retcode = kwargs.get('retcode', 0)
 
100
        result = self.apply_redirected(None, None, None,
 
101
                                       bzrlib.commands.run_bzr, args)
 
102
        self.assertEquals(result, retcode)
 
103
        
 
104
    def check_inventory_shape(self, inv, shape):
 
105
        """
 
106
        Compare an inventory to a list of expected names.
 
107
 
 
108
        Fail if they are not precisely equal.
 
109
        """
 
110
        extras = []
 
111
        shape = list(shape)             # copy
 
112
        for path, ie in inv.entries():
 
113
            name = path.replace('\\', '/')
 
114
            if ie.kind == 'dir':
 
115
                name = name + '/'
 
116
            if name in shape:
 
117
                shape.remove(name)
 
118
            else:
 
119
                extras.append(name)
 
120
        if shape:
 
121
            self.fail("expected paths not found in inventory: %r" % shape)
 
122
        if extras:
 
123
            self.fail("unexpected paths found in inventory: %r" % extras)
 
124
 
 
125
    def apply_redirected(self, stdin=None, stdout=None, stderr=None,
 
126
                         a_callable=None, *args, **kwargs):
 
127
        """Call callable with redirected std io pipes.
 
128
 
 
129
        Returns the return code."""
 
130
        from StringIO import StringIO
 
131
        if not callable(a_callable):
 
132
            raise ValueError("a_callable must be callable.")
 
133
        if stdin is None:
 
134
            stdin = StringIO("")
 
135
        if stdout is None:
 
136
            stdout = self._log_file
 
137
        if stderr is None:
 
138
            stderr = self._log_file
 
139
        real_stdin = sys.stdin
 
140
        real_stdout = sys.stdout
 
141
        real_stderr = sys.stderr
 
142
        result = None
 
143
        try:
 
144
            sys.stdout = stdout
 
145
            sys.stderr = stderr
 
146
            sys.stdin = stdin
 
147
            result = a_callable(*args, **kwargs)
 
148
        finally:
 
149
            sys.stdout = real_stdout
 
150
            sys.stderr = real_stderr
 
151
            sys.stdin = real_stdin
 
152
        return result
 
153
 
 
154
 
 
155
BzrTestBase = TestCase
 
156
 
 
157
     
 
158
class TestCaseInTempDir(TestCase):
 
159
    """Derived class that runs a test within a temporary directory.
 
160
 
 
161
    This is useful for tests that need to create a branch, etc.
 
162
 
 
163
    The directory is created in a slightly complex way: for each
 
164
    Python invocation, a new temporary top-level directory is created.
 
165
    All test cases create their own directory within that.  If the
 
166
    tests complete successfully, the directory is removed.
 
167
 
 
168
    InTempDir is an old alias for FunctionalTestCase.
 
169
    """
 
170
 
 
171
    TEST_ROOT = None
 
172
    _TEST_NAME = 'test'
 
173
    OVERRIDE_PYTHON = 'python'
 
174
 
 
175
    def check_file_contents(self, filename, expect):
 
176
        self.log("check contents of file %s" % filename)
 
177
        contents = file(filename, 'r').read()
 
178
        if contents != expect:
 
179
            self.log("expected: %r" % expect)
 
180
            self.log("actually: %r" % contents)
 
181
            self.fail("contents of %s not as expected")
 
182
 
 
183
    def _make_test_root(self):
 
184
        import os
 
185
        import shutil
 
186
        import tempfile
 
187
        
 
188
        if TestCaseInTempDir.TEST_ROOT is not None:
 
189
            return
 
190
        TestCaseInTempDir.TEST_ROOT = os.path.abspath(
 
191
                                 tempfile.mkdtemp(suffix='.tmp',
 
192
                                                  prefix=self._TEST_NAME + '-',
 
193
                                                  dir=os.curdir))
 
194
    
 
195
        # make a fake bzr directory there to prevent any tests propagating
 
196
        # up onto the source directory's real branch
 
197
        os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
 
198
 
 
199
    def setUp(self):
 
200
        super(TestCaseInTempDir, self).setUp()
 
201
        import os
 
202
        self._make_test_root()
 
203
        self._currentdir = os.getcwdu()
 
204
        self.test_dir = os.path.join(self.TEST_ROOT, self.id())
 
205
        os.mkdir(self.test_dir)
 
206
        os.chdir(self.test_dir)
 
207
        
 
208
    def tearDown(self):
 
209
        import os
 
210
        os.chdir(self._currentdir)
 
211
        super(TestCaseInTempDir, self).tearDown()
 
212
 
 
213
    def _formcmd(self, cmd):
 
214
        if isinstance(cmd, basestring):
 
215
            cmd = cmd.split()
 
216
        if cmd[0] == 'bzr':
 
217
            cmd[0] = self.BZRPATH
 
218
            if self.OVERRIDE_PYTHON:
 
219
                cmd.insert(0, self.OVERRIDE_PYTHON)
 
220
        self.log('$ %r' % cmd)
 
221
        return cmd
 
222
 
 
223
    def runcmd(self, cmd, retcode=0):
 
224
        """Run one command and check the return code.
 
225
 
 
226
        Returns a tuple of (stdout,stderr) strings.
 
227
 
 
228
        If a single string is based, it is split into words.
 
229
        For commands that are not simple space-separated words, please
 
230
        pass a list instead."""
 
231
        try:
 
232
            import shutil
 
233
            from subprocess import call
 
234
        except ImportError, e:
 
235
            _need_subprocess()
 
236
            raise
 
237
        cmd = self._formcmd(cmd)
 
238
        self.log('$ ' + ' '.join(cmd))
 
239
        actual_retcode = call(cmd, stdout=self._log_file, stderr=self._log_file)
 
240
        if retcode != actual_retcode:
 
241
            raise CommandFailed("test failed: %r returned %d, expected %d"
 
242
                                % (cmd, actual_retcode, retcode))
 
243
 
 
244
    def backtick(self, cmd, retcode=0):
 
245
        """Run a command and return its output"""
 
246
        try:
 
247
            import shutil
 
248
            from subprocess import Popen, PIPE
 
249
        except ImportError, e:
 
250
            _need_subprocess()
 
251
            raise
 
252
 
 
253
        cmd = self._formcmd(cmd)
 
254
        child = Popen(cmd, stdout=PIPE, stderr=self._log_file)
 
255
        outd, errd = child.communicate()
 
256
        self.log(outd)
 
257
        actual_retcode = child.wait()
 
258
 
 
259
        outd = outd.replace('\r', '')
 
260
 
 
261
        if retcode != actual_retcode:
 
262
            raise CommandFailed("test failed: %r returned %d, expected %d"
 
263
                                % (cmd, actual_retcode, retcode))
 
264
 
 
265
        return outd
 
266
 
 
267
 
 
268
 
 
269
    def build_tree(self, shape):
 
270
        """Build a test tree according to a pattern.
 
271
 
 
272
        shape is a sequence of file specifications.  If the final
 
273
        character is '/', a directory is created.
 
274
 
 
275
        This doesn't add anything to a branch.
 
276
        """
 
277
        # XXX: It's OK to just create them using forward slashes on windows?
 
278
        import os
 
279
        for name in shape:
 
280
            assert isinstance(name, basestring)
 
281
            if name[-1] == '/':
 
282
                os.mkdir(name[:-1])
 
283
            else:
 
284
                f = file(name, 'wt')
 
285
                print >>f, "contents of", name
 
286
                f.close()
 
287
                
 
288
 
 
289
 
 
290
class MetaTestLog(TestCase):
 
291
    def test_logging(self):
 
292
        """Test logs are captured when a test fails."""
 
293
        logging.info('an info message')
 
294
        warning('something looks dodgy...')
 
295
        logging.debug('hello, test is running')
 
296
        ##assert 0
 
297
 
 
298
 
 
299
def selftest(verbose=False, pattern=".*"):
 
300
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
 
301
 
 
302
 
 
303
def test_suite():
 
304
    from bzrlib.selftest.TestUtil import TestLoader, TestSuite
 
305
    import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
 
306
    import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
 
307
    from doctest import DocTestSuite
 
308
    import os
 
309
    import shutil
 
310
    import time
 
311
    import sys
 
312
 
 
313
    global MODULES_TO_TEST, MODULES_TO_DOCTEST
 
314
 
 
315
    testmod_names = \
 
316
                  ['bzrlib.selftest.MetaTestLog',
 
317
                   'bzrlib.selftest.testinv',
 
318
                   'bzrlib.selftest.testfetch',
 
319
                   'bzrlib.selftest.versioning',
 
320
                   'bzrlib.selftest.whitebox',
 
321
                   'bzrlib.selftest.testmerge3',
 
322
                   'bzrlib.selftest.testhashcache',
 
323
                   'bzrlib.selftest.teststatus',
 
324
                   'bzrlib.selftest.testlog',
 
325
                   'bzrlib.selftest.blackbox',
 
326
                   'bzrlib.selftest.testrevisionnamespaces',
 
327
                   'bzrlib.selftest.testbranch',
 
328
                   'bzrlib.selftest.testrevision',
 
329
                   'bzrlib.selftest.test_merge_core',
 
330
                   'bzrlib.selftest.test_smart_add',
 
331
                   'bzrlib.selftest.testdiff',
 
332
                   'bzrlib.fetch'
 
333
                   ]
 
334
 
 
335
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
 
336
              bzrlib.osutils, bzrlib.commands, bzrlib.merge3):
 
337
        if m not in MODULES_TO_DOCTEST:
 
338
            MODULES_TO_DOCTEST.append(m)
 
339
 
 
340
    TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
 
341
    print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
 
342
    print
 
343
    suite = TestSuite()
 
344
    suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
 
345
    for m in MODULES_TO_TEST:
 
346
         suite.addTest(TestLoader().loadTestsFromModule(m))
 
347
    for m in (MODULES_TO_DOCTEST):
 
348
        suite.addTest(DocTestSuite(m))
 
349
    for p in bzrlib.plugin.all_plugins:
 
350
        if hasattr(p, 'test_suite'):
 
351
            suite.addTest(p.test_suite())
 
352
    return suite
 
353