Example #1
0
 def test_time_accepted_stdlib(self):
     self.result = Python26TestResult()
     self.stream = BytesIO()
     self.protocol = subunit.TestProtocolServer(self.result,
                                                stream=self.stream)
     self.protocol.lineReceived(_b("time: 2001-12-12 12:59:59Z\n"))
     self.assertEqual(_b(""), self.stream.getvalue())
Example #2
0
    def _run(self, result, log):
        # Build things first, which may do nothing (but is quick).
        with self.lock:
            subprocess.check_call(
                ("make", "--quiet", "bin/coverage", self.script),
                stdout=log,
                stderr=log)
        # Run the script in a subprocess, capturing subunit output if
        # with_subunit is set.
        pread, pwrite = os.pipe()
        with io.open(pread, "rb") as preader:
            try:
                args = [self.script]
                if self.with_subunit:
                    args.extend(("--with-subunit", "--subunit-fd=%d" % pwrite))
                command = self.extendCommand(args)
                process = subprocess.Popen(command,
                                           pass_fds={pwrite},
                                           stdout=log,
                                           stderr=log)
            finally:
                os.close(pwrite)

            server = subunit.TestProtocolServer(result, sys.stdout.buffer)
            # Don't use TestProtocolServer.readFrom because it blocks until
            # the stream is complete (it uses readlines).
            for line in preader:
                server.lineReceived(line)
            server.lostConnection()

            return process.wait() == 0
Example #3
0
 def test_story(self):
     client = unittest.TestResult()
     out = BytesIO()
     protocol = subunit.TestProtocolServer(client, forward_stream=out)
     pipe = BytesIO(_b("test old mcdonald\n" "success old mcdonald\n"))
     protocol.readFrom(pipe)
     self.assertEqual(client.testsRun, 1)
     self.assertEqual(pipe.getvalue(), out.getvalue())
Example #4
0
 def test_includes_timing_output(self):
     io = BytesIO()
     runner = SubunitTestRunner(stream=io)
     test = PlaceHolder('name')
     runner.run(test)
     client = TimeCollectingTestResult()
     io.seek(0)
     subunit.TestProtocolServer(client).readFrom(io)
     self.assertTrue(len(client.time_called) > 0)
Example #5
0
 def test_progress_accepted_stdlib(self):
     self.result = Python26TestResult()
     self.stream = BytesIO()
     self.protocol = subunit.TestProtocolServer(self.result,
                                                stream=self.stream)
     self.protocol.lineReceived(_b("progress: 23"))
     self.protocol.lineReceived(_b("progress: -2"))
     self.protocol.lineReceived(_b("progress: +4"))
     self.assertEqual(_b(""), self.stream.getvalue())
Example #6
0
 def test_not_command(self):
     client = unittest.TestResult()
     out = StringIO()
     protocol = subunit.TestProtocolServer(client,
         stream=subunit.DiscardStream(), forward_stream=out)
     pipe = StringIO("success old mcdonald\n")
     protocol.readFrom(pipe)
     self.assertEqual(client.testsRun, 0)
     self.assertEqual("", out.getvalue())
Example #7
0
 def test_story(self):
     client = unittest.TestResult()
     out = StringIO()
     protocol = subunit.TestProtocolServer(client, forward_stream=out)
     pipe = StringIO("test old mcdonald\n"
                     "success old mcdonald\n")
     protocol.readFrom(pipe)
     mcdonald = subunit.RemotedTestCase("old mcdonald")
     self.assertEqual(client.testsRun, 1)
     self.assertEqual(pipe.getvalue(), out.getvalue())
Example #8
0
 def test_time_accepted_extended(self):
     self.result = ExtendedTestResult()
     self.stream = BytesIO()
     self.protocol = subunit.TestProtocolServer(self.result,
                                                stream=self.stream)
     self.protocol.lineReceived(_b("time: 2001-12-12 12:59:59Z\n"))
     self.assertEqual(_b(""), self.stream.getvalue())
     self.assertEqual(
         [('time',
           datetime.datetime(2001, 12, 12, 12, 59, 59, 0, iso8601.Utc()))],
         self.result._events)
