3232import ssl
3333
3434IS_DARWIN = platform .system ().lower () == "darwin"
35+ IS_WINDOWS = platform .system ().lower () == "windows"
3536OSX_SIERRA = LooseVersion ("10.12" )
3637if IS_DARWIN :
3738 IS_HIGH_SIERRA_OR_ABOVE = LooseVersion (platform .mac_ver ()[0 ])
3839 SERIAL_PORT = '/dev/ttyp0' if not IS_HIGH_SIERRA_OR_ABOVE else '/dev/ptyp0'
3940else :
4041 IS_HIGH_SIERRA_OR_ABOVE = False
41- SERIAL_PORT = "/dev/ptmx"
42+ if IS_WINDOWS :
43+ # the use is mocked out
44+ SERIAL_PORT = ""
45+ else :
46+ SERIAL_PORT = "/dev/ptmx"
4247
4348# ---------------------------------------------------------------------------#
4449# Fixture
@@ -186,6 +191,10 @@ def testUdpAsycioClient(self, mock_gather, mock_event_loop):
186191 # Test Serial client
187192 # -----------------------------------------------------------------------#
188193
194+ @pytest .mark .skipif (
195+ sys .platform == 'win32' and platform .python_implementation () == 'PyPy' ,
196+ reason = 'Twisted serial requires pywin32 which is not compatible with PyPy' ,
197+ )
189198 @pytest .mark .parametrize ("method, framer" , [("rtu" , ModbusRtuFramer ),
190199 ("socket" , ModbusSocketFramer ),
191200 ("binary" , ModbusBinaryFramer ),
@@ -196,30 +205,30 @@ def testSerialTwistedClient(self, method, framer):
196205 with patch ("serial.Serial" ) as mock_sp :
197206 from twisted .internet import reactor
198207 from twisted .internet .serialport import SerialPort
208+ with maybe_manage (sys .platform == 'win32' , patch .object (SerialPort , "_finishPortSetup" )):
209+ with patch ('twisted.internet.reactor' ) as mock_reactor :
199210
200- with patch ('twisted.internet.reactor' ) as mock_reactor :
201-
202- protocol , client = AsyncModbusSerialClient (schedulers .REACTOR ,
203- method = method ,
204- port = SERIAL_PORT ,
205- proto_cls = ModbusSerClientProtocol )
211+ protocol , client = AsyncModbusSerialClient (schedulers .REACTOR ,
212+ method = method ,
213+ port = SERIAL_PORT ,
214+ proto_cls = ModbusSerClientProtocol )
206215
207- assert (isinstance (client , SerialPort ))
208- assert (isinstance (client .protocol , ModbusSerClientProtocol ))
209- assert (0 == len (list (client .protocol .transaction )))
210- assert (isinstance (client .protocol .framer , framer ))
211- assert (client .protocol ._connected )
216+ assert (isinstance (client , SerialPort ))
217+ assert (isinstance (client .protocol , ModbusSerClientProtocol ))
218+ assert (0 == len (list (client .protocol .transaction )))
219+ assert (isinstance (client .protocol .framer , framer ))
220+ assert (client .protocol ._connected )
212221
213- def handle_failure (failure ):
214- assert (isinstance (failure .exception (), ConnectionException ))
222+ def handle_failure (failure ):
223+ assert (isinstance (failure .exception (), ConnectionException ))
215224
216- d = client .protocol ._buildResponse (0x00 )
217- d .addCallback (handle_failure )
225+ d = client .protocol ._buildResponse (0x00 )
226+ d .addCallback (handle_failure )
218227
219- assert (client .protocol ._connected )
220- client .protocol .close ()
221- protocol .stop ()
222- assert (not client .protocol ._connected )
228+ assert (client .protocol ._connected )
229+ client .protocol .close ()
230+ protocol .stop ()
231+ assert (not client .protocol ._connected )
223232
224233 @pytest .mark .parametrize ("method, framer" , [("rtu" , ModbusRtuFramer ),
225234 ("socket" , ModbusSocketFramer ),
@@ -228,7 +237,7 @@ def handle_failure(failure):
228237 def testSerialTornadoClient (self , method , framer ):
229238 """ Test the serial tornado client client initialize """
230239 from serial import Serial
231- with maybe_manage (sys .platform == 'darwin' , patch .object (Serial , "open" )):
240+ with maybe_manage (sys .platform in ( 'darwin' , 'win32' ) , patch .object (Serial , "open" )):
232241 protocol , future = AsyncModbusSerialClient (schedulers .IO_LOOP , method = method , port = SERIAL_PORT )
233242 client = future .result ()
234243 assert (isinstance (client , AsyncTornadoModbusSerialClient ))
0 commit comments