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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use impl_prelude::*;
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 ContentCheck for InvalidPaths {
fn name(&self) -> &str {
"invalid-paths"
}
fn check(&self, _: &CheckGitContext, content: &Content) -> Result<CheckResult> {
let mut result = CheckResult::new();
for diff in content.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!("{}adds the `{}` path which contains at \
least one forbidden character: \
`<non-ASCII><whitespace><control>{}`.",
commit_prefix(content),
diff.name,
self.invalid_characters));
}
}
}
Ok(result)
}
}
#[cfg(test)]
mod tests {
use checks::InvalidPaths;
use checks::test::*;
static BAD_TOPIC: &'static str = "f536f44cf96b82e479d4973d5ea1cf78058bd1fb";
static FIX_TOPIC: &'static str = "8ff69e1834ef2e82c0ed5cfb4ba56f1e4de85d03";
#[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 f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
`\"control-character-\\003\"` path which contains at least one forbidden \
character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[1],
"commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
`\"invalid-utf8-\\200\"` path which contains at least one forbidden \
character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[2],
"commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
`\"non-ascii-\\303\\251\"` path which contains at least one forbidden \
character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[3],
"commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the `with whitespace` \
path which contains at least one forbidden character: \
`<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[4],
"commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the `with-dollar-$` path \
which contains at least one forbidden character: \
`<non-ASCII><whitespace><control>$`.");
assert_eq!(result.temporary(), false);
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), false);
}
#[test]
fn test_invalid_paths_topic() {
let check = InvalidPaths::new("$");
let mut conf = GitCheckConfiguration::new();
conf.add_topic_check(&check);
let result = test_check("test_invalid_paths_topic", 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],
"adds the `\"control-character-\\003\"` path which contains at least one \
forbidden character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[1],
"adds the `\"invalid-utf8-\\200\"` path which contains at least one forbidden \
character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[2],
"adds the `\"non-ascii-\\303\\251\"` path which contains at least one \
forbidden character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[3],
"adds the `with whitespace` path which contains at least one forbidden \
character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.errors()[4],
"adds the `with-dollar-$` path which contains at least one forbidden \
character: `<non-ASCII><whitespace><control>$`.");
assert_eq!(result.temporary(), false);
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 f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
`\"control-character-\\003\"` path which contains at least one forbidden \
character: `<non-ASCII><whitespace><control>`.");
assert_eq!(result.errors()[1],
"commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
`\"invalid-utf8-\\200\"` path which contains at least one forbidden \
character: `<non-ASCII><whitespace><control>`.");
assert_eq!(result.errors()[2],
"commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
`\"non-ascii-\\303\\251\"` path which contains at least one forbidden \
character: `<non-ASCII><whitespace><control>`.");
assert_eq!(result.errors()[3],
"commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the `with whitespace` \
path which contains at least one forbidden character: \
`<non-ASCII><whitespace><control>`.");
assert_eq!(result.temporary(), false);
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), false);
}
#[test]
fn test_invalid_paths_topic_fixed() {
let check = InvalidPaths::default();
let mut conf = GitCheckConfiguration::new();
conf.add_topic_check(&check);
let result = test_check("test_invalid_paths_topic_fixed", FIX_TOPIC, &conf);
assert_eq!(result.warnings().len(), 0);
assert_eq!(result.alerts().len(), 0);
assert_eq!(result.errors().len(), 0);
assert_eq!(result.temporary(), false);
assert_eq!(result.allowed(), false);
assert_eq!(result.pass(), true);
}
}