aboutsummaryrefslogtreecommitdiff
path: root/tests/cli_test.rs
blob: 641d6f7dca844f58c98512888c66255258569d83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::env;
use std::fs::read_to_string;
use std::path::PathBuf;
use std::process::Command;
use test_utilities::*;

struct TestDir {
    paths: Vec<PathBuf>,
}

impl Drop for TestDir {
    fn drop(&mut self) {
        for path in &self.paths {
            cleanup_test_dir(path);
        }
    }
}

#[test]
fn test_basic_generation() {
    let test_dir = setup_test_dir();
    let dir_name = test_dir.file_name().unwrap().to_string_lossy();
    let parent = test_dir.parent().unwrap();
    let html_dir = parent.join(format!("{dir_name}_html"));
    let gemini_dir = parent.join(format!("{dir_name}_gemini"));

    let _cleanup = TestDir {
        paths: vec![test_dir.clone(), html_dir.clone(), gemini_dir.clone()],
    };

    // Create test input files
    create_test_file(
        &test_dir.join("_layout.html"),
        "\
<html>
<head><title>{{ title }}</title></head>
<body>{{ content }}</body>
</html>
",
    );
    create_test_file(
        &test_dir.join("test.gmi"),
        "\
--- title: Page Is Cool!
# Test
Hello world
",
    );
    create_test_file(&test_dir.join("test.png"), "A picture of a cute cat");

    // Run the program from the test directory
    let status = Command::new(env!("CARGO_BIN_EXE_page"))
        .current_dir(&test_dir)
        .status()
        .expect("Failed to execute command");

    assert!(status.success());

    assert!(html_dir.exists());
    assert!(gemini_dir.exists());

    let html_output = html_dir.join("test.html");
    assert_file_contents(
        &html_output,
        "\
<html>
<head><title>Page Is Cool!</title></head>
<body><section class=\"h1\">
<h1> Test</h1>
<p>Hello world</p>
</section>
</body>
</html>
",
    );

    let html_asset_output = html_dir.join("test.png");
    assert_file_contents(&html_asset_output, "A picture of a cute cat");

    let gemini_output = gemini_dir.join("test.gmi");
    assert_file_contents(
        &gemini_output,
        "\
# Test
Hello world
",
    );

    let gemini_asset_output = gemini_dir.join("test.png");
    assert!(gemini_asset_output.exists());
    let gemini_asset_content = read_to_string(gemini_asset_output).unwrap();
    assert_eq!(gemini_asset_content, "A picture of a cute cat");
}

#[test]
fn test_missing_layout() {
    let test_dir = setup_test_dir();
    let dir_name = test_dir.file_name().unwrap().to_string_lossy();
    let parent = test_dir.parent().unwrap();
    let html_dir = parent.join(format!("{dir_name}_html"));
    let gemini_dir = parent.join(format!("{dir_name}_gemini"));

    let _cleanup = TestDir {
        paths: vec![test_dir.clone(), html_dir.clone(), gemini_dir.clone()],
    };

    // Create test input files
    create_test_file(
        &test_dir.join("test.gmi"),
        "\
--- title: Page Is Cool!
# Test
Hello world
",
    );
    create_test_file(&test_dir.join("test.png"), "A picture of a cute cat");

    // Run the program from the test directory
    let status = Command::new(env!("CARGO_BIN_EXE_page"))
        .current_dir(&test_dir)
        .status()
        .expect("Failed to execute command");

    assert!(!status.success());

    assert!(!html_dir.exists());
    assert!(!gemini_dir.exists());
}