~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
974.1.70 by Aaron Bentley
Fixed selftest spewage (Brian M. Carlson)
77
    def run(self, result):
78
        self.apply_redirected(None, None, None,
79
                              unittest.TestCase.run, self, result)
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
80
        
81
    def tearDown(self):
82
        logging.getLogger('').removeHandler(self._log_hdlr)
83
        bzrlib.trace.enable_default_logging()
84
        logging.debug('%s teardown', self.id())
85
        self._log_file.close()
86
        unittest.TestCase.tearDown(self)
87
88
89
    def log(self, *args):
90
        logging.debug(*args)
91
92
    def _get_log(self):
93
        """Return as a string the log for this test"""
94
        return open(self._log_file_name).read()
95
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
96
    def run_bzr(self, *args, **kwargs):
1119 by Martin Pool
doc
97
        """Invoke bzr, as if it were run from the command line.
98
99
        This should be the main method for tests that want to exercise the
100
        overall behavior of the bzr application (rather than a unit test
101
        or a functional test of the library.)
102
103
        Much of the old code runs bzr by forking a new copy of Python, but
104
        that is slower, harder to debug, and generally not necessary.
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
105
        """
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
106
        retcode = kwargs.get('retcode', 0)
1092.1.16 by Robert Collins
provide a helper to redirect output as desired
107
        result = self.apply_redirected(None, None, None,
108
                                       bzrlib.commands.run_bzr, args)
109
        self.assertEquals(result, retcode)
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
110
        
1162 by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to
111
        
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
112
    def check_inventory_shape(self, inv, shape):
113
        """
114
        Compare an inventory to a list of expected names.
115
116
        Fail if they are not precisely equal.
117
        """
118
        extras = []
119
        shape = list(shape)             # copy
120
        for path, ie in inv.entries():
121
            name = path.replace('\\', '/')
122
            if ie.kind == 'dir':
123
                name = name + '/'
124
            if name in shape:
125
                shape.remove(name)
126
            else:
127
                extras.append(name)
128
        if shape:
129
            self.fail("expected paths not found in inventory: %r" % shape)
130
        if extras:
131
            self.fail("unexpected paths found in inventory: %r" % extras)
132
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
133
    def apply_redirected(self, stdin=None, stdout=None, stderr=None,
134
                         a_callable=None, *args, **kwargs):
135
        """Call callable with redirected std io pipes.
136
137
        Returns the return code."""
138
        from StringIO import StringIO
139
        if not callable(a_callable):
140
            raise ValueError("a_callable must be callable.")
141
        if stdin is None:
142
            stdin = StringIO("")
143
        if stdout is None:
974.1.70 by Aaron Bentley
Fixed selftest spewage (Brian M. Carlson)
144
            if hasattr(self, "_log_file"):
145
                stdout = self._log_file
146
            else:
147
                stdout = StringIO()
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
148
        if stderr is None:
974.1.70 by Aaron Bentley
Fixed selftest spewage (Brian M. Carlson)
149
            if hasattr(self, "_log_file"):
150
                stderr = self._log_file
151
            else:
152
                stderr = StringIO()
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
153
        real_stdin = sys.stdin
154
        real_stdout = sys.stdout
155
        real_stderr = sys.stderr
156
        try:
157
            sys.stdout = stdout
158
            sys.stderr = stderr
159
            sys.stdin = stdin
1160 by Martin Pool
- tiny refactoring
160
            return a_callable(*args, **kwargs)
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
161
        finally:
162
            sys.stdout = real_stdout
163
            sys.stderr = real_stderr
164
            sys.stdin = real_stdin
165
166
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
167
BzrTestBase = TestCase
168
169
     
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
170
class TestCaseInTempDir(TestCase):
171
    """Derived class that runs a test within a temporary directory.
172
173
    This is useful for tests that need to create a branch, etc.
174
175
    The directory is created in a slightly complex way: for each
176
    Python invocation, a new temporary top-level directory is created.
177
    All test cases create their own directory within that.  If the
178
    tests complete successfully, the directory is removed.
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
179
180
    InTempDir is an old alias for FunctionalTestCase.
181
    """
182
183
    TEST_ROOT = None
184
    _TEST_NAME = 'test'
185
    OVERRIDE_PYTHON = 'python'
186
187
    def check_file_contents(self, filename, expect):
188
        self.log("check contents of file %s" % filename)
189
        contents = file(filename, 'r').read()
190
        if contents != expect:
191
            self.log("expected: %r" % expect)
192
            self.log("actually: %r" % contents)
193
            self.fail("contents of %s not as expected")
194
195
    def _make_test_root(self):
196
        import os
197
        import shutil
198
        import tempfile
199
        
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
200
        if TestCaseInTempDir.TEST_ROOT is not None:
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
201
            return
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
202
        TestCaseInTempDir.TEST_ROOT = os.path.abspath(
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
203
                                 tempfile.mkdtemp(suffix='.tmp',
204
                                                  prefix=self._TEST_NAME + '-',
205
                                                  dir=os.curdir))
206
    
207
        # make a fake bzr directory there to prevent any tests propagating
208
        # up onto the source directory's real branch
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
209
        os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
210
211
    def setUp(self):
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
212
        super(TestCaseInTempDir, self).setUp()
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
213
        import os
214
        self._make_test_root()
215
        self._currentdir = os.getcwdu()
216
        self.test_dir = os.path.join(self.TEST_ROOT, self.id())
217
        os.mkdir(self.test_dir)
218
        os.chdir(self.test_dir)
