2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
|
|
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2016-04-16 20:46:46 +00:00
|
|
|
#![deny(
|
|
|
|
non_camel_case_types,
|
|
|
|
non_snake_case,
|
|
|
|
path_statements,
|
|
|
|
trivial_numeric_casts,
|
|
|
|
unstable_features,
|
|
|
|
unused_allocation,
|
|
|
|
unused_import_braces,
|
|
|
|
unused_imports,
|
|
|
|
unused_must_use,
|
|
|
|
unused_mut,
|
|
|
|
unused_qualifications,
|
|
|
|
while_true,
|
|
|
|
)]
|
|
|
|
|
2016-02-23 11:06:28 +00:00
|
|
|
#[macro_use] extern crate log;
|
|
|
|
extern crate clap;
|
2016-04-12 15:49:23 +00:00
|
|
|
extern crate url;
|
2016-02-23 11:06:28 +00:00
|
|
|
#[macro_use] extern crate version;
|
2017-08-29 15:21:52 +00:00
|
|
|
#[cfg(test)] extern crate toml;
|
|
|
|
#[cfg(test)] extern crate toml_query;
|
2016-02-23 11:06:28 +00:00
|
|
|
|
2016-04-16 19:56:16 +00:00
|
|
|
extern crate libimagentrylink;
|
2016-02-23 11:06:28 +00:00
|
|
|
extern crate libimagrt;
|
|
|
|
extern crate libimagstore;
|
2016-05-16 16:59:02 +00:00
|
|
|
extern crate libimagerror;
|
2017-07-18 19:27:03 +00:00
|
|
|
|
2017-08-30 21:40:31 +00:00
|
|
|
#[cfg(test)]
|
2017-07-18 19:27:03 +00:00
|
|
|
#[macro_use]
|
2016-08-28 12:31:10 +00:00
|
|
|
extern crate libimagutil;
|
2016-02-23 11:06:28 +00:00
|
|
|
|
2017-08-30 21:40:31 +00:00
|
|
|
#[cfg(not(test))]
|
|
|
|
extern crate libimagutil;
|
|
|
|
|
2016-02-23 11:36:32 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
2016-05-18 17:06:05 +00:00
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
2016-02-23 11:36:32 +00:00
|
|
|
use libimagstore::error::StoreError;
|
|
|
|
use libimagstore::store::Entry;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
2016-02-23 11:06:28 +00:00
|
|
|
use libimagstore::store::Store;
|
2016-09-06 09:09:42 +00:00
|
|
|
use libimagerror::trace::{MapErrTrace, trace_error, trace_error_exit};
|
2016-04-16 19:56:16 +00:00
|
|
|
use libimagentrylink::external::ExternalLinker;
|
2017-07-04 16:27:01 +00:00
|
|
|
use libimagentrylink::internal::InternalLinker;
|
2016-08-28 12:31:10 +00:00
|
|
|
use libimagutil::warn_result::*;
|
2016-09-05 17:29:06 +00:00
|
|
|
use libimagutil::warn_exit::warn_exit;
|
2016-09-06 09:09:42 +00:00
|
|
|
use libimagutil::info_result::*;
|
2016-04-12 15:49:23 +00:00
|
|
|
use clap::ArgMatches;
|
|
|
|
use url::Url;
|
2016-02-23 11:06:28 +00:00
|
|
|
|
2016-02-23 11:15:36 +00:00
|
|
|
mod ui;
|
|
|
|
|
|
|
|
use ui::build_ui;
|
|
|
|
|
2016-02-23 11:04:41 +00:00
|
|
|
fn main() {
|
2016-05-18 17:06:05 +00:00
|
|
|
let rt = generate_runtime_setup("imag-link",
|
|
|
|
&version!()[..],
|
|
|
|
"Link entries",
|
|
|
|
build_ui);
|
2016-02-23 11:36:32 +00:00
|
|
|
|
|
|
|
rt.cli()
|
|
|
|
.subcommand_name()
|
|
|
|
.map(|name| {
|
|
|
|
match name {
|
|
|
|
"internal" => handle_internal_linking(&rt),
|
2016-04-12 15:49:23 +00:00
|
|
|
"external" => handle_external_linking(&rt),
|
2016-09-05 17:29:06 +00:00
|
|
|
_ => warn_exit("No commandline call", 1)
|
2016-02-23 11:36:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_internal_linking(rt: &Runtime) {
|
2017-07-04 17:23:15 +00:00
|
|
|
use libimagentrylink::internal::store_check::StoreLinkConsistentExt;
|
2016-02-23 11:36:32 +00:00
|
|
|
|
2016-04-12 19:12:38 +00:00
|
|
|
debug!("Handle internal linking call");
|
|
|
|
let cmd = rt.cli().subcommand_matches("internal").unwrap();
|
2016-02-23 11:36:32 +00:00
|
|
|
|
2017-07-04 17:23:15 +00:00
|
|
|
if cmd.is_present("check-consistency") {
|
|
|
|
match rt.store().check_link_consistency() {
|
|
|
|
Ok(_) => {
|
|
|
|
info!("Store is consistent");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
|
|
|
::std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 19:29:29 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-10-31 22:55:17 +00:00
|
|
|
};
|
2017-09-01 19:29:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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!(),
|
2016-02-23 11:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 16:27:01 +00:00
|
|
|
#[inline]
|
2017-09-01 19:29:29 +00:00
|
|
|
fn handle_internal_linking_list_call(rt: &Runtime, cmd: &ArgMatches, list: &ArgMatches) {
|
2017-07-04 16:27:01 +00:00
|
|
|
use libimagentrylink::external::is_external_link_storeid;
|
|
|
|
|
|
|
|
debug!("List...");
|
2017-09-01 19:29:29 +00:00
|
|
|
for entry in list.values_of("entries").unwrap() { // clap has our back
|
2017-07-04 16:27:01 +00:00
|
|
|
debug!("Listing for '{}'", entry);
|
|
|
|
match get_entry_by_name(rt, entry) {
|
|
|
|
Ok(Some(e)) => {
|
|
|
|
e.get_internal_links()
|
|
|
|
.map(|iter| {
|
|
|
|
iter.filter(move |id| {
|
|
|
|
cmd.is_present("list-externals-too") || !is_external_link_storeid(&id)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.map(|links| {
|
|
|
|
let i = links
|
|
|
|
.filter_map(|l| {
|
|
|
|
l.to_str()
|
|
|
|
.map_warn_err(|e| format!("Failed to convert StoreId to string: {:?}", e))
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.enumerate();
|
|
|
|
|
|
|
|
for (i, link) in i {
|
|
|
|
println!("{: <3}: {}", i, link);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_err_trace()
|
|
|
|
.ok();
|
|
|
|
},
|
|
|
|
|
|
|
|
Ok(None) => {
|
|
|
|
warn!("Entry not found: {:?}", entry);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!("Listing ready!");
|
|
|
|
}
|
|
|
|
|
2017-07-04 16:16:50 +00:00
|
|
|
fn get_from_to_entry<'a>(rt: &'a Runtime, subcommand: &str) -> (FileLockEntry<'a>, Vec<FileLockEntry<'a>>) {
|
|
|
|
let from = match get_from_entry(&rt, subcommand) {
|
|
|
|
None => warn_exit("No 'from' entry", 1),
|
|
|
|
Some(s) => s,
|
|
|
|
};
|
|
|
|
debug!("Link from = {:?}", from.deref());
|
|
|
|
|
|
|
|
let to = match get_to_entries(&rt, subcommand) {
|
|
|
|
None => warn_exit("No 'to' entry", 1),
|
|
|
|
Some(to) => to,
|
|
|
|
};
|
|
|
|
debug!("Link to = {:?}", to.iter().map(|f| f.deref()).collect::<Vec<&Entry>>());
|
|
|
|
|
|
|
|
(from, to)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_from_entry<'a>(rt: &'a Runtime, subcommand: &str) -> Option<FileLockEntry<'a>> {
|
2016-02-23 11:36:32 +00:00
|
|
|
rt.cli()
|
|
|
|
.subcommand_matches("internal")
|
|
|
|
.unwrap() // safe, we know there is an "internal" subcommand"
|
2017-07-04 16:16:50 +00:00
|
|
|
.subcommand_matches(subcommand)
|
2016-04-12 19:56:01 +00:00
|
|
|
.unwrap() // safe, we know there is an "add" subcommand
|
2016-02-23 11:36:32 +00:00
|
|
|
.value_of("from")
|
|
|
|
.and_then(|from_name| {
|
|
|
|
match get_entry_by_name(rt, from_name) {
|
2016-04-12 19:56:01 +00:00
|
|
|
Err(e) => {
|
|
|
|
debug!("We couldn't get the entry from name: '{:?}'", from_name);
|
|
|
|
trace_error(&e); None
|
|
|
|
},
|
2016-08-26 08:20:34 +00:00
|
|
|
Ok(Some(e)) => Some(e),
|
|
|
|
Ok(None) => None,
|
2016-02-23 11:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-04 16:16:50 +00:00
|
|
|
fn get_to_entries<'a>(rt: &'a Runtime, subcommand: &str) -> Option<Vec<FileLockEntry<'a>>> {
|
2016-02-23 11:36:32 +00:00
|
|
|
rt.cli()
|
|
|
|
.subcommand_matches("internal")
|
|
|
|
.unwrap() // safe, we know there is an "internal" subcommand"
|
2017-07-04 16:16:50 +00:00
|
|
|
.subcommand_matches(subcommand)
|
2016-04-12 19:56:01 +00:00
|
|
|
.unwrap() // safe, we know there is an "add" subcommand
|
2016-02-23 11:36:32 +00:00
|
|
|
.values_of("to")
|
|
|
|
.map(|values| {
|
|
|
|
let mut v = vec![];
|
|
|
|
for entry in values.map(|v| get_entry_by_name(rt, v)) {
|
|
|
|
match entry {
|
|
|
|
Err(e) => trace_error(&e),
|
2016-08-26 08:20:34 +00:00
|
|
|
Ok(Some(e)) => v.push(e),
|
|
|
|
Ok(None) => warn!("Entry not found: {:?}", v),
|
2016-02-23 11:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
v
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:20:34 +00:00
|
|
|
fn get_entry_by_name<'a>(rt: &'a Runtime, name: &str) -> Result<Option<FileLockEntry<'a>>, StoreError> {
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use libimagstore::storeid::StoreId;
|
|
|
|
|
|
|
|
StoreId::new(Some(rt.store().path().clone()), PathBuf::from(name))
|
|
|
|
.and_then(|id| rt.store().get(id))
|
2016-02-23 11:04:41 +00:00
|
|
|
}
|
2016-02-23 11:06:28 +00:00
|
|
|
|
2016-04-12 15:49:23 +00:00
|
|
|
fn handle_external_linking(rt: &Runtime) {
|
|
|
|
let scmd = rt.cli().subcommand_matches("external").unwrap();
|
|
|
|
let entry_name = scmd.value_of("id").unwrap(); // enforced by clap
|
2016-10-31 22:56:21 +00:00
|
|
|
let mut entry = match get_entry_by_name(rt, entry_name) {
|
|
|
|
Err(e) => trace_error_exit(&e, 1),
|
|
|
|
Ok(None) => {
|
|
|
|
warn!("Entry not found: {:?}", entry_name);
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
Ok(Some(entry)) => entry
|
|
|
|
};
|
2016-04-12 15:49:23 +00:00
|
|
|
|
|
|
|
if scmd.is_present("add") {
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("Adding link to entry!");
|
2016-04-12 15:49:23 +00:00
|
|
|
add_link_to_entry(rt.store(), scmd, &mut entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if scmd.is_present("remove") {
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("Removing link from entry!");
|
2016-04-12 15:49:23 +00:00
|
|
|
remove_link_from_entry(rt.store(), scmd, &mut entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if scmd.is_present("set") {
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("Setting links in entry!");
|
2016-04-12 15:49:23 +00:00
|
|
|
set_links_for_entry(rt.store(), scmd, &mut entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if scmd.is_present("list") {
|
2016-04-12 20:27:57 +00:00
|
|
|
debug!("Listing links in entry!");
|
2016-04-12 15:49:23 +00:00
|
|
|
list_links_for_entry(rt.store(), &mut entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
panic!("Clap failed to enforce one of 'add', 'remove', 'set' or 'list'");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_link_to_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
|
2016-09-06 09:09:42 +00:00
|
|
|
Url::parse(matches.value_of("add").unwrap())
|
|
|
|
.map_err_trace_exit(1)
|
|
|
|
.map(|link| entry.add_external_link(store, link).map_err_trace().map_info_str("Ok"))
|
|
|
|
.ok();
|
2016-04-12 15:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_link_from_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
|
2016-09-06 09:09:42 +00:00
|
|
|
Url::parse(matches.value_of("remove").unwrap())
|
|
|
|
.map_err_trace_exit(1)
|
|
|
|
.map(|link| entry.remove_external_link(store, link).map_err_trace().map_info_str("Ok"))
|
|
|
|
.ok();
|
2016-04-12 15:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_links_for_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
|
|
|
|
let links = matches
|
|
|
|
.value_of("links")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap()
|
2016-05-02 22:57:41 +00:00
|
|
|
.split(',')
|
2016-04-12 15:49:23 +00:00
|
|
|
.map(|uri| {
|
|
|
|
match Url::parse(uri) {
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Could not parse '{}' as URL, ignoring", uri);
|
|
|
|
trace_error(&e);
|
|
|
|
None
|
|
|
|
},
|
|
|
|
Ok(u) => Some(u),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter_map(|x| x)
|
|
|
|
.collect();
|
|
|
|
|
2016-09-06 09:09:42 +00:00
|
|
|
entry.set_external_links(store, links)
|
|
|
|
.map_err_trace()
|
|
|
|
.map_info_str("Ok")
|
|
|
|
.ok();
|
2016-04-12 15:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn list_links_for_entry(store: &Store, entry: &mut FileLockEntry) {
|
2016-09-06 09:09:42 +00:00
|
|
|
entry.get_external_links(store)
|
2016-04-12 15:49:23 +00:00
|
|
|
.and_then(|links| {
|
2016-10-15 13:45:19 +00:00
|
|
|
for (i, link) in links.enumerate() {
|
2016-10-18 09:22:24 +00:00
|
|
|
match link {
|
|
|
|
Ok(link) => println!("{: <3}: {}", i, link),
|
|
|
|
Err(e) => trace_error(&e),
|
|
|
|
}
|
2016-04-12 15:49:23 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
2016-09-06 09:09:42 +00:00
|
|
|
})
|
|
|
|
.map_err_trace()
|
|
|
|
.map_info_str("Ok")
|
|
|
|
.ok();
|
2016-04-12 15:49:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 23:46:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use handle_internal_linking;
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
|
|
|
use toml::value::Value;
|
2017-06-06 20:42:41 +00:00
|
|
|
use toml_query::read::TomlValueReadExt;
|
|
|
|
use toml_query::error::Result as TomlQueryResult;
|
2017-06-03 23:46:01 +00:00
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
use libimagstore::storeid::StoreId;
|
2017-09-19 16:31:28 +00:00
|
|
|
use libimagstore::store::{Result as StoreResult, FileLockEntry, Entry};
|
2017-06-03 23:46:01 +00:00
|
|
|
|
2017-07-18 19:27:03 +00:00
|
|
|
make_mock_app! {
|
|
|
|
app "imag-link";
|
|
|
|
modulename mock;
|
2017-09-23 20:26:52 +00:00
|
|
|
version "0.5.0";
|
2017-07-18 19:27:03 +00:00
|
|
|
with help "imag-link mocking app";
|
2017-06-03 23:46:01 +00:00
|
|
|
}
|
2017-07-18 19:27:03 +00:00
|
|
|
use self::mock::generate_test_runtime;
|
2017-09-02 08:41:21 +00:00
|
|
|
use self::mock::reset_test_runtime;
|
2017-06-03 23:46:01 +00:00
|
|
|
|
2017-07-18 19:27:03 +00:00
|
|
|
fn create_test_default_entry<'a, S: AsRef<OsStr>>(rt: &'a Runtime, name: S) -> StoreResult<StoreId> {
|
2017-06-03 23:46:01 +00:00
|
|
|
let mut path = PathBuf::new();
|
|
|
|
path.set_file_name(name);
|
|
|
|
|
2017-09-19 16:31:28 +00:00
|
|
|
let default_entry = Entry::new(StoreId::new_baseless(PathBuf::from("")).unwrap()).to_str();
|
|
|
|
|
2017-06-03 23:46:01 +00:00
|
|
|
let id = StoreId::new_baseless(path)?;
|
|
|
|
let mut entry = rt.store().create(id.clone())?;
|
2017-09-19 16:31:28 +00:00
|
|
|
entry.get_content_mut().push_str(&default_entry);
|
2017-06-03 23:46:01 +00:00
|
|
|
|
|
|
|
Ok(id)
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:42:41 +00:00
|
|
|
fn get_entry_links<'a>(entry: &'a FileLockEntry<'a>) -> TomlQueryResult<&'a Value> {
|
2017-09-08 14:18:49 +00:00
|
|
|
match entry.get_header().read(&"links.internal".to_owned()) {
|
2017-08-30 22:11:52 +00:00
|
|
|
Err(e) => Err(e),
|
|
|
|
Ok(Some(v)) => Ok(v),
|
2017-09-08 14:18:49 +00:00
|
|
|
Ok(None) => panic!("Didn't find 'links' in {:?}", entry),
|
2017-08-30 22:11:52 +00:00
|
|
|
}
|
2017-06-03 23:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn links_toml_value<'a, I: IntoIterator<Item = &'static str>>(links: I) -> Value {
|
|
|
|
Value::Array(links
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| Value::String(s.to_owned()))
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_link_modificates() {
|
2017-09-02 08:18:53 +00:00
|
|
|
let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2"])
|
2017-06-03 23:46:01 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2017-07-18 19:27:03 +00:00
|
|
|
let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
|
|
|
|
let test_id2 = create_test_default_entry(&rt, "test2").unwrap();
|
2017-06-03 23:46:01 +00:00
|
|
|
|
|
|
|
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_ne!(*test_links1, links_toml_value(vec![]));
|
|
|
|
assert_ne!(*test_links2, links_toml_value(vec![]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_linking_links() {
|
2017-09-02 08:18:53 +00:00
|
|
|
let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2"])
|
2017-06-03 23:46:01 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2017-07-18 19:27:03 +00:00
|
|
|
let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
|
|
|
|
let test_id2 = create_test_default_entry(&rt, "test2").unwrap();
|
2017-06-03 23:46:01 +00:00
|
|
|
|
|
|
|
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!["test2"]));
|
|
|
|
assert_eq!(*test_links2, links_toml_value(vec!["test1"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multilinking() {
|
2017-09-02 08:18:53 +00:00
|
|
|
let rt = generate_test_runtime(vec!["internal", "add", "test1", "test2"])
|
2017-06-03 23:46:01 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2017-07-18 19:27:03 +00:00
|
|
|
let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
|
|
|
|
let test_id2 = create_test_default_entry(&rt, "test2").unwrap();
|
2017-06-03 23:46:01 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
assert_eq!(*test_links1, links_toml_value(vec!["test2"]));
|
|
|
|
assert_eq!(*test_links2, links_toml_value(vec!["test1"]));
|
|
|
|
}
|
2017-09-02 08:21:20 +00:00
|
|
|
|
|
|
|
#[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"]));
|
|
|
|
}
|
2017-09-02 08:41:21 +00:00
|
|
|
|
|
|
|
// 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![]));
|
|
|
|
}
|
2017-06-03 23:46:01 +00:00
|
|
|
}
|