77
88import tycmd
99
10- BLINK40_HEX = Path (__file__ ).parent .joinpath (" blink40.hex" ).resolve ()
11- BLINK41_HEX = Path (__file__ ).parent .joinpath (" blink41.hex" ).resolve ()
10+ BLINK40_HEX = Path (__file__ ).parent .joinpath (' blink40.hex' ).resolve ()
11+ BLINK41_HEX = Path (__file__ ).parent .joinpath (' blink41.hex' ).resolve ()
1212
1313
1414@pytest .fixture
1515def mock_Popen ():
16- with patch (" tycmd.Popen" , autospec = True ) as mock_Popen :
16+ with patch (' tycmd.Popen' , autospec = True ) as mock_Popen :
1717 context = mock_Popen .return_value .__enter__ .return_value
1818
19- def set_pipes (stdout : list [str ], stderr : list [str ]):
19+ def set_pipes (stdout : list [str ] = [] , stderr : list [str ] = [ ]):
2020 context .stdout = stdout
2121 context .stderr = stderr
2222 context .communicate .return_value = (
23- " \n " .join (context .stdout ),
24- " \n " .join (context .stderr ),
23+ ' \n ' .join (context .stdout ),
24+ ' \n ' .join (context .stderr ),
2525 )
2626
27- def set_returncode (returncode : int ):
27+ def set_returncode (returncode : int = 0 ):
2828 context .returncode = returncode
2929
3030 mock_Popen .set_pipes = set_pipes
@@ -35,30 +35,38 @@ def set_returncode(returncode: int):
3535 yield mock_Popen
3636
3737
38- def test_upload (mock_Popen ):
38+ def test_upload (mock_Popen , caplog ):
39+ mock_Popen .set_pipes (stdout = ['output' ])
40+ caplog .set_level (logging .INFO )
3941 tycmd .upload (BLINK40_HEX , check = True , reset_board = True )
40- assert "--nocheck" not in mock_Popen .call_args [0 ][0 ]
41- assert "--noreset" not in mock_Popen .call_args [0 ][0 ]
42- assert "--rtc" in mock_Popen .call_args [0 ][0 ]
43- assert "--quiet" not in mock_Popen .call_args [0 ][0 ]
42+ assert '--nocheck' not in mock_Popen .call_args [0 ][0 ]
43+ assert '--noreset' not in mock_Popen .call_args [0 ][0 ]
44+ assert '--rtc' in mock_Popen .call_args [0 ][0 ]
45+ assert '--quiet' not in mock_Popen .call_args [0 ][0 ]
46+ assert len (caplog .records ) > 0
47+ assert all ([x .levelname == 'INFO' for x in caplog .records ])
48+
49+ caplog .clear ()
4450 tycmd .upload (BLINK40_HEX , check = False , reset_board = False , log_level = logging .NOTSET )
45- assert "--nocheck" in mock_Popen .call_args [0 ][0 ]
46- assert "--noreset" in mock_Popen .call_args [0 ][0 ]
47- assert "--rtc" in mock_Popen .call_args [0 ][0 ]
48- assert "--quiet" in mock_Popen .call_args [0 ][0 ]
51+ assert '--nocheck' in mock_Popen .call_args [0 ][0 ]
52+ assert '--noreset' in mock_Popen .call_args [0 ][0 ]
53+ assert '--rtc' in mock_Popen .call_args [0 ][0 ]
54+ assert '--quiet' in mock_Popen .call_args [0 ][0 ]
55+ assert len (caplog .records ) == 0
4956
5057
5158def test_reset (mock_Popen , caplog ):
52- output = tycmd .reset (bootloader = True , log_level = 0 )
59+ mock_Popen .set_pipes (['status' ], [])
60+ caplog .set_level (logging .INFO )
61+ tycmd .reset (bootloader = True , log_level = logging .NOTSET )
5362 mock_Popen .assert_called_once ()
54- assert " --bootloader" in mock_Popen .call_args [0 ][0 ]
63+ assert ' --bootloader' in mock_Popen .call_args [0 ][0 ]
5564 assert len (caplog .records ) == 0
56- assert output is None
5765
58- mock_Popen . set_pipes ([ "status" ], [] )
59- tycmd . reset ( log_level = 30 )
60- assert "--bootloader" not in mock_Popen . call_args [ 0 ][ 0 ]
61- assert "status" in caplog .text
66+ tycmd . reset ( )
67+ assert '--bootloader' not in mock_Popen . call_args [ 0 ][ 0 ]
68+ assert len ( caplog . records ) > 0
69+ assert all ([ x . levelname == 'INFO' for x in caplog .records ])
6270
6371 mock_Popen .set_returncode (1 )
6472 with pytest .raises (ChildProcessError ):
@@ -67,12 +75,12 @@ def test_reset(mock_Popen, caplog):
6775
6876def test_identify ():
6977 with TemporaryDirectory () as temp_directory :
70- firmware_file = Path (temp_directory ).joinpath (" firmware.hex" )
78+ firmware_file = Path (temp_directory ).joinpath (' firmware.hex' )
7179 firmware_file .touch ()
7280 with pytest .raises (ChildProcessError ):
7381 tycmd .identify (firmware_file )
74- assert " Teensy 4.0" in tycmd .identify (BLINK40_HEX )
75- assert " Teensy 4.1" in tycmd .identify (BLINK41_HEX )
82+ assert ' Teensy 4.0' in tycmd .identify (BLINK40_HEX )
83+ assert ' Teensy 4.1' in tycmd .identify (BLINK41_HEX )
7684
7785
7886def test_list_boards (mock_Popen ):
@@ -86,9 +94,9 @@ def test_list_boards(mock_Popen):
8694 output = tycmd .list_boards ()
8795 assert isinstance (output , list )
8896 assert isinstance (output [0 ], dict )
89- assert output [0 ][" serial" ] == " 12345678"
97+ assert output [0 ][' serial' ] == ' 12345678'
9098
91- mock_Popen .set_pipes ([" [\n ]\n " ], [])
99+ mock_Popen .set_pipes ([' [\n ]\n ' ], [])
92100 output = tycmd .list_boards ()
93101 assert isinstance (output , list )
94102 assert len (output ) == 0
@@ -97,7 +105,7 @@ def test_list_boards(mock_Popen):
97105def test_version ():
98106 assert tycmd .version () == tycmd ._TYCMD_VERSION
99107 with (
100- patch (" tycmd._call_tycmd" , return_value = " invalid" ) as _ ,
108+ patch (' tycmd._call_tycmd' , return_value = ' invalid' ) as _ ,
101109 pytest .raises (ChildProcessError ),
102110 ):
103111 tycmd .version ()
@@ -107,40 +115,40 @@ def test__parse_firmware_file():
107115 with TemporaryDirectory () as temp_directory :
108116 with pytest .raises (IsADirectoryError ):
109117 tycmd ._parse_firmware_file (temp_directory )
110- firmware_file = Path (temp_directory ).joinpath (" firmware" )
118+ firmware_file = Path (temp_directory ).joinpath (' firmware' )
111119 with pytest .raises (FileNotFoundError ):
112120 tycmd ._parse_firmware_file (firmware_file )
113121 firmware_file .touch ()
114122 with pytest .raises (ValueError ):
115123 tycmd ._parse_firmware_file (firmware_file )
116- firmware_file = firmware_file .with_suffix (".hex" )
124+ firmware_file = firmware_file .with_suffix ('.HEX' )
117125 firmware_file .touch ()
118126 assert tycmd ._parse_firmware_file (firmware_file ).samefile (firmware_file )
119127 assert tycmd ._parse_firmware_file (str (firmware_file )).samefile (firmware_file )
120128
121129
122130def test__call_tycmd (mock_Popen ):
123- mock_Popen .set_pipes ([" status" ], [" error!" ])
131+ mock_Popen .set_pipes ([' status' ], [' error!' ])
124132 tycmd ._call_tycmd ([], raise_on_stderr = False )
125133 with pytest .raises (ChildProcessError ):
126134 tycmd ._call_tycmd ([], raise_on_stderr = True )
127135
128- mock_Popen .set_pipes ([" status" ], [])
136+ mock_Popen .set_pipes ([' status' ], [])
129137 mock_Popen .set_returncode (- 1 )
130138 with pytest .raises (ChildProcessError ):
131139 tycmd ._call_tycmd ([])
132140
133141
134142def test__assemble_args ():
135- output = tycmd ._assemble_args (args = [], serial = " serial" )
136- assert " -B serial" in " " .join (output )
137- output = tycmd ._assemble_args (args = [], family = " family" )
138- assert " -B -family" in " " .join (output )
139- output = tycmd ._assemble_args (args = [], port = " port" )
140- assert " -B @port" in " " .join (output )
143+ output = tycmd ._assemble_args (args = [], serial = ' serial' )
144+ assert ' -B serial' in ' ' .join (output )
145+ output = tycmd ._assemble_args (args = [], family = ' family' )
146+ assert ' -B -family' in ' ' .join (output )
147+ output = tycmd ._assemble_args (args = [], port = ' port' )
148+ assert ' -B @port' in ' ' .join (output )
141149 output = tycmd ._assemble_args (
142- args = [" some_argument" ], serial = " serial" , family = " family" , port = " port"
150+ args = [' some_argument' ], serial = ' serial' , family = ' family' , port = ' port'
143151 )
144- assert " -B serial-family@port" in " " .join (output )
145- assert " tycmd" in output
146- assert " some_argument" in output
152+ assert ' -B serial-family@port' in ' ' .join (output )
153+ assert ' tycmd' in output
154+ assert ' some_argument' in output
0 commit comments