/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/tests/__init__.py

Update to bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
861
861
            return
862
862
        if message is None:
863
863
            message = "texts not equal:\n"
 
864
        if a == b + '\n':
 
865
            message = 'first string is missing a final newline.\n'
 
866
        if a + '\n' == b:
 
867
            message = 'second string is missing a final newline.\n'
864
868
        raise AssertionError(message +
865
869
                             self._ndiff_strings(a, b))
866
870
        
2268
2272
    return condition
2269
2273
 
2270
2274
 
 
2275
def condition_id_startswith(start):
 
2276
    """Create a condition filter verifying that test's id starts with a string.
 
2277
    
 
2278
    :param start: A string.
 
2279
    :return: A callable that returns True if the test's id starts with the
 
2280
        given string.
 
2281
    """
 
2282
    def condition(test):
 
2283
        return test.id().startswith(start)
 
2284
    return condition
 
2285
 
 
2286
 
2271
2287
def exclude_tests_by_condition(suite, condition):
2272
2288
    """Create a test suite which excludes some tests from suite.
2273
2289
 
2324
2340
    return result_suite
2325
2341
 
2326
2342
 
 
2343
def filter_suite_by_id_startswith(suite, start):
 
2344
    """Create a test suite by filtering another one.
 
2345
 
 
2346
    :param suite: The source suite.
 
2347
    :param start: A string the test id must start with.
 
2348
    :returns: the newly created suite
 
2349
    """
 
2350
    condition = condition_id_startswith(start)
 
2351
    result_suite = filter_suite_by_condition(suite, condition)
 
2352
    return result_suite
 
2353
 
 
2354
 
2327
2355
def exclude_tests_by_re(suite, pattern):
2328
2356
    """Create a test suite which excludes some tests from suite.
2329
2357
 
2469
2497
             strict=False,
2470
2498
             load_list=None,
2471
2499
             debug_flags=None,
 
2500
             starting_with=None,
2472
2501
             ):
2473
2502
    """Run the whole test suite under the enhanced runner"""
2474
2503
    # XXX: Very ugly way to do this...
2492
2521
        else:
2493
2522
            keep_only = load_test_id_list(load_list)
2494
2523
        if test_suite_factory is None:
2495
 
            suite = test_suite(keep_only)
 
2524
            suite = test_suite(keep_only, starting_with)
2496
2525
        else:
2497
2526
            suite = test_suite_factory()
2498
2527
        return run_suite(suite, 'testbzr', verbose=verbose, pattern=pattern,
2612
2641
        return self.tests.has_key(test_id)
2613
2642
 
2614
2643
 
2615
 
def test_suite(keep_only=None):
 
2644
def test_suite(keep_only=None, starting_with=None):
2616
2645
    """Build and return TestSuite for the whole of bzrlib.
2617
2646
 
2618
2647
    :param keep_only: A list of test ids limiting the suite returned.
2619
2648
 
 
2649
    :param starting_with: An id limiting the suite returned to the tests
 
2650
         starting with it.
 
2651
 
2620
2652
    This function can be replaced if you need to change the default test
2621
2653
    suite on a global basis, but it is not encouraged.
2622
2654
    """
2713
2745
                   'bzrlib.tests.test_registry',
2714
2746
                   'bzrlib.tests.test_remote',
2715
2747
                   'bzrlib.tests.test_repository',
 
2748
                   'bzrlib.tests.per_repository_reference',
2716
2749
                   'bzrlib.tests.test_revert',
2717
2750
                   'bzrlib.tests.test_revision',
2718
2751
                   'bzrlib.tests.test_revisionspec',
2768
2801
 
2769
2802
    loader = TestUtil.TestLoader()
2770
2803
 
2771
 
    if keep_only is None:
2772
 
        loader = TestUtil.TestLoader()
2773
 
    else:
 
2804
    if starting_with is not None:
 
2805
        # We take precedence over keep_only because *at loading time* using
 
2806
        # both options means we will load less tests for the same final result.
 
2807
        def interesting_module(name):
 
2808
            return (
 
2809
                # Either the module name starts with the specified string
 
2810
                name.startswith(starting_with)
 
2811
                # or it may contain tests starting with the specified string
 
2812
                or starting_with.startswith(name)
 
2813
                )
 
2814
        loader = TestUtil.FilteredByModuleTestLoader(interesting_module)
 
2815
 
 
2816
    elif keep_only is not None:
2774
2817
        id_filter = TestIdList(keep_only)
2775
2818
        loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
 
2819
        def interesting_module(name):
 
2820
            return id_filter.refers_to(name)
 
2821
 
 
2822
    else:
 
2823
        loader = TestUtil.TestLoader()
 
2824
        def interesting_module(name):
 
2825
            # No filtering, all modules are interesting
 
2826
            return True
 
2827
 
2776
2828
    suite = loader.suiteClass()
2777
2829
 
2778
2830
    # modules building their suite with loadTestsFromModuleNames
2795
2847
        ]
