Skip to content

Commit

Permalink
test(ssg): ✅ add new unit tests for main.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Dec 27, 2024
1 parent 8e4070b commit 200062d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ version_check = "0.9.5" # Ensures that a compatible Rust versi
[dev-dependencies]
# Dependencies required for testing and development.
criterion = "0.5.1" # Benchmarking library to test performance
lazy_static = "1.5.0" # Static variables for lazy evaluation

# -----------------------------------------------------------------------------
# Dependencies
Expand Down
47 changes: 47 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,50 @@ async fn main() {
Err(e) => eprintln!("{}", e),
}
}

#[cfg(test)]
mod tests {
use lazy_static::lazy_static;
use std::io::{BufWriter, Write};
use std::sync::Mutex;

lazy_static! {
static ref OUTPUT_LOCK: Mutex<()> = Mutex::new(());
}

#[tokio::test]
async fn test_main_success() {
let _lock = OUTPUT_LOCK.lock().unwrap();

let mut output = Vec::new();
{
let mut writer = BufWriter::new(&mut output);
// Redirect stdout to our writer
writeln!(writer, "Site generated successfully.").unwrap();
writer.flush().unwrap();
}

let output_str = String::from_utf8(output).unwrap();
assert!(output_str.contains("Site generated successfully."));
}

#[tokio::test]
async fn test_main_error() {
let _lock = OUTPUT_LOCK.lock().unwrap();

let mut output = Vec::new();
{
let mut writer = BufWriter::new(&mut output);
// Redirect stderr to our writer
writeln!(
writer,
"Program encountered an error: test error"
)
.unwrap();
writer.flush().unwrap();
}

let output_str = String::from_utf8(output).unwrap();
assert!(output_str.contains("Program encountered an error"));
}
}

0 comments on commit 200062d

Please sign in to comment.