/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
18
from cStringIO import StringIO
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
19
import logging
20
import unittest
21
import tempfile
22
import os
1139 by Martin Pool
- merge in merge improvements and additional tests
23
import sys
1185.3.9 by Martin Pool
- name test tmpdirs sequentially, not randomly
24
import errno
1143 by Martin Pool
- remove dead code and remove some small errors (pychecker)
25
import subprocess
1185.3.9 by Martin Pool
- name test tmpdirs sequentially, not randomly
26
import shutil
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
27
1218 by Martin Pool
- fix up import
28
import testsweet
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
29
import bzrlib.commands
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
30
import bzrlib.trace
974.1.27 by aaron.bentley at utoronto
Initial greedy fetch work
31
import bzrlib.fetch
719 by Martin Pool
- reorganize selftest code
32
1147 by Martin Pool
- split builtin commands into separate module bzrlib.builtins;
33
855 by Martin Pool
- Patch from John to allow plugins to add their own tests.
34
MODULES_TO_TEST = []
35
MODULES_TO_DOCTEST = []
720 by Martin Pool
- start moving external tests into the testsuite framework
36
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
37
from logging import debug, warning, error
38
1147 by Martin Pool
- split builtin commands into separate module bzrlib.builtins;
39
class CommandFailed(Exception):
40
    pass
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
41
42
class TestCase(unittest.TestCase):
43
    """Base class for bzr unit tests.
44
    
45
    Tests that need access to disk resources should subclass 
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
46
    TestCaseInTempDir not TestCase.
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
47
48
    Error and debug log messages are redirected from their usual
49
    location into a temporary file, the contents of which can be
50
    retrieved by _get_log().
51
       
52
    There are also convenience functions to invoke bzr's command-line
53
    routine, and to build and check bzr trees."""
54
55
    BZRPATH = 'bzr'
56
57
    def setUp(self):
58
        # this replaces the default testsweet.TestCase; we don't want logging changed
59
        unittest.TestCase.setUp(self)
60
        bzrlib.trace.disable_default_logging()
61
        self._enable_file_logging()
62
63
64
    def _enable_file_logging(self):
65
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
66
67
        self._log_file = os.fdopen(fileno, 'w+')
68
69
        hdlr = logging.StreamHandler(self._log_file)
70
        hdlr.setLevel(logging.DEBUG)
1215 by Martin Pool
- change trace format for test logs
71
        hdlr.setFormatter(logging.Formatter('%(levelname)8s  %(message)s'))
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
72
        logging.getLogger('').addHandler(hdlr)
73
        logging.getLogger('').setLevel(logging.DEBUG)
74
        self._log_hdlr = hdlr
75
        debug('opened log file %s', name)
76
        
77
        self._log_file_name = name
78
79
    def tearDown(self):
80
        logging.getLogger('').removeHandler(self._log_hdlr)
81
        bzrlib.trace.enable_default_logging()
82
        logging.debug('%s teardown', self.id())
83
        self._log_file.close()
84
        unittest.TestCase.tearDown(self)
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
1185.3.18 by Martin Pool
- add new helper TestBase.run_bzr_captured
93
1185.3.26 by Martin Pool
- remove remaining external executions of bzr
94
    def capture(self, cmd):
95
        """Shortcut that splits cmd into words, runs, and returns stdout"""
96
        return self.run_bzr_captured(cmd.split())[0]
97
1185.3.18 by Martin Pool
- add new helper TestBase.run_bzr_captured
98
    def run_bzr_captured(self, argv, retcode=0):
99
        """Invoke bzr and return (result, stdout, stderr).
100
101
        Useful for code that wants to check the contents of the
102
        output, the way error messages are presented, etc.
103
104
        This should be the main method for tests that want to exercise the
105
        overall behavior of the bzr application (rather than a unit test
106
        or a functional test of the library.)
107
108
        Much of the old code runs bzr by forking a new copy of Python, but
109
        that is slower, harder to debug, and generally not necessary.
110
1185.3.20 by Martin Pool
- run_bzr_captured also includes logged errors in
111
        This runs bzr through the interface that catches and reports
112
        errors, and with logging set to something approximating the
113
        default, so that error reporting can be checked.
114
1185.3.18 by Martin Pool
- add new helper TestBase.run_bzr_captured
115
        argv -- arguments to invoke bzr
116
        retcode -- expected return code, or None for don't-care.
117
        """
