61
64
from bzrlib.revision import common_ancestor
62
65
import bzrlib.store
63
66
import bzrlib.trace
64
from bzrlib.transport import urlescape, get_transport
67
from bzrlib.transport import get_transport
65
68
import bzrlib.transport
66
69
from bzrlib.transport.local import LocalRelpathServer
67
70
from bzrlib.transport.readonly import ReadonlyServer
68
71
from bzrlib.trace import mutter
69
72
from bzrlib.tests.TestUtil import TestLoader, TestSuite
70
73
from bzrlib.tests.treeshape import build_tree_contents
74
import bzrlib.urlutils as urlutils
71
75
from bzrlib.workingtree import WorkingTree, WorkingTreeFormat2
73
77
default_transport = LocalRelpathServer
324
331
test_root = TestCaseInTempDir.TEST_ROOT
325
332
if result.wasSuccessful() or not self.keep_output:
326
333
if test_root is not None:
327
osutils.rmtree(test_root)
334
# If LANG=C we probably have created some bogus paths
335
# which rmtree(unicode) will fail to delete
336
# so make sure we are using rmtree(str) to delete everything
337
osutils.rmtree(test_root.encode(
338
sys.getfilesystemencoding()))
329
340
if self.pb is not None:
330
341
self.pb.note("Failed tests working directories are in '%s'\n",
360
371
class CommandFailed(Exception):
375
class StringIOWrapper(object):
376
"""A wrapper around cStringIO which just adds an encoding attribute.
378
Internally we can check sys.stdout to see what the output encoding
379
should be. However, cStringIO has no encoding attribute that we can
380
set. So we wrap it instead.
385
def __init__(self, s=None):
387
self.__dict__['_cstring'] = StringIO(s)
389
self.__dict__['_cstring'] = StringIO()
391
def __getattr__(self, name, getattr=getattr):
392
return getattr(self.__dict__['_cstring'], name)
394
def __setattr__(self, name, val):
395
if name == 'encoding':
396
self.__dict__['encoding'] = val
398
return setattr(self._cstring, name, val)
363
401
class TestCase(unittest.TestCase):
364
402
"""Base class for bzr unit tests.
446
484
raise AssertionError('pattern "%s" not found in "%s"'
447
485
% (needle_re, haystack))
487
def assertNotContainsRe(self, haystack, needle_re):
488
"""Assert that a does not match a regular expression"""
489
if re.search(needle_re, haystack):
490
raise AssertionError('pattern "%s" found in "%s"'
491
% (needle_re, haystack))
449
493
def assertSubset(self, sublist, superlist):
450
494
"""Assert that every entry in sublist is present in superlist."""
598
642
"""Shortcut that splits cmd into words, runs, and returns stdout"""
599
643
return self.run_bzr_captured(cmd.split(), retcode=retcode)[0]
601
def run_bzr_captured(self, argv, retcode=0, stdin=None):
645
def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None):
602
646
"""Invoke bzr and return (stdout, stderr).
604
648
Useful for code that wants to check the contents of the
615
659
errors, and with logging set to something approximating the
616
660
default, so that error reporting can be checked.
618
argv -- arguments to invoke bzr
619
retcode -- expected return code, or None for don't-care.
662
:param argv: arguments to invoke bzr
663
:param retcode: expected return code, or None for don't-care.
664
:param encoding: encoding for sys.stdout and sys.stderr
620
665
:param stdin: A string to be used as stdin for the command.
668
encoding = bzrlib.user_encoding
622
669
if stdin is not None:
623
670
stdin = StringIO(stdin)
626
self.log('run bzr: %s', ' '.join(argv))
671
stdout = StringIOWrapper()
672
stderr = StringIOWrapper()
673
stdout.encoding = encoding
674
stderr.encoding = encoding
676
self.log('run bzr: %r', argv)
627
677
# FIXME: don't call into logging here
628
678
handler = logging.StreamHandler(stderr)
629
679
handler.setFormatter(bzrlib.trace.QuietFormatter())
643
693
logger.removeHandler(handler)
644
694
bzrlib.ui.ui_factory = old_ui_factory
645
696
out = stdout.getvalue()
646
697
err = stderr.getvalue()
648
self.log('output:\n%s', out)
699
self.log('output:\n%r', out)
650
self.log('errors:\n%s', err)
701
self.log('errors:\n%r', err)
651
702
if retcode is not None:
652
self.assertEquals(result, retcode)
703
self.assertEquals(retcode, result)
655
706
def run_bzr(self, *args, **kwargs):
665
716
:param stdin: A string to be used as stdin for the command.
667
718
retcode = kwargs.pop('retcode', 0)
719
encoding = kwargs.pop('encoding', None)
668
720
stdin = kwargs.pop('stdin', None)
669
return self.run_bzr_captured(args, retcode, stdin)
721
return self.run_bzr_captured(args, retcode=retcode, encoding=encoding, stdin=stdin)
723
def run_bzr_decode(self, *args, **kwargs):
724
if kwargs.has_key('encoding'):
725
encoding = kwargs['encoding']
727
encoding = bzrlib.user_encoding
728
return self.run_bzr(*args, **kwargs)[0].decode(encoding)
730
def run_bzr_subprocess(self, *args, **kwargs):
731
"""Run bzr in a subprocess for testing.
733
This starts a new Python interpreter and runs bzr in there.
734
This should only be used for tests that have a justifiable need for
735
this isolation: e.g. they are testing startup time, or signal
736
handling, or early startup code, etc. Subprocess code can't be
737
profiled or debugged so easily.
739
:param retcode: The status code that is expected. Defaults to 0. If
740
None is supplied, the status code is not checked.
742
bzr_path = os.path.dirname(os.path.dirname(bzrlib.__file__))+'/bzr'
744
process = Popen([sys.executable, bzr_path]+args, stdout=PIPE,
746
out = process.stdout.read()
747
err = process.stderr.read()
748
retcode = process.wait()
749
supplied_retcode = kwargs.get('retcode', 0)
750
if supplied_retcode is not None:
751
assert supplied_retcode == retcode
671
754
def check_inventory_shape(self, inv, shape):
672
755
"""Compare an inventory to a list of expected names.
847
930
raise errors.BzrError('Invalid line ending request %r' % (line_endings,))
848
content = "contents of %s%s" % (name, end)
849
transport.put(urlescape(name), StringIO(content))
931
content = "contents of %s%s" % (name.encode('utf-8'), end)
932
transport.put(urlutils.escape(name), StringIO(content))
851
934
def build_tree_contents(self, shape):
852
935
build_tree_contents(shape)
862
945
def assertFileEqual(self, content, path):
863
946
"""Fail if path does not contain 'content'."""
864
947
self.failUnless(osutils.lexists(path))
948
# TODO: jam 20060427 Shouldn't this be 'rb'?
865
949
self.assertEqualDiff(content, open(path, 'r').read())
970
1054
def make_bzrdir(self, relpath, format=None):
972
1056
url = self.get_url(relpath)
973
segments = relpath.split('/')
1057
mutter('relpath %r => url %r', relpath, url)
1058
segments = url.split('/')
974
1059
if segments and segments[-1] not in ('', '.'):
975
parent = self.get_url('/'.join(segments[:-1]))
1060
parent = '/'.join(segments[:-1])
976
1061
t = get_transport(parent)
978
1063
t.mkdir(segments[-1])
1114
1199
'bzrlib.tests.test_api',
1115
1200
'bzrlib.tests.test_bad_files',
1116
1201
'bzrlib.tests.test_branch',
1202
'bzrlib.tests.test_bundle',
1117
1203
'bzrlib.tests.test_bzrdir',
1118
1204
'bzrlib.tests.test_command',
1119
1205
'bzrlib.tests.test_commit',
1145
1231
'bzrlib.tests.test_options',
1146
1232
'bzrlib.tests.test_osutils',
1147
1233
'bzrlib.tests.test_patch',
1234
'bzrlib.tests.test_patches',
1148
1235
'bzrlib.tests.test_permissions',
1149
1236
'bzrlib.tests.test_plugins',
1150
1237
'bzrlib.tests.test_progress',
1174
1261
'bzrlib.tests.test_tuned_gzip',
1175
1262
'bzrlib.tests.test_ui',
1176
1263
'bzrlib.tests.test_upgrade',
1264
'bzrlib.tests.test_urlutils',
1177
1265
'bzrlib.tests.test_versionedfile',
1178
1266
'bzrlib.tests.test_weave',
1179
1267
'bzrlib.tests.test_whitebox',