Beispiel #1
0
    def test_handles_os_error_with_warning(self, tmp_path, caplog, monkeypatch):
        bad_htm = pathlib.Path(tmp_path, "MBED.HTM")
        bad_htm.touch()
        monkeypatch.setattr(pathlib.Path, "read_text", mock.Mock(side_effect=OSError))

        read_device_files([tmp_path])
        assert str(bad_htm) in caplog.text
Beispiel #2
0
    def test_skips_hidden_files(self, caplog, tmp_path):
        auth = "101000000000000000000002F7F35E602eeb0bb9b632205c51f6c357aeee7bc9"
        file_contents = (
            '<meta http-equiv="refresh" '
            f'content="0; url=http://mbed.org/start?auth={auth}&loader=11972&firmware=16457&configuration=4" />'
        )
        pathlib.Path(tmp_path, "._MBED.HTM").write_text(file_contents)
        pathlib.Path(tmp_path, "._DETAILS.TXT").write_text("Version: 2222")

        assert read_device_files([tmp_path]).product_code is None
        assert not read_device_files([tmp_path]).interface_details
Beispiel #3
0
    def test_interface_empty_if_not_found(self, tmp_path):
        board_html = pathlib.Path(tmp_path, "Board.html")
        board_html.write_text("<html></html>")

        info = read_device_files([tmp_path])

        assert info.interface_details == {}
Beispiel #4
0
    def test_reads_segger_slug(self, tmp_path):
        segger_html = pathlib.Path(tmp_path, "Segger.html")
        segger_html.write_text(build_segger_html("variant"))

        info = read_device_files([tmp_path])

        assert info.interface_details.get("Version") == "variant"
Beispiel #5
0
    def test_id_none_if_no_board_slug(self, tmp_path):
        board_html = pathlib.Path(tmp_path, "Board.html")
        board_html.write_text(build_board_html("http://test.com"))

        info = read_device_files([tmp_path])

        assert info.online_id is None
Beispiel #6
0
    def test_reads_board_slug_ignore_extension(self, tmp_path):
        board_html = pathlib.Path(tmp_path, "Board.html")
        board_html.write_text(build_board_html("http://test.com/slug.html"))

        info = read_device_files([tmp_path])

        assert info.online_id == OnlineId(target_type="jlink", slug="slug")
Beispiel #7
0
    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"),
        )
Beispiel #8
0
    def test_reads_product_code_from_auth_attribute(self, tmp_path):
        auth = "101000000000000000000002F7F35E602eeb0bb9b632205c51f6c357aeee7bc9"
        file_contents = (
            '<meta http-equiv="refresh" '
            f'content="0; url=http://mbed.org/start?auth={auth}&loader=11972&firmware=16457&configuration=4" />'
        )
        pathlib.Path(tmp_path, "MBED.HTM").write_text(file_contents)

        assert read_device_files([tmp_path]).product_code == auth[:4]
Beispiel #9
0
    def test_finds_daplink_compatible_device_files(self, tmp_path):
        details = pathlib.Path(tmp_path, "details.txt")
        htm = pathlib.Path(tmp_path, "mbed.htm")
        htm.write_text("code=2222")
        details.write_text("Version: 2")

        info = read_device_files([tmp_path])

        assert info.product_code is not None
        assert info.interface_details is not None
Beispiel #10
0
    def test_finds_jlink_device_files(self, tmp_path):
        segger_html = pathlib.Path(tmp_path, "Segger.html")
        board_html = pathlib.Path(tmp_path, "Board.html")
        segger_html.write_text(build_segger_html())
        board_html.write_text(build_board_html())

        info = read_device_files([tmp_path])

        assert info.online_id is not None
        assert info.interface_details is not None
Beispiel #11
0
    def test_extracts_first_product_code_found(self, tmp_path):
        auth = "101000000000000000000002F7F35E602eeb0bb9b632205c51f6c357aeee7bc9"
        file_contents_1 = (
            '<meta http-equiv="refresh" '
            f'content="0; url=http://mbed.org/start?auth={auth}&loader=11972&firmware=16457&configuration=4" />'
        )
        code = "02400201B80ECE4A45F033F2"
        file_contents_2 = f'<meta http-equiv="refresh" content="0; url=http://mbed.org/device/?code={code}"/>'
        directory_1 = pathlib.Path(tmp_path, "test-1")
        directory_1.mkdir()
        directory_2 = pathlib.Path(tmp_path, "test-2")
        directory_2.mkdir()
        pathlib.Path(directory_1, "mbed.htm").write_text(file_contents_1)
        pathlib.Path(directory_2, "mbed.htm").write_text(file_contents_2)

        result = read_device_files([directory_1, directory_2])

        assert result.product_code == auth[:4]
Beispiel #12
0
    def test_reads_online_id_from_url(self, tmp_path):
        url = "https://os.mbed.com/platforms/THIS-IS_a_SLUG_123/"
        file_contents = f"window.location.replace({url});"
        pathlib.Path(tmp_path, "MBED.HTM").write_text(file_contents)

        assert read_device_files([tmp_path]).online_id == OnlineId(target_type="platform", slug="THIS-IS_a_SLUG_123")
Beispiel #13
0
    def test_none_if_no_product_code(self, tmp_path):
        file_contents = '<meta http-equiv="refresh" content="0; url=http://mbed.org/config" />'
        pathlib.Path(tmp_path, "MBED.HTM").write_text(file_contents)

        assert read_device_files([tmp_path]).product_code is None
Beispiel #14
0
    def test_parses_details_txt(self, content, expected, tmp_path):
        details_file_path = pathlib.Path(tmp_path, "DETAILS.txt")
        details_file_path.write_text(content)

        result = read_device_files([tmp_path]).interface_details
        assert result == expected
Beispiel #15
0
    def test_reads_product_code_from_code_attribute(self, tmp_path):
        code = "02400201B80ECE4A45F033F2"
        file_contents = f'<meta http-equiv="refresh" content="0; url=http://mbed.org/device/?code={code}"/>'
        pathlib.Path(tmp_path, "MBED.HTM").write_text(file_contents)

        assert read_device_files([tmp_path]).product_code == code[:4]
Beispiel #16
0
    def test_none_if_not_found(self, tmp_path):
        file_contents = "window.location.replace(https://os.mbed.com/about);"
        pathlib.Path(tmp_path, "MBED.HTM").write_text(file_contents)

        assert read_device_files([tmp_path]).online_id is None
Beispiel #17
0
    def test_warns_if_no_device_files_found(self, caplog, tmp_path):
        read_device_files([tmp_path])

        assert str(tmp_path) in caplog.text