def test_newlineStyle(self): """ L{LocalWorker} writes the log data with local newlines. """ amp = SpyDataLocalWorkerAMP() tempDir = FilePath(self.mktemp()) logFile = tempDir.child("test.log") worker = LocalWorker(amp, tempDir.path, "test.log") worker.makeConnection(FakeTransport()) self.addCleanup(worker._outLog.close) self.addCleanup(worker._errLog.close) expected = "Here comes the \N{sun}!" try: amp.testWrite(expected) finally: worker._testLog.close() self.assertEqual( # os.linesep is the local newline. (expected + os.linesep), # getContent reads in binary mode so we'll see the bytes that # actually ended up in the file. logFile.getContent().decode("utf-8"), )
def test_unicodeLogFileUTF8(self): """ L{LocalWorker} write the log data with local newlines but in UTF-8 encoding regardless of the default encoding. """ amp = SpyDataLocalWorkerAMP() tempDir = FilePath(self.mktemp()) logFile = tempDir.child("test.log") # Modern OSes are running default locale in UTF-8 and this is what # is used by Python at startup. # For this test, we force an ASCII default encoding. currentLocale = locale.getlocale() self.addCleanup(locale.setlocale, locale.LC_ALL, currentLocale) locale.setlocale(locale.LC_ALL, ("C", "ascii")) worker = LocalWorker(amp, tempDir.path, "test.log") worker.makeConnection(FakeTransport()) self.addCleanup(worker._outLog.close) self.addCleanup(worker._errLog.close) try: amp.testWrite("Here comes the \N{sun}!") finally: worker._testLog.close() self.assertEqual( b"Here comes the \xe2\x98\x89!" + os.linesep.encode("ascii"), logFile.getContent(), )
def test_startError(self): """ L{LocalWorker} swallows the exceptions returned by the L{AMP} protocol start method, as it generates unnecessary errors. """ def failCallRemote(command, directory): return fail(RuntimeError("oops")) transport = FakeTransport() protocol = FakeAMProtocol() protocol.callRemote = failCallRemote localWorker = LocalWorker(protocol, '.', 'test.log') localWorker.makeConnection(transport) self.assertEqual([], self.flushLoggedErrors(RuntimeError))
def tidyLocalWorker(self, *args, **kwargs): """ Create a L{LocalWorker}, connect it to a transport, and ensure its log files are closed. @param args: See L{LocalWorker} @param kwargs: See L{LocalWorker} @return: a L{LocalWorker} instance """ worker = LocalWorker(*args, **kwargs) worker.makeConnection(FakeTransport()) self.addCleanup(worker._testLog.close) self.addCleanup(worker._outLog.close) self.addCleanup(worker._errLog.close) return worker
def test_outReceived(self): """ L{LocalWorker.outReceived} logs the output into its C{_outLog} log file. """ fakeTransport = FakeTransport() localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log') localWorker.makeConnection(fakeTransport) localWorker._outLog = StringIO() data = "The quick brown fox jumps over the lazy dog" localWorker.outReceived(data) self.assertEqual(data, localWorker._outLog.getvalue())
def test_errReceived(self): """ L{LocalWorker.errReceived} logs the errors into its C{_errLog} log file. """ fakeTransport = FakeTransport() localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log') localWorker.makeConnection(fakeTransport) localWorker._errLog = BytesIO() data = b"The quick brown fox jumps over the lazy dog" localWorker.errReceived(data) self.assertEqual(data, localWorker._errLog.getvalue())
def test_childDataReceived(self): """ L{LocalWorker.childDataReceived} forwards the received data to linked L{AMP} protocol if the right file descriptor, otherwise forwards to C{ProcessProtocol.childDataReceived}. """ fakeTransport = FakeTransport() localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log') localWorker.makeConnection(fakeTransport) localWorker._outLog = BytesIO() localWorker.childDataReceived(4, b"foo") localWorker.childDataReceived(1, b"bar") self.assertEqual(b"foo", localWorker._ampProtocol.dataString) self.assertEqual(b"bar", localWorker._outLog.getvalue())
def createLocalWorkers(self, protocols, workingDirectory): """ Create local worker protocol instances and return them. @param protocols: An iterable of L{LocalWorkerAMP} instances. @param workingDirectory: The base path in which we should run the workers. @type workingDirectory: C{str} @return: A list of C{quantity} C{LocalWorker} instances. """ return [ LocalWorker(protocol, os.path.join(workingDirectory, str(x)), self._logFile) for x, protocol in enumerate(protocols) ]
def test_connectionLost(self): """ L{LocalWorker.connectionLost} closes the log streams. """ class FakeStream(object): callNumber = 0 def close(self): self.callNumber += 1 transport = FakeTransport() localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log') localWorker.makeConnection(transport) localWorker._outLog = FakeStream() localWorker._errLog = FakeStream() localWorker.connectionLost(None) self.assertEqual(localWorker._outLog.callNumber, 1) self.assertEqual(localWorker._errLog.callNumber, 1)
def test_errReceived(self): """ L{LocalWorker.errReceived} logs the errors into its C{_errLog} log file. """ fakeTransport = FakeTransport() localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log') localWorker.makeConnection(fakeTransport) localWorker._errLog = StringIO() data = "The quick brown fox jumps over the lazy dog" localWorker.errReceived(data) self.assertEqual(data, localWorker._errLog.getvalue())
def test_connectionLost(self): """ L{LocalWorker.connectionLost} closes the log streams. """ transport = FakeTransport() localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log') localWorker.makeConnection(transport) localWorker.connectionLost(None) self.assertTrue(localWorker._outLog.closed) self.assertTrue(localWorker._errLog.closed) self.assertTrue(localWorker._testLog.closed)
def test_childDataReceived(self): """ L{LocalWorker.childDataReceived} forwards the received data to linked L{AMP} protocol if the right file descriptor, otherwise forwards to C{ProcessProtocol.childDataReceived}. """ fakeTransport = FakeTransport() localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log') localWorker.makeConnection(fakeTransport) localWorker._outLog = StringIO() localWorker.childDataReceived(4, "foo") localWorker.childDataReceived(1, "bar") self.assertEqual("foo", localWorker._ampProtocol.dataString) self.assertEqual("bar", localWorker._outLog.getvalue())
def test_processEnded(self): """ L{LocalWorker.processEnded} calls C{connectionLost} on itself and on the L{AMP} protocol. """ transport = FakeTransport() protocol = SpyDataLocalWorkerAMP() localWorker = LocalWorker(protocol, self.mktemp(), "test.log") localWorker.makeConnection(transport) localWorker.processEnded(Failure(CONNECTION_DONE)) self.assertTrue(localWorker._outLog.closed) self.assertTrue(localWorker._errLog.closed) self.assertTrue(localWorker._testLog.closed) self.assertIdentical(None, protocol.transport) return self.assertFailure(localWorker.endDeferred, ConnectionDone)
def test_processEnded(self): """ L{LocalWorker.processEnded} calls C{connectionLost} on itself and on the L{AMP} protocol. """ class FakeStream(object): callNumber = 0 def close(self): self.callNumber += 1 transport = FakeTransport() protocol = FakeAMProtocol() localWorker = LocalWorker(protocol, '.', 'test.log') localWorker.makeConnection(transport) localWorker._outLog = FakeStream() localWorker.processEnded(Failure(CONNECTION_DONE)) self.assertEqual(localWorker._outLog.callNumber, 1) self.assertIdentical(None, protocol.transport) return self.assertFailure(localWorker.endDeferred, ConnectionDone)