diff --git a/lib/core/libimagstore/src/file_abstraction/mod.rs b/lib/core/libimagstore/src/file_abstraction/mod.rs index f38b6590..6c3807ea 100644 --- a/lib/core/libimagstore/src/file_abstraction/mod.rs +++ b/lib/core/libimagstore/src/file_abstraction/mod.rs @@ -144,5 +144,28 @@ baz"#, env!("CARGO_PKG_VERSION"))).unwrap(); assert_eq!(bah.get_content(), "Hello World\nbaz"); } + #[test] + fn lazy_file_multiline_trailing_newlines() { + let fs = InMemoryFileAbstraction::new(); + + let mut path = PathBuf::from("tests"); + path.set_file_name("test1"); + let mut lf = InMemoryFileAbstractionInstance::new(fs.backend().clone(), path.clone()); + + let loca = StoreId::new_baseless(path).unwrap(); + let file = Entry::from_str(loca.clone(), &format!(r#"--- +[imag] +version = "{}" +--- +Hello World +baz + +"#, env!("CARGO_PKG_VERSION"))).unwrap(); + + lf.write_file_content(&file).unwrap(); + let bah = lf.get_file_content(loca).unwrap(); + assert_eq!(bah.get_content(), "Hello World\nbaz\n\n"); + } + } diff --git a/lib/core/libimagstore/src/store.rs b/lib/core/libimagstore/src/store.rs index 8e502b09..a94329d8 100644 --- a/lib/core/libimagstore/src/store.rs +++ b/lib/core/libimagstore/src/store.rs @@ -1207,6 +1207,14 @@ version = '0.0.3' --- Hai"; + static TEST_ENTRY_TNL : &'static str = "--- +[imag] +version = '0.0.3' +--- +Hai + +"; + #[test] fn test_entry_from_str() { use super::Entry; @@ -1230,6 +1238,17 @@ Hai"; assert_eq!(TEST_ENTRY, string); } + #[test] + fn test_entry_to_str_trailing_newline() { + use super::Entry; + use std::path::PathBuf; + println!("{}", TEST_ENTRY_TNL); + let entry = Entry::from_str(StoreId::new_baseless(PathBuf::from("test/foo~1.3")).unwrap(), + TEST_ENTRY_TNL).unwrap(); + let string = entry.to_str(); + + assert_eq!(TEST_ENTRY_TNL, string); + } } #[cfg(test)] diff --git a/lib/core/libimagstore/src/util.rs b/lib/core/libimagstore/src/util.rs index 55222f71..e353d3f4 100644 --- a/lib/core/libimagstore/src/util.rs +++ b/lib/core/libimagstore/src/util.rs @@ -46,10 +46,10 @@ pub fn entry_buffer_to_header_content(buf: &str) -> Result<(Value, String)> { let mut content = String::new(); let mut header_consumed = false; - let mut iter = buf.lines().skip(1).peekable(); // the first line is "---" + let mut iter = buf.split("\n").skip(1).peekable(); // the first line is "---" while let Some(line) = iter.next() { - if line == "---" { + if line == "---" && !header_consumed { header_consumed = true; // do not further process the line } else { @@ -68,3 +68,101 @@ pub fn entry_buffer_to_header_content(buf: &str) -> Result<(Value, String)> { Ok((Value::parse(&header)?, String::from(content))) } +#[cfg(test)] +mod test { + use super::entry_buffer_to_header_content; + + fn mkfile(content: &str) -> String { + format!(r#"--- +[imag] +version = '{version}' +--- +{content}"#, version = env!("CARGO_PKG_VERSION"), content = content) + } + + #[test] + fn test_entry_buffer_to_header_content_1() { + let content = "Hai"; + let file = format!(r#"--- +[imag] +version = '{version}' +--- +{content}"#, version = env!("CARGO_PKG_VERSION"), content = content); + + let res = entry_buffer_to_header_content(&file); + + assert!(res.is_ok()); + let (_, res_content) = res.unwrap(); + assert_eq!(res_content, content) + } + + #[test] + fn test_entry_buffer_to_header_content_2() { + let content = r#"Hai +"#; + + let file = mkfile(&content); + eprintln!("FILE: <<<{}>>>", file); + let res = entry_buffer_to_header_content(&file); + + assert!(res.is_ok()); + let (_, res_content) = res.unwrap(); + eprintln!("CONTENT: <<<{}>>>", res_content); + assert_eq!(res_content, content) + } + + #[test] + fn test_entry_buffer_to_header_content_3() { + let content = r#"Hai + +barbar + +"#; + + let file = mkfile(&content); + let res = entry_buffer_to_header_content(&file); + + assert!(res.is_ok()); + let (_, res_content) = res.unwrap(); + assert_eq!(res_content, content) + } + + #[test] + fn test_entry_buffer_to_header_content_4() { + let content = r#"Hai + + --- +barbar + --- + +"#; + + let file = mkfile(&content); + let res = entry_buffer_to_header_content(&file); + + assert!(res.is_ok()); + let (_, res_content) = res.unwrap(); + assert_eq!(res_content, content) + } + + #[test] + fn test_entry_buffer_to_header_content_5() { + let content = r#"Hai + +--- +barbar +--- + + +"#; + + let file = mkfile(&content); + let res = entry_buffer_to_header_content(&file); + + assert!(res.is_ok()); + let (_, res_content) = res.unwrap(); + assert_eq!(res_content, content) + } + +} +