~bzr-pqm/bzr/bzr.dev

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