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
|
use crate::configuration::Configuration;
use std::fmt::Write;
use std::fs::read_dir;
use std::io::{Error, ErrorKind, Result};
use std::path::PathBuf;
pub fn status(configuration: &Configuration) -> Result<String> {
let mut status_message = String::new();
status_message.push_str("# Blog\n");
// Main Configuration Locations
let blog_count = count_entries(&configuration.posts_directory);
writeln!(&mut status_message, "Number of posts in blog: {blog_count}")
.map_err(|_| Error::new(ErrorKind::Other, "Unable to write status"))?;
let archive_count = count_entries(&configuration.archive_directory);
writeln!(
&mut status_message,
"Number of posts in archive: {archive_count}"
)
.map_err(|_| Error::new(ErrorKind::Other, "Unable to write status"))?;
Ok(status_message)
}
fn count_entries(path: &PathBuf) -> String {
match read_dir(path) {
Ok(entries) => entries.filter_map(Result::ok).count().to_string(),
Err(_) => "0".to_string(),
}
}
#[cfg(test)]
mod tests {
use std::fs::create_dir_all;
use std::path::PathBuf;
use crate::configuration::Configuration;
use test_utilities::*;
use super::*;
#[test]
fn count_entries_counts_the_number_of_entries() {
let test_dir = setup_test_dir();
assert_eq!(count_entries(&test_dir), "0");
let first_post_dir = test_dir.join("1736035200055");
create_dir_all(&first_post_dir).expect("Could not create first post dir");
assert_eq!(count_entries(&test_dir), "1");
let second_post_dir = test_dir.join("1736035200058");
create_dir_all(&second_post_dir).expect("Could not create second post dir");
assert_eq!(count_entries(&test_dir), "2");
cleanup_test_dir(&test_dir);
}
#[test]
fn count_entries_defaults_to_zero_in_case_of_error() {
let test_dir = PathBuf::from("/ohnokrillin");
assert_eq!(count_entries(&test_dir), "0");
}
#[test]
fn blog_status_counts_the_number_of_entries_in_blog_and_archive() {
let test_dir = setup_test_dir();
let posts_dir = test_dir.join("blog");
let archive_dir = test_dir.join("archive");
create_dir_all(&posts_dir).expect("Could not create blog dir");
create_dir_all(posts_dir.join("0")).expect("Could not create blog post 0 dir");
create_dir_all(posts_dir.join("1")).expect("Could not create blog post 1 dir");
create_dir_all(posts_dir.join("2")).expect("Could not create blog post 2 dir");
create_dir_all(&archive_dir).expect("Could not create blog dir");
create_dir_all(archive_dir.join("0")).expect("Could not create blog post 0 dir");
create_dir_all(archive_dir.join("1")).expect("Could not create blog post 1 dir");
create_dir_all(archive_dir.join("2")).expect("Could not create blog post 2 dir");
create_dir_all(archive_dir.join("3")).expect("Could not create blog post 3 dir");
create_dir_all(archive_dir.join("4")).expect("Could not create blog post 4 dir");
create_dir_all(archive_dir.join("5")).expect("Could not create blog post 5 dir");
let mut configuration = Configuration::new().unwrap();
configuration.archive_directory = archive_dir.clone();
configuration.posts_directory = posts_dir.clone();
if let Ok(output) = status(&configuration) {
assert!(output.contains("blog: 3"));
assert!(output.contains("archive: 6"));
} else {
panic!("Could not generate status output.");
}
}
#[test]
fn blog_status_prints_zero_if_directories_do_not_exist() {
let test_dir = setup_test_dir();
let posts_dir = test_dir.join("blog");
let archive_dir = test_dir.join("archive");
let mut configuration = Configuration::new().unwrap();
configuration.archive_directory = archive_dir.clone();
configuration.posts_directory = posts_dir.clone();
if let Ok(output) = status(&configuration) {
assert!(output.contains("blog: 0"));
assert!(output.contains("archive: 0"));
} else {
panic!("Could not generate status output.");
}
}
}
|