Merge pull request #1319 from matthiasbeyer/libimagstore/fix-read-into-single-line-bug

libimagstore: fix read into single line bug
This commit is contained in:
Matthias Beyer 2018-03-02 23:10:06 +01:00 committed by GitHub
commit 7cd3bb059f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View file

@ -60,6 +60,8 @@ This section contains the changelog from the last release to the next release.
when tracing. when tracing.
* A parsing error in `libimagstore`, which caused the parsing of entries * A parsing error in `libimagstore`, which caused the parsing of entries
with a line "---" in the content part to fail, was fixed. with a line "---" in the content part to fail, was fixed.
* 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.
## 0.6.1 ## 0.6.1

View file

@ -123,5 +123,26 @@ Hello World"#).unwrap();
assert_eq!(bah.get_content(), "Hello World"); assert_eq!(bah.get_content(), "Hello World");
} }
#[test]
fn lazy_file_multiline() {
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(), r#"---
[imag]
version = "0.7.0"
---
Hello World
baz"#).unwrap();
lf.write_file_content(&file).unwrap();
let bah = lf.get_file_content(loca).unwrap();
assert_eq!(bah.get_content(), "Hello World\nbaz");
}
} }

View file

@ -46,7 +46,9 @@ 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;
for line in buf.lines().skip(1) { // the first line is "---" let mut iter = buf.lines().skip(1).peekable(); // the first line is "---"
while let Some(line) = iter.next() {
if line == "---" { if line == "---" {
header_consumed = true; header_consumed = true;
// do not further process the line // do not further process the line
@ -54,7 +56,11 @@ pub fn entry_buffer_to_header_content(buf: &str) -> Result<(Value, String)> {
if !header_consumed { if !header_consumed {
let _ = writeln!(header, "{}", line)?; let _ = writeln!(header, "{}", line)?;
} else { } else {
let _ = write!(content, "{}", line)?; if iter.peek().is_some() {
let _ = writeln!(content, "{}", line)?;
} else {
let _ = write!(content, "{}", line)?;
}
} }
} }
} }