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

Tests for 22 #42

Merged
merged 7 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
91 changes: 91 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tempfile = "3.2"

[dev-dependencies]
assert_cmd = "1.0"
rstest = "0.10"

[workspace]
members = [".", "xtest"]
Empty file added test-flip-link-app/.empty.x
Empty file.
7 changes: 7 additions & 0 deletions test-flip-link-app/.memory.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MEMORY
{
FLASH : ORIGIN = 0x00000000, LENGTH = 256K
RAM : ORIGIN = 0x20000000, LENGTH = 64K
}

INCLUDE empty.x
10 changes: 9 additions & 1 deletion test-flip-link-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ version = "0.1.0"
cortex-m = "0.7"
cortex-m-rt = "0.6"
cortex-m-semihosting = "0.3"
lm3s6965 = "0.1"
panic-semihosting = { version = "0.5", features = ["exit"] }

# optional
lm3s6965 = { version = "0.1", optional = true }
Urhengulas marked this conversation as resolved.
Show resolved Hide resolved

[build-dependencies]
anyhow = "1.0"

[features]
default = ["lm3s6965"]

[workspace] # needed to exclude package from parent workspace
19 changes: 19 additions & 0 deletions test-flip-link-app/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
fn main() -> anyhow::Result<()> {
// include following if hal is excluded (aka. default features are disabled)
#[cfg(not(feature = "default"))]
Urhengulas marked this conversation as resolved.
Show resolved Hide resolved
{
use std::{env, fs::File, io::Write, path::PathBuf};

let out = PathBuf::from(env::var_os("OUT_DIR").expect("`OUT_DIR` is not set"));

File::create(out.join("memory.x"))?.write_all(include_bytes!(".memory.x"))?;
File::create(out.join("empty.x"))?.write_all(include_bytes!(".empty.x"))?;

println!("cargo:rustc-link-search={}", out.display());
println!("cargo:rerun-if-changed=.memory.x");
println!("cargo:rerun-if-changed=.empty.x");
println!("cargo:rerun-if-changed=build.rs");
}

Ok(())
}
1 change: 1 addition & 0 deletions test-flip-link-app/examples/crash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use core::ptr;

use cortex_m_rt::entry;
#[cfg(feature = "default")]
Urhengulas marked this conversation as resolved.
Show resolved Hide resolved
use lm3s6965 as _;
use panic_semihosting as _;

Expand Down
1 change: 1 addition & 0 deletions test-flip-link-app/examples/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use cortex_m::{peripheral::syst::SystClkSource, Peripherals};
use cortex_m_rt::{entry, exception};
use cortex_m_semihosting::hprint;
#[cfg(feature = "default")]
use lm3s6965 as _;
use panic_semihosting as _;

Expand Down
1 change: 1 addition & 0 deletions test-flip-link-app/examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};
#[cfg(feature = "default")]
use lm3s6965 as _;
use panic_semihosting as _;

Expand Down
1 change: 1 addition & 0 deletions test-flip-link-app/examples/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![no_std]

use cortex_m_rt::entry;
#[cfg(feature = "default")]
use lm3s6965 as _;
use panic_semihosting as _;

Expand Down
22 changes: 16 additions & 6 deletions tests/verify.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
use std::fs;

use rstest::rstest;

/// Path to test app
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";

#[test]
fn should_link_example_firmware() -> anyhow::Result<()> {
#[rstest]
#[case::normal(true)]
#[case::custom_linkerscript(false)]
fn should_link_example_firmware(#[case] default_features: bool) -> anyhow::Result<()> {
Urhengulas marked this conversation as resolved.
Show resolved Hide resolved
// Arrange
cargo::check_flip_link();

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

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

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

// Assert
for elf_path in elf::paths() {
Expand Down Expand Up @@ -58,13 +62,19 @@ mod cargo {

/// Build all examples in `$REPO/$rel_path`
#[must_use]
pub fn build_example_firmware(rel_path: &str) -> Assert {
pub fn build_example_firmware(rel_path: &str, 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);

// disable default features or use `-v` as a no-op
let default_features = match default_features {
false => "--no-default-features",
true => "-v",
Urhengulas marked this conversation as resolved.
Show resolved Hide resolved
};

Command::new("cargo")
.args(&["build", "--examples"])
.args(&["build", "--examples", default_features])
.current_dir(firmware_dir)
.unwrap()
.assert()
Expand Down
5 changes: 4 additions & 1 deletion xtest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ mod cargo {
}

pub fn test() -> anyhow::Result<()> {
let status = Command::new("cargo").arg("test").status()?;
let status = Command::new("cargo")
// `--test-threads=1` prevents race conditions accessing the elf-file
.args(&["test", "--", "--test-threads=1"])
.status()?;
match status.success() {
false => Err(anyhow!("running `cargo test`")),
true => Ok(()),
Expand Down