37
37
"""Base class for bzr unit tests.
39
39
Tests that need access to disk resources should subclass
40
FunctionalTestCase not TestCase.
40
TestCaseInTempDir not TestCase.
42
42
Error and debug log messages are redirected from their usual
43
43
location into a temporary file, the contents of which can be
123
123
self.fail("unexpected paths found in inventory: %r" % extras)
125
def apply_redirected(self, stdin=None, stdout=None, stderr=None,
126
a_callable=None, *args, **kwargs):
127
"""Call callable with redirected std io pipes.
129
Returns the return code."""
130
from StringIO import StringIO
131
if not callable(a_callable):
132
raise ValueError("a_callable must be callable.")
136
stdout = self._log_file
138
stderr = self._log_file
139
real_stdin = sys.stdin
140
real_stdout = sys.stdout
141
real_stderr = sys.stderr
147
result = a_callable(*args, **kwargs)
149
sys.stdout = real_stdout
150
sys.stderr = real_stderr
151
sys.stdin = real_stdin
125
155
BzrTestBase = TestCase
128
class FunctionalTestCase(TestCase):
129
"""Base class for tests that perform function testing - running bzr,
130
using files on disk, and similar activities.
158
class TestCaseInTempDir(TestCase):
159
"""Derived class that runs a test within a temporary directory.
161
This is useful for tests that need to create a branch, etc.
163
The directory is created in a slightly complex way: for each
164
Python invocation, a new temporary top-level directory is created.
165
All test cases create their own directory within that. If the
166
tests complete successfully, the directory is removed.
132
168
InTempDir is an old alias for FunctionalTestCase.
152
if FunctionalTestCase.TEST_ROOT is not None:
188
if TestCaseInTempDir.TEST_ROOT is not None:
154
FunctionalTestCase.TEST_ROOT = os.path.abspath(
190
TestCaseInTempDir.TEST_ROOT = os.path.abspath(
155
191
tempfile.mkdtemp(suffix='.tmp',
156
192
prefix=self._TEST_NAME + '-',
159
195
# make a fake bzr directory there to prevent any tests propagating
160
196
# up onto the source directory's real branch
161
os.mkdir(os.path.join(FunctionalTestCase.TEST_ROOT, '.bzr'))
197
os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
164
super(FunctionalTestCase, self).setUp()
200
super(TestCaseInTempDir, self).setUp()
166
202
self._make_test_root()
167
203
self._currentdir = os.getcwdu()
172
208
def tearDown(self):
174
210
os.chdir(self._currentdir)
175
super(FunctionalTestCase, self).tearDown()
211
super(TestCaseInTempDir, self).tearDown()
177
213
def _formcmd(self, cmd):
178
214
if isinstance(cmd, basestring):
253
def apply_redirected(self, stdin=None, stdout=None, stderr=None,
254
a_callable=None, *args, **kwargs):
255
"""Call callable with redirected std io pipes.
257
Returns the return code."""
258
from StringIO import StringIO
259
if not callable(a_callable):
260
raise ValueError("a_callable must be callable.")
264
stdout = self._log_file
266
stderr = self._log_file
267
real_stdin = sys.stdin
268
real_stdout = sys.stdout
269
real_stderr = sys.stderr
275
result = a_callable(*args, **kwargs)
277
sys.stdout = real_stdout
278
sys.stderr = real_stderr
279
sys.stdin = real_stdin
283
InTempDir = FunctionalTestCase
286
290
class MetaTestLog(TestCase):
287
291
def test_logging(self):