2796
2848
 
2797
2849
    for mod in modules_to_doctest:
2798
 
        if not (keep_only is None or id_filter.refers_to(mod)):
 
2850
        if not interesting_module(mod):
2799
2851
            # No tests to keep here, move along
2800
2852
            continue
2801
2853
        try:
2807
2859
 
2808
2860
    default_encoding = sys.getdefaultencoding()
2809
2861
    for name, plugin in bzrlib.plugin.plugins().items():
2810
 
        if keep_only is not None:
2811
 
            if not id_filter.refers_to(plugin.module.__name__):
2812
 
                continue
 
2862
        if not interesting_module(plugin.module.__name__):
 
2863
            continue
2813
2864
        plugin_suite = plugin.test_suite()
2814
2865
        # We used to catch ImportError here and turn it into just a warning,
2815
2866
        # but really if you don't have --no-plugins this should be a failure.
2825
2876
            reload(sys)
2826
2877
            sys.setdefaultencoding(default_encoding)
2827
2878
 
 
2879
    if starting_with is not None:
 
2880
        suite = filter_suite_by_id_startswith(suite, starting_with)
 
2881
 
2828
2882
    if keep_only is not None:
2829
2883
        # Now that the referred modules have loaded their tests, keep only the
2830
2884
        # requested ones.
2831
2885
        suite = filter_suite_by_id_list(suite, id_filter)
2832
2886
        # Do some sanity checks on the id_list filtering
2833
2887
        not_found, duplicates = suite_matches_id_list(suite, keep_only)
2834
 
        for id in not_found:
2835
 
            bzrlib.trace.warning('"%s" not found in the test suite', id)
 
2888
        if starting_with is not None:
 
2889
            # The tester has used both keep_only and starting_with, so he is
 
2890
            # already aware that some tests are excluded from the list, there
 
2891
            # is no need to tell him which.
 
2892
            pass
 
2893
        else:
 
2894
            # Some tests mentioned in the list are not in the test suite. The
 
2895
            # list may be out of date, report to the tester.
 
2896
            for id in not_found:
 
2897
                bzrlib.trace.warning('"%s" not found in the test suite', id)
2836
2898
        for id in duplicates:
2837
2899
            bzrlib.trace.warning('"%s" is used as an id by several tests', id)
2838
2900
 
2904
2966
 
2905
2967
def adapt_modules(mods_list, adapter, loader, suite):
2906
2968
    """Adapt the modules in mods_list using adapter and add to suite."""
2907
 
    for test in iter_suite_tests(loader.loadTestsFromModuleNames(mods_list)):
 
2969
    tests = loader.loadTestsFromModuleNames(mods_list)
 
2970
    adapt_tests(tests, adapter, suite)
 
2971
 
 
2972
 
 
2973
def adapt_tests(tests_list, adapter, suite):
 
2974
    """Adapt the tests in tests_list using adapter and add to suite."""
 
2975
    for test in iter_suite_tests(tests_list):
2908
2976
        suite.addTests(adapter.adapt(test))
2909
2977
 
2910
2978
 
2911
 
def adapt_tests(tests_list, adapter, loader, suite):
2912
 
    """Adapt the tests in tests_list using adapter and add to suite."""
2913
 
    for test in tests_list:
2914
 
        suite.addTests(adapter.adapt(loader.loadTestsFromName(test)))
2915
 
 
2916
 
 
2917
2979
def _rmtree_temp_dir(dirname):
2918
2980
    # If LANG=C we probably have created some bogus paths
2919
2981
    # which rmtree(unicode) will fail to delete
2998
3060
OsFifoFeature = _OsFifoFeature()
2999
3061
 
3000
3062
 
 
3063
class _UnicodeFilenameFeature(Feature):
 
3064
    """Does the filesystem support Unicode filenames?"""
 
3065
 
 
3066
    def _probe(self):
 
3067
        try:
 
3068
            os.stat(u'\u03b1')
 
3069
        except UnicodeEncodeError:
 
3070
            return False
 
3071
        except (IOError, OSError):
 
3072
            # The filesystem allows the Unicode filename but the file doesn't
 
3073
            # exist.
 
3074
            return True
 
3075
        else:
 
3076
            # The filesystem allows the Unicode filename and the file exists,
 
3077
            # for some reason.
 
3078
            return True
 
3079
 
 
3080
UnicodeFilenameFeature = _UnicodeFilenameFeature()
 
3081
 
 
3082
 
3001
3083
class TestScenarioApplier(object):
3002
3084
    """A tool to apply scenarios to tests."""
3003
3085