Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: better test

+27 -67
+13 -10
crates/maudit/src/assets.rs
··· 533 533 534 534 #[cfg(test)] 535 535 mod tests { 536 - use super::*; 536 + use std::path::PathBuf; 537 + 538 + use crate::{ 539 + AssetHashingStrategy, 540 + assets::{ 541 + Asset, ImageFormat, ImageOptions, RouteAssets, RouteAssetsOptions, StyleOptions, 542 + make_filename, 543 + }, 544 + }; 537 545 538 546 fn setup_temp_dir() -> tempfile::TempDir { 539 547 let temp_dir = tempfile::tempdir().unwrap(); ··· 679 687 let temp_dir = setup_temp_dir(); 680 688 let image_path = temp_dir.path().join("image.png"); 681 689 682 - // Create a simple test PNG (1x1 transparent pixel) 683 - let png_data = [ 684 - 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 685 - 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 686 - 0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0B, 0x49, 0x44, 0x41, 0x54, 0x78, 687 - 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 688 - 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82, 689 - ]; 690 - std::fs::write(&image_path, png_data).unwrap(); 690 + let img = image::ImageBuffer::<image::Rgba<u8>, _>::from_fn(1, 1, |_x, _y| { 691 + image::Rgba([255, 0, 0, 255]) 692 + }); 693 + img.save(&image_path).unwrap(); 691 694 692 695 let mut page_assets = RouteAssets::new( 693 696 &RouteAssetsOptions {
+14 -57
crates/maudit/src/assets/image.rs
··· 527 527 528 528 #[cfg(test)] 529 529 mod tests { 530 + use crate::errors::AssetError; 531 + 530 532 use super::*; 531 - use std::path::PathBuf; 533 + use std::{error::Error, path::PathBuf}; 532 534 533 535 #[test] 534 536 fn test_placeholder_with_missing_file() { ··· 536 538 537 539 let result = get_placeholder(&nonexistent_path, None); 538 540 539 - // Should return an error, not panic 540 541 assert!(result.is_err()); 541 - 542 - if let Err(crate::errors::AssetError::ImageLoadFailed { path, .. }) = result { 542 + if let Err(AssetError::ImageLoadFailed { path, .. }) = result { 543 543 assert_eq!(path, nonexistent_path); 544 544 } else { 545 545 panic!("Expected ImageLoadFailed error"); ··· 547 547 } 548 548 549 549 #[test] 550 - fn test_placeholder_with_invalid_image_data() { 550 + fn test_placeholder_with_valid_image() { 551 551 let temp_dir = tempfile::tempdir().unwrap(); 552 + let image_path = temp_dir.path().join("test.png"); 552 553 553 - // Create a file with invalid image data 554 - let invalid_image_path = temp_dir.path().join("invalid.png"); 555 - std::fs::write(&invalid_image_path, b"This is not a valid PNG file").unwrap(); 556 - 557 - let result = get_placeholder(&invalid_image_path, None); 554 + // Create a minimal valid 1x1 PNG file using the image crate to ensure correct CRCs 555 + let img = image::ImageBuffer::<image::Rgba<u8>, _>::from_fn(1, 1, |_x, _y| { 556 + image::Rgba([255, 0, 0, 255]) 557 + }); 558 + img.save(&image_path).unwrap(); 558 559 559 - // Should return an error, not panic 560 - assert!(result.is_err()); 560 + let result = get_placeholder(&image_path, None); 561 561 562 - if let Err(crate::errors::AssetError::ImageLoadFailed { path, .. }) = result { 563 - assert_eq!(path, invalid_image_path); 564 - } else { 565 - panic!("Expected ImageLoadFailed error"); 562 + if let Err(e) = &result { 563 + eprintln!("get_placeholder failed: {:?}", e.source()); 566 564 } 567 565 568 - // Cleanup 569 - std::fs::remove_file(&invalid_image_path).ok(); 570 - } 571 - 572 - #[test] 573 - fn test_placeholder_with_valid_image() { 574 - use std::path::Path; 575 - 576 - // Try to find an existing image in the examples directory 577 - let project_root = Path::new(env!("CARGO_MANIFEST_DIR")) 578 - .parent() 579 - .unwrap() 580 - .parent() 581 - .unwrap(); 582 - let test_image = project_root.join("examples/image-processing/images/walrus.jpg"); 583 - 584 - // Skip test if the image doesn't exist (e.g., in CI without examples) 585 - if !test_image.exists() { 586 - eprintln!("Skipping test: test image not found at {:?}", test_image); 587 - return; 588 - } 589 - 590 - let result = get_placeholder(&test_image, None); 591 - 592 - // Should succeed 593 566 assert!(result.is_ok()); 594 - 595 567 let placeholder = result.unwrap(); 596 - // Verify the placeholder has a thumbhash 597 568 assert!(!placeholder.thumbhash.is_empty()); 598 569 assert!(!placeholder.thumbhash_base64.is_empty()); 599 - } 600 - 601 - #[test] 602 - fn test_placeholder_with_empty_file() { 603 - let temp_dir = tempfile::tempdir().unwrap(); 604 - 605 - // Create an empty file 606 - let empty_file_path = temp_dir.path().join("empty.png"); 607 - std::fs::write(&empty_file_path, b"").unwrap(); 608 - 609 - let result = get_placeholder(&empty_file_path, None); 610 - 611 - // Should return an error for empty/invalid image 612 - assert!(result.is_err()); 613 570 } 614 571 }