Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests #94

Merged
merged 6 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

env:
CARGO_TERM_COLOR: always
CORE_TARGET: thumbv7m-none-eabi # needed by `core`
CORE_TARGET: thumbv7em-none-eabi # needed by `core`

jobs:
test:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- [#94] Refactor tests
- [#93] Remove TryInto import (in prelude since 2021 edition)
- [#92] Recommend `target.'cfg(..)'.linker` for recent Cargo versions
- [#90] Configure release-plz
- [#88] Setup release-plz

[#94]: https://github.com/knurling-rs/flip-link/pull/94
[#93]: https://github.com/knurling-rs/flip-link/pull/93
[#92]: https://github.com/knurling-rs/flip-link/pull/92
[#90]: https://github.com/knurling-rs/flip-link/pull/90
Expand Down
68 changes: 34 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ readme = "README.md"
env_logger = { version = "0.11", default-features = false }
getrandom = "0.2"
log = "0.4"
object = { version = "0.33", default-features = false, features = ["read_core", "elf", "std"] }
object = { version = "0.35", default-features = false, features = ["read_core", "elf", "std"] }

[dev-dependencies]
assert_cmd = "2.0"
rstest = { version = "0.18", default-features = false }
rstest = { version = "0.19", default-features = false }

[workspace]
members = [".", "xtest"]
Expand Down
4 changes: 2 additions & 2 deletions test-flip-link-app/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[target.thumbv7m-none-eabi]
[target.thumbv7em-none-eabi]
runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
rustflags = [
Urhengulas marked this conversation as resolved.
Show resolved Hide resolved
"-C", "linker=flip-link",
"-C", "link-arg=-Tlink.x",
]

[build]
target = "thumbv7m-none-eabi"
target = "thumbv7em-none-eabi"
57 changes: 24 additions & 33 deletions tests/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const CRATE: &str = "test-flip-link-app";
/// Example firmware in `$CRATE/examples`
const FILES: [&str; 4] = ["crash", "exception", "hello", "panic"];
/// Compilation target firmware is build for
const TARGET: &str = "thumbv7m-none-eabi";
const TARGET: &str = "thumbv7em-none-eabi";

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

Expand All @@ -19,7 +19,7 @@ fn should_link_example_firmware(#[case] default_features: bool) {
cargo::check_flip_link();

// Act
let cmd = cargo::build_example_firmware(CRATE, default_features);
let cmd = cargo::build_example_firmware(default_features);

// Assert
cmd.success();
Expand All @@ -31,7 +31,7 @@ fn should_verify_memory_layout() -> Result<()> {
cargo::check_flip_link();

// Act
cargo::build_example_firmware(CRATE, true).success();
cargo::build_example_firmware(true).success();

// Assert
for elf_path in elf::paths() {
Expand All @@ -40,7 +40,7 @@ fn should_verify_memory_layout() -> Result<()> {
let object = object::File::parse(&*elf)?;

// get the relevant sections
let (bss, data, uninit, vector_table) = elf::get_sections(&object)?;
let [bss, data, uninit, vector_table] = elf::get_sections(&object);
// compute the initial stack-pointer from `.vector_table`
let initial_sp = elf::compute_initial_sp(&vector_table)?;
// get the bounds of 'static RAM'
Expand All @@ -59,12 +59,14 @@ mod cargo {

use assert_cmd::{assert::Assert, prelude::*};

use super::*;

/// Build all examples in `$REPO/$rel_path`
#[must_use]
pub fn build_example_firmware(rel_path: &str, default_features: bool) -> Assert {
pub(crate) fn build_example_firmware(default_features: bool) -> Assert {
// append `rel_path` to the current working directory
let mut firmware_dir = std::env::current_dir().unwrap();
firmware_dir.push(rel_path);
firmware_dir.push(CRATE);

// disable default features or use `-v` as a no-op
let default_features = match default_features {
Expand All @@ -80,7 +82,7 @@ mod cargo {
}

/// Check that `flip-link` is present on the system
pub fn check_flip_link() {
pub(crate) fn check_flip_link() {
Command::new("which")
.arg("flip-link")
.unwrap()
Expand All @@ -100,18 +102,18 @@ mod elf {
///
/// It is the first 32-bit word in the `.vector_table` section,
/// according to the "ARMv6-M Architecture Reference Manual".
pub fn compute_initial_sp(vector_table: &Section) -> Result<u64> {
pub(crate) fn compute_initial_sp(vector_table: &Section) -> Result<u64> {
let data = vector_table.uncompressed_data()?;
let sp = u32::from_le_bytes(data[..4].try_into()?);
Ok(sp as u64)
}

/// Get [`RangeInclusive`] from lowest to highest address of all sections
pub fn get_bounds(sections: &[Section]) -> Result<RangeInclusive<u64>> {
pub(crate) fn get_bounds(sections: &[Section]) -> Result<RangeInclusive<u64>> {
// get beginning and end of all sections
let addresses = sections
.iter()
.flat_map(|sec| vec![sec.address(), sec.address() + sec.size()])
.flat_map(|sec| [sec.address(), sec.address() + sec.size()])
.collect::<Vec<_>>();

// get highest and lowest address of all sections
Expand All @@ -126,34 +128,23 @@ mod elf {
/// * `.data`
/// * `.uninit`
/// * `.vector_table`
pub fn get_sections<'data, 'file>(
object: &'file File<'data>,
) -> Result<(
Section<'data, 'file>,
Section<'data, 'file>,
Section<'data, 'file>,
Section<'data, 'file>,
)> {
pub(crate) fn get_sections<'file>(object: &'file File<'_>) -> [Section<'file, 'file>; 4] {
// try to get section, else error
let get_section = |section_name| {
object
.section_by_name(section_name)
.ok_or(format!("error getting section `{}`", section_name))
};

Ok((
get_section(".bss")?,
get_section(".data")?,
get_section(".uninit")?,
get_section(".vector_table")?,
))
let get_section = |section_name| object.section_by_name(section_name).expect(section_name);

[
get_section(".bss"),
get_section(".data"),
get_section(".uninit"),
get_section(".vector_table"),
]
}

/// Paths to firmware binaries.
pub fn paths() -> Vec<PathBuf> {
pub(crate) fn paths() -> Vec<PathBuf> {
FILES
.iter()
.map(|file_name| format!("{}/target/{}/debug/examples/{}", CRATE, TARGET, file_name))
.into_iter()
.map(|file_name| format!("{CRATE}/target/{TARGET}/debug/examples/{file_name}"))
.map(PathBuf::from)
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion xtest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod cargo {
mod rustup {
use super::*;

const TARGET: &str = "thumbv7m-none-eabi";
const TARGET: &str = "thumbv7em-none-eabi";

pub fn install_target() -> Result<()> {
let status = Command::new("rustup")
Expand Down
Loading