Skip to content

Commit 93bf118

Browse files
committed
add quote tests (#301)
1 parent 8ec7b30 commit 93bf118

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

git-attributes/src/parse/attribute.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ impl<'a> Iterator for Lines<'a> {
6464
fn next(&mut self) -> Option<Self::Item> {
6565
for line in self.lines.by_ref() {
6666
self.line_no += 1;
67+
if line.first() == Some(&b'#') {
68+
continue;
69+
}
6770
match parse_line(line) {
6871
None => continue,
6972
Some(Ok((pattern, flags, attrs))) => {

git-attributes/src/parse/ignore.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ impl<'a> Iterator for Lines<'a> {
2222
fn next(&mut self) -> Option<Self::Item> {
2323
for line in self.lines.by_ref() {
2424
self.line_no += 1;
25+
if line.first() == Some(&b'#') {
26+
continue;
27+
}
2528
match parse_line(line) {
2629
None => continue,
2730
Some((line, flags)) => return Some((line, flags, self.line_no)),
@@ -37,9 +40,7 @@ pub(crate) fn parse_line(mut line: &[u8]) -> Option<(BString, ignore::pattern::M
3740
if line.is_empty() {
3841
return None;
3942
};
40-
if line.first() == Some(&b'#') {
41-
return None;
42-
} else if line.first() == Some(&b'!') {
43+
if line.first() == Some(&b'!') {
4344
mode |= ignore::pattern::Mode::NEGATIVE;
4445
line = &line[1..];
4546
} else if line.first() == Some(&b'\\') {

git-attributes/tests/parse/attribute.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@ fn line_endings_can_be_windows_or_unix() {
4242
#[test]
4343
fn comment_lines_are_ignored() {
4444
assert!(git_attributes::parse(b"# hello world").next().is_none());
45+
assert!(git_attributes::parse(b"# \"hello world\"").next().is_none());
4546
}
4647

4748
#[test]
48-
fn comment_can_be_escaped_like_gitignore() {
49+
fn comment_can_be_escaped_like_gitignore_or_quoted() {
4950
assert_eq!(
5051
line(r"\#hello"),
5152
(r"#hello".into(), Mode::NO_SUB_DIR, vec![], 1),
5253
"undocumented, but definitely works"
5354
);
55+
assert_eq!(line("\"# hello\""), (r"# hello".into(), Mode::NO_SUB_DIR, vec![], 1));
5456
}
5557

5658
#[test]
@@ -60,6 +62,26 @@ fn esclamation_marks_must_be_escaped_or_error_unlike_gitignore() {
6062
try_line(r"!hello"),
6163
Err(parse::attribute::Error::PatternNegation { line_number: 1, .. })
6264
));
65+
assert!(
66+
matches!(
67+
try_line(r#""!hello""#),
68+
Err(parse::attribute::Error::PatternNegation { line_number: 1, .. }),
69+
),
70+
"even in quotes they trigger…"
71+
);
72+
assert_eq!(
73+
line(r#""\\!hello""#),
74+
(r"!hello".into(), Mode::NO_SUB_DIR, vec![], 1),
75+
"…and must be escaped"
76+
);
77+
}
78+
79+
#[test]
80+
fn invalid_escapes_in_quotes_are_an_error() {
81+
assert!(matches!(
82+
try_line(r#""\!hello""#),
83+
Err(parse::attribute::Error::Unquote(_)),
84+
),);
6385
}
6486

6587
#[test]
@@ -85,7 +107,7 @@ fn try_line(input: &str) -> Result<ExpandedAttribute, parse::attribute::Error> {
85107

86108
fn line(input: &str) -> ExpandedAttribute {
87109
let mut lines = git_attributes::parse(input.as_bytes());
88-
let res = expand(lines.next().unwrap()).unwrap();
110+
let res = expand(lines.next().expect("single line")).unwrap();
89111
assert!(lines.next().is_none(), "expected only one line");
90112
res
91113
}

0 commit comments

Comments
 (0)