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
use super::super::file::shallow_scan_project_dir_for_nonempty_file_name_match;
use super::{Rule, RuleContext, RuleOutcome};
use regex::Regex;
#[derive(Debug, Default)]
pub struct HasReadmeFile;
lazy_static! {
static ref HAS_README_FILE: Regex =
Regex::new(r"^README\.?.*").expect("Failed to create HasReadmeFile regex.");
}
impl Rule for HasReadmeFile {
fn description(&self) -> &'static str {
"Should have a README.md file in the project directory."
}
fn evaluate(&self, context: RuleContext) -> RuleOutcome {
shallow_scan_project_dir_for_nonempty_file_name_match(
&HAS_README_FILE,
context.cargo_manifest_file_path,
)
}
}
#[cfg(test)]
mod tests {
use super::super::test_support::*;
use super::*;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;
#[test]
fn has_readme_happy_path() {
let dir = tempdir().expect("Failed to make a temp dir");
let file_path = dir.path().join("README.md");
let mut file = File::create(file_path).expect("Could not make target file");
file.write_all(b"Hello, I am a README file.")
.expect("Could not write to target file");
let rule = HasReadmeFile::default();
let VerbosityOutcomes {
verbose,
not_verbose,
} = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
assert_eq!(RuleOutcome::Success, verbose.outcome);
assert_eq!(RuleOutcome::Success, not_verbose.outcome);
}
#[test]
fn empty_readme_file_fails() {
let dir = tempdir().expect("Failed to make a temp dir");
{
let file_path = dir.path().join("README.md");
let mut f = File::create(file_path).expect("Could not make target file");
f.write_all(b"").expect("Could not write emptiness to file");
f.flush().expect("Could not flush file");
f.sync_all().expect("Could not sync file");
}
let rule = HasReadmeFile::default();
let VerbosityOutcomes {
verbose,
not_verbose,
} = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
assert_eq!(RuleOutcome::Failure, verbose.outcome);
assert_eq!(RuleOutcome::Failure, not_verbose.outcome);
}
#[test]
fn no_readme_file_at_all_fails() {
let dir = tempdir().expect("Failed to make a temp dir");
let rule = HasReadmeFile::default();
let VerbosityOutcomes {
verbose,
not_verbose,
} = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
assert_eq!(RuleOutcome::Failure, verbose.outcome);
assert_eq!(RuleOutcome::Failure, not_verbose.outcome);
}
#[test]
fn non_md_extension_acceptable() {
let dir = tempdir().expect("Failed to make a temp dir");
let file_path = dir.path().join("README.txt");
let mut file = File::create(file_path).expect("Could not make target file");
file.write_all(b"Hello, I am a README file.")
.expect("Could not write to target file");
let rule = HasReadmeFile::default();
let VerbosityOutcomes {
verbose,
not_verbose,
} = execute_rule_against_project_dir_all_verbosities(dir.path(), &rule);
assert_eq!(RuleOutcome::Success, verbose.outcome);
assert_eq!(RuleOutcome::Success, not_verbose.outcome);
}
}