aboutsummaryrefslogtreecommitdiff
path: root/src/file_handler/file_strategies/layout.rs
blob: 9a60f12396a2dfb20ffd76d25ccbec2a71973a1e (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
pub struct Strategy {}

use std::path::Path;

use crate::file_handler::{File, FileType, Strategy as FileHandlerStrategy};

impl FileHandlerStrategy for Strategy {
    fn is(&self, path: &Path) -> bool {
        !path.is_dir() && path.ends_with("_layout.html")
    }

    fn identify(&self) -> FileType {
        FileType::Layout
    }

    fn can_handle(&self, file_type: &FileType) -> bool {
        matches!(file_type, FileType::Layout)
    }

    // We don't implement handling for layout, as we assume there's only one
    // and it got handled before.
    fn handle_html(&self, _s: &Path, _d: &Path, _f: &File, _l: &str) {}
    fn handle_gemini(&self, _s: &Path, _d: &Path, _f: &File) {}
}

#[cfg(test)]
mod tests {
    use std::fs::create_dir_all;
    use std::path::PathBuf;

    use super::*;

    use test_utilities::*;

    #[test]
    fn identifies_layout_file() {
        let test_dir = setup_test_dir();
        create_test_file(&test_dir.join("_layout.html"), "");
        let strategy = Strategy {};
        assert!(strategy.is(&test_dir.join("_layout.html")));
    }

    #[test]
    fn rejects_non_layout_html() {
        let test_dir = setup_test_dir();
        create_test_file(&test_dir.join("regular.html"), "");
        let strategy = Strategy {};
        assert!(!strategy.is(&test_dir.join("regular.html")));
    }

    #[test]
    fn rejects_layout_with_different_extension() {
        let test_dir = setup_test_dir();
        create_test_file(&test_dir.join("_layout.txt"), "");
        let strategy = Strategy {};
        assert!(!strategy.is(&test_dir.join("_layout.txt")));
    }

    #[test]
    fn rejects_layout_with_prefix() {
        let test_dir = setup_test_dir();
        create_test_file(&test_dir.join("prefix_layout.txt"), "");
        let strategy = Strategy {};
        assert!(!strategy.is(&test_dir.join("prefix_layout.txt")));
    }

    #[test]
    fn rejects_directory_named_layout() {
        let test_dir = setup_test_dir();
        let layout_dir = test_dir.join("_layout");
        create_dir_all(&layout_dir).expect("Could not create _layout test directory");
        let strategy = Strategy {};
        assert!(!strategy.is(&layout_dir));
    }

    #[test]
    fn identifies_layout_type() {
        let strategy = Strategy {};
        assert!(matches!(strategy.identify(), FileType::Layout));
    }

    #[test]
    fn handles_layout_type() {
        let strategy = Strategy {};
        assert!(strategy.can_handle(&FileType::Layout));
    }

    #[test]
    fn rejects_non_layout_types() {
        let strategy = Strategy {};
        assert!(!strategy.can_handle(&FileType::File));
        assert!(!strategy.can_handle(&FileType::Gemini));
        assert!(!strategy.can_handle(&FileType::Unknown));
    }

    #[test]
    fn handle_html_does_nothing() {
        let strategy = Strategy {};
        let file = File {
            path: PathBuf::from("_layout.html"),
            file_type: FileType::Layout,
        };

        strategy.handle_html(
            &PathBuf::from("source"),
            &PathBuf::from("dest"),
            &file,
            "layout content",
        );
    }

    #[test]
    fn handle_gemini_does_nothing() {
        let strategy = Strategy {};
        let file = File {
            path: PathBuf::from("test.gmi"),
            file_type: FileType::Layout,
        };

        // Should not panic
        strategy.handle_gemini(&PathBuf::from("source"), &PathBuf::from("dest"), &file);
    }
}