1
- use bstr:: { BStr , BString } ;
1
+ use bstr:: { BStr , BString , ByteSlice } ;
2
2
use git_attributes:: ignore:: pattern:: Mode ;
3
- use git_attributes:: { ignore, parse} ;
3
+ use git_attributes:: { ignore, parse, State } ;
4
4
use git_testtools:: fixture_path;
5
5
6
6
#[ test]
@@ -43,6 +43,16 @@ fn line_endings_can_be_windows_or_unix() {
43
43
fn comment_lines_are_ignored ( ) {
44
44
assert ! ( git_attributes:: parse( b"# hello world" ) . next( ) . is_none( ) ) ;
45
45
assert ! ( git_attributes:: parse( b"# \" hello world\" " ) . next( ) . is_none( ) ) ;
46
+ assert ! (
47
+ git_attributes:: parse( b" \t \r # \" hello world\" " ) . next( ) . is_none( ) ,
48
+ "also behind leading whitespace"
49
+ ) ;
50
+ }
51
+
52
+ #[ test]
53
+ fn leading_whitespace_is_ignored ( ) {
54
+ assert_eq ! ( line( " \r \t p" ) , ( r"p" . into( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
55
+ assert_eq ! ( line( " \r \t \" p\" " ) , ( r"p" . into( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
46
56
}
47
57
48
58
#[ test]
@@ -56,7 +66,7 @@ fn comment_can_be_escaped_like_gitignore_or_quoted() {
56
66
}
57
67
58
68
#[ test]
59
- fn esclamation_marks_must_be_escaped_or_error_unlike_gitignore ( ) {
69
+ fn exclamation_marks_must_be_escaped_or_error_unlike_gitignore ( ) {
60
70
assert_eq ! ( line( r"\!hello" ) , ( r"!hello" . into( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
61
71
assert ! ( matches!(
62
72
try_line( r"!hello" ) ,
@@ -72,7 +82,7 @@ fn esclamation_marks_must_be_escaped_or_error_unlike_gitignore() {
72
82
assert_eq ! (
73
83
line( r#""\\!hello""# ) ,
74
84
( r"!hello" . into( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ,
75
- "…and must be escaped"
85
+ "…and must be double- escaped, once to get through quote, then to get through parse ignore line "
76
86
) ;
77
87
}
78
88
@@ -86,9 +96,48 @@ fn invalid_escapes_in_quotes_are_an_error() {
86
96
87
97
#[ test]
88
98
#[ ignore]
99
+ fn custom_macros_can_be_defined ( ) {
100
+ todo ! ( "name validation, leave rejecting them based on location to the caller" )
101
+ }
102
+
103
+ #[ test]
89
104
fn attributes_are_parsed_behind_various_whitespace_characters ( ) {
90
- // see https://github.com/git/git/blob/master/attr.c#L280:L280
91
- todo ! ( )
105
+ assert_eq ! (
106
+ line( r#"p a b"# ) ,
107
+ ( "p" . into( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
108
+ "behind space"
109
+ ) ;
110
+ assert_eq ! (
111
+ line( r#""p" a b"# ) ,
112
+ ( "p" . into( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
113
+ "behind space"
114
+ ) ;
115
+ assert_eq ! (
116
+ line( "p\t a\t b" ) ,
117
+ ( "p" . into( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
118
+ "behind tab"
119
+ ) ;
120
+ assert_eq ! (
121
+ line( "\" p\" \t a\t b" ) ,
122
+ ( "p" . into( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
123
+ "behind tab"
124
+ ) ;
125
+ assert_eq ! (
126
+ line( "p \t a \t b" ) ,
127
+ ( "p" . into( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
128
+ "behind a mix of space and tab"
129
+ ) ;
130
+ assert_eq ! (
131
+ line( "\" p\" \t a \t b" ) ,
132
+ ( "p" . into( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
133
+ "behind a mix of space and tab"
134
+ ) ;
135
+ }
136
+
137
+ #[ test]
138
+ fn trailing_whitespace_in_attributes_is_ignored ( ) {
139
+ assert_eq ! ( line( "p a \r \t " ) , ( "p" . into( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) ] , 1 ) , ) ;
140
+ assert_eq ! ( line( "\" p\" a \r \t " ) , ( "p" . into( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) ] , 1 ) , ) ;
92
141
}
93
142
94
143
type ExpandedAttribute < ' a > = (
@@ -98,6 +147,10 @@ type ExpandedAttribute<'a> = (
98
147
usize ,
99
148
) ;
100
149
150
+ fn set ( attr : & str ) -> ( & BStr , State ) {
151
+ ( attr. as_bytes ( ) . as_bstr ( ) , State :: Set )
152
+ }
153
+
101
154
fn try_line ( input : & str ) -> Result < ExpandedAttribute , parse:: attribute:: Error > {
102
155
let mut lines = git_attributes:: parse ( input. as_bytes ( ) ) ;
103
156
let res = expand ( lines. next ( ) . unwrap ( ) ) ?;
0 commit comments