|
@@ -10,6 +10,13 @@ pub struct Post {
|
10
|
10
|
pub slug: String,
|
11
|
11
|
}
|
12
|
12
|
|
|
13
|
+pub fn read_posts_dir(cwd: &path::PathBuf) -> fs::ReadDir {
|
|
14
|
+ match fs::read_dir(cwd) {
|
|
15
|
+ Ok(posts) => posts,
|
|
16
|
+ Err(err) => panic!(err),
|
|
17
|
+ }
|
|
18
|
+}
|
|
19
|
+
|
13
|
20
|
pub fn parse_post(path: path::PathBuf) -> Post {
|
14
|
21
|
let contents = fs::read_to_string(&path).expect("Couldn't read post file");
|
15
|
22
|
|
|
@@ -34,10 +41,56 @@ mod tests {
|
34
|
41
|
#[allow(unused_imports)]
|
35
|
42
|
use super::*;
|
36
|
43
|
#[allow(unused_imports)]
|
37
|
|
- use std::{env, fs};
|
|
44
|
+ #[allow(unused_imports)]
|
|
45
|
+ use std::{env, fs, path};
|
38
|
46
|
#[allow(unused_imports)]
|
39
|
47
|
use uuid::Uuid;
|
40
|
48
|
|
|
49
|
+ #[test]
|
|
50
|
+ fn test_read_posts_dir() {
|
|
51
|
+ let temp_dir = env::temp_dir();
|
|
52
|
+ let working_dir = temp_dir.join(&Uuid::new_v4().to_string());
|
|
53
|
+ fs::create_dir(&working_dir).unwrap();
|
|
54
|
+ env::set_current_dir(&working_dir).unwrap();
|
|
55
|
+
|
|
56
|
+ let cwd = env::current_dir().unwrap();
|
|
57
|
+ fs::create_dir(cwd.join("posts")).unwrap();
|
|
58
|
+
|
|
59
|
+ let post_body = "# This is a post\n\nHere is some content that goes in the post";
|
|
60
|
+
|
|
61
|
+ let mut uuids: Vec<String> = vec![];
|
|
62
|
+
|
|
63
|
+ for _ in 1..11 {
|
|
64
|
+ let uuid = String::from(Uuid::new_v4().to_string());
|
|
65
|
+ uuids.push(uuid.clone());
|
|
66
|
+ fs::write(
|
|
67
|
+ cwd.join("posts").join(format!("{}.md", &uuid)),
|
|
68
|
+ &String::from(post_body),
|
|
69
|
+ ).unwrap();
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ let mut expected_paths: Vec<String> = uuids
|
|
73
|
+ .into_iter()
|
|
74
|
+ .map(|uuid| {
|
|
75
|
+ String::from(
|
|
76
|
+ cwd.join("posts")
|
|
77
|
+ .join(format!("{}.md", uuid))
|
|
78
|
+ .to_str()
|
|
79
|
+ .unwrap(),
|
|
80
|
+ )
|
|
81
|
+ }).collect();
|
|
82
|
+ expected_paths.sort();
|
|
83
|
+ let mut actual_paths: Vec<String> = read_posts_dir(&cwd.join("posts"))
|
|
84
|
+ .into_iter()
|
|
85
|
+ .map(|dir_entry| String::from(dir_entry.unwrap().path().to_str().unwrap()))
|
|
86
|
+ .collect();
|
|
87
|
+ actual_paths.sort();
|
|
88
|
+
|
|
89
|
+ assert_eq!(expected_paths, actual_paths);
|
|
90
|
+
|
|
91
|
+ fs::remove_dir_all(temp_dir.join(&working_dir)).unwrap();
|
|
92
|
+ }
|
|
93
|
+
|
41
|
94
|
#[test]
|
42
|
95
|
fn test_parse_post() {
|
43
|
96
|
let temp_dir = env::temp_dir();
|