Skip to content

Commit

Permalink
fix(filesource): make some constructs only available via the test f…
Browse files Browse the repository at this point in the history
…eature
  • Loading branch information
rami3l committed May 7, 2024
1 parent c7f5367 commit 00f94f5
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/currentprocess/filesource.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::io::{self, BufRead, Cursor, Read, Result, Write};
use std::sync::{Arc, Mutex, MutexGuard};
use std::io::{self, BufRead, Read, Result, Write};

use enum_dispatch::enum_dispatch;

use super::terminalsource::{ColorableTerminal, StreamSelector};
use crate::currentprocess::process;

use super::terminalsource::{ColorableTerminal, StreamSelector};
#[cfg(feature = "test")]
use std::{
io::Cursor,
sync::{Arc, Mutex, MutexGuard},
};

/// Stand-in for std::io::Stdin
pub trait Stdin {
Expand Down Expand Up @@ -44,18 +48,22 @@ impl StdinSource for super::OSProcess {

// ----------------------- test support for stdin ------------------

#[cfg(feature = "test")]
struct TestStdinLock<'a> {
inner: MutexGuard<'a, Cursor<String>>,
}

#[cfg(feature = "test")]
impl StdinLock for TestStdinLock<'_> {}

#[cfg(feature = "test")]
impl Read for TestStdinLock<'_> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}

#[cfg(feature = "test")]
impl BufRead for TestStdinLock<'_> {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
self.inner.fill_buf()
Expand All @@ -65,10 +73,13 @@ impl BufRead for TestStdinLock<'_> {
}
}

#[cfg(feature = "test")]
pub(crate) type TestStdinInner = Arc<Mutex<Cursor<String>>>;

#[cfg(feature = "test")]
struct TestStdin(TestStdinInner);

#[cfg(feature = "test")]
impl Stdin for TestStdin {
fn lock(&self) -> Box<dyn StdinLock + '_> {
Box::new(TestStdinLock {
Expand Down Expand Up @@ -179,12 +190,15 @@ impl StderrSource for super::OSProcess {

// ----------------------- test support for writers ------------------

#[cfg(feature = "test")]
pub(super) struct TestWriterLock<'a> {
inner: MutexGuard<'a, Vec<u8>>,
}

#[cfg(feature = "test")]
impl WriterLock for TestWriterLock<'_> {}

#[cfg(feature = "test")]
impl Write for TestWriterLock<'_> {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.inner.write(buf)
Expand Down

0 comments on commit 00f94f5

Please sign in to comment.