From d27c8ced34eb28b735315e0f323acb0edcf89340 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 15 Oct 2016 10:33:30 +0200 Subject: [PATCH 1/7] Add first internal-linking test --- libimagentrylink/src/internal.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 54d750b0..6fe2cc6e 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -249,3 +249,27 @@ fn process_rw_result(links: StoreResult>) -> Result { Ok(LinkIter::new(links)) } +#[cfg(test)] +mod test { + use std::path::PathBuf; + + use libimagstore::store::Store; + + use super::InternalLinker; + + pub fn get_store() -> Store { + Store::new(PathBuf::from("/"), None).unwrap() + } + + #[test] + fn test_new_entry_no_links() { + let store = get_store(); + let entry = store.create(PathBuf::from("test_new_entry_no_links")).unwrap(); + let links = entry.get_internal_links(); + assert!(links.is_ok()); + let links = links.unwrap(); + assert_eq!(links.collect::>().len(), 0); + } + +} + From 36b77f4e38bcda0f7973e9a3a9ad4782d71bbc8d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 15 Oct 2016 10:41:13 +0200 Subject: [PATCH 2/7] Add test for linking+unlinking --- libimagentrylink/src/internal.rs | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 6fe2cc6e..d0cfec4f 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -271,5 +271,41 @@ mod test { assert_eq!(links.collect::>().len(), 0); } + #[test] + fn test_link_two_entries() { + let store = get_store(); + let mut e1 = store.create(PathBuf::from("test_link_two_entries1")).unwrap(); + assert!(e1.get_internal_links().is_ok()); + + let mut e2 = store.create(PathBuf::from("test_link_two_entries2")).unwrap(); + assert!(e2.get_internal_links().is_ok()); + + { + assert!(e1.add_internal_link(&mut e2).is_ok()); + + let e1_links = e1.get_internal_links().unwrap().collect::>(); + let e2_links = e2.get_internal_links().unwrap().collect::>(); + + assert_eq!(e1_links.len(), 1); + assert_eq!(e2_links.len(), 1); + + assert!(e1_links.first().map(|l| l.clone().with_base(store.path().clone()) == *e2.get_location()).unwrap_or(false)); + assert!(e2_links.first().map(|l| l.clone().with_base(store.path().clone()) == *e1.get_location()).unwrap_or(false)); + } + + { + assert!(e1.remove_internal_link(&mut e2).is_ok()); + + println!("{:?}", e2.to_str()); + let e2_links = e2.get_internal_links().unwrap().collect::>(); + assert_eq!(e2_links.len(), 0, "Expected [], got: {:?}", e2_links); + + println!("{:?}", e1.to_str()); + let e1_links = e1.get_internal_links().unwrap().collect::>(); + assert_eq!(e1_links.len(), 0, "Expected [], got: {:?}", e1_links); + + } + } + } From 4e2bcf7d584314fcfcf14eb8d730c781e1a027cf Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 15 Oct 2016 10:58:50 +0200 Subject: [PATCH 3/7] Add env-logger for test logging --- libimagentrylink/Cargo.toml | 1 + libimagentrylink/src/internal.rs | 7 +++++++ libimagentrylink/src/lib.rs | 3 +++ 3 files changed, 11 insertions(+) diff --git a/libimagentrylink/Cargo.toml b/libimagentrylink/Cargo.toml index e90cc0f5..2628938b 100644 --- a/libimagentrylink/Cargo.toml +++ b/libimagentrylink/Cargo.toml @@ -10,6 +10,7 @@ toml = "0.2.*" semver = "0.2" url = "1.1" rust-crypto = "0.2.35" +env_logger = "0.3" [dependencies.libimagstore] path = "../libimagstore" diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index d0cfec4f..79e37329 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -257,12 +257,18 @@ mod test { use super::InternalLinker; + fn setup_logging() { + use env_logger; + let _ = env_logger::init().unwrap_or(()); + } + pub fn get_store() -> Store { Store::new(PathBuf::from("/"), None).unwrap() } #[test] fn test_new_entry_no_links() { + setup_logging(); let store = get_store(); let entry = store.create(PathBuf::from("test_new_entry_no_links")).unwrap(); let links = entry.get_internal_links(); @@ -273,6 +279,7 @@ mod test { #[test] fn test_link_two_entries() { + setup_logging(); let store = get_store(); let mut e1 = store.create(PathBuf::from("test_link_two_entries1")).unwrap(); assert!(e1.get_internal_links().is_ok()); diff --git a/libimagentrylink/src/lib.rs b/libimagentrylink/src/lib.rs index 31442503..ccc503fd 100644 --- a/libimagentrylink/src/lib.rs +++ b/libimagentrylink/src/lib.rs @@ -38,6 +38,9 @@ extern crate semver; extern crate url; extern crate crypto; +#[cfg(test)] +extern crate env_logger; + #[macro_use] extern crate libimagstore; #[macro_use] extern crate libimagerror; #[macro_use] extern crate libimagutil; From 9c29bf19dadb35914942925c8c1716266f313ecf Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 15 Oct 2016 11:24:14 +0200 Subject: [PATCH 4/7] Add debugging output --- libimagentrylink/src/internal.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 79e37329..267739ef 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -144,6 +144,8 @@ impl InternalLinker for Entry { fn add_internal_link(&mut self, link: &mut Entry) -> Result<()> { let new_link = link.get_location().clone(); + debug!("Adding internal link from {:?} to {:?}", self.get_location(), new_link); + add_foreign_link(link, self.get_location().clone()) .and_then(|_| { self.get_internal_links() @@ -158,13 +160,17 @@ impl InternalLinker for Entry { let own_loc = link.get_location().clone(); let other_loc = link.get_location().clone(); + debug!("Removing internal link from {:?} to {:?}", own_loc, other_loc); + link.get_internal_links() .and_then(|links| { + debug!("Rewriting own links for {:?}, without {:?}", other_loc, own_loc); rewrite_links(self.get_header_mut(), links.filter(|l| *l != own_loc)) }) .and_then(|_| { self.get_internal_links() .and_then(|links| { + debug!("Rewriting own links for {:?}, without {:?}", own_loc, other_loc); rewrite_links(link.get_header_mut(), links.filter(|l| *l != other_loc)) }) }) @@ -184,6 +190,7 @@ fn rewrite_links>(header: &mut EntryHeader, links: I) - }) })); + debug!("Setting new link array: {:?}", links); let process = header.set("imag.links", Value::Array(links)); process_rw_result(process).map(|_| ()) } @@ -191,6 +198,7 @@ fn rewrite_links>(header: &mut EntryHeader, links: I) - /// When Linking A -> B, the specification wants us to link back B -> A. /// This is a helper function which does this. fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> { + debug!("Linking back from {:?} to {:?}", target.get_location(), from); target.get_internal_links() .and_then(|links| { let links = try!(links @@ -205,6 +213,7 @@ fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> { }) }) })); + debug!("Setting links in {:?}: {:?}", target.get_location(), links); process_rw_result(target.get_header_mut().set("imag.links", Value::Array(links))) .map(|_| ()) }) @@ -293,6 +302,9 @@ mod test { let e1_links = e1.get_internal_links().unwrap().collect::>(); let e2_links = e2.get_internal_links().unwrap().collect::>(); + debug!("1 has links: {:?}", e1_links); + debug!("2 has links: {:?}", e2_links); + assert_eq!(e1_links.len(), 1); assert_eq!(e2_links.len(), 1); From 6d31913321090c819bdcd5f43441a5c8efc22a13 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 15 Oct 2016 11:24:42 +0200 Subject: [PATCH 5/7] Fix bug: We have to compare StoreId objects without their base --- libimagentrylink/src/internal.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 267739ef..966968a5 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -157,8 +157,8 @@ impl InternalLinker for Entry { } fn remove_internal_link(&mut self, link: &mut Entry) -> Result<()> { - let own_loc = link.get_location().clone(); - let other_loc = link.get_location().clone(); + let own_loc = self.get_location().clone().without_base(); + let other_loc = link.get_location().clone().without_base(); debug!("Removing internal link from {:?} to {:?}", own_loc, other_loc); From b64eae9654e930f6683bdc6300e87e82b1e53901 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 15 Oct 2016 11:31:30 +0200 Subject: [PATCH 6/7] Add multi-link test --- libimagentrylink/src/internal.rs | 77 ++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 966968a5..4f294035 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -326,5 +326,82 @@ mod test { } } + #[test] + fn test_multiple_links() { + setup_logging(); + let store = get_store(); + + let mut e1 = store.retrieve(PathBuf::from("1")).unwrap(); + let mut e2 = store.retrieve(PathBuf::from("2")).unwrap(); + let mut e3 = store.retrieve(PathBuf::from("3")).unwrap(); + let mut e4 = store.retrieve(PathBuf::from("4")).unwrap(); + let mut e5 = store.retrieve(PathBuf::from("5")).unwrap(); + + assert!(e1.add_internal_link(&mut e2).is_ok()); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e4.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e5.get_internal_links().unwrap().collect::>().len(), 0); + + assert!(e1.add_internal_link(&mut e3).is_ok()); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 2); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e4.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e5.get_internal_links().unwrap().collect::>().len(), 0); + + assert!(e1.add_internal_link(&mut e4).is_ok()); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 3); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e4.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e5.get_internal_links().unwrap().collect::>().len(), 0); + + assert!(e1.add_internal_link(&mut e5).is_ok()); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 4); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e4.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e5.get_internal_links().unwrap().collect::>().len(), 1); + + assert!(e5.remove_internal_link(&mut e1).is_ok()); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 3); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e4.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e5.get_internal_links().unwrap().collect::>().len(), 0); + + assert!(e4.remove_internal_link(&mut e1).is_ok()); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 2); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e4.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e5.get_internal_links().unwrap().collect::>().len(), 0); + + assert!(e3.remove_internal_link(&mut e1).is_ok()); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 1); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e4.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e5.get_internal_links().unwrap().collect::>().len(), 0); + + assert!(e2.remove_internal_link(&mut e1).is_ok()); + + assert_eq!(e1.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e2.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e3.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e4.get_internal_links().unwrap().collect::>().len(), 0); + assert_eq!(e5.get_internal_links().unwrap().collect::>().len(), 0); + + } + } From a796de322aea91993549da25ad60aa9435d1f5ae Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 15 Oct 2016 11:36:25 +0200 Subject: [PATCH 7/7] Fix bug: Links were swapped --- libimagentrylink/src/internal.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libimagentrylink/src/internal.rs b/libimagentrylink/src/internal.rs index 4f294035..3110ff79 100644 --- a/libimagentrylink/src/internal.rs +++ b/libimagentrylink/src/internal.rs @@ -165,13 +165,13 @@ impl InternalLinker for Entry { link.get_internal_links() .and_then(|links| { debug!("Rewriting own links for {:?}, without {:?}", other_loc, own_loc); - rewrite_links(self.get_header_mut(), links.filter(|l| *l != own_loc)) + rewrite_links(link.get_header_mut(), links.filter(|l| *l != own_loc)) }) .and_then(|_| { self.get_internal_links() .and_then(|links| { debug!("Rewriting own links for {:?}, without {:?}", own_loc, other_loc); - rewrite_links(link.get_header_mut(), links.filter(|l| *l != other_loc)) + rewrite_links(self.get_header_mut(), links.filter(|l| *l != other_loc)) }) }) }