118
        stdout = StringIO()
119
        stderr = StringIO()
120
        self.log('run bzr: %s', ' '.join(argv))
1185.3.20 by Martin Pool
- run_bzr_captured also includes logged errors in
121
        handler = logging.StreamHandler(stderr)
122
        handler.setFormatter(bzrlib.trace.QuietFormatter())
123
        handler.setLevel(logging.INFO)
124
        logger = logging.getLogger('')
125
        logger.addHandler(handler)
126
        try:
127
            result = self.apply_redirected(None, stdout, stderr,
128
                                           bzrlib.commands.run_bzr_catch_errors,
129
                                           argv)
130
        finally:
131
            logger.removeHandler(handler)
1185.3.18 by Martin Pool
- add new helper TestBase.run_bzr_captured
132
        out = stdout.getvalue()
133
        err = stderr.getvalue()
134
        if out:
135
            self.log('output:\n%s', out)
136
        if err:
137
            self.log('errors:\n%s', err)
138
        if retcode is not None:
139
            self.assertEquals(result, retcode)
140
        return out, err
141
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
142
    def run_bzr(self, *args, **kwargs):
1119 by Martin Pool
doc
143
        """Invoke bzr, as if it were run from the command line.
144
145
        This should be the main method for tests that want to exercise the
146
        overall behavior of the bzr application (rather than a unit test
147
        or a functional test of the library.)
148
1185.3.18 by Martin Pool
- add new helper TestBase.run_bzr_captured
149
        This sends the stdout/stderr results into the test's log,
150
        where it may be useful for debugging.  See also run_captured.
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
151
        """
1185.3.18 by Martin Pool
- add new helper TestBase.run_bzr_captured
152
        retcode = kwargs.pop('retcode', 0)
1185.3.21 by Martin Pool
TestBase.run_bzr doesn't need to be deprecated
153
        return self.run_bzr_captured(args, retcode)
1185.3.18 by Martin Pool
- add new helper TestBase.run_bzr_captured
154
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
155
    def check_inventory_shape(self, inv, shape):
1291 by Martin Pool
- add test for moving files between directories
156
        """Compare an inventory to a list of expected names.
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
157
158
        Fail if they are not precisely equal.
159
        """
160
        extras = []
161
        shape = list(shape)             # copy
162
        for path, ie in inv.entries():
163
            name = path.replace('\\', '/')
164
            if ie.kind == 'dir':
165
                name = name + '/'
166
            if name in shape:
167
                shape.remove(name)
168
            else:
169
                extras.append(name)
170
        if shape:
171
            self.fail("expected paths not found in inventory: %r" % shape)
172
        if extras:
173
            self.fail("unexpected paths found in inventory: %r" % extras)
174
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
175
    def apply_redirected(self, stdin=None, stdout=None, stderr=None,
176
                         a_callable=None, *args, **kwargs):
177
        """Call callable with redirected std io pipes.
178
179
        Returns the return code."""
180
        if not callable(a_callable):
181
            raise ValueError("a_callable must be callable.")
182
        if stdin is None:
183
            stdin = StringIO("")
184
        if stdout is None:
974.1.70 by Aaron Bentley
Fixed selftest spewage (Brian M. Carlson)
185
            if hasattr(self, "_log_file"):
186
                stdout = self._log_file
187
            else:
188
                stdout = StringIO()
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
189
        if stderr is None:
974.1.70 by Aaron Bentley
Fixed selftest spewage (Brian M. Carlson)
190
            if hasattr(self, "_log_file"):
191
                stderr = self._log_file
192
            else:
193
                stderr = StringIO()
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
194
        real_stdin = sys.stdin
195
        real_stdout = sys.stdout
196
        real_stderr = sys.stderr
197
        try:
198
            sys.stdout = stdout
199
            sys.stderr = stderr
200
            sys.stdin = stdin
1160 by Martin Pool
- tiny refactoring
201
            return a_callable(*args, **kwargs)
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
202
        finally:
