~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

  • Committer: Martin Pool
  • Date: 2005-09-13 23:42:32 UTC
  • mto: (1185.8.2) (974.1.91)
  • mto: This revision was merged to the branch mainline in revision 1390.
  • Revision ID: mbp@sourcefrog.net-20050913234232-4d901f2d843a35f3
- ignore .DS_Store by default

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import tempfile
21
21
import os
22
22
import sys
 
23
import errno
 
24
import subprocess
 
25
import shutil
23
26
 
24
 
from testsweet import run_suite
 
27
import testsweet
25
28
import bzrlib.commands
26
29
 
27
30
import bzrlib.trace
28
31
import bzrlib.fetch
29
32
 
 
33
 
30
34
MODULES_TO_TEST = []
31
35
MODULES_TO_DOCTEST = []
32
36
 
33
37
from logging import debug, warning, error
34
38
 
 
39
class CommandFailed(Exception):
 
40
    pass
35
41
 
36
42
class TestCase(unittest.TestCase):
37
43
    """Base class for bzr unit tests.
70
76
        
71
77
        self._log_file_name = name
72
78
 
73
 
        
74
79
    def tearDown(self):
75
80
        logging.getLogger('').removeHandler(self._log_hdlr)
76
81
        bzrlib.trace.enable_default_logging()
101
106
                                       bzrlib.commands.run_bzr, args)
102
107
        self.assertEquals(result, retcode)
103
108
        
 
109
        
104
110
    def check_inventory_shape(self, inv, shape):
105
111
        """
106
112
        Compare an inventory to a list of expected names.
133
139
        if stdin is None:
134
140
            stdin = StringIO("")
135
141
        if stdout is None:
136
 
            stdout = self._log_file
 
142
            if hasattr(self, "_log_file"):
 
143
                stdout = self._log_file
 
144
            else:
 
145
                stdout = StringIO()
137
146
        if stderr is None:
138
 
            stderr = self._log_file
 
147
            if hasattr(self, "_log_file"):
 
148
                stderr = self._log_file
 
149
            else:
 
150
                stderr = StringIO()
139
151
        real_stdin = sys.stdin
140
152
        real_stdout = sys.stdout
141
153
        real_stderr = sys.stderr
142
 
        result = None
143
154
        try:
144
155
            sys.stdout = stdout
145
156
            sys.stderr = stderr
146
157
            sys.stdin = stdin
147
 
            result = a_callable(*args, **kwargs)
 
158
            return a_callable(*args, **kwargs)
148
159
        finally:
149
160
            sys.stdout = real_stdout
150
161
            sys.stderr = real_stderr
151
162
            sys.stdin = real_stdin
152
 
        return result
153
163
 
154
164
 
155
165
BzrTestBase = TestCase
181
191
            self.fail("contents of %s not as expected")
182
192
 
183
193
    def _make_test_root(self):
184
 
        import os
185
 
        import shutil
186
 
        import tempfile
187
 
        
188
194
        if TestCaseInTempDir.TEST_ROOT is not None:
189
195
            return
190
 
        TestCaseInTempDir.TEST_ROOT = os.path.abspath(
191
 
                                 tempfile.mkdtemp(suffix='.tmp',
192
 
                                                  prefix=self._TEST_NAME + '-',
193
 
                                                  dir=os.curdir))
194
 
    
 
196
        i = 0
 
197
        while True:
 
198
            root = 'test%04d.tmp' % i
 
199
            try:
 
200
                os.mkdir(root)
 
201
            except OSError, e:
 
202
                if e.errno == errno.EEXIST:
 
203
                    i += 1
 
204
                    continue
 
205
                else:
 
206
                    raise
 
207
            # successfully created
 
208
            TestCaseInTempDir.TEST_ROOT = os.path.abspath(root)
 
209
            break
195
210
        # make a fake bzr directory there to prevent any tests propagating
196
211
        # up onto the source directory's real branch
197
212
        os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
228
243
        If a single string is based, it is split into words.
229
244
        For commands that are not simple space-separated words, please
230
245
        pass a list instead."""
231
 
        try:
232
 
            import shutil
233
 
            from subprocess import call
234
 
        except ImportError, e:
235
 
            _need_subprocess()
236
 
            raise
237
246
        cmd = self._formcmd(cmd)
238
247
        self.log('$ ' + ' '.join(cmd))
239
 
        actual_retcode = call(cmd, stdout=self._log_file, stderr=self._log_file)
 
248
        actual_retcode = subprocess.call(cmd, stdout=self._log_file,
 
249
                                         stderr=self._log_file)
240
250
        if retcode != actual_retcode:
241
251
            raise CommandFailed("test failed: %r returned %d, expected %d"
242
252
                                % (cmd, actual_retcode, retcode))
243
253
 
244
254
    def backtick(self, cmd, retcode=0):
245
255
        """Run a command and return its output"""
246
 
        try:
247
 
            import shutil
248
 
            from subprocess import Popen, PIPE
249
 
        except ImportError, e:
250
 
            _need_subprocess()
251
 
            raise
252
 
 
253
256
        cmd = self._formcmd(cmd)
254
 
        child = Popen(cmd, stdout=PIPE, stderr=self._log_file)
 
257
        child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=self._log_file)
255
258
        outd, errd = child.communicate()
256
259
        self.log(outd)
257
260
        actual_retcode = child.wait()
297
300
 
298
301
 
299
302
def selftest(verbose=False, pattern=".*"):
300
 
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
 
303
    return testsweet.run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
301
304
 
302
305
 
303
306
def test_suite():
314
317
 
315
318
    testmod_names = \
316
319
                  ['bzrlib.selftest.MetaTestLog',
 
320
                   'bzrlib.selftest.test_parent',
317
321
                   'bzrlib.selftest.testinv',
318
322
                   'bzrlib.selftest.testfetch',
319
323
                   'bzrlib.selftest.versioning',
329
333
                   'bzrlib.selftest.test_merge_core',
330
334
                   'bzrlib.selftest.test_smart_add',
331
335
                   'bzrlib.selftest.testdiff',
332
 
                   'bzrlib.fetch'
 
336
                   'bzrlib.selftest.test_xml',
 
337
                   'bzrlib.fetch',
 
338
                   'bzrlib.selftest.teststore',
 
339
                   'bzrlib.selftest.testgraph',
333
340
                   ]
334
341
 
335
342
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,