def test_open_ignore_non_hex(): """ Ignores any other than hex file types. """ view = mock.MagicMock() editor = mock.MagicMock() mm = MicrobitMode(editor, view) mock_open = mock.mock_open() with mock.patch("builtins.open", mock_open), mock.patch( "mu.contrib.uflash.extract_script", return_value="Should not be called") as extract_script: text, newline = mm.open_file("path_to_file.py") assert text is None assert newline is None assert extract_script.call_count == 0 assert mock_open.call_count == 0 mock_open.reset_mock() with mock.patch("builtins.open", mock_open), mock.patch( "mu.contrib.uflash.extract_script", return_value="Should not be called") as extract_script: text, newline = mm.open_file("file_no_extension") assert text is None assert newline is None assert extract_script.call_count == 0 assert mock_open.call_count == 0
def test_open_hex(): """ Tries to open hex files with uFlash. """ view = mock.MagicMock() editor = mock.MagicMock() mm = MicrobitMode(editor, view) mock_open = mock.mock_open() hex_extracted = 'RECOVERED' with mock.patch('builtins.open', mock_open), \ mock.patch('mu.contrib.uflash.extract_script', return_value=hex_extracted) as extract_script: text = mm.open_file('path_to_file.hex') assert text == hex_extracted assert extract_script.call_count == 1 assert mock_open.call_count == 1 mock_open.reset_mock() with mock.patch('builtins.open', mock_open), \ mock.patch('mu.contrib.uflash.extract_script', return_value=hex_extracted) as extract_script: text = mm.open_file('path_to_file.HEX') assert text == hex_extracted assert extract_script.call_count == 1 assert mock_open.call_count == 1
def test_open_ignore_non_hex(): """ Ignores any other than hex file types. """ view = mock.MagicMock() editor = mock.MagicMock() mm = MicrobitMode(editor, view) mock_open = mock.mock_open() with mock.patch('builtins.open', mock_open), \ mock.patch('mu.contrib.uflash.extract_script', return_value='Should not be called') as extract_script: text = mm.open_file('path_to_file.py') assert text is None assert extract_script.call_count == 0 assert mock_open.call_count == 0 mock_open.reset_mock() with mock.patch('builtins.open', mock_open), \ mock.patch('mu.contrib.uflash.extract_script', return_value='Should not be called') as extract_script: text = mm.open_file('file_no_extension') assert text is None assert extract_script.call_count == 0 assert mock_open.call_count == 0
def test_open_hex_with_exception(): """ If an exception is encountered when trying to open the hex file, make sure it is swallowed and return None. """ view = mock.MagicMock() editor = mock.MagicMock() mm = MicrobitMode(editor, view) mock_open = mock.mock_open() mock_extract = mock.MagicMock(side_effect=Exception(':(')) with mock.patch('builtins.open', mock_open), \ mock.patch('mu.contrib.uflash.extract_script', mock_extract): text = mm.open_file('path_to_file.hex') assert text is None assert mock_extract.call_count == 1 assert mock_open.call_count == 1
def test_open_hex(): """ Tries to open hex files with uFlash. """ view = mock.MagicMock() editor = mock.MagicMock() mm = MicrobitMode(editor, view) mock_open = mock.mock_open() hex_extracted = "RECOVERED" with mock.patch("builtins.open", mock_open), mock.patch( "mu.contrib.uflash.extract_script", return_value=hex_extracted ) as extract_script: text, newline = mm.open_file("path_to_file.hex") assert text == hex_extracted assert newline == os.linesep assert extract_script.call_count == 1 assert mock_open.call_count == 1