Skip to content

Commit

Permalink
Merge pull request #166 from LedgerHQ/update-find-app-for-lib-mode
Browse files Browse the repository at this point in the history
Update path finding to main app in library mode
  • Loading branch information
agrojean-ledger authored Feb 9, 2024
2 parents 063cd2c + b7ca81b commit 15a3774
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.14.3] - 2024-02-09

### Changed
- Update search tree for main app binary in library mode

## [1.14.2] - 2024-01-31

### Fixed
Expand Down
14 changes: 13 additions & 1 deletion src/ragger/conftest/base_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,19 @@ def prepare_speculos_args(root_pytest_dir: Path, firmware: Firmware, display: bo
project_root_dir = find_project_root_dir(root_pytest_dir)

# Find the standalone application for the requested device
app_path = find_main_application(project_root_dir / conf.OPTIONAL.APP_DIR, device)
# If the app is to be loaded as a library, the main app should be located in a subfolder of
# project_root_dir / conf.OPTIONAL.APP_DIR. There should be only one subfolder in the path.
app_path = None
if conf.OPTIONAL.LOAD_MAIN_APP_AS_LIBRARY:
app_dir_children = list((project_root_dir / conf.OPTIONAL.APP_DIR).iterdir())
if len(app_dir_children) != 1:
raise ValueError(
f"Expected a single folder in {conf.OPTIONAL.APP_DIR}, found {len(app_dir_children)}"
)
app_path = find_main_application(app_dir_children[0], device)
# If the app is standalone, the main app should be located in project_root_dir / conf.OPTIONAL.APP_DIR
else:
app_path = find_main_application(project_root_dir / conf.OPTIONAL.APP_DIR, device)

# Find all libraries that have to be sideloaded
if conf.OPTIONAL.LOAD_MAIN_APP_AS_LIBRARY:
Expand Down
23 changes: 14 additions & 9 deletions tests/unit/conftests/test_base_conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
from typing import Tuple
from unittest import TestCase
from unittest.mock import patch

Expand All @@ -8,12 +9,15 @@
from ..helpers import temporary_directory


def prepare_base_dir(directory: Path) -> Path:
def prepare_base_dir(directory: Path) -> Tuple[Path, Path]:
(directory / ".git").mkdir()
(directory / "build" / "stax" / "bin").mkdir(parents=True, exist_ok=True)
(directory / "deps" / "dep" / "build" / "stax" / "bin").mkdir(parents=True, exist_ok=True)
dep_path = (directory / "deps" / "dep" / "build" / "stax" / "bin" / "app.elf")
dep_path.touch()
app_path = (directory / "build" / "stax" / "bin" / "app.elf")
app_path.touch()
return app_path
return app_path, dep_path


class TestBaseConftest(TestCase):
Expand All @@ -23,28 +27,29 @@ def setUp(self):

def test_prepare_speculos_args_simplest(self):
with temporary_directory() as temp_dir:
app_path = prepare_base_dir(temp_dir)
app_path, _ = prepare_base_dir(temp_dir)
result_app, result_args = bc.prepare_speculos_args(temp_dir, Firmware.STAX, False,
self.seed)
self.assertEqual(result_app, app_path)
self.assertEqual(result_args, {"args": ["--seed", self.seed]})

def test_prepare_speculos_args_simple_with_gui(self):
with temporary_directory() as temp_dir:
app_path = prepare_base_dir(temp_dir)
app_path, _ = prepare_base_dir(temp_dir)
result_app, result_args = bc.prepare_speculos_args(temp_dir, Firmware.STAX, True,
self.seed)
self.assertEqual(result_app, app_path)
self.assertEqual(result_args, {"args": ["--display", "qt", "--seed", self.seed]})

def test_prepare_speculos_args_main_as_library(self):
with temporary_directory() as temp_dir:
app_path = prepare_base_dir(temp_dir)
app_path, dep_path = prepare_base_dir(temp_dir)
with patch("ragger.conftest.base_conftest.conf.OPTIONAL.LOAD_MAIN_APP_AS_LIBRARY",
True):
result_app, result_args = bc.prepare_speculos_args(temp_dir, Firmware.STAX, False,
self.seed)
self.assertEqual(result_app, app_path)
with patch("ragger.conftest.base_conftest.conf.OPTIONAL.APP_DIR", "deps"):
result_app, result_args = bc.prepare_speculos_args(
temp_dir, Firmware.STAX, False, self.seed)
self.assertEqual(result_app, dep_path)
self.assertEqual(result_args, {"args": [f"-l{app_path}", "--seed", self.seed]})

def test_prepare_speculos_args_sideloaded_apps_nok_no_dir(self):
Expand All @@ -58,7 +63,7 @@ def test_prepare_speculos_args_sideloaded_apps_nok_no_dir(self):
def test_prepare_speculos_args_sideloaded_apps_ok(self):
lib1_bin, lib1_name, lib2_bin, lib2_name = "lib1", "name1", "lib2", "name2"
with temporary_directory() as temp_dir:
app_path = prepare_base_dir(temp_dir)
app_path, _ = prepare_base_dir(temp_dir)
sideloaded_apps_dir = temp_dir / "here"
sideloaded_apps_dir.mkdir()
lib1_path = sideloaded_apps_dir / f"{lib1_bin}_stax.elf"
Expand Down

0 comments on commit 15a3774

Please sign in to comment.