]>
Commit | Line | Data |
---|---|---|
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!("{}_html", dir_name)); | |
25 | let gemini_dir = parent.join(format!("{}_gemini", dir_name)); | |
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(&test_dir.join("_layout.html"), "\ | |
33 | <html> | |
34 | <head><title>{{ title }}</title></head> | |
35 | <body>{{ content }}</body> | |
36 | </html> | |
37 | "); | |
38 | create_test_file(&test_dir.join("test.gmi"), "\ | |
39 | --- title: Page Is Cool! | |
40 | # Test | |
41 | Hello world | |
42 | "); | |
43 | create_test_file(&test_dir.join("test.png"), "A picture of a cute cat"); | |
44 | ||
45 | // Run the program from the test directory | |
46 | let status = Command::new(env!("CARGO_BIN_EXE_page")) | |
47 | .current_dir(&test_dir) | |
48 | .status() | |
49 | .expect("Failed to execute command"); | |
50 | ||
51 | assert!(status.success()); | |
52 | ||
53 | assert!(html_dir.exists()); | |
54 | assert!(gemini_dir.exists()); | |
55 | ||
56 | let html_output = html_dir.join("test.html"); | |
57 | assert_file_contents(&html_output, "\ | |
58 | <html> | |
59 | <head><title>Page Is Cool!</title></head> | |
60 | <body><section class=\"h1\"> | |
61 | <h1> Test</h1> | |
62 | <p>Hello world</p> | |
63 | </section> | |
64 | </body> | |
65 | </html> | |
66 | "); | |
67 | ||
68 | let html_asset_output = html_dir.join("test.png"); | |
69 | assert_file_contents(&html_asset_output, "A picture of a cute cat"); | |
70 | ||
71 | let gemini_output = gemini_dir.join("test.gmi"); | |
72 | assert_file_contents(&gemini_output, "\ | |
73 | # Test | |
74 | Hello world | |
75 | "); | |
76 | ||
77 | let gemini_asset_output = gemini_dir.join("test.png"); | |
78 | assert!(gemini_asset_output.exists()); | |
79 | let gemini_asset_content = read_to_string(gemini_asset_output).unwrap(); | |
80 | assert_eq!(gemini_asset_content, "A picture of a cute cat"); | |
81 | } | |
82 | ||
83 | #[test] | |
84 | fn test_missing_layout() { | |
85 | let test_dir = setup_test_dir(); | |
86 | let dir_name = test_dir.file_name().unwrap().to_string_lossy(); | |
87 | let parent = test_dir.parent().unwrap(); | |
88 | let html_dir = parent.join(format!("{}_html", dir_name)); | |
89 | let gemini_dir = parent.join(format!("{}_gemini", dir_name)); | |
90 | ||
91 | let _cleanup = TestDir { | |
92 | paths: vec![test_dir.clone(), html_dir.clone(), gemini_dir.clone()] | |
93 | }; | |
94 | ||
95 | // Create test input files | |
96 | create_test_file(&test_dir.join("test.gmi"), "\ | |
97 | --- title: Page Is Cool! | |
98 | # Test | |
99 | Hello world | |
100 | "); | |
101 | create_test_file(&test_dir.join("test.png"), "A picture of a cute cat"); | |
102 | ||
103 | // Run the program from the test directory | |
104 | let status = Command::new(env!("CARGO_BIN_EXE_page")) | |
105 | .current_dir(&test_dir) | |
106 | .status() | |
107 | .expect("Failed to execute command"); | |
108 | ||
109 | assert!(!status.success()); | |
110 | ||
111 | assert!(!html_dir.exists()); | |
112 | assert!(!gemini_dir.exists()); | |
113 | } |