Merge pull request #1329 from matthiasbeyer/libimagstore/file-parsing-fix

This fixes the file parsing (again)
This commit is contained in:
Matthias Beyer 2018-03-10 15:45:31 +01:00 committed by GitHub
commit 80cb70d77c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 144 additions and 2 deletions

View file

@ -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 * 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. entries to be read as a single line, which was fixed as well.
* `imag-diary create --timed` did not work as expected * `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 ## 0.6.1

View file

@ -144,5 +144,28 @@ baz"#, env!("CARGO_PKG_VERSION"))).unwrap();
assert_eq!(bah.get_content(), "Hello World\nbaz"); 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");
}
} }

View file

@ -1207,6 +1207,14 @@ version = '0.0.3'
--- ---
Hai"; Hai";
static TEST_ENTRY_TNL : &'static str = "---
[imag]
version = '0.0.3'
---
Hai
";
#[test] #[test]
fn test_entry_from_str() { fn test_entry_from_str() {
use super::Entry; use super::Entry;
@ -1230,6 +1238,17 @@ Hai";
assert_eq!(TEST_ENTRY, string); 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)] #[cfg(test)]

View file

@ -46,10 +46,10 @@ pub fn entry_buffer_to_header_content(buf: &str) -> Result<(Value, String)> {
let mut content = String::new(); let mut content = String::new();
let mut header_consumed = false; 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() { while let Some(line) = iter.next() {
if line == "---" { if line == "---" && !header_consumed {
header_consumed = true; header_consumed = true;
// do not further process the line // do not further process the line
} else { } else {
@ -68,3 +68,101 @@ pub fn entry_buffer_to_header_content(buf: &str) -> Result<(Value, String)> {
Ok((Value::parse(&header)?, String::from(content))) 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)
}
}