| 1 | use std::env; |
| 2 | use std::fs::read_to_string; |
| 3 | use std::path::PathBuf; |
| 4 | use std::process::Command; |
| 5 | use test_utilities::*; |
| 6 | |
| 7 | struct TestDir { |
| 8 | paths: Vec<PathBuf>, |
| 9 | } |
| 10 | |
| 11 | impl Drop for TestDir { |
| 12 | fn drop(&mut self) { |
| 13 | for path in &self.paths { |
| 14 | cleanup_test_dir(path); |
| 15 | } |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | fn test_basic_generation() { |
| 21 | let test_dir = setup_test_dir(); |
| 22 | let dir_name = test_dir.file_name().unwrap().to_string_lossy(); |
| 23 | let parent = test_dir.parent().unwrap(); |
| 24 | let html_dir = parent.join(format!("{dir_name}_html")); |
| 25 | let gemini_dir = parent.join(format!("{dir_name}_gemini")); |
| 26 | |
| 27 | let _cleanup = TestDir { |
| 28 | paths: vec![test_dir.clone(), html_dir.clone(), gemini_dir.clone()], |
| 29 | }; |
| 30 | |
| 31 | // Create test input files |
| 32 | create_test_file( |
| 33 | &test_dir.join("_layout.html"), |
| 34 | "\ |
| 35 | <html> |
| 36 | <head><title>{{ title }}</title></head> |
| 37 | <body>{{ content }}</body> |
| 38 | </html> |
| 39 | ", |
| 40 | ); |
| 41 | create_test_file( |
| 42 | &test_dir.join("test.gmi"), |
| 43 | "\ |
| 44 | --- title: Page Is Cool! |
| 45 | # Test |
| 46 | Hello world |
| 47 | ", |
| 48 | ); |
| 49 | create_test_file(&test_dir.join("test.png"), "A picture of a cute cat"); |
| 50 | |
| 51 | // Run the program from the test directory |
| 52 | let status = Command::new(env!("CARGO_BIN_EXE_page")) |
| 53 | .current_dir(&test_dir) |
| 54 | .status() |
| 55 | .expect("Failed to execute command"); |
| 56 | |
| 57 | assert!(status.success()); |
| 58 | |
| 59 | assert!(html_dir.exists()); |
| 60 | assert!(gemini_dir.exists()); |
| 61 | |
| 62 | let html_output = html_dir.join("test.html"); |
| 63 | assert_file_contents( |
| 64 | &html_output, |
| 65 | "\ |
| 66 | <html> |
| 67 | <head><title>Page Is Cool!</title></head> |
| 68 | <body><section class=\"h1\"> |
| 69 | <h1> Test</h1> |
| 70 | <p>Hello world</p> |
| 71 | </section> |
| 72 | </body> |
| 73 | </html> |
| 74 | ", |
| 75 | ); |
| 76 | |
| 77 | let html_asset_output = html_dir.join("test.png"); |
| 78 | assert_file_contents(&html_asset_output, "A picture of a cute cat"); |
| 79 | |
| 80 | let gemini_output = gemini_dir.join("test.gmi"); |
| 81 | assert_file_contents( |
| 82 | &gemini_output, |
| 83 | "\ |
| 84 | # Test |
| 85 | Hello world |
| 86 | ", |
| 87 | ); |
| 88 | |
| 89 | let gemini_asset_output = gemini_dir.join("test.png"); |
| 90 | assert!(gemini_asset_output.exists()); |
| 91 | let gemini_asset_content = read_to_string(gemini_asset_output).unwrap(); |
| 92 | assert_eq!(gemini_asset_content, "A picture of a cute cat"); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn test_missing_layout() { |
| 97 | let test_dir = setup_test_dir(); |
| 98 | let dir_name = test_dir.file_name().unwrap().to_string_lossy(); |
| 99 | let parent = test_dir.parent().unwrap(); |
| 100 | let html_dir = parent.join(format!("{dir_name}_html")); |
| 101 | let gemini_dir = parent.join(format!("{dir_name}_gemini")); |
| 102 | |
| 103 | let _cleanup = TestDir { |
| 104 | paths: vec![test_dir.clone(), html_dir.clone(), gemini_dir.clone()], |
| 105 | }; |
| 106 | |
| 107 | // Create test input files |
| 108 | create_test_file( |
| 109 | &test_dir.join("test.gmi"), |
| 110 | "\ |
| 111 | --- title: Page Is Cool! |
| 112 | # Test |
| 113 | Hello world |
| 114 | ", |
| 115 | ); |
| 116 | create_test_file(&test_dir.join("test.png"), "A picture of a cute cat"); |
| 117 | |
| 118 | // Run the program from the test directory |
| 119 | let status = Command::new(env!("CARGO_BIN_EXE_page")) |
| 120 | .current_dir(&test_dir) |
| 121 | .status() |
| 122 | .expect("Failed to execute command"); |
| 123 | |
| 124 | assert!(!status.success()); |
| 125 | |
| 126 | assert!(!html_dir.exists()); |
| 127 | assert!(!gemini_dir.exists()); |
| 128 | } |