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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use super::super::*;
use std::ascii::AsciiExt;
#[derive(Debug, Default)]
pub struct InvalidPaths {
invalid_characters: String,
}
impl InvalidPaths {
pub fn new<S: ToString>(chars: S) -> Self {
InvalidPaths {
invalid_characters: chars.to_string(),
}
}
fn has_invalid_chars(&self, chars: &str) -> bool {
chars.chars()
.any(|ref c| {
!c.is_ascii() || c.is_whitespace() || c.is_control() ||
self.invalid_characters.chars().any(|ref ic| ic == c)
})
}
fn has_invalid_bytes(&self, bytes: &[u8]) -> bool {
bytes.iter()
.any(|&c| !c.is_ascii() || c < 32)
}
}
impl Check for InvalidPaths {
fn name(&self) -> &str {
"invalid-paths"
}
fn check(&self, _: &CheckGitContext, commit: &Commit) -> Result<CheckResult> {
let mut result = CheckResult::new();
for diff in &commit.diffs {
if let StatusChange::Added = diff.status {
let have_bad_char = self.has_invalid_chars(diff.name.as_str());
let have_bad_bytes = self.has_invalid_bytes(diff.name.as_bytes());
if have_bad_char || have_bad_bytes {
result.add_error(format!("commit {} adds the `{}` path which contains at \
least one forbidden character: \
`<non-ASCII><whitespace><control>{}`.",
commit.sha1_short,
diff.name,
self.invalid_characters));
}
}
}
Ok(result)
}
}
#[cfg(test)]
mod tests {
use super::InvalidPaths;
use super::super::test::*;
static BAD_TOPIC: &'static str = "f536f44cf96b82e479d4973d5ea1cf78058bd1fb";
#[test]
fn test_invalid_paths() {
let check = InvalidPaths::new("$");
let mut conf = GitCheckConfiguration::new();
conf.add_check(&check);
let result = test_check("test_invalid_paths", BAD_TOPIC, &conf);
assert_eq!(result.warnings().len(), 0);
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 5);
assert_eq!(result.errors()[0],
"commit f536f44 adds the `\"control-character-\\003\"` path which contains \
at least one forbidden character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[1],
"commit f536f44 adds the `\"invalid-utf8-\\200\"` path which contains at \
least one forbidden character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[2],
"commit f536f44 adds the `\"non-ascii-\\303\\251\"` path which contains at \
least one forbidden character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[3],
"commit f536f44 adds the `with whitespace` path which contains at least one \
forbidden character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[4],
"commit f536f44 adds the `with-dollar-$` path which contains at least one \
forbidden character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), false);
}
#[test]
fn test_invalid_paths_default() {
let check = InvalidPaths::default();
let mut conf = GitCheckConfiguration::new();
conf.add_check(&check);
let result = test_check("test_invalid_paths_default", BAD_TOPIC, &conf);
assert_eq!(result.warnings().len(), 0);
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 4);
assert_eq!(result.errors()[0],
"commit f536f44 adds the `\"control-character-\\003\"` path which contains \
at least one forbidden character: `<non-ASCII><whitespace><control>`.");
assert_eq!(result.errors()[1],
"commit f536f44 adds the `\"invalid-utf8-\\200\"` path which contains at \
least one forbidden character: `<non-ASCII><whitespace><control>`.");
assert_eq!(result.errors()[2],
"commit f536f44 adds the `\"non-ascii-\\303\\251\"` path which contains at \
least one forbidden character: `<non-ASCII><whitespace><control>`.");
assert_eq!(result.errors()[3],
"commit f536f44 adds the `with whitespace` path which contains at least one \
forbidden character: `<non-ASCII><whitespace><control>`.");
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), false);
}
}