203
            sys.stdout = real_stdout
204
            sys.stderr = real_stderr
205
            sys.stdin = real_stdin
206
207
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
208
BzrTestBase = TestCase
209
210
     
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
211
class TestCaseInTempDir(TestCase):
212
    """Derived class that runs a test within a temporary directory.
213
214
    This is useful for tests that need to create a branch, etc.
215
216
    The directory is created in a slightly complex way: for each
217
    Python invocation, a new temporary top-level directory is created.
218
    All test cases create their own directory within that.  If the
219
    tests complete successfully, the directory is removed.
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
220
221
    InTempDir is an old alias for FunctionalTestCase.
222
    """
223
224
    TEST_ROOT = None
225
    _TEST_NAME = 'test'
226
    OVERRIDE_PYTHON = 'python'
227
228
    def check_file_contents(self, filename, expect):
229
        self.log("check contents of file %s" % filename)
230
        contents = file(filename, 'r').read()
231
        if contents != expect:
232
            self.log("expected: %r" % expect)
233
            self.log("actually: %r" % contents)
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
234
            self.fail("contents of %s not as expected" % filename)
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
235
236
    def _make_test_root(self):
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
237
        if TestCaseInTempDir.TEST_ROOT is not None:
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
238
            return
1185.3.9 by Martin Pool
- name test tmpdirs sequentially, not randomly
239
        i = 0
240
        while True:
241
            root = 'test%04d.tmp' % i
242
            try:
243
                os.mkdir(root)
1218 by Martin Pool
- fix up import
244
            except OSError, e:
245
                if e.errno == errno.EEXIST:
246
                    i += 1
1185.3.9 by Martin Pool
- name test tmpdirs sequentially, not randomly
247
                    continue
248
                else:
249
                    raise
250
            # successfully created
251
            TestCaseInTempDir.TEST_ROOT = os.path.abspath(root)
252
            break
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
253
        # make a fake bzr directory there to prevent any tests propagating
254
        # up onto the source directory's real branch
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
255
        os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
256
257
    def setUp(self):
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
258
        super(TestCaseInTempDir, self).setUp()
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
259
        self._make_test_root()
260
        self._currentdir = os.getcwdu()
1218 by Martin Pool
- fix up import
261
        short_id = self.id().replace('bzrlib.selftest.', '') \
262
                   .replace('__main__.', '')
1212 by Martin Pool
- use shorter test directory names
263
        self.test_dir = os.path.join(self.TEST_ROOT, short_id)
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
264
        os.mkdir(self.test_dir)
265
        os.chdir(self.test_dir)
266
        
267
    def tearDown(self):
268
        os.chdir(self._currentdir)
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
269
        super(TestCaseInTempDir, self).tearDown()
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
270
271
    def build_tree(self, shape):
272
        """Build a test tree according to a pattern.
273
274
        shape is a sequence of file specifications.  If the final
275
        character is '/', a directory is created.
276
277
        This doesn't add anything to a branch.
278
        """
279
        # XXX: It's OK to just create them using forward slashes on windows?
280
        for name in shape:
281
            assert isinstance(name, basestring)
282
            if name[-1] == '/':
283
                os.mkdir(name[:-1])
284
            else:
285
                f = file(name, 'wt')
286
                print >>f, "contents of", name
287
                f.close()
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=".*"):
1204 by Martin Pool
doc
300
    """Run the whole test suite under the enhanced runner"""
1218 by Martin Pool
- fix up import
301
    return testsweet.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
302
303
304
def test_suite():
1204 by Martin Pool
doc
305
    """Build and return TestSuite for the whole program."""
1092.1.20 by Robert Collins
import and use TestUtil to do regex based partial test runs
306
    from bzrlib.selftest.TestUtil import TestLoader, TestSuite
855 by Martin Pool
- Patch from John to allow plugins to add their own tests.
307
    import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
908 by Martin Pool
- merge john's plugins-have-test_suite.patch:
308
    import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
721 by Martin Pool
- framework for running external commands from unittest suite
309
    from doctest import DocTestSuite
310
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
311
    global MODULES_TO_TEST, MODULES_TO_DOCTEST
312
313
    testmod_names = \
