From 46dcbb828ec648b2a48c7a552ad367dafc7b73e5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 1 Sep 2017 21:16:45 +0200 Subject: [PATCH 1/5] Rewrite UI to use positional arguments --- bin/core/imag-link/src/ui.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/bin/core/imag-link/src/ui.rs b/bin/core/imag-link/src/ui.rs index 2b83fb1e..d8d0b7a7 100644 --- a/bin/core/imag-link/src/ui.rs +++ b/bin/core/imag-link/src/ui.rs @@ -28,15 +28,14 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .about("Add link from one entry to another (and vice-versa)") .version("0.1") .arg(Arg::with_name("from") - .long("from") - .short("f") + .index(1) .takes_value(true) .required(true) + .multiple(false) .help("Link from this entry") .value_name("ENTRY")) .arg(Arg::with_name("to") - .long("to") - .short("t") + .index(2) .takes_value(true) .required(true) .multiple(true) @@ -48,15 +47,14 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .about("Remove a link between two or more entries") .version("0.1") .arg(Arg::with_name("from") - .long("from") - .short("f") + .index(1) .takes_value(true) .required(true) + .multiple(false) .help("Remove Link from this entry") .value_name("ENTRY")) .arg(Arg::with_name("to") - .long("to") - .short("t") + .index(2) .takes_value(true) .required(true) .multiple(true) From 2a203060995133cf35889cd93d6867b9b32503f5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 1 Sep 2017 21:29:29 +0200 Subject: [PATCH 2/5] Rewrite link listing to work with positional argument --- bin/core/imag-link/src/main.rs | 51 +++++++++++++++++----------------- bin/core/imag-link/src/ui.rs | 29 ++++++++++--------- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/bin/core/imag-link/src/main.rs b/bin/core/imag-link/src/main.rs index 0acbf710..8914930d 100644 --- a/bin/core/imag-link/src/main.rs +++ b/bin/core/imag-link/src/main.rs @@ -108,40 +108,39 @@ fn handle_internal_linking(rt: &Runtime) { } } - match cmd.value_of("list") { - Some(list) => handle_internal_linking_list_call(rt, cmd, list), - None => { - match cmd.subcommand_name() { - Some("add") => { - let (mut from, to) = get_from_to_entry(&rt, "add"); - for mut to_entry in to { - if let Err(e) = to_entry.add_internal_link(&mut from) { - trace_error_exit(&e, 1); - } - } - }, - - Some("remove") => { - let (mut from, to) = get_from_to_entry(&rt, "remove"); - for mut to_entry in to { - if let Err(e) = to_entry.remove_internal_link(&mut from) { - trace_error_exit(&e, 1); - } - } - }, - - _ => unreachable!(), + match cmd.subcommand_name() { + Some("list") => { + cmd.subcommand_matches("list") + .map(|matches| handle_internal_linking_list_call(rt, cmd, matches)); + }, + Some("add") => { + let (mut from, to) = get_from_to_entry(&rt, "add"); + for mut to_entry in to { + if let Err(e) = to_entry.add_internal_link(&mut from) { + trace_error_exit(&e, 1); + } }; - } + }, + + Some("remove") => { + let (mut from, to) = get_from_to_entry(&rt, "remove"); + for mut to_entry in to { + if let Err(e) = to_entry.remove_internal_link(&mut from) { + trace_error_exit(&e, 1); + } + }; + }, + + _ => unreachable!(), } } #[inline] -fn handle_internal_linking_list_call(rt: &Runtime, cmd: &ArgMatches, list: &str) { +fn handle_internal_linking_list_call(rt: &Runtime, cmd: &ArgMatches, list: &ArgMatches) { use libimagentrylink::external::is_external_link_storeid; debug!("List..."); - for entry in list.split(',') { + for entry in list.values_of("entries").unwrap() { // clap has our back debug!("Listing for '{}'", entry); match get_entry_by_name(rt, entry) { Ok(Some(e)) => { diff --git a/bin/core/imag-link/src/ui.rs b/bin/core/imag-link/src/ui.rs index d8d0b7a7..7459a25d 100644 --- a/bin/core/imag-link/src/ui.rs +++ b/bin/core/imag-link/src/ui.rs @@ -62,20 +62,23 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .value_name("ENTRIES")) ) - .arg(Arg::with_name("list") - .long("list") - .short("l") - .takes_value(true) - .required(false) - .help("List links to this entry") - .value_name("ENTRY")) + .subcommand(SubCommand::with_name("list") + .about("List links to this entry") + .version("0.1") + .arg(Arg::with_name("entries") + .index(1) + .takes_value(true) + .multiple(true) + .required(true) + .help("List these entries, seperate by comma") + .value_name("ENTRIES")) - - .arg(Arg::with_name("list-externals-too") - .long("list-external") - .takes_value(false) - .required(false) - .help("If --list is provided, also list external links (debugging helper that might be removed at some point")) + .arg(Arg::with_name("list-externals-too") + .long("list-external") + .takes_value(false) + .required(false) + .help("If --list is provided, also list external links (debugging helper that might be removed at some point")) + ) .arg(Arg::with_name("check-consistency") .long("check-consistency") From d538d5fffe6157b5f371427858f2df32d8e9f16e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 2 Sep 2017 10:18:53 +0200 Subject: [PATCH 3/5] Adapt tests to new UI --- bin/core/imag-link/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/core/imag-link/src/main.rs b/bin/core/imag-link/src/main.rs index 8914930d..163bb629 100644 --- a/bin/core/imag-link/src/main.rs +++ b/bin/core/imag-link/src/main.rs @@ -391,7 +391,7 @@ mod tests { #[test] fn test_link_modificates() { - let rt = generate_test_runtime(vec!["internal", "add", "--from", "test1", "--to", "test2"]) + let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2"]) .unwrap(); let test_id1 = create_test_default_entry(&rt, "test1").unwrap(); @@ -411,7 +411,7 @@ mod tests { #[test] fn test_linking_links() { - let rt = generate_test_runtime(vec!["internal", "add", "--from", "test1", "--to", "test2"]) + let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2"]) .unwrap(); let test_id1 = create_test_default_entry(&rt, "test1").unwrap(); @@ -431,7 +431,7 @@ mod tests { #[test] fn test_multilinking() { - let rt = generate_test_runtime(vec!["internal", "add", "--from", "test1", "--to", "test2"]) + let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2"]) .unwrap(); let test_id1 = create_test_default_entry(&rt, "test1").unwrap(); From e7ecea0065d4eccc337f20c4758d3eb5828b23c5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 2 Sep 2017 10:21:20 +0200 Subject: [PATCH 4/5] Add test to check links from one entry to two others --- bin/core/imag-link/src/main.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/bin/core/imag-link/src/main.rs b/bin/core/imag-link/src/main.rs index 163bb629..9c3f20ab 100644 --- a/bin/core/imag-link/src/main.rs +++ b/bin/core/imag-link/src/main.rs @@ -449,4 +449,30 @@ mod tests { assert_eq!(*test_links1, links_toml_value(vec!["test2"])); assert_eq!(*test_links2, links_toml_value(vec!["test1"])); } + + #[test] + fn test_linking_more_than_two() { + let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2", "test3"]) + .unwrap(); + + let test_id1 = create_test_default_entry(&rt, "test1").unwrap(); + let test_id2 = create_test_default_entry(&rt, "test2").unwrap(); + let test_id3 = create_test_default_entry(&rt, "test3").unwrap(); + + handle_internal_linking(&rt); + handle_internal_linking(&rt); + + let test_entry1 = rt.store().get(test_id1).unwrap().unwrap(); + let test_links1 = get_entry_links(&test_entry1).unwrap(); + + let test_entry2 = rt.store().get(test_id2).unwrap().unwrap(); + let test_links2 = get_entry_links(&test_entry2).unwrap(); + + let test_entry3 = rt.store().get(test_id3).unwrap().unwrap(); + let test_links3 = get_entry_links(&test_entry3).unwrap(); + + assert_eq!(*test_links1, links_toml_value(vec!["test2", "test3"])); + assert_eq!(*test_links2, links_toml_value(vec!["test1"])); + assert_eq!(*test_links3, links_toml_value(vec!["test1"])); + } } From 3a959ddfd1f14bd71d649f84ba5102ab4ce8760b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 2 Sep 2017 10:41:21 +0200 Subject: [PATCH 5/5] Add multi-call tests: add-remove --- bin/core/imag-link/Cargo.toml | 6 ++++ bin/core/imag-link/src/main.rs | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/bin/core/imag-link/Cargo.toml b/bin/core/imag-link/Cargo.toml index ce87acaf..15b7cc0e 100644 --- a/bin/core/imag-link/Cargo.toml +++ b/bin/core/imag-link/Cargo.toml @@ -33,3 +33,9 @@ path = "../../../lib/etc/libimagutil" default-features = false features = ["testing"] +[dev-dependencies.libimagrt] +version = "0.4.0" +path = "../../../lib/core/libimagrt" +default-features = false +features = ["testing"] + diff --git a/bin/core/imag-link/src/main.rs b/bin/core/imag-link/src/main.rs index 9c3f20ab..72b9498c 100644 --- a/bin/core/imag-link/src/main.rs +++ b/bin/core/imag-link/src/main.rs @@ -361,6 +361,7 @@ mod tests { with help "imag-link mocking app"; } use self::mock::generate_test_runtime; + use self::mock::reset_test_runtime; use libimagutil::testing::DEFAULT_ENTRY; fn create_test_default_entry<'a, S: AsRef>(rt: &'a Runtime, name: S) -> StoreResult { @@ -475,4 +476,61 @@ mod tests { assert_eq!(*test_links2, links_toml_value(vec!["test1"])); assert_eq!(*test_links3, links_toml_value(vec!["test1"])); } + + // Remove tests + + #[test] + fn test_linking_links_unlinking_removes_links() { + let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2"]) + .unwrap(); + + let test_id1 = create_test_default_entry(&rt, "test1").unwrap(); + let test_id2 = create_test_default_entry(&rt, "test2").unwrap(); + + handle_internal_linking(&rt); + + let rt = reset_test_runtime(vec!["internal", "remove", "test1", "test2"], rt) + .unwrap(); + + handle_internal_linking(&rt); + + let test_entry1 = rt.store().get(test_id1).unwrap().unwrap(); + let test_links1 = get_entry_links(&test_entry1).unwrap(); + + let test_entry2 = rt.store().get(test_id2).unwrap().unwrap(); + let test_links2 = get_entry_links(&test_entry2).unwrap(); + + assert_eq!(*test_links1, links_toml_value(vec![])); + assert_eq!(*test_links2, links_toml_value(vec![])); + } + + #[test] + fn test_linking_and_unlinking_more_than_two() { + let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2", "test3"]) + .unwrap(); + + let test_id1 = create_test_default_entry(&rt, "test1").unwrap(); + let test_id2 = create_test_default_entry(&rt, "test2").unwrap(); + let test_id3 = create_test_default_entry(&rt, "test3").unwrap(); + + handle_internal_linking(&rt); + + let rt = reset_test_runtime(vec!["internal", "remove", "test1", "test2", "test3"], rt) + .unwrap(); + + handle_internal_linking(&rt); + + let test_entry1 = rt.store().get(test_id1).unwrap().unwrap(); + let test_links1 = get_entry_links(&test_entry1).unwrap(); + + let test_entry2 = rt.store().get(test_id2).unwrap().unwrap(); + let test_links2 = get_entry_links(&test_entry2).unwrap(); + + let test_entry3 = rt.store().get(test_id3).unwrap().unwrap(); + let test_links3 = get_entry_links(&test_entry3).unwrap(); + + assert_eq!(*test_links1, links_toml_value(vec![])); + assert_eq!(*test_links2, links_toml_value(vec![])); + assert_eq!(*test_links3, links_toml_value(vec![])); + } }