Skip to content

Commit d364ac4

Browse files
authored
Merge pull request #229 from epage/cfg
fix(stream): Respect 'test' feature
2 parents ab37812 + ad3f458 commit d364ac4

File tree

2 files changed

+41
-64
lines changed

2 files changed

+41
-64
lines changed

crates/anstream/src/macros.rs renamed to crates/anstream/src/_macros.rs

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,9 @@
6060
#[macro_export]
6161
macro_rules! print {
6262
($($arg:tt)*) => {{
63-
if cfg!(any(feature = "test", test)) {
64-
use std::io::Write as _;
65-
66-
let stdio = std::io::stdout();
67-
let choice = $crate::AutoStream::choice(&stdio);
68-
let buffer = Vec::new();
69-
let mut stream = $crate::AutoStream::new(buffer, choice);
70-
// Ignore errors rather than panic
71-
let _ = ::std::write!(&mut stream, $($arg)*);
72-
let buffer = stream.into_inner();
73-
// Should be UTF-8 but not wanting to panic
74-
let buffer = String::from_utf8_lossy(&buffer);
63+
if cfg!(test) || $crate::_macros::FEATURE_TEST_ACTIVATED {
64+
let target_stream = std::io::stdout();
65+
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
7566
::std::print!("{}", buffer)
7667
} else {
7768
use std::io::Write as _;
@@ -141,18 +132,9 @@ macro_rules! println {
141132
$crate::print!("\n")
142133
};
143134
($($arg:tt)*) => {{
144-
if cfg!(any(feature = "test", test)) {
145-
use std::io::Write as _;
146-
147-
let stdio = std::io::stdout();
148-
let choice = $crate::AutoStream::choice(&stdio);
149-
let buffer = Vec::new();
150-
let mut stream = $crate::AutoStream::new(buffer, choice);
151-
// Ignore errors rather than panic
152-
let _ = ::std::write!(&mut stream, $($arg)*);
153-
let buffer = stream.into_inner();
154-
// Should be UTF-8 but not wanting to panic
155-
let buffer = String::from_utf8_lossy(&buffer);
135+
if cfg!(test) || $crate::_macros::FEATURE_TEST_ACTIVATED {
136+
let target_stream = std::io::stdout();
137+
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
156138
::std::println!("{}", buffer)
157139
} else {
158140
use std::io::Write as _;
@@ -201,26 +183,17 @@ macro_rules! println {
201183
#[macro_export]
202184
macro_rules! eprint {
203185
($($arg:tt)*) => {{
204-
if cfg!(any(feature = "test", test)) {
205-
use std::io::Write as _;
206-
207-
let stdio = std::io::stderr();
208-
let choice = $crate::AutoStream::choice(&stdio);
209-
let buffer = Vec::new();
210-
let mut stream = $crate::AutoStream::new(buffer, choice);
211-
// Ignore errors rather than panic
212-
let _ = ::std::write!(&mut stream, $($arg)*);
213-
let buffer = stream.into_inner();
214-
// Should be UTF-8 but not wanting to panic
215-
let buffer = String::from_utf8_lossy(&buffer);
186+
if cfg!(test) || $crate::_macros::FEATURE_TEST_ACTIVATED {
187+
let target_stream = std::io::stderr();
188+
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
216189
::std::eprint!("{}", buffer)
217190
} else {
218191
use std::io::Write as _;
219192

220193
let mut stream = $crate::stderr();
221194
match ::std::write!(&mut stream, $($arg)*) {
222195
Err(e) if e.kind() != ::std::io::ErrorKind::BrokenPipe => {
223-
::std::panic!("failed printing to stdout: {e}");
196+
::std::panic!("failed printing to stderr: {e}");
224197
}
225198
Err(_) | Ok(_) => {}
226199
}
@@ -264,26 +237,17 @@ macro_rules! eprintln {
264237
$crate::eprint!("\n")
265238
};
266239
($($arg:tt)*) => {{
267-
if cfg!(any(feature = "test", test)) {
268-
use std::io::Write as _;
269-
270-
let stdio = std::io::stderr();
271-
let choice = $crate::AutoStream::choice(&stdio);
272-
let buffer = Vec::new();
273-
let mut stream = $crate::AutoStream::new(buffer, choice);
274-
// Ignore errors rather than panic
275-
let _ = ::std::write!(&mut stream, $($arg)*);
276-
let buffer = stream.into_inner();
277-
// Should be UTF-8 but not wanting to panic
278-
let buffer = String::from_utf8_lossy(&buffer);
240+
if cfg!(test) || $crate::_macros::FEATURE_TEST_ACTIVATED {
241+
let target_stream = std::io::stderr();
242+
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
279243
::std::eprintln!("{}", buffer)
280244
} else {
281245
use std::io::Write as _;
282246

283247
let mut stream = $crate::stderr();
284248
match ::std::writeln!(&mut stream, $($arg)*) {
285249
Err(e) if e.kind() != ::std::io::ErrorKind::BrokenPipe => {
286-
::std::panic!("failed printing to stdout: {e}");
250+
::std::panic!("failed printing to stderr: {e}");
287251
}
288252
Err(_) | Ok(_) => {}
289253
}
@@ -373,17 +337,29 @@ macro_rules! panic {
373337
::std::panic!()
374338
};
375339
($($arg:tt)*) => {{
376-
use std::io::Write as _;
377-
378-
let panic_stream = std::io::stderr();
379-
let choice = $crate::AutoStream::choice(&panic_stream);
380-
let buffer = Vec::new();
381-
let mut stream = $crate::AutoStream::new(buffer, choice);
382-
// Ignore errors rather than panic
383-
let _ = ::std::write!(&mut stream, $($arg)*);
384-
let buffer = stream.into_inner();
385-
// Should be UTF-8 but not wanting to panic
386-
let buffer = String::from_utf8_lossy(&buffer).into_owned();
340+
let target_stream = std::io::stderr();
341+
let buffer = $crate::_macros::to_adapted_string(&format_args!($($arg)*), &target_stream);
387342
::std::panic!("{}", buffer)
388343
}};
389344
}
345+
346+
#[cfg(feature = "auto")]
347+
pub const FEATURE_TEST_ACTIVATED: bool = cfg!(feature = "test");
348+
349+
#[cfg(feature = "auto")]
350+
pub fn to_adapted_string(
351+
display: &dyn std::fmt::Display,
352+
stream: &impl crate::stream::RawStream,
353+
) -> String {
354+
use std::io::Write as _;
355+
356+
let choice = crate::AutoStream::choice(stream);
357+
let buffer = Vec::new();
358+
let mut stream = crate::AutoStream::new(buffer, choice);
359+
// Ignore errors rather than panic
360+
let _ = ::std::write!(&mut stream, "{display}");
361+
let buffer = stream.into_inner();
362+
// Should be UTF-8 but not wanting to panic
363+
let buffer = String::from_utf8_lossy(&buffer).into_owned();
364+
buffer
365+
}

crates/anstream/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@
3838

3939
pub mod adapter;
4040
pub mod stream;
41-
42-
mod buffer;
41+
#[doc(hidden)]
4342
#[macro_use]
44-
mod macros;
43+
pub mod _macros;
44+
4545
mod auto;
46+
mod buffer;
4647
mod fmt;
4748
mod strip;
4849
#[cfg(all(windows, feature = "wincon"))]

0 commit comments

Comments
 (0)