From da408b9e677f7db9d2e699ba306874c68fc3f784 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 10 Mar 2018 14:53:15 +0100 Subject: [PATCH 1/2] This fixes the file parsing (again) Unfortunately, our latest fix to file parsing did not solve all issues. So we have to fix it _again_. The problem was the `std::str::Lines` iterator, which apparently fails this: assert_eq!(1, "".lines().count()); as an empty line seems not to be a line. Because of that, when reading a file with an empty line at its bottom got stripped off that line. This patch removes the use of the `lines()` iterator and uses `split("\n")` instead. This only works on Unix operating systems, but as we only target unix operating systems with imag, this is not considered an issue right now. This patch also adds extensive tests on multiple levels in the `libimagstore` implementation: * On the parsing level, for the function which implements the parsing * On the filesystem abstraction levels * On the `Store` levels to make sure that everything is parsed correctly. --- .../libimagstore/src/file_abstraction/mod.rs | 23 ++++ lib/core/libimagstore/src/store.rs | 19 ++++ lib/core/libimagstore/src/util.rs | 102 +++++++++++++++++- 3 files changed, 142 insertions(+), 2 deletions(-) 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) + } + +} + From e07e30a502a69d6844e00e911d67120cad5b9958 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 10 Mar 2018 15:00:38 +0100 Subject: [PATCH 2/2] Add changelog entry --- doc/src/09020-changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/src/09020-changelog.md b/doc/src/09020-changelog.md index a0a63846..84be3f2b 100644 --- a/doc/src/09020-changelog.md +++ b/doc/src/09020-changelog.md @@ -64,6 +64,8 @@ This section contains the changelog from the last release to the next release. * The patch explained by the point above introduced a bug which caused entries to be read as a single line, which was fixed as well. * `imag-diary create --timed` did not work as expected + * `libimagstore` got another fix with the file parsing, as the + `std::str::Lines` iterator takes empty lines as no lines. ## 0.6.1