219
        
220
    def tearDown(self):
221
        import os
222
        os.chdir(self._currentdir)
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
223
        super(TestCaseInTempDir, self).tearDown()
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
224
225
    def _formcmd(self, cmd):
226
        if isinstance(cmd, basestring):
227
            cmd = cmd.split()
228
        if cmd[0] == 'bzr':
229
            cmd[0] = self.BZRPATH
230
            if self.OVERRIDE_PYTHON:
231
                cmd.insert(0, self.OVERRIDE_PYTHON)
232
        self.log('$ %r' % cmd)
233
        return cmd
234
235
    def runcmd(self, cmd, retcode=0):
236
        """Run one command and check the return code.
237
238
        Returns a tuple of (stdout,stderr) strings.
239
240
        If a single string is based, it is split into words.
241
        For commands that are not simple space-separated words, please
242
        pass a list instead."""
243
        cmd = self._formcmd(cmd)
244
        self.log('$ ' + ' '.join(cmd))
1143 by Martin Pool
- remove dead code and remove some small errors (pychecker)
245
        actual_retcode = subprocess.call(cmd, stdout=self._log_file,
246
                                         stderr=self._log_file)
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
247
        if retcode != actual_retcode:
248
            raise CommandFailed("test failed: %r returned %d, expected %d"
249
                                % (cmd, actual_retcode, retcode))
250
251
    def backtick(self, cmd, retcode=0):
252
        """Run a command and return its output"""
253
        cmd = self._formcmd(cmd)
1143 by Martin Pool
- remove dead code and remove some small errors (pychecker)
254
        child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=self._log_file)
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
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
1092.1.20 by Robert Collins
import and use TestUtil to do regex based partial test runs
299
def selftest(verbose=False, pattern=".*"):
300
    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
301
302
303
def test_suite():
1092.1.20 by Robert Collins
import and use TestUtil to do regex based partial test runs
304
    from bzrlib.selftest.TestUtil import TestLoader, TestSuite
855 by Martin Pool
- Patch from John to allow plugins to add their own tests.
305
    import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
908 by Martin Pool
- merge john's plugins-have-test_suite.patch:
306
    import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
721 by Martin Pool
- framework for running external commands from unittest suite
307
    from doctest import DocTestSuite
308
    import os
309
    import shutil
310
    import time
745 by Martin Pool
- redirect stdout/stderr while running tests
311
    import sys
721 by Martin Pool
- framework for running external commands from unittest suite
312
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
313
    global MODULES_TO_TEST, MODULES_TO_DOCTEST
314
315
    testmod_names = \
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
316
                  ['bzrlib.selftest.MetaTestLog',
1149 by Martin Pool
- make get_parent() be a method of Branch; add simple tests for it
317
                   'bzrlib.selftest.test_parent',
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
318
                   'bzrlib.selftest.testinv',
319
                   'bzrlib.selftest.testfetch',
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
320
                   'bzrlib.selftest.versioning',
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
321
                   'bzrlib.selftest.whitebox',
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
322
                   'bzrlib.selftest.testmerge3',
323
                   'bzrlib.selftest.testhashcache',
324
                   'bzrlib.selftest.teststatus',
325
                   'bzrlib.selftest.testlog',
326
                   'bzrlib.selftest.blackbox',
327
                   'bzrlib.selftest.testrevisionnamespaces',
328
                   'bzrlib.selftest.testbranch',
329
                   'bzrlib.selftest.testrevision',
1092.1.23 by Robert Collins
move merge_core tests into the selftest package. Also reduce double-run of those tests
330
                   'bzrlib.selftest.test_merge_core',
1092.1.26 by Robert Collins
start writing star-topology test, realise we need smart-add change
331
                   'bzrlib.selftest.test_smart_add',
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
332
                   'bzrlib.selftest.testdiff',
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
333
                   'bzrlib.selftest.test_xml',
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
334
                   'bzrlib.fetch',
335
                   'bzrlib.selftest.teststore',
974.1.57 by aaron.bentley at utoronto
Started work on djkstra longest-path algorithm
336
                   'bzrlib.selftest.testgraph',
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
337
                   ]
338
855 by Martin Pool
- Patch from John to allow plugins to add their own tests.
339
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
340
              bzrlib.osutils, bzrlib.commands, bzrlib.merge3):
341
        if m not in MODULES_TO_DOCTEST:
342
            MODULES_TO_DOCTEST.append(m)
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
343
1102 by Martin Pool
- merge test refactoring from robertc
344
    TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
345
    print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
744 by Martin Pool
- show nicer descriptions while running tests
346
    print
721 by Martin Pool
- framework for running external commands from unittest suite
347
    suite = TestSuite()
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
348
    suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
855 by Martin Pool
- Patch from John to allow plugins to add their own tests.
349
    for m in MODULES_TO_TEST:
350
         suite.addTest(TestLoader().loadTestsFromModule(m))
351
    for m in (MODULES_TO_DOCTEST):
721 by Martin Pool
- framework for running external commands from unittest suite
352
        suite.addTest(DocTestSuite(m))
908 by Martin Pool
- merge john's plugins-have-test_suite.patch:
353
    for p in bzrlib.plugin.all_plugins:
354
        if hasattr(p, 'test_suite'):
355
            suite.addTest(p.test_suite())
1092.1.17 by Robert Collins
remove TEST_CLASSES dead code and provide a bzrlib.test_suite() convenience method
356
    return suite
764 by Martin Pool
- log messages from a particular test are printed if that test fails
357