Use the correct stderr when testing libstd
This commit is contained in:
parent
350674b718
commit
c0e8cf9410
4 changed files with 41 additions and 19 deletions
|
@ -165,6 +165,20 @@ impl<B: BufRead + ?Sized> BufRead for Box<B> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Used by panicking::default_hook
|
||||||
|
#[cfg(test)]
|
||||||
|
/// This impl is only used by printing logic, so any error returned is always
|
||||||
|
/// of kind `Other`, and should be ignored.
|
||||||
|
impl Write for Box<dyn (::realstd::io::Write) + Send> {
|
||||||
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||||
|
(**self).write(buf).map_err(|_| ErrorKind::Other.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> io::Result<()> {
|
||||||
|
(**self).flush().map_err(|_| ErrorKind::Other.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// In-memory buffer implementations
|
// In-memory buffer implementations
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![cfg_attr(test, allow(unused))]
|
||||||
|
|
||||||
use crate::io::prelude::*;
|
use crate::io::prelude::*;
|
||||||
|
|
||||||
use crate::cell::RefCell;
|
use crate::cell::RefCell;
|
||||||
|
@ -16,6 +18,13 @@ thread_local! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Stderr used by eprint! and eprintln! macros, and panics
|
||||||
|
thread_local! {
|
||||||
|
static LOCAL_STDERR: RefCell<Option<Box<dyn Write + Send>>> = {
|
||||||
|
RefCell::new(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A handle to a raw instance of the standard input stream of this process.
|
/// A handle to a raw instance of the standard input stream of this process.
|
||||||
///
|
///
|
||||||
/// This handle is not synchronized or buffered in any fashion. Constructed via
|
/// This handle is not synchronized or buffered in any fashion. Constructed via
|
||||||
|
@ -668,7 +677,6 @@ impl fmt::Debug for StderrLock<'_> {
|
||||||
issue = "0")]
|
issue = "0")]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn set_panic(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write + Send>> {
|
pub fn set_panic(sink: Option<Box<dyn Write + Send>>) -> Option<Box<dyn Write + Send>> {
|
||||||
use crate::panicking::LOCAL_STDERR;
|
|
||||||
use crate::mem;
|
use crate::mem;
|
||||||
LOCAL_STDERR.with(move |slot| {
|
LOCAL_STDERR.with(move |slot| {
|
||||||
mem::replace(&mut *slot.borrow_mut(), sink)
|
mem::replace(&mut *slot.borrow_mut(), sink)
|
||||||
|
@ -740,6 +748,7 @@ where
|
||||||
reason = "implementation detail which may disappear or be replaced at any time",
|
reason = "implementation detail which may disappear or be replaced at any time",
|
||||||
issue = "0")]
|
issue = "0")]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
#[cfg(not(test))]
|
||||||
pub fn _print(args: fmt::Arguments) {
|
pub fn _print(args: fmt::Arguments) {
|
||||||
print_to(args, &LOCAL_STDOUT, stdout, "stdout");
|
print_to(args, &LOCAL_STDOUT, stdout, "stdout");
|
||||||
}
|
}
|
||||||
|
@ -748,11 +757,14 @@ pub fn _print(args: fmt::Arguments) {
|
||||||
reason = "implementation detail which may disappear or be replaced at any time",
|
reason = "implementation detail which may disappear or be replaced at any time",
|
||||||
issue = "0")]
|
issue = "0")]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
#[cfg(not(test))]
|
||||||
pub fn _eprint(args: fmt::Arguments) {
|
pub fn _eprint(args: fmt::Arguments) {
|
||||||
use crate::panicking::LOCAL_STDERR;
|
|
||||||
print_to(args, &LOCAL_STDERR, stderr, "stderr");
|
print_to(args, &LOCAL_STDERR, stderr, "stderr");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub use realstd::io::{_eprint, _print};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::panic::{UnwindSafe, RefUnwindSafe};
|
use crate::panic::{UnwindSafe, RefUnwindSafe};
|
||||||
|
|
|
@ -219,7 +219,7 @@
|
||||||
// std may use features in a platform-specific way
|
// std may use features in a platform-specific way
|
||||||
#![allow(unused_features)]
|
#![allow(unused_features)]
|
||||||
|
|
||||||
#![cfg_attr(test, feature(test, update_panic_count))]
|
#![cfg_attr(test, feature(print_internals, set_stdio, test, update_panic_count))]
|
||||||
#![cfg_attr(all(target_vendor = "fortanix", target_env = "sgx"),
|
#![cfg_attr(all(target_vendor = "fortanix", target_env = "sgx"),
|
||||||
feature(global_asm, range_contains, slice_index_methods,
|
feature(global_asm, range_contains, slice_index_methods,
|
||||||
decl_macro, coerce_unsized, sgx_platform, ptr_wrapping_offset_from))]
|
decl_macro, coerce_unsized, sgx_platform, ptr_wrapping_offset_from))]
|
||||||
|
|
|
@ -7,13 +7,9 @@
|
||||||
//! * Executing a panic up to doing the actual implementation
|
//! * Executing a panic up to doing the actual implementation
|
||||||
//! * Shims around "try"
|
//! * Shims around "try"
|
||||||
|
|
||||||
use core::panic::BoxMeUp;
|
use core::panic::{BoxMeUp, PanicInfo, Location};
|
||||||
use core::panic::{PanicInfo, Location};
|
|
||||||
|
|
||||||
use crate::io::prelude::*;
|
|
||||||
|
|
||||||
use crate::any::Any;
|
use crate::any::Any;
|
||||||
use crate::cell::RefCell;
|
|
||||||
use crate::fmt;
|
use crate::fmt;
|
||||||
use crate::intrinsics;
|
use crate::intrinsics;
|
||||||
use crate::mem;
|
use crate::mem;
|
||||||
|
@ -25,11 +21,12 @@ use crate::sys_common::thread_info;
|
||||||
use crate::sys_common::util;
|
use crate::sys_common::util;
|
||||||
use crate::thread;
|
use crate::thread;
|
||||||
|
|
||||||
thread_local! {
|
#[cfg(not(test))]
|
||||||
pub static LOCAL_STDERR: RefCell<Option<Box<dyn Write + Send>>> = {
|
use crate::io::set_panic;
|
||||||
RefCell::new(None)
|
// make sure to use the stderr output configured
|
||||||
}
|
// by libtest in the real copy of std
|
||||||
}
|
#[cfg(test)]
|
||||||
|
use realstd::io::set_panic;
|
||||||
|
|
||||||
// Binary interface to the panic runtime that the standard library depends on.
|
// Binary interface to the panic runtime that the standard library depends on.
|
||||||
//
|
//
|
||||||
|
@ -205,12 +202,11 @@ fn default_hook(info: &PanicInfo) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(mut local) = LOCAL_STDERR.with(|s| s.borrow_mut().take()) {
|
if let Some(mut local) = set_panic(None) {
|
||||||
write(&mut *local);
|
// NB. In `cfg(test)` this uses the forwarding impl
|
||||||
let mut s = Some(local);
|
// for `Box<dyn (::realstd::io::Write) + Send>`.
|
||||||
LOCAL_STDERR.with(|slot| {
|
write(&mut local);
|
||||||
*slot.borrow_mut() = s.take();
|
set_panic(Some(local));
|
||||||
});
|
|
||||||
} else if let Some(mut out) = panic_output() {
|
} else if let Some(mut out) = panic_output() {
|
||||||
write(&mut out);
|
write(&mut out);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue