Skip to content

Commit

Permalink
Enable test-only code via feature instead of target_arch.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Nov 30, 2020
1 parent 0bca4ef commit 5025315
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
shell: bash
- name: Run tests on Ubuntu/Windows
if: matrix.os != 'macOS-latest'
run: cargo test --workspace
run: cargo test --workspace --features internal-testing
- name: Run tests on macOS
# NOTE defmt does not build for macOS because its `cortex-m-rt` dependency doesn't
# (see https://github.com/rust-embedded/cortex-m-rt/issues/74), so we cannot use
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ version = "0.1.2"
[features]
alloc = []

# WARNING: for internal use only, not covered by semver guarantees
internal-testing = ["defmt-macros/internal-testing"]

[dependencies]
defmt-macros = { path = "macros", version = "0.1.1" }
heapless = "0.5.6"
Expand Down
4 changes: 4 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ version = "0.1.1"
[lib]
proc-macro = true

[features]
# WARNING: for internal use only, not covered by semver guarantees
internal-testing = []

[dependencies]
defmt-parser = { path = "../parser", features = ["unstable"], version = "0.1.0" }
quote = "1.0.7"
Expand Down
8 changes: 4 additions & 4 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,11 +924,11 @@ pub fn internp(ts: TokenStream) -> TokenStream {
let section = format!(".defmt.prim.{}", sym);

quote!(match () {
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
() => {
defmt::export::fetch_add_string_index() as u8
}
#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "internal-testing"))]
() => {
#[link_section = #section]
#[export_name = #sym]
Expand Down Expand Up @@ -992,11 +992,11 @@ fn mksym(string: &str, tag: &str, is_log_statement: bool) -> TokenStream2 {
format_ident!("S")
};
quote!(match () {
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
() => {
defmt::export::fetch_add_string_index()
}
#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "internal-testing"))]
() => {
#[link_section = #section]
#[export_name = #sym]
Expand Down
22 changes: 11 additions & 11 deletions src/export.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{Formatter, Str};

#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
thread_local! {
static I: core::sync::atomic::AtomicU8 =
core::sync::atomic::AtomicU8::new(0);
Expand All @@ -11,23 +11,23 @@ thread_local! {
// NOTE we limit these values to 7-bit to avoid LEB128 encoding while writing the expected answers
// in unit tests
/// For testing purposes
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
pub fn fetch_string_index() -> u8 {
I.with(|i| i.load(core::sync::atomic::Ordering::Relaxed)) & 0x7f
}

/// For testing purposes
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
pub fn fetch_add_string_index() -> usize {
(I.with(|i| i.fetch_add(1, core::sync::atomic::Ordering::Relaxed)) & 0x7f) as usize
}

#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
pub fn acquire() -> Option<Formatter> {
None
}

#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "internal-testing"))]
#[inline(never)]
pub fn acquire() -> Option<Formatter> {
extern "Rust" {
Expand All @@ -36,10 +36,10 @@ pub fn acquire() -> Option<Formatter> {
unsafe { _defmt_acquire() }
}

#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
pub fn release(_: Formatter) {}

#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "internal-testing"))]
#[inline(never)]
pub fn release(fmt: Formatter) {
extern "Rust" {
Expand All @@ -49,12 +49,12 @@ pub fn release(fmt: Formatter) {
}

/// For testing purposes
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
pub fn timestamp() -> u64 {
(T.with(|i| i.fetch_add(1, core::sync::atomic::Ordering::Relaxed)) & 0x7f) as u64
}

#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "internal-testing"))]
pub fn timestamp() -> u64 {
extern "Rust" {
fn _defmt_timestamp() -> u64;
Expand Down Expand Up @@ -191,12 +191,12 @@ pub fn into_result<T: sealed::IntoResult>(x: T) -> Result<T::Ok, T::Error> {
}

/// For testing purposes
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
pub fn panic() -> ! {
panic!()
}

#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "internal-testing"))]
pub fn panic() -> ! {
extern "Rust" {
fn _defmt_panic() -> !;
Expand Down
2 changes: 1 addition & 1 deletion src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
use crate as defmt;
use defmt_macros::internp;

Expand Down
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! with an incompatible version will result in an error. This means that you have to update both
//! the host and target side if a breaking change in defmt is released.
#![cfg_attr(not(target_arch = "x86_64"), no_std)]
#![cfg_attr(not(feature = "internal-testing"), no_std)]
#![warn(missing_docs)]

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -304,9 +304,9 @@ pub struct Str {

/// Handle to a defmt logger.
pub struct Formatter {
#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "internal-testing"))]
writer: NonNull<dyn Write>,
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
bytes: Vec<u8>,
bool_flags: u8, // the current group of consecutive bools
bools_left: u8, // the number of bits that we can still set in bool_flag
Expand All @@ -327,7 +327,7 @@ const MAX_NUM_BOOL_FLAGS: u8 = 8;
#[doc(hidden)]
impl Formatter {
/// Only for testing on x86_64
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
pub fn new() -> Self {
Self {
bytes: vec![],
Expand All @@ -339,23 +339,23 @@ impl Formatter {
}

/// Only for testing on x86_64
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
pub fn bytes(&self) -> &[u8] {
&self.bytes
}

#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
pub fn write(&mut self, bytes: &[u8]) {
self.bytes.extend_from_slice(bytes)
}

#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "internal-testing"))]
pub fn write(&mut self, bytes: &[u8]) {
unsafe { self.writer.as_mut().write(bytes) }
}

/// Implementation detail
#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "internal-testing"))]
pub unsafe fn from_raw(writer: NonNull<dyn Write>) -> Self {
Self {
writer,
Expand All @@ -367,7 +367,7 @@ impl Formatter {
}

/// Implementation detail
#[cfg(not(target_arch = "x86_64"))]
#[cfg(not(feature = "internal-testing"))]
pub unsafe fn into_raw(self) -> NonNull<dyn Write> {
self.writer
}
Expand Down Expand Up @@ -574,7 +574,7 @@ impl Formatter {
// these need to be in a separate module or `unreachable!` will end up calling `defmt::panic` and
// this will not compile
// (using `core::unreachable!` instead of `unreachable!` doesn't help)
#[cfg(target_arch = "x86_64")]
#[cfg(feature = "internal-testing")]
mod x86_64 {
use core::ptr::NonNull;

Expand Down

0 comments on commit 5025315

Please sign in to comment.