]> git.r.bdr.sh - rbdr/page/blame - tests/cli_test.rs
Update version of gema_texto
[rbdr/page] / tests / cli_test.rs
CommitLineData
1c5797fa
RBR
1use std::env;
2use std::fs::read_to_string;
3use std::path::PathBuf;
4use std::process::Command;
5use test_utilities::*;
6
7struct TestDir {
8 paths: Vec<PathBuf>,
9}
10
11impl 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]
20fn 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();
3a7229d5
RBR
24 let html_dir = parent.join(format!("{dir_name}_html"));
25 let gemini_dir = parent.join(format!("{dir_name}_gemini"));
1c5797fa 26
5732d284
RBR
27 let _cleanup = TestDir {
28 paths: vec![test_dir.clone(), html_dir.clone(), gemini_dir.clone()],
1c5797fa
RBR
29 };
30
31 // Create test input files
5732d284
RBR
32 create_test_file(
33 &test_dir.join("_layout.html"),
34 "\
1c5797fa
RBR
35<html>
36<head><title>{{ title }}</title></head>
37<body>{{ content }}</body>
38</html>
5732d284
RBR
39",
40 );
41 create_test_file(
42 &test_dir.join("test.gmi"),
43 "\
1c5797fa
RBR
44--- title: Page Is Cool!
45# Test
46Hello world
5732d284
RBR
47",
48 );
1c5797fa
RBR
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");
5732d284
RBR
63 assert_file_contents(
64 &html_output,
65 "\
1c5797fa
RBR
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>
5732d284
RBR
74",
75 );
1c5797fa
RBR
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");
5732d284
RBR
81 assert_file_contents(
82 &gemini_output,
83 "\
1c5797fa
RBR
84# Test
85Hello world
5732d284
RBR
86",
87 );
1c5797fa
RBR
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]
96fn 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();
3a7229d5
RBR
100 let html_dir = parent.join(format!("{dir_name}_html"));
101 let gemini_dir = parent.join(format!("{dir_name}_gemini"));
1c5797fa 102
5732d284
RBR
103 let _cleanup = TestDir {
104 paths: vec![test_dir.clone(), html_dir.clone(), gemini_dir.clone()],
1c5797fa
RBR
105 };
106
107 // Create test input files
5732d284
RBR
108 create_test_file(
109 &test_dir.join("test.gmi"),
110 "\
1c5797fa
RBR
111--- title: Page Is Cool!
112# Test
113Hello world
5732d284
RBR
114",
115 );
1c5797fa
RBR
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}