Skip to content

Commit 608f72d

Browse files
authored
Move constant functions to conftest. (#752)
Move constant functions to conftest.
1 parent 189bc76 commit 608f72d

File tree

8 files changed

+31
-61
lines changed

8 files changed

+31
-61
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ test/__pycache__/
4040
/build/
4141
/dist/
4242
**/.DS_Store
43+
/venv

doc/TODO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ General
1818
- convert twister to a plugin
1919
- get coverage > 98%
2020
- clean raise to be at frame level
21+
- solve "TO FIX" in code
2122

2223
---------------------------------------------------------------------------
2324
Protocols

test/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import platform
2+
from pkg_resources import parse_version
3+
4+
5+
IS_DARWIN = platform.system().lower() == "darwin"
6+
IS_WINDOWS = platform.system().lower() == "windows"
7+
8+
if IS_DARWIN:
9+
# check for SIERRA
10+
if parse_version("10.12") < parse_version(platform.mac_ver()[0]):
11+
SERIAL_PORT = '/dev/ptyp0'
12+
else:
13+
SERIAL_PORT = '/dev/ttyp0'
14+
else:
15+
SERIAL_PORT = "/dev/ptmx"

test/test_client_async.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pymodbus.client.asynchronous.async_io import AsyncioModbusSerialClient
1010

1111
import platform
12-
from pkg_resources import parse_version
12+
1313

1414
from pymodbus.client.asynchronous.serial import AsyncModbusSerialClient
1515
from pymodbus.client.asynchronous.tcp import AsyncModbusTCPClient
@@ -25,23 +25,10 @@
2525
from pymodbus.transaction import ModbusSocketFramer, ModbusTlsFramer, ModbusRtuFramer
2626
from pymodbus.transaction import ModbusAsciiFramer, ModbusBinaryFramer
2727
from pymodbus.client.asynchronous.twisted import ModbusSerClientProtocol
28+
from test.conftest import SERIAL_PORT
2829

2930
import ssl
3031

31-
IS_DARWIN = platform.system().lower() == "darwin"
32-
IS_WINDOWS = platform.system().lower() == "windows"
33-
OSX_SIERRA = parse_version("10.12")
34-
if IS_DARWIN:
35-
IS_HIGH_SIERRA_OR_ABOVE = OSX_SIERRA < parse_version(platform.mac_ver()[0])
36-
SERIAL_PORT = '/dev/ptyp0' if not IS_HIGH_SIERRA_OR_ABOVE else '/dev/ttyp0'
37-
else:
38-
IS_HIGH_SIERRA_OR_ABOVE = False
39-
if IS_WINDOWS:
40-
# the use is mocked out
41-
SERIAL_PORT = ""
42-
else:
43-
SERIAL_PORT = "/dev/ptmx"
44-
4532
# ---------------------------------------------------------------------------#
4633
# Fixture
4734
# ---------------------------------------------------------------------------#

test/test_client_async_tornado.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,11 @@
1111
from pymodbus.exceptions import ConnectionException
1212
from pymodbus.transaction import ModbusSocketFramer, ModbusRtuFramer
1313
from pymodbus.bit_read_message import ReadCoilsRequest, ReadCoilsResponse
14+
from test.conftest import SERIAL_PORT
1415

1516
# ---------------------------------------------------------------------------#
1617
# Fixture
1718
# ---------------------------------------------------------------------------#
18-
import platform
19-
from pkg_resources import parse_version
20-
21-
IS_DARWIN = platform.system().lower() == "darwin"
22-
OSX_SIERRA = parse_version("10.12")
23-
if IS_DARWIN:
24-
IS_HIGH_SIERRA_OR_ABOVE = OSX_SIERRA < parse_version(platform.mac_ver()[0])
25-
SERIAL_PORT = '/dev/ptyp0' if not IS_HIGH_SIERRA_OR_ABOVE else '/dev/ttyp0'
26-
else:
27-
IS_HIGH_SIERRA_OR_ABOVE = False
28-
SERIAL_PORT = "/dev/ptmx"
2919

3020

3121
class AsynchronousClientTest(unittest.TestCase):

test/test_server_async.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
import pytest
44
from unittest.mock import patch, Mock, MagicMock
5+
import platform
56

67
from pymodbus.device import ModbusDeviceIdentification
78
from pymodbus.server.asynchronous import ModbusTcpProtocol, ModbusUdpProtocol
@@ -13,22 +14,13 @@
1314
from pymodbus.compat import byte2int
1415
from pymodbus.transaction import ModbusSocketFramer
1516
from pymodbus.exceptions import NoSuchSlaveException, ModbusIOException
17+
from test.conftest import SERIAL_PORT
1618

1719
import sys
1820
# --------------------------------------------------------------------------- #
1921
# Fixture
2022
# --------------------------------------------------------------------------- #
21-
import platform
22-
from pkg_resources import parse_version
23-
24-
IS_DARWIN = platform.system().lower() == "darwin"
25-
OSX_SIERRA = parse_version("10.12")
26-
if IS_DARWIN:
27-
IS_HIGH_SIERRA_OR_ABOVE = OSX_SIERRA < parse_version(platform.mac_ver()[0])
28-
SERIAL_PORT = '/dev/ptyp0' if not IS_HIGH_SIERRA_OR_ABOVE else '/dev/ttyp0'
29-
else:
30-
IS_HIGH_SIERRA_OR_ABOVE = False
31-
SERIAL_PORT = "/dev/ptmx"
23+
3224

3325
no_twisted_serial_on_windows_with_pypy = pytest.mark.skipif(
3426
sys.platform == 'win32' and platform.python_implementation() == 'PyPy',

test/test_server_asyncio.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,15 @@
1717

1818

1919
from pymodbus.exceptions import NoSuchSlaveException
20+
from test.conftest import IS_WINDOWS, SERIAL_PORT
2021

2122
# ---------------------------------------------------------------------------#
2223
# Fixture
2324
# ---------------------------------------------------------------------------#
2425
import platform
2526
import ssl
26-
from pkg_resources import parse_version
2727
_logger = logging.getLogger()
2828

29-
IS_DARWIN = platform.system().lower() == "darwin"
30-
OSX_SIERRA = parse_version("10.12")
31-
if IS_DARWIN:
32-
IS_HIGH_SIERRA_OR_ABOVE = OSX_SIERRA < parse_version(platform.mac_ver()[0])
33-
SERIAL_PORT = '/dev/ptyp0' if not IS_HIGH_SIERRA_OR_ABOVE else '/dev/ttyp0'
34-
else:
35-
IS_HIGH_SIERRA_OR_ABOVE = False
36-
SERIAL_PORT = "/dev/ptmx"
37-
3829

3930
class AsyncioServerTest(asynctest.TestCase):
4031
"""
@@ -121,7 +112,7 @@ def eof_received(self):
121112
self.assertTrue( process.call_args[1]["data"] == data )
122113
server.server_close()
123114

124-
115+
@pytest.mark.skipif(IS_WINDOWS, reason="To fix")
125116
async def testTcpServerRoundtrip(self):
126117
''' Test sending and receiving data on tcp socket '''
127118
data = b"\x01\x00\x00\x00\x00\x06\x01\x03\x00\x00\x00\x01" # unit 1, read register
@@ -159,8 +150,10 @@ def eof_received(self):
159150
await asyncio.sleep(0)
160151
server.server_close()
161152

153+
@pytest.mark.skipif(IS_WINDOWS, reason="To fix")
162154
async def testTcpServerConnectionLost(self):
163155
""" Test tcp stream interruption """
156+
164157
data = b"\x01\x00\x00\x00\x00\x06\x01\x01\x00\x00\x00\x01"
165158
server = await StartTcpServer(context=self.context, address=("127.0.0.1", 0), loop=self.loop)
166159

@@ -218,12 +211,12 @@ def connection_made(self, transport):
218211

219212
# On Windows we seem to need to give this an extra chance to finish,
220213
# otherwise there ends up being an active connection at the assert.
221-
await asyncio.sleep(0.0)
214+
await asyncio.sleep(0.5)
222215
server.server_close()
223216

224217
# close isn't synchronous and there's no notification that it's done
225218
# so we have to wait a bit
226-
await asyncio.sleep(0.0)
219+
await asyncio.sleep(0.5)
227220
self.assertTrue( len(server.active_connections) == 0 )
228221

229222

test/test_server_sync.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,9 @@
2020
from pymodbus.transaction import ModbusTlsFramer
2121

2222
from pymodbus.compat import socketserver
23+
from test.conftest import SERIAL_PORT
24+
2325

24-
import platform
25-
from pkg_resources import parse_version
26-
27-
IS_DARWIN = platform.system().lower() == "darwin"
28-
OSX_SIERRA = parse_version("10.12")
29-
if IS_DARWIN:
30-
IS_HIGH_SIERRA_OR_ABOVE = OSX_SIERRA < parse_version(platform.mac_ver()[0])
31-
SERIAL_PORT = '/dev/ptyp0' if not IS_HIGH_SIERRA_OR_ABOVE else '/dev/ttyp0'
32-
else:
33-
IS_HIGH_SIERRA_OR_ABOVE = False
34-
SERIAL_PORT = "/dev/ptmx"
3526
# --------------------------------------------------------------------------- #
3627
# Mock Classes
3728
# --------------------------------------------------------------------------- #

0 commit comments

Comments
 (0)