diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5b64511..b413d02 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -37,6 +37,7 @@ jobs: cargo build --profile=${{ matrix.profile }} cargo build --all-features --profile=${{ matrix.profile }} cargo test --profile=${{ matrix.profile }} + cargo test --no-default-features --features trace no_std_required --profile=${{ matrix.profile }} build-minimum: name: Build using minimum versions of dependencies runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index 6e78b25..1f7cd66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,15 +27,21 @@ tracing infrastructure before running tests. """ include = ["src/lib.rs", "LICENSE-*", "README.md", "CHANGELOG.md"] +[[test]] +name = "no_std_test" +path = "tests/no_std_required.rs" +required-features = ["trace"] + [[test]] name = "default_log_filter" path = "tests/default_log_filter.rs" required-features = ["log", "unstable"] [features] -default = ["log", "color"] +default = ["log", "color", "std"] +std = ["test-log-macros/std"] trace = ["dep:tracing-subscriber", "test-log-macros/trace"] -log = ["dep:env_logger", "test-log-macros/log", "tracing-subscriber?/tracing-log"] +log = ["dep:env_logger", "test-log-macros/log", "tracing-subscriber?/tracing-log", "std"] color = ["env_logger?/auto-color", "tracing-subscriber?/ansi"] # Enable unstable features. These are generally exempt from any semantic # versioning guarantees. @@ -45,7 +51,7 @@ unstable = ["test-log-macros/unstable"] members = ["macros"] [dependencies] -test-log-macros = {version = "0.2.15", path = "macros"} +test-log-macros = {version = "0.2.15", default-features = false, path = "macros"} tracing-subscriber = {version = "0.3.17", default-features = false, optional = true, features = ["env-filter", "fmt"]} env_logger = {version = "0.11", default-features = false, optional = true} diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 09aa829..095bbd4 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -14,6 +14,8 @@ Supporting procedural macro crate for test-log. proc-macro = true [features] +default = ["std"] +std = [] trace = [] log = [] unstable = [] diff --git a/macros/src/lib.rs b/macros/src/lib.rs index c78f9e6..2fde2ae 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -1,8 +1,15 @@ // Copyright (C) 2019-2023 Daniel Mueller // SPDX-License-Identifier: (Apache-2.0 OR MIT) +#![cfg_attr(not(feature = "std"), no_std)] + +extern crate alloc; extern crate proc_macro; +use alloc::string::String; +use alloc::vec; +use alloc::vec::Vec; + use proc_macro::TokenStream; use proc_macro2::TokenStream as Tokens; @@ -199,10 +206,17 @@ fn expand_tracing_init(attribute_args: &AttributeArgs) -> Tokens { let __internal_event_filter = { use ::test_log::tracing_subscriber::fmt::format::FmtSpan; - match ::std::env::var_os("RUST_LOG_SPAN_EVENTS") { + #[cfg(feature = "std")] + let env_var = match ::std::env::var("RUST_LOG_SPAN_EVENTS") { + Ok(val) => Some(val), + Err(::std::env::VarError::NotPresent) => None, + Err(::std::env::VarError::NotUnicode(os_string)) => panic!("RUST_LOG_SPAN_EVENTS must be unicode compliant, found {os_string:?}"), + }; + #[cfg(not(feature = "std"))] + let env_var = ::core::option_env!("RUST_LOG_SPAN_EVENTS"); + + match env_var { Some(mut value) => { - value.make_ascii_lowercase(); - let value = value.to_str().expect("test-log: RUST_LOG_SPAN_EVENTS must be valid UTF-8"); value .split(",") .map(|filter| match filter.trim() { diff --git a/src/lib.rs b/src/lib.rs index 59084b9..44b51f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: (Apache-2.0 OR MIT) #![deny(missing_docs)] +#![cfg_attr(not(feature = "std"), no_std)] //! A crate providing a replacement #[[macro@test]] attribute that //! initializes logging and/or tracing infrastructure before running diff --git a/tests/no_std_required.rs b/tests/no_std_required.rs new file mode 100644 index 0000000..5aba62d --- /dev/null +++ b/tests/no_std_required.rs @@ -0,0 +1,10 @@ +#![no_std] + +use test_log::test; +use tracing::debug; + +#[test] +fn no_std_required() { + debug!("It works without std!"); + assert_eq!(1 + 1, 2); +}