Merge pull request #1043 from matthiasbeyer/imag-link/positional-args

imag-link: positional args
This commit is contained in:
Matthias Beyer 2017-09-02 20:49:31 +02:00 committed by GitHub
commit 609c7fef93
3 changed files with 140 additions and 50 deletions

View file

@ -33,3 +33,9 @@ path = "../../../lib/etc/libimagutil"
default-features = false default-features = false
features = ["testing"] features = ["testing"]
[dev-dependencies.libimagrt]
version = "0.4.0"
path = "../../../lib/core/libimagrt"
default-features = false
features = ["testing"]

View file

@ -108,17 +108,18 @@ 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() { match cmd.subcommand_name() {
Some("list") => {
cmd.subcommand_matches("list")
.map(|matches| handle_internal_linking_list_call(rt, cmd, matches));
},
Some("add") => { Some("add") => {
let (mut from, to) = get_from_to_entry(&rt, "add"); let (mut from, to) = get_from_to_entry(&rt, "add");
for mut to_entry in to { for mut to_entry in to {
if let Err(e) = to_entry.add_internal_link(&mut from) { if let Err(e) = to_entry.add_internal_link(&mut from) {
trace_error_exit(&e, 1); trace_error_exit(&e, 1);
} }
} };
}, },
Some("remove") => { Some("remove") => {
@ -127,21 +128,19 @@ fn handle_internal_linking(rt: &Runtime) {
if let Err(e) = to_entry.remove_internal_link(&mut from) { if let Err(e) = to_entry.remove_internal_link(&mut from) {
trace_error_exit(&e, 1); trace_error_exit(&e, 1);
} }
} };
}, },
_ => unreachable!(), _ => unreachable!(),
};
}
} }
} }
#[inline] #[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; use libimagentrylink::external::is_external_link_storeid;
debug!("List..."); debug!("List...");
for entry in list.split(',') { for entry in list.values_of("entries").unwrap() { // clap has our back
debug!("Listing for '{}'", entry); debug!("Listing for '{}'", entry);
match get_entry_by_name(rt, entry) { match get_entry_by_name(rt, entry) {
Ok(Some(e)) => { Ok(Some(e)) => {
@ -362,6 +361,7 @@ mod tests {
with help "imag-link mocking app"; with help "imag-link mocking app";
} }
use self::mock::generate_test_runtime; use self::mock::generate_test_runtime;
use self::mock::reset_test_runtime;
use libimagutil::testing::DEFAULT_ENTRY; use libimagutil::testing::DEFAULT_ENTRY;
fn create_test_default_entry<'a, S: AsRef<OsStr>>(rt: &'a Runtime, name: S) -> StoreResult<StoreId> { fn create_test_default_entry<'a, S: AsRef<OsStr>>(rt: &'a Runtime, name: S) -> StoreResult<StoreId> {
@ -392,7 +392,7 @@ mod tests {
#[test] #[test]
fn test_link_modificates() { 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(); .unwrap();
let test_id1 = create_test_default_entry(&rt, "test1").unwrap(); let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
@ -412,7 +412,7 @@ mod tests {
#[test] #[test]
fn test_linking_links() { 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(); .unwrap();
let test_id1 = create_test_default_entry(&rt, "test1").unwrap(); let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
@ -432,7 +432,7 @@ mod tests {
#[test] #[test]
fn test_multilinking() { 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(); .unwrap();
let test_id1 = create_test_default_entry(&rt, "test1").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_links1, links_toml_value(vec!["test2"]));
assert_eq!(*test_links2, links_toml_value(vec!["test1"])); 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![]));
}
} }

View file

@ -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)") .about("Add link from one entry to another (and vice-versa)")
.version("0.1") .version("0.1")
.arg(Arg::with_name("from") .arg(Arg::with_name("from")
.long("from") .index(1)
.short("f")
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.multiple(false)
.help("Link from this entry") .help("Link from this entry")
.value_name("ENTRY")) .value_name("ENTRY"))
.arg(Arg::with_name("to") .arg(Arg::with_name("to")
.long("to") .index(2)
.short("t")
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.multiple(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") .about("Remove a link between two or more entries")
.version("0.1") .version("0.1")
.arg(Arg::with_name("from") .arg(Arg::with_name("from")
.long("from") .index(1)
.short("f")
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.multiple(false)
.help("Remove Link from this entry") .help("Remove Link from this entry")
.value_name("ENTRY")) .value_name("ENTRY"))
.arg(Arg::with_name("to") .arg(Arg::with_name("to")
.long("to") .index(2)
.short("t")
.takes_value(true) .takes_value(true)
.required(true) .required(true)
.multiple(true) .multiple(true)
@ -64,20 +62,23 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.value_name("ENTRIES")) .value_name("ENTRIES"))
) )
.arg(Arg::with_name("list") .subcommand(SubCommand::with_name("list")
.long("list") .about("List links to this entry")
.short("l") .version("0.1")
.arg(Arg::with_name("entries")
.index(1)
.takes_value(true) .takes_value(true)
.required(false) .multiple(true)
.help("List links to this entry") .required(true)
.value_name("ENTRY")) .help("List these entries, seperate by comma")
.value_name("ENTRIES"))
.arg(Arg::with_name("list-externals-too") .arg(Arg::with_name("list-externals-too")
.long("list-external") .long("list-external")
.takes_value(false) .takes_value(false)
.required(false) .required(false)
.help("If --list is provided, also list external links (debugging helper that might be removed at some point")) .help("If --list is provided, also list external links (debugging helper that might be removed at some point"))
)
.arg(Arg::with_name("check-consistency") .arg(Arg::with_name("check-consistency")
.long("check-consistency") .long("check-consistency")