/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

remove more duplicate merged hunks. Bad MERGE3, BAD.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
 
from cStringIO import StringIO
19
18
import logging
20
19
import unittest
21
20
import tempfile
22
21
import os
23
22
import sys
24
 
import errno
25
23
import subprocess
26
 
import shutil
27
 
import testsweet
28
24
 
 
25
from testsweet import run_suite
29
26
import bzrlib.commands
 
27
 
30
28
import bzrlib.trace
31
29
import bzrlib.fetch
32
30
 
76
74
        
77
75
        self._log_file_name = name
78
76
 
 
77
    def run(self, result):
 
78
        self.apply_redirected(None, None, None,
 
79
                              unittest.TestCase.run, self, result)
 
80
        
79
81
    def tearDown(self):
80
82
        logging.getLogger('').removeHandler(self._log_hdlr)
81
83
        bzrlib.trace.enable_default_logging()
83
85
        self._log_file.close()
84
86
        unittest.TestCase.tearDown(self)
85
87
 
 
88
 
86
89
    def log(self, *args):
87
90
        logging.debug(*args)
88
91
 
90
93
        """Return as a string the log for this test"""
91
94
        return open(self._log_file_name).read()
92
95
 
93
 
 
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
 
 
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.
 
96
    def run_bzr(self, *args, **kwargs):
 
97
        """Invoke bzr, as if it were run from the command line.
103
98
 
104
99
        This should be the main method for tests that want to exercise the
105
100
        overall behavior of the bzr application (rather than a unit test
107
102
 
108
103
        Much of the old code runs bzr by forking a new copy of Python, but
109
104
        that is slower, harder to debug, and generally not necessary.
110
 
 
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
 
 
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))
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)
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
 
 
142
 
    def run_bzr(self, *args, **kwargs):
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
 
 
149
 
        This sends the stdout/stderr results into the test's log,
150
 
        where it may be useful for debugging.  See also run_captured.
151
 
        """
152
 
        retcode = kwargs.pop('retcode', 0)
153
 
        return self.run_bzr_captured(args, retcode)
154
 
 
 
105
        """
 
106
        retcode = kwargs.get('retcode', 0)
 
107
        result = self.apply_redirected(None, None, None,
 
108
                                       bzrlib.commands.run_bzr, args)
 
109
        self.assertEquals(result, retcode)
 
110
        
 
111
        
155
112
    def check_inventory_shape(self, inv, shape):
156
113
        """
157
114
        Compare an inventory to a list of expected names.
178
135
        """Call callable with redirected std io pipes.
179
136
 
180
137
        Returns the return code."""
 
138
        from StringIO import StringIO
181
139
        if not callable(a_callable):
182
140
            raise ValueError("a_callable must be callable.")
183
141
        if stdin is None:
235
193
            self.fail("contents of %s not as expected")
236
194
 
237
195
    def _make_test_root(self):
 
196
        import os
 
197
        import shutil
 
198
        import tempfile
 
199
        
238
200
        if TestCaseInTempDir.TEST_ROOT is not None:
239
201
            return
240
 
        i = 0
241
 
        while True:
242
 
            root = 'test%04d.tmp' % i
243
 
            try:
244
 
                os.mkdir(root)
245
 
            except OSError, e:
246
 
                if e.errno == errno.EEXIST:
247
 
                    i += 1
248
 
                    continue
249
 
                else:
250
 
                    raise
251
 
            # successfully created
252
 
            TestCaseInTempDir.TEST_ROOT = os.path.abspath(root)
253
 
            break
 
202
        TestCaseInTempDir.TEST_ROOT = os.path.abspath(
 
203
                                 tempfile.mkdtemp(suffix='.tmp',
 
204
                                                  prefix=self._TEST_NAME + '-',
 
205
                                                  dir=os.curdir))
 
206
    
254
207
        # make a fake bzr directory there to prevent any tests propagating
255
208
        # up onto the source directory's real branch
256
209
        os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
257
210
 
258
211
    def setUp(self):
259
212
        super(TestCaseInTempDir, self).setUp()
 
213
        import os
260
214
        self._make_test_root()
261
215
        self._currentdir = os.getcwdu()
262
216
        self.test_dir = os.path.join(self.TEST_ROOT, self.id())
264
218
        os.chdir(self.test_dir)
265
219
        
266
220
    def tearDown(self):
 
221
        import os
267
222
        os.chdir(self._currentdir)
268
223
        super(TestCaseInTempDir, self).tearDown()
269
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))
 
245
        actual_retcode = subprocess.call(cmd, stdout=self._log_file,
 
246
                                         stderr=self._log_file)
 
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)
 
254
        child = subprocess.Popen(cmd, stdout=subprocess.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
 
270
269
    def build_tree(self, shape):
271
270
        """Build a test tree according to a pattern.
272
271
 
276
275
        This doesn't add anything to a branch.
277
276
        """
278
277
        # XXX: It's OK to just create them using forward slashes on windows?
 
278
        import os
279
279
        for name in shape:
280
280
            assert isinstance(name, basestring)
281
281
            if name[-1] == '/':
284
284
                f = file(name, 'wt')
285
285
                print >>f, "contents of", name
286
286
                f.close()
 
287
                
287
288
 
288
289
 
289
290
class MetaTestLog(TestCase):
296
297
 
297
298
 
298
299
def selftest(verbose=False, pattern=".*"):
299
 
    return testsweet.run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
 
300
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
300
301
 
301
302
 
302
303
def test_suite():
304
305
    import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
305
306
    import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
306
307
    from doctest import DocTestSuite
 
308
    import os
 
309
    import shutil
 
310
    import time
 
311
    import sys
307
312
 
308
313
    global MODULES_TO_TEST, MODULES_TO_DOCTEST
309
314
 
315
320
                   'bzrlib.selftest.versioning',
316
321
                   'bzrlib.selftest.whitebox',
317
322
                   'bzrlib.selftest.testmerge3',
318
 
                   'bzrlib.selftest.testmerge',
319
323
                   'bzrlib.selftest.testhashcache',
320
324
                   'bzrlib.selftest.teststatus',
321
325
                   'bzrlib.selftest.testlog',
322
326
                   'bzrlib.selftest.blackbox',
323
327
                   'bzrlib.selftest.testrevisionnamespaces',
324
328
                   'bzrlib.selftest.testbranch',
325
 
                   'bzrlib.selftest.testremotebranch',
326
329
                   'bzrlib.selftest.testrevision',
327
 
                   'bzrlib.selftest.test_revision_info',
328
330
                   'bzrlib.selftest.test_merge_core',
329
331
                   'bzrlib.selftest.test_smart_add',
330
332
                   'bzrlib.selftest.testdiff',