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 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 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. """ connected_devices = ConnectedDevices() for candidate_device in detect_candidate_devices(): device = Device.from_candidate(candidate_device) connected_devices.add_device(device) return connected_devices
def test_identifiers_appended_when_identical_boards_found( self, get_connected_devices): device = create_fake_device() device_2 = create_fake_device() board = device.mbed_board get_connected_devices.return_value = ConnectedDevices( identified_devices=[device, device_2]) result = CliRunner().invoke(list_connected_devices, "--format=json") expected_output = [ { "build_targets": [ f"{board.board_type}_{board.build_variant[0]}[0]", f"{board.board_type}_{board.build_variant[1]}[0]", f"{board.board_type}[0]", ], }, { "build_targets": [ f"{board.board_type}_{board.build_variant[0]}[1]", f"{board.board_type}_{board.build_variant[1]}[1]", f"{board.board_type}[1]", ], }, ] assert result.exit_code == 0 for actual, expected in zip(json.loads(result.output), expected_output): assert actual["mbed_board"]["build_targets"] == expected[ "build_targets"]
def test_sort_order(self, get_connected_devices): devices = [ create_fake_device( serial_number="11111111", board_name="AAAAAAA1FirstBoard", build_variant=(), mount_points=(pathlib.Path("/media/you/First"), ), ), create_fake_device( serial_number="22222222", board_name="SameBoard[0]", build_variant=(), mount_points=(pathlib.Path("/media/you/Second"), ), ), create_fake_device( serial_number="33333333", board_name="SameBoard[1]", build_variant=(), mount_points=(pathlib.Path("/media/you/Third"), ), ), ] get_connected_devices.return_value = ConnectedDevices( identified_devices=[devices[2], devices[0], devices[1]]) result = CliRunner().invoke(list_connected_devices, "--show-all") output_lines = result.output.splitlines() assert devices[0].mbed_board.board_name in output_lines[2] assert devices[1].mbed_board.board_name in output_lines[3] assert devices[2].mbed_board.board_name in output_lines[4]
def test_appends_identifier_when_identical_boards_found( self, get_connected_devices): heading_name = "Build target(s)" device_a = create_fake_device() device_b = create_fake_device() board = device_a.mbed_board get_connected_devices.return_value = ConnectedDevices( identified_devices=[device_a, device_b]) build_targets = [ f"{board.board_type}_{board.build_variant[0]}[0]", f"{board.board_type}_{board.build_variant[1]}[0]", f"{board.board_type}[0]", f"{board.board_type}_{board.build_variant[0]}[1]", f"{board.board_type}_{board.build_variant[1]}[1]", f"{board.board_type}[1]", ] result = CliRunner().invoke(list_connected_devices) output_lines = result.output.splitlines() assert result.exit_code == 0 assert heading_name in output_lines[0] for i, bt in enumerate(build_targets): assert bt in output_lines[2 + i] assert output_lines[0].find(heading_name) == output_lines[ 2 + i].find(bt)
def test_informs_when_no_devices_are_connected(self, get_connected_devices): get_connected_devices.return_value = ConnectedDevices() result = CliRunner().invoke(list_connected_devices) assert result.exit_code == 0 assert "No connected Mbed devices found." in result.output
def test_informs_when_no_devices_are_connected(self, get_connected_devices): get_connected_devices.return_value = ConnectedDevices() result = CliRunner().invoke(list_connected_devices) self.assertEqual(result.exit_code, 0) self.assertIn("No connected Mbed devices found.", result.output)
def test_board_attr_included(self, board_attr, get_connected_devices): device = create_fake_device() get_connected_devices.return_value = ConnectedDevices( identified_devices=[device]) attr = getattr(device.mbed_board, board_attr) # JSON encoder converts tuples to lists, so we need to convert the test data also to match attr = [str(a) for a in attr] if isinstance(attr, tuple) else str(attr) result = CliRunner().invoke(list_connected_devices, "--format=json") assert result.exit_code == 0 assert attr == json.loads(result.output)[0]["mbed_board"][board_attr]
def test_displays_unknown_serial_port_value(self, get_connected_devices): heading_name = "Serial port" device = create_fake_device(serial_port=None) get_connected_devices.return_value = ConnectedDevices( identified_devices=[device]) result = CliRunner().invoke(list_connected_devices) output_lines = result.output.splitlines() assert result.exit_code == 0 assert heading_name in output_lines[0] assert output_lines[0].find(heading_name) == output_lines[2].find( "<unknown>")
def test_board_attr_included(self, header, board_attr, get_connected_devices): heading_name = header device = create_fake_device() get_connected_devices.return_value = ConnectedDevices( identified_devices=[device]) attr = getattr(device.mbed_board, board_attr) result = CliRunner().invoke(list_connected_devices) output_lines = result.output.splitlines() assert result.exit_code == 0 assert heading_name in output_lines[0] assert attr in output_lines[2] assert output_lines[0].find(heading_name) == output_lines[2].find(attr)
def test_given_json_flag_lists_devices_using_json_output( self, _build_json_output, _sort_devices, get_connected_devices): identified_devices = [mock.Mock(spec_set=Device)] unidentified_devices = [mock.Mock(spec_set=Device)] get_connected_devices.return_value = ConnectedDevices( identified_devices=identified_devices, unidentified_devices=unidentified_devices) _build_json_output.return_value = "some output" result = CliRunner().invoke(list_connected_devices, "--format=json") self.assertEqual(result.exit_code, 0) self.assertIn(_build_json_output.return_value, result.output) _build_json_output.assert_called_once_with(_sort_devices.return_value) _sort_devices.assert_called_once_with(identified_devices)
def test_multiline_device_attr_included(self, header, device_attr, get_connected_devices): heading_name = header device = create_fake_device() get_connected_devices.return_value = ConnectedDevices( identified_devices=[device]) attr = getattr(device, device_attr) result = CliRunner().invoke(list_connected_devices) output_lines = result.output.splitlines() assert result.exit_code == 0 assert heading_name in output_lines[0] for i, a in enumerate(attr): assert str(a) in output_lines[2 + i] assert output_lines[0].find(heading_name) == output_lines[ 2 + i].find(str(a))
def test_given_show_all(self, _build_tabular_output, _sort_devices, get_connected_devices): identified_devices = [mock.Mock(spec_set=Device)] unidentified_devices = [mock.Mock(spec_set=Device)] get_connected_devices.return_value = ConnectedDevices( identified_devices=identified_devices, unidentified_devices=unidentified_devices) _build_tabular_output.return_value = "some output" result = CliRunner().invoke(list_connected_devices, "--show-all") self.assertEqual(result.exit_code, 0) self.assertIn(_build_tabular_output.return_value, result.output) _build_tabular_output.assert_called_once_with( _sort_devices.return_value) _sort_devices.assert_called_once_with(identified_devices + unidentified_devices)