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