def test_raises_when_board_not_found(self, get_board_by_product_code, read_online_id, read_product_code, _get_all_htm_files_contents): get_board_by_product_code.side_effect = UnknownBoard candidate = CandidateDeviceFactory() with self.assertRaises(NoBoardForCandidate): resolve_board(candidate)
def test_raises_when_board_not_found(self, get_board_by_online_id, read_online_id, read_product_code, _get_all_htm_files_contents): read_online_id.return_value = OnlineId(target_type="hat", slug="boat") get_board_by_online_id.side_effect = UnknownBoard candidate = CandidateDeviceFactory() with self.assertRaises(NoBoardForCandidate): resolve_board(candidate)
def test_raises_when_database_lookup_fails(self, get_board_by_product_code_mock, caplog): get_board_by_product_code_mock.side_effect = MbedTargetsError product_code = "0123" with pytest.raises(ResolveBoardError): resolve_board(product_code=product_code) assert product_code in caplog.text assert caplog.records[-1].levelname == "ERROR"
def test_raises_when_database_lookup_fails(self, get_board_by_online_id_mock, caplog): get_board_by_online_id_mock.side_effect = MbedTargetsError online_id = OnlineId(target_type="hat", slug="boat") with pytest.raises(ResolveBoardError): resolve_board(online_id=online_id) assert repr(online_id) in caplog.text assert caplog.records[-1].levelname == "ERROR"
def get_connected_devices() -> ConnectedDevices: """Returns Mbed Devices connected to host computer. Connected devices which have been identified as Mbed Boards and also connected devices which are potentially Mbed Boards (but not could not be identified in the database) are returned. Raises: DeviceLookupFailed: If there is a problem with the process of identifying Mbed Boards from connected devices. """ connected_devices = ConnectedDevices() board: Optional["Board"] for candidate_device in detect_candidate_devices(): try: board = resolve_board(candidate_device) except NoBoardForCandidate: board = None except MbedTargetsError as err: raise DeviceLookupFailed( "A problem occurred when looking up board data for connected devices." ) from err connected_devices.add_device(candidate_device, board) return connected_devices
def from_candidate(cls, candidate: CandidateDevice) -> "Device": """Contruct a Device from a CandidateDevice. We try to resolve a board using data files that may be stored on the CandidateDevice. If this fails we set the board to `None` which means we couldn't verify this Device as being an Mbed enabled device. Args: candidate: The CandidateDevice we're using to create the Device. """ device_file_info = read_device_files(candidate.mount_points) try: mbed_board = resolve_board(device_file_info.product_code, device_file_info.online_id, candidate.serial_number) mbed_enabled = True except NoBoardForCandidate: # Create an empty Board to ensure the device is fully populated and rendering is simple mbed_board = Board.from_offline_board_entry({}) mbed_enabled = False except ResolveBoardError: raise DeviceLookupFailed( f"Failed to resolve the board for candidate device {candidate!r}. There was a problem looking up the " "board data in the database.") return Device( serial_port=candidate.serial_port, serial_number=candidate.serial_number, mount_points=candidate.mount_points, mbed_board=mbed_board, mbed_enabled=mbed_enabled, interface_version=device_file_info.interface_details.get( "Version"), )
def get_connected_devices() -> ConnectedDevices: """Returns Mbed Devices connected to host computer. Connected devices which have been identified as Mbed Boards and also connected devices which are potentially Mbed Boards (but not could not be identified in the database) are returned. Raises: DeviceLookupFailed: If there is a problem with the process of identifying Mbed Boards from connected devices. """ connected_devices = ConnectedDevices() board: Optional["Board"] for candidate_device in detect_candidate_devices(): try: board = resolve_board(candidate_device) except NoBoardForCandidate: board = None except MbedTargetsError as err: raise DeviceLookupFailed( f"We found a potential connected device ({candidate_device!r}) but could not identify it as being " "Mbed enabled. This is because we couldn't find a known product code from the available data on your " "device. Check your device contains a valid HTM file with a product code, and that it is added as an " "Mbed enabled device on os.mbed.com.") from err connected_devices.add_device(candidate_device, board) return connected_devices
def test_returns_resolved_board(self, get_board_by_jlink_slug_mock): online_id = OnlineId("jlink", "test-board") subject = resolve_board(online_id=online_id) assert subject == get_board_by_jlink_slug_mock.return_value get_board_by_jlink_slug_mock.assert_called_once_with(online_id.slug)
def test_resolves_board_using_product_code_when_available( self, get_board_by_product_code_mock): serial_number = "0A9KJFKD0993WJKUFS0KLJ329090" subject = resolve_board(serial_number=serial_number) assert subject == get_board_by_product_code_mock.return_value get_board_by_product_code_mock.assert_called_once_with( serial_number[:4])
def test_returns_resolved_board(self, get_board_by_online_id_mock): online_id = OnlineId(target_type="hat", slug="boat") subject = resolve_board(online_id=online_id) assert subject == get_board_by_online_id_mock.return_value get_board_by_online_id_mock.assert_called_once_with( target_type=online_id.target_type, slug=online_id.slug)
def test_returns_resolved_target(self, get_board_by_product_code_mock): dev_info = DeviceFileInfo("0123", None, None) subject = resolve_board(product_code=dev_info.product_code) assert subject == get_board_by_product_code_mock.return_value get_board_by_product_code_mock.assert_called_once_with( dev_info.product_code)
def test_resolves_board_using_product_code_when_available( self, get_board_by_product_code, read_online_id, read_product_code, _get_all_htm_files_contents): candidate = CandidateDeviceFactory() subject = resolve_board(candidate) self.assertEqual(subject, get_board_by_product_code.return_value) get_board_by_product_code.assert_called_once_with( candidate.serial_number[:4])
def test_returns_resolved_board(self, get_board_by_online_id, read_online_id, read_product_code, _get_all_htm_files_contents): online_id = OnlineId(target_type="hat", slug="boat") read_online_id.return_value = online_id candidate = CandidateDeviceFactory() subject = resolve_board(candidate) self.assertEqual(subject, get_board_by_online_id.return_value) read_online_id.assert_called_with( _get_all_htm_files_contents.return_value[0]) get_board_by_online_id.assert_called_once_with( target_type=online_id.target_type, slug=online_id.slug)
def test_returns_resolved_target(self, get_board_by_product_code, read_product_code, _get_all_htm_files_contents): read_product_code.return_value = "0123" candidate = CandidateDeviceFactory() subject = resolve_board(candidate) self.assertEqual(subject, get_board_by_product_code.return_value) get_board_by_product_code.assert_called_once_with( read_product_code.return_value) read_product_code.assert_called_once_with( _get_all_htm_files_contents.return_value[0]) _get_all_htm_files_contents.assert_called_once_with( candidate.mount_points)
def test_raises_when_board_not_found(self, get_board_by_online_id_mock): get_board_by_online_id_mock.side_effect = UnknownBoard with pytest.raises(NoBoardForCandidate): resolve_board(online_id=OnlineId(target_type="hat", slug="boat"))
def test_raises_when_board_not_found(self, get_board_by_product_code_mock): get_board_by_product_code_mock.side_effect = UnknownBoard with pytest.raises(NoBoardForCandidate): resolve_board(product_code="0123")
def test_raises_when_board_not_found(self, get_board_by_jlink_slug_mock): get_board_by_jlink_slug_mock.side_effect = UnknownBoard online_id = OnlineId("jlink", "test-board") with pytest.raises(NoBoardForCandidate): resolve_board(online_id=online_id)