Example #9
0
    def run(self, result):
        try:
            self.checkDependencies()
        except NotSupported as e:
            result.startTest(self)
            result.addSkip(self, str(e))
            result.stopTest(self)
            return
        js = self.findJavascriptInterpreter()
        try:
            script = self.makeScript(self.testMethod())
        except KeyError:
            result.addError(self, sys.exc_info())
            return

        server = subunit.TestProtocolServer(result)
        protocol = TestProtocolLineReceiverServer(server)

        # What this *SHOULD BE*
        # spawnProcess(protocol, js, (script,))
        # return protocol.someDisconnectCallback()

        # However, *run cannot return a Deferred profanity profanity profanity
        # profanity*, so instead it is *profanity* this:
        def run():
            argv = [js, script]
            msg("Running JavaScript interpreter, argv = %r" % (argv, ))
            child = Popen(argv, stdout=PIPE)
            while True:
                bytes = child.stdout.read(4096)
                if bytes:
                    protocol.dataReceived(bytes)
                else:
                    break
            returnCode = child.wait()
            if returnCode < 0:
                result.addError(
                    self, (Exception,
                           "JavaScript interpreter exited due to signal %d" %
                           (-returnCode, ), None))
            elif returnCode:
                result.addError(
                    self,
                    (Exception, "JavaScript interpreter had error exit: %d" %
                     (returnCode, ), None))

        self._runWithSigchild(run)
Example #10
0
 def test_progress_accepted_extended(self):
     # With a progress capable TestResult, progress events are emitted.
     self.result = ExtendedTestResult()
     self.stream = BytesIO()
     self.protocol = subunit.TestProtocolServer(self.result,
                                                stream=self.stream)
     self.protocol.lineReceived(_b("progress: 23"))
     self.protocol.lineReceived(_b("progress: push"))
     self.protocol.lineReceived(_b("progress: -2"))
     self.protocol.lineReceived(_b("progress: pop"))
     self.protocol.lineReceived(_b("progress: +4"))
     self.assertEqual(_b(""), self.stream.getvalue())
     self.assertEqual([
         ('progress', 23, subunit.PROGRESS_SET),
         ('progress', None, subunit.PROGRESS_PUSH),
         ('progress', -2, subunit.PROGRESS_CUR),
         ('progress', None, subunit.PROGRESS_POP),
         ('progress', 4, subunit.PROGRESS_CUR),
     ], self.result._events)
Example #11
0
 def test_story(self):
     client = unittest.TestResult()
     protocol = subunit.TestProtocolServer(client)
     pipe = StringIO("test old mcdonald\n"
                     "success old mcdonald\n"
                     "test bing crosby\n"
                     "failure bing crosby [\n"
                     "foo.c:53:ERROR invalid state\n"
                     "]\n"
                     "test an error\n"
                     "error an error\n")
     protocol.readFrom(pipe)
     mcdonald = subunit.RemotedTestCase("old mcdonald")
     bing = subunit.RemotedTestCase("bing crosby")
     an_error = subunit.RemotedTestCase("an error")
     self.assertEqual(client.errors, [(an_error, 'RemoteException: \n\n')])
     self.assertEqual(
         client.failures,
         [(bing, "RemoteException: foo.c:53:ERROR invalid state\n\n")])
     self.assertEqual(client.testsRun, 3)
Example #12
0
    def __init__(self, tree, name, command, fail_quickly=False):
        super(SubunitTreeStageBuilder, self).__init__(tree, name, command,
                                                      fail_quickly)
        self.failed_test = None
        self.subunit_path = os.path.join(
            gitroot, "%s.%s.subunit" % (self.tree.tag, self.name))
        self.tree.logfiles.append(
            (self.subunit_path, os.path.basename(self.subunit_path),
             "text/x-subunit"))
        self.subunit = open(self.subunit_path, 'w')

        formatter = subunithelper.PlainFormatter(False, True, {})
        clients = [
            formatter,
            subunit.TestProtocolClient(self.subunit),
            FailureTrackingTestResult(self)
        ]
        if fail_quickly:
            clients.append(AbortingTestResult(self))
        self.subunit_server = subunit.TestProtocolServer(
            testtools.MultiTestResult(*clients), self.subunit)
        self.buffered = ""
Example #13
0
 def test_story(self):
     client = unittest.TestResult()
     protocol = subunit.TestProtocolServer(client)
     pipe = BytesIO(
         _b("test old mcdonald\n"
            "success old mcdonald\n"
            "test bing crosby\n"
            "failure bing crosby [\n"
            "foo.c:53:ERROR invalid state\n"
            "]\n"
            "test an error\n"
            "error an error\n"))
     protocol.readFrom(pipe)
     bing = subunit.RemotedTestCase("bing crosby")
     an_error = subunit.RemotedTestCase("an error")
     self.assertEqual(client.errors,
                      [(an_error, _remote_exception_str + '\n')])
     self.assertEqual(
         client.failures,
         [(bing, _remote_exception_str + ": Text attachment: traceback\n"
           "------------\nfoo.c:53:ERROR invalid state\n"
           "------------\n\n")])
     self.assertEqual(client.testsRun, 3)
