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.
This commit is contained in:
Matthias Beyer 2018-03-10 14:53:15 +01:00
parent a0b989efdf
commit da408b9e67
3 changed files with 142 additions and 2 deletions

View file

@ -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");
}
}

View file

@ -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)]

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 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)
}
}