Skip to content

Commit

Permalink
add a test
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Brue <[email protected]>
  • Loading branch information
ryanabx committed Aug 20, 2024
1 parent ebd2800 commit c603dcf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use walkdir::WalkDir;

use clap::Parser;

#[cfg(test)]
mod tests;
mod utils;

/// Djot static site generator
Expand All @@ -29,6 +31,10 @@ fn main() -> anyhow::Result<()> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
log::trace!("Begin djot-ssg::main()");
let args = ConsoleArgs::parse();
run_program(args)
}

fn run_program(args: ConsoleArgs) -> anyhow::Result<()> {
let output_path = args
.output_path
.unwrap_or(env::current_dir()?.join("output"));
Expand Down Expand Up @@ -103,9 +109,3 @@ fn generate_site(target_path: &Path, output_path: &Path, no_warn: bool) -> anyho
}
Ok(())
}

#[cfg(test)]
mod test {
#[test]
fn generate_sample_site() {}
}
53 changes: 53 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::{
env::temp_dir,
fs::{create_dir_all, remove_dir_all, File},
io::Write,
};

use crate::ConsoleArgs;

#[test]
fn site_warn_without_index() -> anyhow::Result<()> {
let temp_dir = temp_dir();
// Perform test with catch
let res: anyhow::Result<()> = (|| {
create_dir_all(&temp_dir.join("target/nested"))?;
let mut djot_file_1 = File::create(&temp_dir.join("target/nested/example.dj"))?;
write!(
djot_file_1,
"# Hey everyone!\n\nThis is an example djot file!\n\n> Hey what's up"
)?;
djot_file_1.flush()?;

let mut djot_file_2 = File::create(&temp_dir.join("target/example2.dj"))?;
write!(
djot_file_2,
"# Hey everyone!\n\nThis is another example djot file!\n\n> Hey what's up!!"
)?;
djot_file_2.flush()?;

let args = ConsoleArgs {
target_path: temp_dir.join("target"),
output_path: Some(temp_dir.join("output")),
clean: false,
no_warn: true,
};
crate::run_program(args)?;
Ok(())
})();
let _ = remove_dir_all(&temp_dir);
match res {
Ok(()) => {
// This should have errored out
Err(anyhow::anyhow!("This should have errored out"))
}
Err(e) => {
// Ok(())
if e.to_string() == "index.{dj|djot} not found! consider creating one in the base target directory as the default page.".to_string() {
Ok(())
} else {
Err(e)
}
}
}
}

0 comments on commit c603dcf

Please sign in to comment.