Example #14
0
 def setUp(self):
     self.client = ExtendedTestResult()
     self.protocol = subunit.TestProtocolServer(self.client)
Example #15
0
 def setUp(self):
     self.client = MockTestProtocolServerClient()
     self.protocol = subunit.TestProtocolServer(self.client)
Example #16
0
 def setUp(self):
     from StringIO import StringIO
     self.stdout = StringIO()
     self.test = subunit.RemotedTestCase("old mcdonald")
     self.client = MockTestProtocolServerClient()
     self.protocol = subunit.TestProtocolServer(self.client, self.stdout)
Example #17
0
 def setUp(self):
     """Setup a test object ready to be skipped."""
     self.client = ExtendedTestResult()
     self.protocol = subunit.TestProtocolServer(self.client)
     self.protocol.lineReceived(_b("test mcdonalds farm\n"))
     self.test = self.client._events[-1][-1]
Example #18
0
 def setUp(self):
     self.client = ExtendedTestResult()
     self.protocol = subunit.TestProtocolServer(self.client)
     self.protocol.lineReceived(_b("test mcdonalds farm\n"))
     self.test = subunit.RemotedTestCase("mcdonalds farm")
Example #19
0
 def setup_protocol(self):
     """Setup the protocol based on self.client."""
     self.protocol = subunit.TestProtocolServer(self.client)
     self.protocol.lineReceived(_b("test mcdonalds farm\n"))
     self.test = self.client._events[-1][-1]
Example #20
0
 def setUp(self):
     self.client = MockTestProtocolServerClient()
     self.protocol = subunit.TestProtocolServer(self.client)
     self.test = subunit.RemotedTestCase("old mcdonald")
Example #21
0
 def setUp(self):
     self.client = Python26TestResult()
     self.protocol = subunit.TestProtocolServer(self.client)
     self.test = subunit.RemotedTestCase("old mcdonald")
Example #22
0
 def setUp(self):
     self.client = MockTestProtocolServerClient()
     self.protocol = subunit.TestProtocolServer(self.client)
     self.protocol.lineReceived("test mcdonalds farm\n")
     self.test = subunit.RemotedTestCase("mcdonalds farm")
Example #23
0
class JavaScriptTestCase(TrialTestCase):
    def __init__(self, methodName='runTest'):
        TrialTestCase.__init__(self, methodName)
        self.testMethod = getattr(self, methodName)

    def checkDependencies(self):
        """
        Check that all the dependencies of the test are satisfied.

        @raise NotSupported: If any one of the dependencies is not satisfied.
        """
        js = findJavascriptInterpreter()
        if js is None:
            raise NotSupported("Could not find JavaScript interpreter")
        if subunit is None:
            raise NotSupported("Could not import 'subunit'")

    def _writeToTemp(self, contents):
        fname = self.mktemp()
        fd = file(fname, 'w')
        try:
            fd.write(contents)
        finally:
            fd.close()
        return fname

    def makeScript(self, testModule):
        js = """
// import Divmod.UnitTest
// import %(module)s

Divmod.UnitTest.runRemote(Divmod.UnitTest.loadFromModule(%(module)s));
""" % {
            'module': testModule
        }
        jsfile = self._writeToTemp(js)
        scriptFile = self._writeToTemp(generateTestScript(jsfile))
        return scriptFile

    def _runWithSigchild(self, f, *a, **kw):
        """
        Run the given function with an alternative SIGCHLD handler.
        """
        oldHandler = signal.signal(signal.SIGCHLD, signal.SIG_DFL)
        try:
            return f(*a, **kw)
        finally:
            signal.signal(signal.SIGCHLD, oldHandler)

    def run(self, result):
        try:
            self.checkDependencies()
        except NotSupported, e:
            result.startTest(self)
            result.addSkip(self, str(e))
            result.stopTest(self)
            return
        js = findJavascriptInterpreter()
        script = self.makeScript(self.testMethod())
        server = subunit.TestProtocolServer(result)
        protocol = TestProtocolLineReceiverServer(server)

        # What this *SHOULD BE*
        # spawnProcess(protocol, js, (script,))
        # return protocol.someDisconnectCallback()

        # However, *run cannot return a Deferred profanity profanity profanity
        # profanity*, so instead it is *profanity* this:
        def run():
            r, w = popen2([js, script])
            while True:
                bytes = r.read(4096)
                if bytes:
                    protocol.dataReceived(bytes)
                else:
                    break

        self._runWithSigchild(run)
