Skip to content

Commit 328ca23

Browse files
committed
Set up a test harness for the flipperzero crate
The tests can be run with `cargo test` from within the `crates/` directory.
1 parent f72c098 commit 328ca23

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

crates/.cargo/config.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[target.thumbv7em-none-eabihf]
2+
runner = "../tools/cargo-runner.sh"
23
rustflags = [
34
# CPU is Cortex-M4 (STM32WB55)
45
"-C", "target-cpu=cortex-m4",

crates/flipperzero/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ all-features = true
1818

1919
[lib]
2020
bench = false
21-
test = false
21+
harness = false
2222

2323
[dependencies]
2424
flipperzero-sys.workspace = true

crates/flipperzero/src/lib.rs

+54
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! High-level bindings for the Flipper Zero.
22
33
#![no_std]
4+
#![cfg_attr(test, no_main)]
45

56
#[cfg(feature = "alloc")]
67
extern crate alloc;
@@ -16,3 +17,56 @@ pub mod __internal {
1617
// Re-export for use in macros
1718
pub use ufmt;
1819
}
20+
21+
// Infrastructure for running unit tests.
22+
#[cfg(test)]
23+
mod tests {
24+
// Required for panic handler
25+
extern crate flipperzero_rt;
26+
27+
// Required for allocator
28+
#[cfg(feature = "alloc")]
29+
extern crate flipperzero_alloc;
30+
31+
use core::time::Duration;
32+
33+
use flipperzero_rt::{entry, manifest};
34+
use flipperzero_sys as sys;
35+
36+
use crate::{furi::thread, println};
37+
38+
manifest!(name = "Rust Unit Tests");
39+
entry!(main);
40+
41+
// Test runner entry point
42+
fn main(_args: *mut u8) -> i32 {
43+
let heap_before = unsafe { sys::memmgr_get_free_heap() };
44+
let cycle_counter = unsafe { sys::furi_get_tick() };
45+
46+
// TODO: Collect and run tests
47+
let failed = 0;
48+
49+
println!("");
50+
println!("Failed tests: {}\r", failed);
51+
52+
// Time report
53+
println!(
54+
"Consumed: {} ms\r",
55+
unsafe { sys::furi_get_tick() } - cycle_counter
56+
);
57+
58+
// Wait for tested services and apps to deallocate memory
59+
thread::sleep(Duration::from_millis(200));
60+
let heap_after = unsafe { sys::memmgr_get_free_heap() };
61+
println!("Leaked: {}\r", heap_before - heap_after);
62+
63+
// Final Report
64+
if failed == 0 {
65+
println!("Status: PASSED\r");
66+
0
67+
} else {
68+
println!("Status: FAILED\r");
69+
1
70+
}
71+
}
72+
}

tools/cargo-runner.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
# Figure out where this script is located (which is also where the tools are).
4+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
5+
6+
# Change directory to the tools directory, to ensure the CWD doesn't contain any
7+
# .cargo/config files that would prevent us from building on the host machine.
8+
cd $SCRIPT_DIR
9+
10+
# Run the given FAP binary on a connected Flipper Zero.
11+
cargo run --quiet --release --bin run-fap -- $@

0 commit comments

Comments
 (0)