Add ability to write unittests#5
Open
ImplOfAnImpl wants to merge 9 commits into
Open
Conversation
…re as well for consistency
…messages" to "mintlayer-messages".
1d65aae to
9abba00
Compare
9abba00 to
8d6c07a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Unstable rust has a way to specify a custom test runner and this is used e.g. in the ledger rust sdk itself to implement unit tests that can be run on device.
The way it works is:
#![test_runner(...)].In our case the test runner is
ledger_device_sdk::testing::sdk_test_runner- it iterates over the passed collection of test cases (each of which isledger_device_sdk::testing::TestType), does some address adjustment (apparently, link-time addresses differ from run-time ones in ledger apps, and for some reason in this case the adjustment doesn't happen automatically), and calls the test body stored insideTestType.#[test]doesn't work in this case and the low-level#[test_case]must be used (this is also from unstable rust). Ledger has thetestmacrocrate, which provides the#[test_item]macro, which encapsulates#[test_case]and generates a test case item of theledger_device_sdk::testing::TestTypetype.test_mainfromsample_main. If a test fails, the return value will be non-zero and it will become the exit code of speculos itself.In this PR:
mintlayer-app-core. This is because I'd prefer the lib'ssample_mainto be dedicated to testing only and the app'ssample_mainto be production-only.To make the src structure look better, I created a
cratesdirectory in the root and put both the "app-core" and "messages" there.For consistency, the package name of "messages" is now "mintlayer-messages".
The main reason is to avoid accidental "result unused" warnings in tests, but having clippy is nice in general.
For now I've only implemented a sample test in
crates/app-core/src/handlers/sign_tx/summary_collector.rs. More tests need to be added.Additional notes:
But some existing ledger apps seem to also use it (e.g. app-sui), so I guess it's consedered to be "stable enough".
Result<(), ()>and if an error is returned from one test case, the rest will still be run. But there is no way to specify the reason for the failure (except for logging something before a failure can occur), which makes failures not very informative.With panicking, the panic reason will be logged by our custom panic handler and will appear in speculos console output. I.e. both approaches have drawbacks; I'd say let's stick to panicking, because it's simpler.
cargo test. I.e. though the solution is not pretty, the problem is solvable.P.S. I also removed this:
from
.cargo/config.toml. I'm not sure why it's needed there (because normally you would always specify the model explicitly anyway) and it interferes with VSCode's attempts to runcargo check. E.g. I have this in my.vscode/settings.json:and rust-analyzer works fine if those lines in
.cargo/config.tomlare removed, but if they're present, it fails with some weird errors. Let me know if you needed them for some reason.