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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2016 Kitware, Inc.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// We need to support older Rust versions which require `AsciiExt`.
#![allow(deprecated)]
#![allow(unused_imports)]

use impl_prelude::*;

use std::ascii::AsciiExt;

#[derive(Debug, Default)]
/// A check which denies commits which adds files containing special characters in their paths.
pub struct InvalidPaths {
    /// Characters not allowed within a filename.
    invalid_characters: String,
    /// Allow the space character (ASCII x20)
    allow_space: bool,
}

impl InvalidPaths {
    /// Create a check which rejects paths which contain invalid characters.
    ///
    /// In addition to whitespace and non-ASCII characters, the characters given here are also
    /// disallowed.
    pub fn new<S: ToString>(chars: S) -> Self {
        Self {
            invalid_characters: chars.to_string(),
            allow_space: false,
        }
    }

    /// Whether to allow spaces in paths.
    ///
    /// This allows for exempting ` ` in paths.
    pub fn allow_space(&mut self, allow_space: bool) -> &mut Self {
        self.allow_space = allow_space;
        self
    }

    fn disallowed_whitespace(&self, c: &char) -> bool {
        c.is_whitespace() && (!self.allow_space || *c != ' ')
    }

    /// Whether a string has any invalid characters.
    fn has_invalid_chars(&self, chars: &str) -> bool {
        chars.chars()
            .any(|ref c| {
                !c.is_ascii() || self.disallowed_whitespace(c) || c.is_control() ||
                self.invalid_characters.chars().any(|ref ic| ic == c)
            })
    }

    /// Whether the string has any invalid bytes.
    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::*;

    const BAD_TOPIC: &str = "f536f44cf96b82e479d4973d5ea1cf78058bd1fb";
    const FIX_TOPIC: &str = "8ff69e1834ef2e82c0ed5cfb4ba56f1e4de85d03";

    #[test]
    fn test_allow_space() {
        let mut check = InvalidPaths::default();
        assert!(!check.disallowed_whitespace(&'a'));
        assert!(check.disallowed_whitespace(&' '));
        assert!(check.disallowed_whitespace(&'\n'));
        check.allow_space(true);
        assert!(!check.disallowed_whitespace(&'a'));
        assert!(!check.disallowed_whitespace(&' '));
        assert!(check.disallowed_whitespace(&'\n'));
    }

    #[test]
    fn test_invalid_paths() {
        let check = InvalidPaths::new("$");
        let result = run_check("test_invalid_paths", BAD_TOPIC, check);
        test_result_errors(result, &[
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
             `\"control-character-\\003\"` path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>$`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
             `\"invalid-utf8-\\200\"` path which contains at least one forbidden \
             character: `<non-ASCII><whitespace><control>$`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
             `\"non-ascii-\\303\\251\"` path which contains at least one forbidden \
             character: `<non-ASCII><whitespace><control>$`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the `with whitespace` \
             path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>$`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the `with-dollar-$` path \
             which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>$`.",
        ]);
    }

    #[test]
    fn test_invalid_paths_allow_space() {
        let mut check = InvalidPaths::new("$");
        check.allow_space(true);
        let result = run_check("test_invalid_paths_allow_space", BAD_TOPIC, check);
        test_result_errors(result, &[
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
             `\"control-character-\\003\"` path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>$`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
             `\"invalid-utf8-\\200\"` path which contains at least one forbidden \
             character: `<non-ASCII><whitespace><control>$`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
             `\"non-ascii-\\303\\251\"` path which contains at least one forbidden \
             character: `<non-ASCII><whitespace><control>$`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the `with-dollar-$` path \
             which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>$`.",
        ]);
    }

    #[test]
    fn test_invalid_paths_topic() {
        let check = InvalidPaths::new("$");
        let result = run_topic_check("test_invalid_paths_topic", BAD_TOPIC, check);
        test_result_errors(result, &[
            "adds the `\"control-character-\\003\"` path which contains at least one forbidden \
             character: `<non-ASCII><whitespace><control>$`.",
            "adds the `\"invalid-utf8-\\200\"` path which contains at least one forbidden \
             character: `<non-ASCII><whitespace><control>$`.",
            "adds the `\"non-ascii-\\303\\251\"` path which contains at least one forbidden \
             character: `<non-ASCII><whitespace><control>$`.",
            "adds the `with whitespace` path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>$`.",
            "adds the `with-dollar-$` path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>$`.",
        ]);
    }

    #[test]
    fn test_invalid_paths_topic_allow_space() {
        let mut check = InvalidPaths::new("$");
        check.allow_space(true);
        let result = run_topic_check("test_invalid_paths_topic_allow_space", BAD_TOPIC, check);
        test_result_errors(result, &[
            "adds the `\"control-character-\\003\"` path which contains at least one forbidden \
             character: `<non-ASCII><whitespace><control>$`.",
            "adds the `\"invalid-utf8-\\200\"` path which contains at least one forbidden \
             character: `<non-ASCII><whitespace><control>$`.",
            "adds the `\"non-ascii-\\303\\251\"` path which contains at least one forbidden \
             character: `<non-ASCII><whitespace><control>$`.",
            "adds the `with-dollar-$` path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>$`.",
        ]);
    }

    #[test]
    fn test_invalid_paths_default() {
        let check = InvalidPaths::default();
        let result = run_check("test_invalid_paths_default", BAD_TOPIC, check);
        test_result_errors(result, &[
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
             `\"control-character-\\003\"` path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the `\"invalid-utf8-\\200\"` \
             path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the `\"non-ascii-\\303\\251\"` \
             path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the `with whitespace` path \
             which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>`.",
        ]);
    }

    #[test]
    fn test_invalid_paths_default_allow_space() {
        let mut check = InvalidPaths::default();
        check.allow_space(true);
        let result = run_check("test_invalid_paths_default_allow_space", BAD_TOPIC, check);
        test_result_errors(result, &[
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the \
             `\"control-character-\\003\"` path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the `\"invalid-utf8-\\200\"` \
             path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>`.",
            "commit f536f44cf96b82e479d4973d5ea1cf78058bd1fb adds the `\"non-ascii-\\303\\251\"` \
             path which contains at least one forbidden character: \
             `<non-ASCII><whitespace><control>`.",
        ]);
    }

    #[test]
    fn test_invalid_paths_topic_fixed() {
        let check = InvalidPaths::default();
        run_topic_check_ok("test_invalid_paths_topic_fixed", FIX_TOPIC, check);
    }
}