diff --git a/tools/test_apps/storage/fatfsgen/pytest_fatfsgen_example.py b/tools/test_apps/storage/fatfsgen/pytest_fatfsgen_example.py index e50bdc800ba..acf5c220468 100644 --- a/tools/test_apps/storage/fatfsgen/pytest_fatfsgen_example.py +++ b/tools/test_apps/storage/fatfsgen/pytest_fatfsgen_example.py @@ -4,11 +4,12 @@ import os import re import shutil import sys -import typing as t from datetime import datetime -from subprocess import run from subprocess import STDOUT +from subprocess import CalledProcessError +from subprocess import run from time import sleep +from typing import Union import pytest from pytest_embedded import Dut @@ -22,7 +23,7 @@ sys.path.append(parttool_dir) from parttool import PartitionName, ParttoolTarget # noqa E402 # pylint: disable=C0413 -FileStructure = t.Dict[str, t.Union[t.Optional[str], 'FileStructure']] +FileStructure = dict[str, Union[str, None, 'FileStructure']] # noqa: UP007 class DirectoryStructureError(Exception): @@ -69,7 +70,7 @@ def validate_directory_structure(base_path: str, expected_structure: FileStructu :raises DirectoryStructureError: If the structure or contents do not match. """ - def normalize_case(items: t.List[str]) -> t.Dict[str, str]: + def normalize_case(items: list[str]) -> dict[str, str]: return {item.lower(): item for item in items} def escape_output(text: str) -> str: @@ -93,7 +94,7 @@ def validate_directory_structure(base_path: str, expected_structure: FileStructu if isinstance(substructure, str): # Check file content try: - with open(full_path, 'r', encoding='utf-8') as file: + with open(full_path, encoding='utf-8') as file: content = file.read() if content != substructure: raise ContentMismatchError( @@ -141,7 +142,7 @@ def test_examples_fatfsgen(config: str, dut: Dut) -> None: dut.expect(msg, timeout=timeout) # Expects list of strings sequentially - def expect_all(msg_list: t.List[str], timeout: int = timeout) -> None: + def expect_all(msg_list: list[str], timeout: int = timeout) -> None: for msg in msg_list: expect(msg, timeout) @@ -177,12 +178,14 @@ def test_examples_fatfsgen(config: str, dut: Dut) -> None: expect('example: Mounting FAT filesystem') if not config_read_only: - expect_all([ - 'example: Opening file', - 'example: File written', - 'example: Reading file', - 'example: Read from file: ' + "'This is written by the device'", - ]) + expect_all( + [ + 'example: Opening file', + 'example: File written', + 'example: Reading file', + 'example: Read from file: ' + "'This is written by the device'", + ] + ) expect('example: Reading file') @@ -199,8 +202,18 @@ def test_examples_fatfsgen(config: str, dut: Dut) -> None: dut.serial.close() sleep(1) - target = ParttoolTarget(dut.serial.port, 1843200) - target.read_partition(PartitionName('storage'), 'temp.img') + # Host read-flash after closing the test UART can fail at very high baud (e.g. exit code 2 on + # multi-Mbit reads). Use the same order of magnitude as post-stub flash programming (921600). + read_baud = 921600 + for attempt in range(3): + try: + target = ParttoolTarget(dut.serial.port, read_baud) + target.read_partition(PartitionName('storage'), 'temp.img') + break + except CalledProcessError: + if attempt == 2: + raise + sleep(2) if config_long_names: run(['python', fatfs_parser_path, '--long-name-support', 'temp.img'], stderr=STDOUT) else: