conftest.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import logging
  4. import os
  5. import pytest
  6. from _pytest.fixtures import FixtureRequest
  7. from _pytest.monkeypatch import MonkeyPatch
  8. from pytest_embedded_idf.app import FlashFile
  9. from pytest_embedded_idf.serial import IdfSerial
  10. # This is a custom IdfSerial class to support custom functionality
  11. # which is required only for this test
  12. class FlashBootloader(IdfSerial):
  13. def bootloader_flash(self, binary_path: str) -> None:
  14. """
  15. Flash bootloader.
  16. :return: None
  17. """
  18. logging.info('Flashing bootloader')
  19. bootloader_path = os.path.join(binary_path, 'bootloader', 'bootloader.bin')
  20. logging.info(bootloader_path)
  21. offs = int(self.app.sdkconfig.get('BOOTLOADER_OFFSET_IN_FLASH', 0))
  22. logging.info('bootloader offset is {0}'.format(hex(offs)))
  23. prev_flash_files = self.app.flash_files
  24. flash_files = []
  25. flash_files.append(
  26. FlashFile(
  27. offs,
  28. bootloader_path,
  29. False,
  30. )
  31. )
  32. self.app.flash_files = flash_files
  33. self.flash()
  34. # Restore self.app.flash files to original value
  35. self.app.flash_files = prev_flash_files
  36. @pytest.fixture(scope='module')
  37. def monkeypatch_module(request: FixtureRequest) -> MonkeyPatch:
  38. mp = MonkeyPatch()
  39. request.addfinalizer(mp.undo)
  40. return mp
  41. @pytest.fixture(scope='module', autouse=True)
  42. def replace_dut_class(monkeypatch_module: MonkeyPatch) -> None:
  43. monkeypatch_module.setattr('pytest_embedded_idf.IdfSerial', FlashBootloader)