Skip to content

Commit

Permalink
Added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaslyang authored and nicholaslyang committed Aug 14, 2023
1 parent c6c7bcb commit 984f99c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
14 changes: 14 additions & 0 deletions crates/turborepo-ui/src/color_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,17 @@ impl ColorSelector {
style.apply_to(format!("{}: ", prefix)).to_string()
}
}

#[cfg(test)]
mod tests {

#[test]
fn test_color_selector() {
let mut selector = super::ColorSelector::default();
let color1 = selector.color_for_key("key1");
let color2 = selector.color_for_key("key2");
let color3 = selector.color_for_key("key1");
assert_eq!(color1, color3);
assert_ne!(color1, color2);
}
}
42 changes: 40 additions & 2 deletions crates/turborepo-ui/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,55 @@ pub fn replay_logs<W: Write>(

#[cfg(test)]
mod tests {
use std::fs;
use std::{fs, io::Write};

use anyhow::Result;
use tempfile::tempdir;
use turbopath::AbsoluteSystemPathBuf;

use crate::{
logs::{replay_logs, PrefixedUI},
BOLD, CYAN, UI,
LogWriter, PrefixedWriter, BOLD, CYAN, UI,
};

#[test]
fn test_log_writer() -> Result<()> {
let dir = tempdir()?;
let log_file_path = AbsoluteSystemPathBuf::try_from(dir.path().join("test.txt"))?;
let mut prefixed_writer_output = Vec::new();
let mut log_writer = LogWriter::default();
let ui = UI::new(false);

log_writer.with_log_file(&log_file_path)?;
log_writer.with_prefixed_writer(PrefixedWriter::new(
ui,
CYAN.apply_to(">".to_string()),
&mut prefixed_writer_output,
));

writeln!(log_writer, "one fish")?;
writeln!(log_writer, "two fish")?;
writeln!(log_writer, "red fish")?;
writeln!(log_writer, "blue fish")?;

log_writer.flush()?;

assert_eq!(
String::from_utf8(prefixed_writer_output)?,
"\u{1b}[36m>\u{1b}[0mone fish\n\u{1b}[36m>\u{1b}[0mtwo fish\n\u{1b}[36m>\u{1b}[0mred \
fish\n\u{1b}[36m>\u{1b}[0mblue fish\n"
);

let log_file_contents = log_file_path.read_to_string()?;

assert_eq!(
log_file_contents,
"one fish\ntwo fish\nred fish\nblue fish\n"
);

Ok(())
}

#[test]
fn test_replay_logs() -> Result<()> {
let ui = UI::new(false);
Expand Down
24 changes: 19 additions & 5 deletions crates/turborepo-ui/src/prefixed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{fmt::Display, io::Write};
use std::{
fmt::{Debug, Display, Formatter},
io::Write,
};

use console::StyledObject;
use tracing::error;
Expand Down Expand Up @@ -64,18 +67,29 @@ impl<W: Write> PrefixedUI<W> {
pub struct PrefixedWriter<W> {
prefix: StyledObject<String>,
writer: W,
ui: UI,
}

impl<W> Debug for PrefixedWriter<W> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PrefixedWriter")
.field("prefix", &self.prefix)
.field("ui", &self.ui)
.finish()
}
}

impl<W: Write> PrefixedWriter<W> {
pub fn new(prefix: StyledObject<String>, writer: W) -> Self {
Self { prefix, writer }
pub fn new(ui: UI, prefix: StyledObject<String>, writer: W) -> Self {
Self { ui, prefix, writer }
}
}

impl<W: Write> Write for PrefixedWriter<W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let prefix = self.prefix.to_string();
self.writer.write(prefix.as_bytes())?;
let prefix = self.prefix.clone();
let prefix = self.ui.apply(prefix);
self.writer.write(prefix.to_string().as_bytes())?;
self.writer.write(buf)
}

Expand Down

0 comments on commit 984f99c

Please sign in to comment.