Skip to content

Commit

Permalink
fix(stream): Respect 'test' feature
Browse files Browse the repository at this point in the history
We were putting it into the generated code, not our code, causing it to
be an "unexpected cfg".

It would be nice to use the macro's cfgs in the generated code...
  • Loading branch information
epage committed Nov 4, 2024
1 parent a5daf82 commit a5e20ad
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions crates/anstream/src/_macros.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
#[doc = include_str!("print.md")]
#[cfg(feature = "auto")]
#[cfg(feature = "test")]
#[macro_export]
macro_rules! print {
($($arg:tt)*) => {{
if cfg!(any(feature = "test", test)) {
let target_stream = std::io::stdout();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::print!("{}", buffer)
}};
}

#[doc = include_str!("print.md")]
#[cfg(feature = "auto")]
#[cfg(not(feature = "test"))]
#[macro_export]
macro_rules! print {
($($arg:tt)*) => {{
if cfg!(test) {
let target_stream = std::io::stdout();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::print!("{}", buffer)
Expand All @@ -23,13 +36,29 @@ macro_rules! print {

#[doc = include_str!("println.md")]
#[cfg(feature = "auto")]
#[cfg(feature = "test")]
#[macro_export]
macro_rules! println {
() => {
$crate::print!("\n")
};
($($arg:tt)*) => {{
let target_stream = std::io::stdout();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::println!("{}", buffer)
}};
}

#[doc = include_str!("println.md")]
#[cfg(feature = "auto")]
#[cfg(not(feature = "test"))]
#[macro_export]
macro_rules! println {
() => {
$crate::print!("\n")
};
($($arg:tt)*) => {{
if cfg!(any(feature = "test", test)) {
if cfg!(test) {
let target_stream = std::io::stdout();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::println!("{}", buffer)
Expand All @@ -49,10 +78,23 @@ macro_rules! println {

#[doc = include_str!("eprint.md")]
#[cfg(feature = "auto")]
#[cfg(feature = "test")]
#[macro_export]
macro_rules! eprint {
($($arg:tt)*) => {{
let target_stream = std::io::stderr();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::eprint!("{}", buffer)
}};
}

#[doc = include_str!("eprint.md")]
#[cfg(feature = "auto")]
#[cfg(not(feature = "test"))]
#[macro_export]
macro_rules! eprint {
($($arg:tt)*) => {{
if cfg!(any(feature = "test", test)) {
if cfg!(test) {
let target_stream = std::io::stderr();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::eprint!("{}", buffer)
Expand All @@ -72,13 +114,29 @@ macro_rules! eprint {

#[doc = include_str!("eprintln.md")]
#[cfg(feature = "auto")]
#[cfg(feature = "test")]
#[macro_export]
macro_rules! eprintln {
() => {
$crate::eprint!("\n")
};
($($arg:tt)*) => {{
let target_stream = std::io::stderr();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::eprintln!("{}", buffer)
}};
}

#[doc = include_str!("eprintln.md")]
#[cfg(feature = "auto")]
#[cfg(not(feature = "test"))]
#[macro_export]
macro_rules! eprintln {
() => {
$crate::eprint!("\n")
};
($($arg:tt)*) => {{
if cfg!(any(feature = "test", test)) {
if cfg!(test) {
let target_stream = std::io::stderr();
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
::std::eprintln!("{}", buffer)
Expand Down

0 comments on commit a5e20ad

Please sign in to comment.