K2LL33D SHELL

 Apache/2.4.7 (Ubuntu)
 Linux sman1baleendah 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64
 uid=33(www-data) gid=33(www-data) groups=33(www-data)
 safemode : OFF
 MySQL: ON | Perl: ON | cURL: OFF | WGet: ON
  >  / usr / lib / python2.7 / dist-packages / twisted / trial / _dist / test /
server ip : 172.67.156.115

your ip : 172.70.100.131

H O M E


Filename/usr/lib/python2.7/dist-packages/twisted/trial/_dist/test/test_workertrial.py
Size4.91 kb
Permissionrw-r--r--
Ownerroot : root
Create time27-Apr-2025 09:56
Last modified30-May-2013 09:52
Last accessed07-Jul-2025 20:59
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Tests for L{twisted.trial._dist.workertrial}.
"""

import errno
import sys
import os
from cStringIO import StringIO

from twisted.protocols.amp import AMP
from twisted.test.proto_helpers import StringTransport
from twisted.trial.unittest import TestCase
from twisted.trial._dist.workertrial import WorkerLogObserver, main, _setupPath
from twisted.trial._dist import (
workertrial, _WORKER_AMP_STDIN, _WORKER_AMP_STDOUT, workercommands,
managercommands)



class FakeAMP(AMP):
"""
A fake amp protocol.
"""



class WorkerLogObserverTestCase(TestCase):
"""
Tests for L{WorkerLogObserver}.
"""

def test_emit(self):
"""
L{WorkerLogObserver} forwards data to L{managercommands.TestWrite}.
"""
calls = []

class FakeClient(object):

def callRemote(self, method, **kwargs):
calls.append((method, kwargs))

observer = WorkerLogObserver(FakeClient())
observer.emit({'message': ['Some log']})
self.assertEqual(
calls, [(managercommands.TestWrite, {'out': 'Some log'})])



class MainTestCase(TestCase):
"""
Tests for L{main}.
"""

def setUp(self):
self.readStream = StringIO()
self.writeStream = StringIO()
self.patch(workertrial, 'startLoggingWithObserver',
self.startLoggingWithObserver)
self.addCleanup(setattr, sys, "argv", sys.argv)
sys.argv = ["trial"]


def fdopen(self, fd, mode=None):
"""
Fake C{os.fdopen} implementation which returns C{self.readStream} for
the stdin fd and C{self.writeStream} for the stdout fd.
"""
if fd == _WORKER_AMP_STDIN:
self.assertIdentical(None, mode)
return self.readStream
elif fd == _WORKER_AMP_STDOUT:
self.assertEqual('w', mode)
return self.writeStream
else:
raise AssertionError("Unexpected fd %r" % (fd,))


def startLoggingWithObserver(self, emit, setStdout):
"""
Override C{startLoggingWithObserver} for not starting logging.
"""
self.assertFalse(setStdout)


def test_empty(self):
"""
If no data is ever written, L{main} exits without writing data out.
"""
main(self.fdopen)
self.assertEqual('', self.writeStream.getvalue())


def test_forwardCommand(self):
"""
L{main} forwards data from its input stream to a L{WorkerProtocol}
instance which writes data to the output stream.
"""
client = FakeAMP()
clientTransport = StringTransport()
client.makeConnection(clientTransport)
client.callRemote(workercommands.Run, testCase="doesntexist")
self.readStream = clientTransport.io
self.readStream.seek(0, 0)
main(self.fdopen)
self.assertIn(
"No module named 'doesntexist'", self.writeStream.getvalue())


def test_readInterrupted(self):
"""
If reading the input stream fails with a C{IOError} with errno
C{EINTR}, L{main} ignores it and continues reading.
"""
excInfos = []

class FakeStream(object):
count = 0

def read(oself, size):
oself.count += 1
if oself.count == 1:
raise IOError(errno.EINTR)
else:
excInfos.append(sys.exc_info())
return ''

self.readStream = FakeStream()
main(self.fdopen)
self.assertEqual('', self.writeStream.getvalue())
self.assertEqual([(None, None, None)], excInfos)


def test_otherReadError(self):
"""
L{main} only ignores C{IOError} with C{EINTR} errno: otherwise, the
error pops out.
"""

class FakeStream(object):
count = 0

def read(oself, size):
oself.count += 1
if oself.count == 1:
raise IOError("Something else")
return ''

self.readStream = FakeStream()
self.assertRaises(IOError, main, self.fdopen)



class SetupPathTestCase(TestCase):
"""
Tests for L{_setupPath} C{sys.path} manipulation.
"""

def setUp(self):
self.addCleanup(setattr, sys, "path", sys.path[:])


def test_overridePath(self):
"""
L{_setupPath} overrides C{sys.path} if B{TRIAL_PYTHONPATH} is specified
in the environment.
"""
environ = {"TRIAL_PYTHONPATH": os.pathsep.join(["foo", "bar"])}
_setupPath(environ)
self.assertEqual(["foo", "bar"], sys.path)


def test_noVariable(self):
"""
L{_setupPath} doesn't change C{sys.path} if B{TRIAL_PYTHONPATH} is not
present in the environment.
"""
originalPath = sys.path[:]
_setupPath({})
self.assertEqual(originalPath, sys.path)