def test_extract_paths(): """ Test the different paths of the extract() function. It should open and extract the contents of the file (input arg) When called with only an input it should print the output of extract_script When called with two arguments it should write the output to the output arg """ mock_e = mock.MagicMock(return_value=b'print("hello, world!")') mock_o = mock.MagicMock() mock_o.return_value.__enter__ = lambda s: s mock_o.return_value.__exit__ = mock.Mock() mock_o.return_value.read.return_value = 'script' mock_o.return_value.write = mock.Mock() with mock.patch('uflash.extract_script', mock_e) as mock_extract_script, \ mock.patch.object(builtins, 'print') as mock_print, \ mock.patch.object(builtins, 'open', mock_o) as mock_open: uflash.extract('foo.hex') mock_open.assert_called_once_with('foo.hex', 'r') mock_extract_script.assert_called_once_with('script') mock_print.assert_called_once_with(b'print("hello, world!")') uflash.extract('foo.hex', 'out.py') assert mock_open.call_count == 3 assert mock_open.called_with('out.py', 'w') assert mock_open.return_value.write.call_count == 1
def test_extract_paths(): """ Test the different paths of the extract() function. It should open and extract the contents of the file (input arg) When called with only an input it should print the output of extract_script When called with two arguments it should write the output to the output arg """ mock_e = mock.MagicMock(return_value=b'print("hello, world!")') mock_o = mock.mock_open(read_data='script') with mock.patch('uflash.extract_script', mock_e) as mock_extract_script, \ mock.patch.object(builtins, 'print') as mock_print, \ mock.patch.object(builtins, 'open', mock_o) as mock_open: uflash.extract('foo.hex') mock_open.assert_called_once_with('foo.hex', 'r') mock_extract_script.assert_called_once_with('script') mock_print.assert_called_once_with(b'print("hello, world!")') uflash.extract('foo.hex', 'out.py') assert mock_open.call_count == 3 mock_open.assert_called_with('out.py', 'w') assert mock_open.return_value.write.call_count == 1
def test_extract_command_no_source(): """ If there is no source file the extract command should complain """ with pytest.raises(TypeError): uflash.extract(None, None)