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 0acbf710..72b9498c 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)) => { @@ -362,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 { @@ -392,7 +392,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(); @@ -412,7 +412,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(); @@ -432,7 +432,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(); @@ -450,4 +450,87 @@ 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"])); + } + + // 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![])); + } } diff --git a/bin/core/imag-link/src/ui.rs b/bin/core/imag-link/src/ui.rs index 2b83fb1e..7459a25d 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) @@ -64,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")