Skip to content

Commit

Permalink
Ensure that a serial port is ready before running unit tests on a rem…
Browse files Browse the repository at this point in the history
…ote target // Resolve #3742
  • Loading branch information
ivankravets committed Mar 24, 2021
1 parent 0230374 commit 37e601e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ PlatformIO Core 5
* `Cppcheck <https://docs.platformio.org/page/plus/check-tools/cppcheck.html>`__ v2.4.1 with new checks and MISRA improvements
* `PVS-Studio <https://docs.platformio.org/page/plus/check-tools/pvs-studio.html>`__ v7.12 with new diagnostics and extended capabilities for security and safety standards

* **Miscellaneous**

- Ensure that a serial port is ready before running unit tests on a remote target (`issue #3742 <https://github.com/platformio/platformio-core/issues/3742>`_)

5.1.1 (2021-03-17)
~~~~~~~~~~~~~~~~~~

Expand Down
22 changes: 17 additions & 5 deletions platformio/commands/test/embedded.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,10 @@ def get_test_port(self):
port = item["port"]
for hwid in board_hwids:
hwid_str = ("%s:%s" % (hwid[0], hwid[1])).replace("0x", "")
if hwid_str in item["hwid"]:
if hwid_str in item["hwid"] and self.is_serial_port_ready(port):
return port

# check if port is already configured
try:
serial.Serial(port, timeout=self.SERIAL_TIMEOUT).close()
except serial.SerialException:
if port and not self.is_serial_port_ready(port):
port = None

if not port:
Expand All @@ -136,3 +133,18 @@ def get_test_port(self):
"global `--test-port` option."
)
return port

@staticmethod
def is_serial_port_ready(port, timeout=3):
if not port:
return False
elapsed = 0
while elapsed < timeout:
try:
serial.Serial(port, timeout=1).close()
return True
except: # pylint: disable=bare-except
pass
sleep(1)
elapsed += 1
return False

0 comments on commit 37e601e

Please sign in to comment.