Ticket #5787: trial-order.patch

File trial-order.patch, 4.4 KB (added by Julian Berman, 8 years ago)
  • twisted/scripts/trial.py

    diff --git a/twisted/scripts/trial.py b/twisted/scripts/trial.py
    index 2afa4b9..7cc7ebc 100644
    a b  
    55
    66
    77from __future__ import print_function
    8 import dis
    98import gc
    109import inspect
    1110import os
    def _maybeFindSourceLine(testThing): 
    106105    """
    107106    Try to find the source line of the given test thing.
    108107
    109 
    110     @param testThing: a test method or class
     108    @param testThing: the test item to attempt to inspect
     109    @type testThing: an L{TestCase}, test method, or module, though only the
     110        former two have a chance to succeed
    111111    @rtype: int
    112112    @return: the starting source line, or -1 if one couldn't be found
    113113    """
    114114
     115    # an instance of L{TestCase} -- locate the test it will run
    115116    method = getattr(testThing, "_testMethodName", None)
    116117    if method is not None:
    117118        testThing = getattr(testThing, method)
    118119
    119120    # If it's a function, we can get the line number even if the source file no
    120121    # longer exists
    121     code = getattr(testThing, "func_code", None)
     122    code = getattr(testThing, "__code__", None)
    122123    if code is not None:
    123         _, startLine = next(dis.findlinestarts(code))
    124         return startLine
     124        return code.co_firstlineno
    125125
    126126    try:
    127127        return inspect.getsourcelines(testThing)[1]
    def _coerceOrder(order): 
    150150    tests should be done when it actually will be used, as the default argument
    151151    will not be coerced by this function.
    152152
    153     @param order: one of the known orders in L{_runOrders}
     153    @param order: one of the known orders in C{_runOrders}
    154154    @return: the order unmodified
    155155    """
    156156    if order not in _runOrders:
    157157        raise usage.UsageError(
    158158            "--order must be one of: %s. See --help-orders for details" %
    159             ", ".join(repr(order) for order in _runOrders))
     159            (", ".join(repr(order) for order in _runOrders),))
    160160    return order
    161161
    162162
    class _BasicOptions(object): 
    270270
    271271    def opt_help_orders(self):
    272272        synopsis = ("Trial can attempt to run test cases and their methods in "
    273                     "a few different\n orders. You can select any of the "
     273                    "a few different orders. You can select any of the "
    274274                    "following options using --order=<foo>.\n")
    275275
    276276        print(synopsis)
    class Options(_BasicOptions, usage.Options, app.ReactorSelectionMixin): 
    466466                raise usage.UsageError("You must specify --debug when using "
    467467                                       "--nopm ")
    468468            failure.DO_POST_MORTEM = False
     469        if self['order'] and self['random']:
     470            raise usage.UsageError(
     471                "You can't specify --random when using --order")
    469472
    470473
    471474
  • twisted/trial/test/test_script.py

    diff --git a/twisted/trial/test/test_script.py b/twisted/trial/test/test_script.py
    index 59c1a73..fa7b788 100644
    a b class OptionsTestCase(unittest.TestCase): 
    480480        self.assertEqual(["--recursionlimit", "2000"], args)
    481481
    482482
     483    def test_randomConflictsWithOrder(self):
     484        """
     485        C{parseOptions} raises a C{UsageError} when C{--random} is passed along
     486        C{--order}.
     487        """
     488        opts = ["--random", "4", "--order", "alphabetical"]
     489        error = self.assertRaises(UsageError, self.options.parseOptions, opts)
     490        self.assertEqual("You can't specify --random when using --order",
     491                         str(error))
     492
     493
    483494    def test_jobsConflictWithDebug(self):
    484495        """
    485496        C{parseOptions} raises a C{UsageError} when C{--debug} is passed along
    class HelpOrderTests(unittest.TestCase): 
    810821        """
    811822        --help-orders prints each of the available orders and then exits.
    812823        """
    813         self.status = None
    814         self.patch(
    815             trial.sys, "exit", lambda status: setattr(self, "status", status))
    816824        self.patch(sys, "stdout", StringIO.StringIO())
    817825
    818         trial.Options().parseOptions(["--help-orders"])
     826        exc = self.assertRaises(
     827            SystemExit, trial.Options().parseOptions, ["--help-orders"])
     828        self.assertEqual(exc.code, 0)
    819829
    820830        for orderName, (orderDesc, _) in trial._runOrders.items():
    821831            match = re.search(
    822832                "{0}.*{1}".format(orderName, orderDesc), sys.stdout.getvalue(),
    823833            )
    824834            self.assertTrue(match)
    825         self.assertEqual(self.status, 0)