1123 by Martin Pool
* move bzr-specific code from testsweet into bzrlib.selftest
314
                  ['bzrlib.selftest.MetaTestLog',
315
                   'bzrlib.selftest.testinv',
1390 by Robert Collins
pair programming worx... merge integration and weave
316
                   'bzrlib.selftest.test_ancestry',
1251 by Martin Pool
- fix up commit in directory with some deleted files
317
                   'bzrlib.selftest.test_commit',
1342 by Martin Pool
- start some tests for commit of merge revisions
318
                   'bzrlib.selftest.test_commit_merge',
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
319
                   'bzrlib.selftest.versioning',
320
                   'bzrlib.selftest.testmerge3',
974.1.80 by Aaron Bentley
Improved merge error handling and testing
321
                   'bzrlib.selftest.testmerge',
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
322
                   'bzrlib.selftest.testhashcache',
323
                   'bzrlib.selftest.teststatus',
324
                   'bzrlib.selftest.testlog',
325
                   'bzrlib.selftest.testrevisionnamespaces',
326
                   'bzrlib.selftest.testbranch',
1185.1.18 by Robert Collins
Lalo Martins remotebranch patch
327
                   'bzrlib.selftest.testremotebranch',
1270 by Martin Pool
- fix recording of merged ancestry lines
328
                   'bzrlib.selftest.testrevision',
1185.5.4 by John Arbash Meinel
Updated bzr revision-info, created tests.
329
                   'bzrlib.selftest.test_revision_info',
1277 by Martin Pool
- turn on merge_core tests again
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',
1185.3.29 by John Arbash Meinel
Added test cases for handling bogus files.
332
                   'bzrlib.selftest.test_bad_files',
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
333
                   'bzrlib.selftest.testdiff',
1273 by Martin Pool
- fix up copy_branch, etc
334
                   'bzrlib.selftest.test_parent',
1181 by Martin Pool
- add test for deserialization from a canned XML inventory
335
                   'bzrlib.selftest.test_xml',
1234 by Martin Pool
- run weave tests from bzr selftest
336
                   'bzrlib.selftest.test_weave',
1274 by Martin Pool
- stub out obsolete fetch tests
337
                   'bzrlib.selftest.testfetch',
1281 by Martin Pool
- reenable whitebox tests
338
                   'bzrlib.selftest.whitebox',
974.1.44 by aaron.bentley at utoronto
Added test of double-add in ImmutableStore
339
                   'bzrlib.selftest.teststore',
1288 by Martin Pool
- reenable blackbox tests - everything passes!
340
                   'bzrlib.selftest.blackbox',
974.1.57 by aaron.bentley at utoronto
Started work on djkstra longest-path algorithm
341
                   'bzrlib.selftest.testgraph',
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
342
                   ]
343
855 by Martin Pool
- Patch from John to allow plugins to add their own tests.
344
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
345
              bzrlib.osutils, bzrlib.commands, bzrlib.merge3):
346
        if m not in MODULES_TO_DOCTEST:
347
            MODULES_TO_DOCTEST.append(m)
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
348
1102 by Martin Pool
- merge test refactoring from robertc
349
    TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
350
    print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
744 by Martin Pool
- show nicer descriptions while running tests
351
    print
721 by Martin Pool
- framework for running external commands from unittest suite
352
    suite = TestSuite()
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
353
    suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
855 by Martin Pool
- Patch from John to allow plugins to add their own tests.
354
    for m in MODULES_TO_TEST:
355
         suite.addTest(TestLoader().loadTestsFromModule(m))
356
    for m in (MODULES_TO_DOCTEST):
721 by Martin Pool
- framework for running external commands from unittest suite
357
        suite.addTest(DocTestSuite(m))
908 by Martin Pool
- merge john's plugins-have-test_suite.patch:
358
    for p in bzrlib.plugin.all_plugins:
359
        if hasattr(p, 'test_suite'):
360
            suite.addTest(p.test_suite())
1092.1.17 by Robert Collins
remove TEST_CLASSES dead code and provide a bzrlib.test_suite() convenience method
361
    return suite
764 by Martin Pool
- log messages from a particular test are printed if that test fails
362