Example #24
0
 def setUp(self):
     self.client = Python26TestResult()
     self.stream = BytesIO()
     self.protocol = subunit.TestProtocolServer(self.client, self.stream)
Example #25
0
 def setUp(self):
     self.stdout = BytesIO()
     self.test = subunit.RemotedTestCase("old mcdonald")
     self.client = ExtendedTestResult()
     self.protocol = subunit.TestProtocolServer(self.client, self.stdout)
Example #26
0
class JavaScriptTestCase(TrialTestCase):
    def __init__(self, methodName='runTest'):
        TrialTestCase.__init__(self, methodName)
        self.testMethod = getattr(self, methodName)

    def findJavascriptInterpreter(self):
        """
        @see L{nevow.jsutil.findJavascriptInterpreter}
        """
        return findJavascriptInterpreter()

    def checkDependencies(self):
        """
        Check that all the dependencies of the test are satisfied.

        @raise NotSupported: If any one of the dependencies is not satisfied.
        """
        js = self.findJavascriptInterpreter()
        if js is None:
            raise NotSupported("Could not find JavaScript interpreter")
        if subunit is None:
            raise NotSupported("Could not import 'subunit'")
        if Popen3 is None:
            raise NotSupported("Could not import 'popen2.Popen3'")
        for name in ['WEXITSTATUS', 'WIFSIGNALED', 'WTERMSIG']:
            if getattr(os, name, None) is None:
                raise NotSupported("os.%s unavailable" % (name, ))

    def _writeToTemp(self, contents):
        fname = self.mktemp()
        fd = file(fname, 'w')
        try:
            fd.write(contents)
        finally:
            fd.close()
        return fname

    def createSource(self, testModule):
        """
        Return a string of JavaScript source code which, when executed, will
        run the JavaScript unit tests defined in the given module.

        @type testModule: C{str}
        @param testModule: The JavaScript module name which contains the
        tests to run.

        @rtype: C{str}
        """
        js = """
// import Divmod.UnitTest
// import %(module)s

Divmod.UnitTest.runRemote(Divmod.UnitTest.loadFromModule(%(module)s));
""" % {
            'module': testModule
        }
        return js

    def makeScript(self, testModule):
        """
        Write JavaScript source for executing the JavaScript unit tests in
        the given JavaScript module to a file and return the name of that
        file.

        @type testModule: C{str}
        @param testModule: The JavaScript module name which contains the
        tests to run.

        @rtype: C{str}
        """
        jsfile = self._writeToTemp(self.createSource(testModule))
        scriptFile = self._writeToTemp(generateTestScript(jsfile))
        return scriptFile

    def _runWithSigchild(self, f, *a, **kw):
        """
        Run the given function with an alternative SIGCHLD handler.
        """
        oldHandler = signal.signal(signal.SIGCHLD, signal.SIG_DFL)
        try:
            return f(*a, **kw)
        finally:
            signal.signal(signal.SIGCHLD, oldHandler)

    def run(self, result):
        try:
            self.checkDependencies()
        except NotSupported, e:
            result.startTest(self)
            result.addSkip(self, str(e))
            result.stopTest(self)
            return
        js = self.findJavascriptInterpreter()
        try:
            script = self.makeScript(self.testMethod())
        except KeyError:
            result.addError(self, sys.exc_info())
            return

        server = subunit.TestProtocolServer(result)
        protocol = TestProtocolLineReceiverServer(server)

        # What this *SHOULD BE*
        # spawnProcess(protocol, js, (script,))
        # return protocol.someDisconnectCallback()

        # However, *run cannot return a Deferred profanity profanity profanity
        # profanity*, so instead it is *profanity* this:
        def run():
            child = Popen3([js, script])
            while True:
                bytes = child.fromchild.read(4096)
                if bytes:
                    protocol.dataReceived(bytes)
                else:
                    break
            exitCode = child.wait()
            if os.WIFSIGNALED(exitCode):
                result.addError(
                    self, (Exception,
                           "JavaScript interpreter exited due to signal %d" %
                           (os.WTERMSIG(exitCode), ), None))
            else:
                exitStatus = os.WEXITSTATUS(exitCode)
                if exitStatus:
                    result.addError(
                        self, (Exception,
                               "JavaScript interpreter had error exit: %d" %
                               (exitStatus, ), None))

        self._runWithSigchild(run)
Example #27
0
 def setUp(self):
     self.client = Python26TestResult()
     self.protocol = subunit.TestProtocolServer(self.client)