History

fix windows tests (#331)

Sebastian Thiel committed 4 years ago

Commit fbcf40e1 parent 0b05be84 files · +71 −14Browse files

Unified · Split

4 files
  1. git-config/src/parse/nom/mod.rs +1 −1
  2. git-config/src/parse/nom/tests.rs +49 −0
  3. git-config/tests/file/mutable/section.rs +9 −7
  4. git-config/tests/file/mutable/value.rs +12 −6
git-config/src/parse/nom/mod.rs+1−1
@@ -372,7 +372,7 @@ fn value_impl<'a>(i: &'a [u8], receive_event: &mut impl FnMut(Event<'a>)) -> IRe · 371 unmodified lines
372372 }
373373
374374 let (i, remainder_value) = {
375− let mut new_index = parsed_index;
375+ let mut new_index = 0;
376376 for index in (offset..parsed_index).rev() {
377377 if !i[index].is_ascii_whitespace() {
378378 new_index = index + 1;
git-config/src/parse/nom/tests.rs+49−0
@@ -163,6 +163,7 @@ mod config_name { · 162 unmodified lines
163163 }
164164
165165 mod section {
166+ use crate::parse::tests::util::newline_custom_event;
166167 use crate::parse::{
167168 error::ParseNode,
168169 section,
@@ -199,6 +200,54 @@ mod section { · 30 unmodified lines
199200 })
200201 }
201202
203+ #[test]
204+ fn empty_value_with_windows_newlines() {
205+ let mut node = ParseNode::SectionHeader;
206+ assert_eq!(
207+ section(b"[a] k = \r\n", &mut node).unwrap(),
208+ fully_consumed((
209+ Section {
210+ header: parsed_section_header("a", None),
211+ events: vec![
212+ whitespace_event(" "),
213+ name_event("k"),
214+ whitespace_event(" "),
215+ Event::KeyValueSeparator,
216+ whitespace_event(" "),
217+ value_event(""),
218+ newline_custom_event("\r\n")
219+ ]
220+ .into(),
221+ },
222+ 1
223+ )),
224+ );
225+ }
226+
227+ #[test]
228+ fn simple_value_with_windows_newlines() {
229+ let mut node = ParseNode::SectionHeader;
230+ assert_eq!(
231+ section(b"[a] k = v\r\n", &mut node).unwrap(),
232+ fully_consumed((
233+ Section {
234+ header: parsed_section_header("a", None),
235+ events: vec![
236+ whitespace_event(" "),
237+ name_event("k"),
238+ whitespace_event(" "),
239+ Event::KeyValueSeparator,
240+ whitespace_event(" "),
241+ value_event("v"),
242+ newline_custom_event("\r\n")
243+ ]
244+ .into(),
245+ },
246+ 1
247+ )),
248+ );
249+ }
250+
202251 #[test]
203252 fn empty_section() {
204253 let mut node = ParseNode::SectionHeader;
git-config/tests/file/mutable/section.rs+9−7
@@ -128,18 +128,20 @@ mod push { · 127 unmodified lines
128128 #[test]
129129 fn values_are_escaped() {
130130 for (value, expected) in [
131− ("a b", "$head\tk = a b\n"),
132− (" a b", "$head\tk = \" a b\"\n"),
133− ("a b\t", "$head\tk = \"a b\\t\"\n"),
134− (";c", "$head\tk = \";c\"\n"),
135− ("#c", "$head\tk = \"#c\"\n"),
136− ("a\nb\n\tc", "$head\tk = a\\nb\\n\\tc\n"),
131+ ("a b", "$head\tk = a b$nl"),
132+ (" a b", "$head\tk = \" a b\"$nl"),
133+ ("a b\t", "$head\tk = \"a b\\t\"$nl"),
134+ (";c", "$head\tk = \";c\"$nl"),
135+ ("#c", "$head\tk = \"#c\"$nl"),
136+ ("a\nb\n\tc", "$head\tk = a\\nb\\n\\tc$nl"),
137137 ] {
138138 let mut config = git_config::File::default();
139139 let mut section = config.new_section("a", None).unwrap();
140140 section.set_implicit_newline(false);
141141 section.push(Key::try_from("k").unwrap(), value);
142− let expected = expected.replace("$head", &format!("[a]{nl}", nl = section.newline()));
142+ let expected = expected
143+ .replace("$head", &format!("[a]{nl}", nl = section.newline()))
144+ .replace("$nl", &section.newline().to_string());
143145 assert_eq!(config.to_bstring(), expected);
144146 }
145147 }
git-config/tests/file/mutable/value.rs+12−6
@@ -42,26 +42,32 @@ mod set_string { · 41 unmodified lines
4242 use crate::file::mutable::value::init_config;
4343
4444 fn assert_set_string(expected: &str) {
45+ let nl = git_config::File::default().detect_newline_style().to_string();
4546 for input in [
4647 "[a] k = v",
4748 "[a] k = ",
4849 "[a] k =",
49− "[a] k =\n",
50+ "[a] k =$nl",
5051 "[a] k ",
51− "[a] k\n",
52+ "[a] k$nl",
5253 "[a] k",
5354 ] {
54− let mut file: git_config::File = input.parse().unwrap();
55+ let mut file: git_config::File = input.replace("$nl", &nl).parse().unwrap();
5556 let mut v = file.raw_value_mut("a", None, "k").unwrap();
5657 v.set_string(expected);
5758
5859 assert_eq!(v.get().unwrap().as_ref(), expected);
5960
60− let file: git_config::File = match file.to_string().parse() {
61+ let file_string = file.to_string();
62+ let file: git_config::File = match file_string.parse() {
6163 Ok(f) => f,
62− Err(err) => panic!("{:?} failed with: {}", file.to_string(), err),
64+ Err(err) => panic!("{:?} failed with: {}", file_string, err),
6365 };
64− assert_eq!(file.raw_value("a", None, "k").expect("present").as_ref(), expected);
66+ assert_eq!(
67+ file.raw_value("a", None, "k").expect("present").as_ref(),
68+ expected,
69+ "{file_string:?}"
70+ );
6571 }
6672 }
6773