Skip to content

Commit

Permalink
test(frontmatter-gen): ✅ enhance test_validate_command_all_fields_pre…
Browse files Browse the repository at this point in the history
…sent with temporary file handling
  • Loading branch information
sebastienrousseau committed Nov 17, 2024
1 parent 5652e19 commit b2fcc55
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ log = { version = "0.4.22", optional = true }
clap = { version = "4.5.21", features = ["derive"], optional = true }
dtt = { version = "0.0.8", optional = true }
pulldown-cmark = { version = "0.12.2", optional = true }
tempfile = "3.14.0"
tera = { version = "1.20.0", optional = true }
tokio = { version = "1.41.1", features = ["full"], optional = true }
url = { version = "2.5.3", optional = true }
Expand All @@ -124,7 +125,7 @@ version_check = "0.9.5"
[dev-dependencies]
criterion = "0.5.1"
pretty_assertions = "1.4.1"
tempfile = "3.14.0"


# -----------------------------------------------------------------------------
# Examples
Expand Down
25 changes: 21 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ use anyhow::{Context, Result};
use clap::{Arg, Command};
use frontmatter_gen::{engine::Engine, to_format, Config, Format};
use serde::Deserialize;
use std::fs;
use std::path::{Path, PathBuf};
use std::{
fs,
path::{Path, PathBuf},
};
use thiserror::Error;

/// Custom error types for front matter validation.
Expand Down Expand Up @@ -397,6 +399,20 @@ async fn build_command(
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;

/// Helper function to test `validate_command` with direct content.
async fn validate_with_content(
content: &str,
required_fields: &[&str],
) -> Result<()> {
// Create a temporary file
let mut temp_file = tempfile::NamedTempFile::new()?;
temp_file.write_all(content.as_bytes())?;

// Call the validate_command function with the path of the temporary file
validate_command(temp_file.path(), required_fields).await
}

#[tokio::test]
async fn test_validate_command_all_fields_present() {
Expand All @@ -409,8 +425,9 @@ author: "Jane Doe"
// Convert Vec<String> to Vec<&str>
let required_fields = vec!["title", "date", "author"];

// Run the validate_command function
let result = validate_command(content, &required_fields).await;
// Run the helper function with content
let result =
validate_with_content(content, &required_fields).await;

// Debugging: Check the result of the validation
if let Err(e) = &result {
Expand Down

0 comments on commit b2fcc55

Please sign in to comment.