66
75
def packages_to_test():
76
"""Return a list of packages to test.
78
The packages are not globally imported so that import failures are
79
triggered when running selftest, not when importing the command.
67
82
import bzrlib.tests.blackbox
83
import bzrlib.tests.branch_implementations
86
bzrlib.tests.branch_implementations,
73
class EarlyStoppingTestResultAdapter(object):
74
"""An adapter for TestResult to stop at the first first failure or error"""
76
def __init__(self, result):
79
def addError(self, test, err):
80
self._result.addError(test, err)
83
def addFailure(self, test, err):
84
self._result.addFailure(test, err)
87
def __getattr__(self, name):
88
return getattr(self._result, name)
90
def __setattr__(self, name, value):
92
object.__setattr__(self, name, value)
93
return setattr(self._result, name, value)
96
90
class _MyResult(unittest._TextTestResult):
97
91
"""Custom TestResult.
99
93
Shows output in a different format, including displaying runtime for tests.
102
97
def _elapsedTime(self):
103
98
return "%5dms" % (1000 * (time.time() - self._start_time))
678
679
self.assertEqualDiff(content, open(path, 'r').read())
682
class TestCaseWithTransport(TestCaseInTempDir):
683
"""A test case that provides get_url and get_readonly_url facilities.
685
These back onto two transport servers, one for readonly access and one for
688
If no explicit class is provided for readonly access, a
689
ReadonlyTransportDecorator is used instead which allows the use of non disk
690
based read write transports.
692
If an explicit class is provided for readonly access, that server and the
693
readwrite one must both define get_url() as resolving to os.getcwd().
696
def __init__(self, methodName='testMethod'):
697
super(TestCaseWithTransport, self).__init__(methodName)
698
self.__readonly_server = None
700
self.transport_server = default_transport
701
self.transport_readonly_server = None
703
def get_readonly_url(self, relpath=None):
704
"""Get a URL for the readonly transport.
706
This will either be backed by '.' or a decorator to the transport
707
used by self.get_url()
708
relpath provides for clients to get a path relative to the base url.
709
These should only be downwards relative, not upwards.
711
if self.__readonly_server is None:
712
if self.transport_readonly_server is None:
713
# readonly decorator requested
714
# bring up the server
716
self.__readonly_server = ReadonlyServer()
717
self.__readonly_server.setUp(self.__server)
719
self.__readonly_server = self.transport_readonly_server()
720
self.__readonly_server.setUp()
721
self.addCleanup(self.__readonly_server.tearDown)
722
base = self.__readonly_server.get_url()
723
if relpath is not None:
724
if not base.endswith('/'):
726
base = base + relpath
729
def get_url(self, relpath=None):
730
"""Get a URL for the readwrite transport.
732
This will either be backed by '.' or to an equivalent non-file based
734
relpath provides for clients to get a path relative to the base url.
735
These should only be downwards relative, not upwards.
737
if self.__server is None:
738
self.__server = self.transport_server()
739
self.__server.setUp()
740
self.addCleanup(self.__server.tearDown)
741
base = self.__server.get_url()
742
if relpath is not None and relpath != '.':
743
if not base.endswith('/'):
745
base = base + relpath
748
def make_branch(self, relpath):
749
"""Create a branch on the transport at relpath."""
751
url = self.get_url(relpath)
752
segments = relpath.split('/')
753
if segments and segments[-1] not in ('', '.'):
754
parent = self.get_url('/'.join(segments[:-1]))
755
t = bzrlib.transport.get_transport(parent)
757
t.mkdir(segments[-1])
760
return bzrlib.branch.Branch.create(url)
761
except UninitializableFormat:
762
raise TestSkipped("Format %s is not initializable.")
764
def make_branch_and_tree(self, relpath):
765
"""Create a branch on the transport and a tree locally.
769
b = self.make_branch(relpath)
770
return WorkingTree.create(b, relpath)
773
class ChrootedTestCase(TestCaseWithTransport):
774
"""A support class that provides readonly urls outside the local namespace.
776
This is done by checking if self.transport_server is a MemoryServer. if it
777
is then we are chrooted already, if it is not then an HttpServer is used
780
TODO RBC 20060127: make this an option to TestCaseWithTransport so it can
781
be used without needed to redo it when a different
786
super(ChrootedTestCase, self).setUp()
787
if not self.transport_server == bzrlib.transport.memory.MemoryServer:
788
self.transport_readonly_server = bzrlib.transport.http.HttpServer
681
791
def filter_suite_by_re(suite, pattern):
682
792
result = TestSuite()
683
793
filter_re = re.compile(pattern)
715
826
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
717
829
"""Run the whole test suite under the enhanced runner"""
718
return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern,
719
stop_on_failure=stop_on_failure, keep_output=keep_output)
830
global default_transport
831
if transport is None:
832
transport = default_transport
833
old_transport = default_transport
834
default_transport = transport
837
return run_suite(suite, 'testbzr', verbose=verbose, pattern=pattern,
838
stop_on_failure=stop_on_failure, keep_output=keep_output,
841
default_transport = old_transport
722
845
def test_suite():
737
860
'bzrlib.tests.test_commit_merge',
738
861
'bzrlib.tests.test_config',
739
862
'bzrlib.tests.test_conflicts',
863
'bzrlib.tests.test_decorators',
740
864
'bzrlib.tests.test_diff',
741
'bzrlib.tests.test_decorators',
865
'bzrlib.tests.test_doc_generate',
742
866
'bzrlib.tests.test_fetch',
743
867
'bzrlib.tests.test_fileid_involved',
744
868
'bzrlib.tests.test_glob_matcher',
819
938
for m in (MODULES_TO_DOCTEST):
820
939
suite.addTest(DocTestSuite(m))
821
940
for name, plugin in bzrlib.plugin.all_plugins().items():
822
if hasattr(plugin, 'test_suite'):
941
if getattr(plugin, 'test_suite', None) is not None:
823
942
suite.addTest(plugin.test_suite())
946
def adapt_modules(mods_list, adapter, loader, suite):
947
"""Adapt the modules in mods_list using adapter and add to suite."""
948
for mod_name in mods_list:
949
mod = _load_module_by_name(mod_name)
950
for test in iter_suite_tests(loader.loadTestsFromModule(mod)):
951
suite.addTests(adapter.adapt(test))
827
954
def _load_module_by_name(mod_name):
828
955
parts = mod_name.split('.')
829
956
module = __import__(mod_name)