11from pathlib import Path
22from tempfile import TemporaryDirectory
3+ from unittest .mock import patch
34
45import pytest
56
67import tycmd
7- import re
88
99
1010BLINK_HEX = Path (__file__ ).parent .joinpath ("blink.hex" ).resolve ()
1111
1212
13+ @pytest .fixture
14+ def mock_check_output ():
15+ with patch ("tycmd.check_output" ) as mock_check_output :
16+ yield mock_check_output
17+
18+
1319def test_identify ():
1420 with TemporaryDirectory () as temp_directory :
1521 firmware_file = Path (temp_directory ).joinpath ("firmware.hex" )
@@ -19,13 +25,34 @@ def test_identify():
1925 assert "Teensy 4.0" in tycmd .identify (BLINK_HEX )
2026
2127
28+ def test_list_boards (mock_check_output ):
29+ mock_check_output .return_value = (
30+ '[\n {"action": "add", "tag": "12345678-Teensy", "serial": "12345678", '
31+ '"description": "USB Serial", "model": "Teensy 4.1", "location": "usb-3-3", '
32+ '"capabilities": ["unique", "run", "rtc", "reboot", "serial"], '
33+ '"interfaces": [["Serial", "/dev/ttyACM0"]]}\n ]\n '
34+ )
35+ output = tycmd .list_boards ()
36+ mock_check_output .assert_called_once_with (
37+ ["tycmd" , "list" , "-O" , "json" , "-v" ], text = True
38+ )
39+ assert isinstance (output , list )
40+ assert isinstance (output [0 ], dict )
41+ assert output [0 ]["serial" ] == "12345678"
42+
43+ mock_check_output .return_value = "[\n ]\n "
44+ output = tycmd .list_boards ()
45+ assert isinstance (output , list )
46+ assert len (output ) == 0
47+
48+
2249def test_version ():
23- output = tycmd .version (full = True )
24- assert isinstance ( output , str )
25- match = re . search ( r"^.+(\d+\.\d+\.\d+) " , output )
26- assert match is not None
27- assert match . groups ()[ 0 ] == tycmd . _TYCMD_VERSION
28- assert tycmd .version (full = False ) == tycmd . _TYCMD_VERSION
50+ assert tycmd .version () == tycmd . _TYCMD_VERSION
51+ with (
52+ patch ( "tycmd.check_output " , return_value = "invalid" ) as _ ,
53+ pytest . raises ( RuntimeError ),
54+ ):
55+ tycmd .version ()
2956
3057
3158def test__parse_firmware_file ():
0 commit comments