Browse Source

Reorganize

master
Dylan Baker 6 years ago
parent
commit
5d398836d5
2 changed files with 56 additions and 12 deletions
  1. 2
    11
      src/main.rs
  2. 54
    1
      src/post.rs

+ 2
- 11
src/main.rs View File

5
 extern crate regex;
5
 extern crate regex;
6
 extern crate uuid;
6
 extern crate uuid;
7
 
7
 
8
-use std::env;
9
-use std::fs;
10
-use std::path;
8
+use std::{env, fs};
11
 
9
 
12
 use clap::{App, Arg};
10
 use clap::{App, Arg};
13
 
11
 
14
-use post::parse_post;
12
+use post::{parse_post, read_posts_dir};
15
 use write::{write_post, write_post_listing};
13
 use write::{write_post, write_post_listing};
16
 
14
 
17
 mod post;
15
 mod post;
18
 mod render;
16
 mod render;
19
 mod write;
17
 mod write;
20
 
18
 
21
-fn read_posts_dir(cwd: &path::PathBuf) -> fs::ReadDir {
22
-    match fs::read_dir(cwd) {
23
-        Ok(posts) => posts,
24
-        Err(err) => panic!(err),
25
-    }
26
-}
27
-
28
 fn build() {
19
 fn build() {
29
     let cwd = env::current_dir().expect("Couldn't read current directory");
20
     let cwd = env::current_dir().expect("Couldn't read current directory");
30
 
21
 

+ 54
- 1
src/post.rs View File

10
     pub slug: String,
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
 pub fn parse_post(path: path::PathBuf) -> Post {
20
 pub fn parse_post(path: path::PathBuf) -> Post {
14
     let contents = fs::read_to_string(&path).expect("Couldn't read post file");
21
     let contents = fs::read_to_string(&path).expect("Couldn't read post file");
15
 
22
 
34
     #[allow(unused_imports)]
41
     #[allow(unused_imports)]
35
     use super::*;
42
     use super::*;
36
     #[allow(unused_imports)]
43
     #[allow(unused_imports)]
37
-    use std::{env, fs};
44
+    #[allow(unused_imports)]
45
+    use std::{env, fs, path};
38
     #[allow(unused_imports)]
46
     #[allow(unused_imports)]
39
     use uuid::Uuid;
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
     #[test]
94
     #[test]
42
     fn test_parse_post() {
95
     fn test_parse_post() {
43
         let temp_dir = env::temp_dir();
96
         let temp_dir = env::temp_dir();

Loading…
Cancel
Save