|
| 1 | +mod parse { |
| 2 | + mod ignore { |
| 3 | + use git_attributes::ignore::pattern::Mode; |
| 4 | + use git_testtools::fixture_path; |
| 5 | + |
| 6 | + #[test] |
| 7 | + fn line_numbers_are_counted_correctly() { |
| 8 | + let ignore = std::fs::read(fixture_path("ignore/various.txt")).unwrap(); |
| 9 | + let actual: Vec<_> = git_attributes::parse::ignore(&ignore).collect(); |
| 10 | + assert_eq!( |
| 11 | + actual, |
| 12 | + vec![ |
| 13 | + ("*.[oa]".into(), Mode::NO_SUB_DIR, 2), |
| 14 | + ("*.html".into(), Mode::NO_SUB_DIR | Mode::ENDS_WITH, 5), |
| 15 | + ("foo.html".into(), Mode::NO_SUB_DIR | Mode::NEGATIVE, 8), |
| 16 | + ("/*".into(), Mode::empty(), 11), |
| 17 | + ("/foo".into(), Mode::NEGATIVE, 12), |
| 18 | + ("/foo/*".into(), Mode::empty(), 13), |
| 19 | + ("/foo/bar".into(), Mode::NEGATIVE, 14) |
| 20 | + ] |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + #[test] |
| 25 | + fn line_endings_can_be_windows_or_unix() { |
| 26 | + assert_eq!( |
| 27 | + git_attributes::parse::ignore(b"unix\nwindows\r\nlast").collect::<Vec<_>>(), |
| 28 | + vec![ |
| 29 | + (r"unix".into(), Mode::NO_SUB_DIR, 1), |
| 30 | + (r"windows".into(), Mode::NO_SUB_DIR, 2), |
| 31 | + (r"last".into(), Mode::NO_SUB_DIR, 3) |
| 32 | + ] |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + #[test] |
| 37 | + fn mark_ends_with_pattern_specifically() { |
| 38 | + assert_eq!( |
| 39 | + git_attributes::parse::ignore(br"*literal").next(), |
| 40 | + Some((r"*literal".into(), Mode::NO_SUB_DIR | Mode::ENDS_WITH, 1)) |
| 41 | + ); |
| 42 | + assert_eq!( |
| 43 | + git_attributes::parse::ignore(br"**literal").next(), |
| 44 | + Some((r"**literal".into(), Mode::NO_SUB_DIR, 1)), |
| 45 | + "double-asterisk won't allow for fast comparisons" |
| 46 | + ); |
| 47 | + assert_eq!( |
| 48 | + git_attributes::parse::ignore(br"*litera[l]").next(), |
| 49 | + Some((r"*litera[l]".into(), Mode::NO_SUB_DIR, 1)) |
| 50 | + ); |
| 51 | + assert_eq!( |
| 52 | + git_attributes::parse::ignore(br"*litera?").next(), |
| 53 | + Some((r"*litera?".into(), Mode::NO_SUB_DIR, 1)) |
| 54 | + ); |
| 55 | + assert_eq!( |
| 56 | + git_attributes::parse::ignore(br"*litera\?").next(), |
| 57 | + Some((r"*litera\?".into(), Mode::NO_SUB_DIR, 1)), |
| 58 | + "for now we don't handle escapes properly like git seems to do" |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn comments_are_ignored() { |
| 64 | + assert!(git_attributes::parse::ignore(b"# hello world").next().is_none()); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn backslashes_before_hashes_are_no_comments() { |
| 69 | + assert_eq!( |
| 70 | + git_attributes::parse::ignore(br"\#hello").next(), |
| 71 | + Some((r"#hello".into(), Mode::NO_SUB_DIR, 1)) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn backslashes_are_part_of_the_pattern_if_not_in_specific_positions() { |
| 77 | + assert_eq!( |
| 78 | + git_attributes::parse::ignore(br"\hello\world").next(), |
| 79 | + Some((r"\hello\world".into(), Mode::NO_SUB_DIR, 1)) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn leading_exclamation_mark_negates_pattern() { |
| 85 | + assert_eq!( |
| 86 | + git_attributes::parse::ignore(b"!hello").next(), |
| 87 | + Some(("hello".into(), Mode::NEGATIVE | Mode::NO_SUB_DIR, 1)) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn leading_exclamation_marks_can_be_escaped_with_backslash() { |
| 93 | + assert_eq!( |
| 94 | + git_attributes::parse::ignore(br"\!hello").next(), |
| 95 | + Some(("!hello".into(), Mode::NO_SUB_DIR, 1)) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn absence_of_sub_directories_are_marked() { |
| 101 | + assert_eq!( |
| 102 | + git_attributes::parse::ignore(br"a/b").next(), |
| 103 | + Some(("a/b".into(), Mode::empty(), 1)) |
| 104 | + ); |
| 105 | + assert_eq!( |
| 106 | + git_attributes::parse::ignore(br"ab").next(), |
| 107 | + Some(("ab".into(), Mode::NO_SUB_DIR, 1)) |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn trailing_slashes_are_marked_and_removed() { |
| 113 | + assert_eq!( |
| 114 | + git_attributes::parse::ignore(b"dir/").next(), |
| 115 | + Some(("dir".into(), Mode::MUST_BE_DIR | Mode::NO_SUB_DIR, 1)) |
| 116 | + ); |
| 117 | + assert_eq!( |
| 118 | + git_attributes::parse::ignore(b"dir///").next(), |
| 119 | + Some(("dir//".into(), Mode::MUST_BE_DIR, 1)), |
| 120 | + "but only the last slash is removed" |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn trailing_spaces_are_ignored() { |
| 126 | + assert_eq!( |
| 127 | + git_attributes::parse::ignore(br"a ").next(), |
| 128 | + Some(("a".into(), Mode::NO_SUB_DIR, 1)) |
| 129 | + ); |
| 130 | + assert_eq!( |
| 131 | + git_attributes::parse::ignore(b"a\t\t ").next(), |
| 132 | + Some(("a\t\t".into(), Mode::NO_SUB_DIR, 1)), |
| 133 | + "trailing tabs are not ignored" |
| 134 | + ); |
| 135 | + } |
| 136 | + #[test] |
| 137 | + fn trailing_spaces_can_be_escaped_to_be_literal() { |
| 138 | + assert_eq!( |
| 139 | + git_attributes::parse::ignore(br"a \ ").next(), |
| 140 | + Some(("a ".into(), Mode::NO_SUB_DIR, 1)), |
| 141 | + "a single escape in front of the last desired space is enough" |
| 142 | + ); |
| 143 | + assert_eq!( |
| 144 | + git_attributes::parse::ignore(br"a b c ").next(), |
| 145 | + Some(("a b c".into(), Mode::NO_SUB_DIR, 1)), |
| 146 | + "spaces in the middle are fine" |
| 147 | + ); |
| 148 | + assert_eq!( |
| 149 | + git_attributes::parse::ignore(br"a\ \ \ ").next(), |
| 150 | + Some(("a ".into(), Mode::NO_SUB_DIR, 1)), |
| 151 | + "one can also escape every single one" |
| 152 | + ); |
| 153 | + assert_eq!( |
| 154 | + git_attributes::parse::ignore(br"a \ ").next(), |
| 155 | + Some(("a ".into(), Mode::NO_SUB_DIR, 1)), |
| 156 | + "or just the one in the middle, losing the last actual space" |
| 157 | + ); |
| 158 | + assert_eq!( |
| 159 | + git_attributes::parse::ignore(br"a \").next(), |
| 160 | + Some(("a ".into(), Mode::NO_SUB_DIR, 1)), |
| 161 | + "escaping nothing also works as a whitespace protection" |
| 162 | + ); |
| 163 | + assert_eq!( |
| 164 | + git_attributes::parse::ignore(br"a \\\ ").next(), |
| 165 | + Some((r"a ".into(), Mode::NO_SUB_DIR, 1)), |
| 166 | + "strange things like these work too" |
| 167 | + ); |
| 168 | + assert_eq!( |
| 169 | + git_attributes::parse::ignore(br"a \\ ").next(), |
| 170 | + Some((r"a ".into(), Mode::NO_SUB_DIR, 1)), |
| 171 | + "strange things like these work as well" |
| 172 | + ); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments