| @@ -1,204 +1,212 @@ |
| 1 | 1 | use git_attributes::State; |
| 2 | | −use git_pathspec::parse::Error; |
| 3 | 2 | use git_pathspec::{MagicSignature, Pattern, SearchMode}; |
| 4 | 3 | |
| 5 | | −#[test] |
| 6 | | −fn can_parse_empty_signatures() { |
| 7 | | − let inputs = vec![ |
| 8 | | − ("some/path", pat_with_path("some/path")), |
| 9 | | − (":some/path", pat_with_path("some/path")), |
| 10 | | − (":()some/path", pat_with_path("some/path")), |
| 11 | | − ("::some/path", pat_with_path("some/path")), |
| 12 | | − (":::some/path", pat_with_path(":some/path")), |
| 13 | | − ]; |
| 14 | | − |
| 15 | | − check_valid_inputs(inputs) |
| 16 | | −} |
| 4 | +mod succeed { |
| 5 | + use crate::{check_valid_inputs, pat, pat_with_path, pat_with_path_and_sig}; |
| 6 | + use git_attributes::State; |
| 7 | + use git_pathspec::{MagicSignature, SearchMode}; |
| 8 | + |
| 9 | + #[test] |
| 10 | + fn empty_signatures() { |
| 11 | + let inputs = vec![ |
| 12 | + ("some/path", pat_with_path("some/path")), |
| 13 | + (":some/path", pat_with_path("some/path")), |
| 14 | + (":()some/path", pat_with_path("some/path")), |
| 15 | + ("::some/path", pat_with_path("some/path")), |
| 16 | + (":::some/path", pat_with_path(":some/path")), |
| 17 | + ]; |
| 18 | + |
| 19 | + check_valid_inputs(inputs) |
| 20 | + } |
| 17 | 21 | |
| 18 | | −#[test] |
| 19 | | −fn can_parse_short_signatures() { |
| 20 | | − let inputs = vec![ |
| 21 | | − (":/some/path", pat_with_path_and_sig("some/path", MagicSignature::TOP)), |
| 22 | | − ( |
| 23 | | − ":^some/path", |
| 24 | | − pat_with_path_and_sig("some/path", MagicSignature::EXCLUDE), |
| 25 | | − ), |
| 26 | | − ( |
| 27 | | − ":!some/path", |
| 28 | | − pat_with_path_and_sig("some/path", MagicSignature::EXCLUDE), |
| 29 | | − ), |
| 30 | | − ( |
| 31 | | − ":/!some/path", |
| 32 | | − pat_with_path_and_sig("some/path", MagicSignature::TOP | MagicSignature::EXCLUDE), |
| 33 | | − ), |
| 34 | | − ( |
| 35 | | − ":!/^/:", |
| 36 | | − pat_with_path_and_sig("", MagicSignature::TOP | MagicSignature::EXCLUDE), |
| 37 | | − ), |
| 38 | | − ]; |
| 39 | | − |
| 40 | | − check_valid_inputs(inputs) |
| 41 | | −} |
| 22 | + #[test] |
| 23 | + fn short_signatures() { |
| 24 | + let inputs = vec![ |
| 25 | + (":/some/path", pat_with_path_and_sig("some/path", MagicSignature::TOP)), |
| 26 | + ( |
| 27 | + ":^some/path", |
| 28 | + pat_with_path_and_sig("some/path", MagicSignature::EXCLUDE), |
| 29 | + ), |
| 30 | + ( |
| 31 | + ":!some/path", |
| 32 | + pat_with_path_and_sig("some/path", MagicSignature::EXCLUDE), |
| 33 | + ), |
| 34 | + ( |
| 35 | + ":/!some/path", |
| 36 | + pat_with_path_and_sig("some/path", MagicSignature::TOP | MagicSignature::EXCLUDE), |
| 37 | + ), |
| 38 | + ( |
| 39 | + ":!/^/:", |
| 40 | + pat_with_path_and_sig("", MagicSignature::TOP | MagicSignature::EXCLUDE), |
| 41 | + ), |
| 42 | + ]; |
| 42 | 43 | |
| 43 | | −#[test] |
| 44 | | −fn can_parse_signatures_and_searchmodes() { |
| 45 | | − let inputs = vec![ |
| 46 | | − (":(top)", pat_with_path_and_sig("", MagicSignature::TOP)), |
| 47 | | − (":(icase)", pat_with_path_and_sig("", MagicSignature::ICASE)), |
| 48 | | − (":(attr)", pat_with_path_and_sig("", MagicSignature::ATTR)), |
| 49 | | − (":(exclude)", pat_with_path_and_sig("", MagicSignature::EXCLUDE)), |
| 50 | | − ( |
| 51 | | − ":(literal)", |
| 52 | | − pat("", MagicSignature::empty(), SearchMode::Literal, vec![]), |
| 53 | | − ), |
| 54 | | − (":(glob)", pat("", MagicSignature::empty(), SearchMode::Glob, vec![])), |
| 55 | | − ( |
| 56 | | − ":(top,exclude)", |
| 57 | | − pat_with_path_and_sig("", MagicSignature::TOP | MagicSignature::EXCLUDE), |
| 58 | | − ), |
| 59 | | − ( |
| 60 | | − ":(icase,literal)", |
| 61 | | − pat("", MagicSignature::ICASE, SearchMode::Literal, vec![]), |
| 62 | | − ), |
| 63 | | − ( |
| 64 | | − ":!(literal)some/*path", |
| 65 | | − pat("some/*path", MagicSignature::EXCLUDE, SearchMode::Literal, vec![]), |
| 66 | | − ), |
| 67 | | − ( |
| 68 | | − ":(top,literal,icase,attr,exclude)some/path", |
| 69 | | − pat("some/path", MagicSignature::all(), SearchMode::Literal, vec![]), |
| 70 | | − ), |
| 71 | | − ( |
| 72 | | − ":(top,glob,icase,attr,exclude)some/path", |
| 73 | | − pat("some/path", MagicSignature::all(), SearchMode::Glob, vec![]), |
| 74 | | − ), |
| 75 | | − ]; |
| 76 | | − |
| 77 | | − check_valid_inputs(inputs); |
| 78 | | −} |
| 44 | + check_valid_inputs(inputs) |
| 45 | + } |
| 79 | 46 | |
| 80 | | −#[test] |
| 81 | | −fn can_parse_attributes_in_signature() { |
| 82 | | − let inputs = vec![ |
| 83 | | − ( |
| 84 | | − ":(attr:someAttr)", |
| 85 | | − pat( |
| 86 | | − "", |
| 87 | | − MagicSignature::ATTR, |
| 88 | | − SearchMode::Default, |
| 89 | | − vec![("someAttr", State::Set)], |
| 47 | + #[test] |
| 48 | + fn signatures_and_searchmodes() { |
| 49 | + let inputs = vec![ |
| 50 | + (":(top)", pat_with_path_and_sig("", MagicSignature::TOP)), |
| 51 | + (":(icase)", pat_with_path_and_sig("", MagicSignature::ICASE)), |
| 52 | + (":(attr)", pat_with_path_and_sig("", MagicSignature::ATTR)), |
| 53 | + (":(exclude)", pat_with_path_and_sig("", MagicSignature::EXCLUDE)), |
| 54 | + ( |
| 55 | + ":(literal)", |
| 56 | + pat("", MagicSignature::empty(), SearchMode::Literal, vec![]), |
| 90 | 57 | ), |
| 91 | | − ), |
| 92 | | − ( |
| 93 | | − ":(attr:!someAttr)", |
| 94 | | − pat( |
| 95 | | − "", |
| 96 | | − MagicSignature::ATTR, |
| 97 | | − SearchMode::Default, |
| 98 | | − vec![("someAttr", State::Unspecified)], |
| 58 | + (":(glob)", pat("", MagicSignature::empty(), SearchMode::Glob, vec![])), |
| 59 | + ( |
| 60 | + ":(top,exclude)", |
| 61 | + pat_with_path_and_sig("", MagicSignature::TOP | MagicSignature::EXCLUDE), |
| 99 | 62 | ), |
| 100 | | − ), |
| 101 | | − ( |
| 102 | | − ":(attr:-someAttr)", |
| 103 | | − pat( |
| 104 | | − "", |
| 105 | | − MagicSignature::ATTR, |
| 106 | | − SearchMode::Default, |
| 107 | | − vec![("someAttr", State::Unset)], |
| 63 | + ( |
| 64 | + ":(icase,literal)", |
| 65 | + pat("", MagicSignature::ICASE, SearchMode::Literal, vec![]), |
| 108 | 66 | ), |
| 109 | | − ), |
| 110 | | − ( |
| 111 | | − ":(attr:someAttr=value)", |
| 112 | | − pat( |
| 113 | | − "", |
| 114 | | − MagicSignature::ATTR, |
| 115 | | − SearchMode::Default, |
| 116 | | − vec![("someAttr", State::Value("value".into()))], |
| 67 | + ( |
| 68 | + ":!(literal)some/*path", |
| 69 | + pat("some/*path", MagicSignature::EXCLUDE, SearchMode::Literal, vec![]), |
| 117 | 70 | ), |
| 118 | | − ), |
| 119 | | − ( |
| 120 | | − ":(attr:someAttr anotherAttr)", |
| 121 | | − pat( |
| 122 | | − "", |
| 123 | | − MagicSignature::ATTR, |
| 124 | | − SearchMode::Default, |
| 125 | | − vec![("someAttr", State::Set), ("anotherAttr", State::Set)], |
| 71 | + ( |
| 72 | + ":(top,literal,icase,attr,exclude)some/path", |
| 73 | + pat("some/path", MagicSignature::all(), SearchMode::Literal, vec![]), |
| 126 | 74 | ), |
| 127 | | − ), |
| 128 | | − ]; |
| 129 | | − |
| 130 | | − check_valid_inputs(inputs) |
| 131 | | −} |
| 75 | + ( |
| 76 | + ":(top,glob,icase,attr,exclude)some/path", |
| 77 | + pat("some/path", MagicSignature::all(), SearchMode::Glob, vec![]), |
| 78 | + ), |
| 79 | + ]; |
| 132 | 80 | |
| 133 | | −#[test] |
| 134 | | −fn should_fail_on_empty_input() { |
| 135 | | − let input = ""; |
| 81 | + check_valid_inputs(inputs); |
| 82 | + } |
| 136 | 83 | |
| 137 | | − assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input); |
| 84 | + #[test] |
| 85 | + fn attributes_in_signature() { |
| 86 | + let inputs = vec![ |
| 87 | + ( |
| 88 | + ":(attr:someAttr)", |
| 89 | + pat( |
| 90 | + "", |
| 91 | + MagicSignature::ATTR, |
| 92 | + SearchMode::Default, |
| 93 | + vec![("someAttr", State::Set)], |
| 94 | + ), |
| 95 | + ), |
| 96 | + ( |
| 97 | + ":(attr:!someAttr)", |
| 98 | + pat( |
| 99 | + "", |
| 100 | + MagicSignature::ATTR, |
| 101 | + SearchMode::Default, |
| 102 | + vec![("someAttr", State::Unspecified)], |
| 103 | + ), |
| 104 | + ), |
| 105 | + ( |
| 106 | + ":(attr:-someAttr)", |
| 107 | + pat( |
| 108 | + "", |
| 109 | + MagicSignature::ATTR, |
| 110 | + SearchMode::Default, |
| 111 | + vec![("someAttr", State::Unset)], |
| 112 | + ), |
| 113 | + ), |
| 114 | + ( |
| 115 | + ":(attr:someAttr=value)", |
| 116 | + pat( |
| 117 | + "", |
| 118 | + MagicSignature::ATTR, |
| 119 | + SearchMode::Default, |
| 120 | + vec![("someAttr", State::Value("value".into()))], |
| 121 | + ), |
| 122 | + ), |
| 123 | + ( |
| 124 | + ":(attr:someAttr anotherAttr)", |
| 125 | + pat( |
| 126 | + "", |
| 127 | + MagicSignature::ATTR, |
| 128 | + SearchMode::Default, |
| 129 | + vec![("someAttr", State::Set), ("anotherAttr", State::Set)], |
| 130 | + ), |
| 131 | + ), |
| 132 | + ]; |
| 138 | 133 | |
| 139 | | − let output = git_pathspec::parse(input.as_bytes()); |
| 140 | | − assert!(output.is_err()); |
| 141 | | − assert!(matches!(output.unwrap_err(), Error::EmptyString { .. })); |
| 134 | + check_valid_inputs(inputs) |
| 135 | + } |
| 142 | 136 | } |
| 143 | 137 | |
| 144 | | −#[test] |
| 145 | | −fn should_fail_on_invalid_keywords() { |
| 146 | | − let inputs = vec![ |
| 147 | | − ":( )some/path", |
| 148 | | − ":(tp)some/path", |
| 149 | | − ":(top, exclude)some/path", |
| 150 | | − ":(top,exclude,icse)some/path", |
| 151 | | − ]; |
| 138 | +mod fail { |
| 139 | + use crate::is_valid_in_git; |
| 140 | + use git_pathspec::parse::Error; |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn empty_input() { |
| 144 | + let input = ""; |
| 152 | 145 | |
| 153 | | − inputs.into_iter().for_each(|input| { |
| 154 | 146 | assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input); |
| 155 | 147 | |
| 156 | 148 | let output = git_pathspec::parse(input.as_bytes()); |
| 157 | 149 | assert!(output.is_err()); |
| 158 | | − assert!(matches!(output.unwrap_err(), Error::InvalidKeyword { .. })); |
| 159 | | − }); |
| 160 | | −} |
| 150 | + assert!(matches!(output.unwrap_err(), Error::EmptyString { .. })); |
| 151 | + } |
| 161 | 152 | |
| 162 | | −#[test] |
| 163 | | −fn should_fail_on_invalid_attributes() { |
| 164 | | − let inputs = vec![ |
| 165 | | − ":(attr:+invalidAttr)some/path", |
| 166 | | − ":(attr:validAttr +invalidAttr)some/path", |
| 167 | | − ]; |
| 153 | + #[test] |
| 154 | + fn invalid_keywords() { |
| 155 | + let inputs = vec![ |
| 156 | + ":( )some/path", |
| 157 | + ":(tp)some/path", |
| 158 | + ":(top, exclude)some/path", |
| 159 | + ":(top,exclude,icse)some/path", |
| 160 | + ]; |
| 161 | + |
| 162 | + inputs.into_iter().for_each(|input| { |
| 163 | + assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input); |
| 164 | + |
| 165 | + let output = git_pathspec::parse(input.as_bytes()); |
| 166 | + assert!(output.is_err()); |
| 167 | + assert!(matches!(output.unwrap_err(), Error::InvalidKeyword { .. })); |
| 168 | + }); |
| 169 | + } |
| 168 | 170 | |
| 169 | | − for input in inputs { |
| 170 | | − assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input); |
| 171 | + #[test] |
| 172 | + fn invalid_attributes() { |
| 173 | + let inputs = vec![ |
| 174 | + ":(attr:+invalidAttr)some/path", |
| 175 | + ":(attr:validAttr +invalidAttr)some/path", |
| 176 | + ]; |
| 171 | 177 | |
| 172 | | − let output = git_pathspec::parse(input.as_bytes()); |
| 173 | | − assert!(output.is_err()); |
| 174 | | − assert!(matches!(output.unwrap_err(), Error::InvalidAttribute { .. })); |
| 178 | + for input in inputs { |
| 179 | + assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input); |
| 180 | + |
| 181 | + let output = git_pathspec::parse(input.as_bytes()); |
| 182 | + assert!(output.is_err()); |
| 183 | + assert!(matches!(output.unwrap_err(), Error::InvalidAttribute { .. })); |
| 184 | + } |
| 175 | 185 | } |
| 176 | | −} |
| 177 | 186 | |
| 178 | | −#[test] |
| 179 | | −fn should_fail_on_missing_parentheses() { |
| 180 | | − let input = ":(top"; |
| 187 | + #[test] |
| 188 | + fn missing_parentheses() { |
| 189 | + let input = ":(top"; |
| 181 | 190 | |
| 182 | | − assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input); |
| 191 | + assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input); |
| 183 | 192 | |
| 184 | | − let output = git_pathspec::parse(input.as_bytes()); |
| 185 | | − assert!(output.is_err()); |
| 186 | | − assert!(matches!(output.unwrap_err(), Error::MissingClosingParenthesis { .. })); |
| 187 | | −} |
| 193 | + let output = git_pathspec::parse(input.as_bytes()); |
| 194 | + assert!(output.is_err()); |
| 195 | + assert!(matches!(output.unwrap_err(), Error::MissingClosingParenthesis { .. })); |
| 196 | + } |
| 188 | 197 | |
| 189 | | −#[test] |
| 190 | | −fn should_fail_on_glob_and_literal_present() { |
| 191 | | − let input = ":(glob,literal)some/path"; |
| 198 | + #[test] |
| 199 | + fn glob_and_literal_keywords_present() { |
| 200 | + let input = ":(glob,literal)some/path"; |
| 192 | 201 | |
| 193 | | − assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input); |
| 202 | + assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input); |
| 194 | 203 | |
| 195 | | − let output = git_pathspec::parse(input.as_bytes()); |
| 196 | | − assert!(output.is_err()); |
| 197 | | − assert!(matches!(output.unwrap_err(), Error::IncompatibleSearchmodes)); |
| 204 | + let output = git_pathspec::parse(input.as_bytes()); |
| 205 | + assert!(output.is_err()); |
| 206 | + assert!(matches!(output.unwrap_err(), Error::IncompatibleSearchModes)); |
| 207 | + } |
| 198 | 208 | } |
| 199 | 209 | |
| 200 | | −// --- Helpers --- |
| 201 | | − |
| 202 | 210 | fn check_valid_inputs(inputs: Vec<(&str, Pattern)>) { |
| 203 | 211 | inputs.into_iter().for_each(|(input, expected)| { |
| 204 | 212 | assert!(is_valid_in_git(input), "This pathspec is invalid in git: {}", input); |