2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
|
2018-09-27 06:13:58 +00:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
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;
|
2018-10-30 17:40:53 +00:00
|
|
|
extern crate failure;
|
2018-03-23 14:58:45 +00:00
|
|
|
#[macro_use] extern crate prettytable;
|
2017-08-29 15:21:52 +00:00
|
|
|
#[cfg(test)] extern crate toml;
|
|
|
|
#[cfg(test)] extern crate toml_query;
|
2018-03-18 13:27:45 +00:00
|
|
|
#[cfg(test)] extern crate env_logger;
|
2016-02-23 11:06:28 +00:00
|
|
|
|
2016-04-16 19:56:16 +00:00
|
|
|
extern crate libimagentrylink;
|
2018-01-31 10:09:00 +00:00
|
|
|
#[macro_use] extern crate libimagrt;
|
2016-02-23 11:06:28 +00:00
|
|
|
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;
|
|
|
|
|
2018-02-14 22:32:23 +00:00
|
|
|
use std::io::Write;
|
2017-11-12 12:41:26 +00:00
|
|
|
use std::path::PathBuf;
|
2016-02-23 11:36:32 +00:00
|
|
|
|
2018-10-30 17:40:53 +00:00
|
|
|
use failure::Error;
|
|
|
|
use failure::err_msg;
|
|
|
|
|
2017-11-12 12:41:26 +00:00
|
|
|
use libimagentrylink::external::ExternalLinker;
|
|
|
|
use libimagentrylink::internal::InternalLinker;
|
|
|
|
use libimagentrylink::internal::store_check::StoreLinkConsistentExt;
|
2018-02-11 18:39:32 +00:00
|
|
|
use libimagerror::trace::{MapErrTrace, trace_error};
|
2018-02-14 22:32:23 +00:00
|
|
|
use libimagerror::exit::ExitUnwrap;
|
|
|
|
use libimagerror::io::ToExitCode;
|
2016-02-23 11:36:32 +00:00
|
|
|
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::store::FileLockEntry;
|
2018-02-06 23:01:53 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2016-09-05 17:29:06 +00:00
|
|
|
use libimagutil::warn_exit::warn_exit;
|
2017-11-12 12:41:26 +00:00
|
|
|
use libimagutil::warn_result::*;
|
|
|
|
|
2016-04-12 15:49:23 +00:00
|
|
|
use url::Url;
|
2018-10-30 17:40:53 +00:00
|
|
|
use failure::Fallible as Result;
|
2016-02-23 11:06:28 +00:00
|
|
|
|
2016-02-23 11:15:36 +00:00
|
|
|
mod ui;
|
|
|
|
|
2019-02-28 16:28:32 +00:00
|
|
|
use crate::ui::build_ui;
|
2016-02-23 11:15:36 +00:00
|
|
|
|
2016-02-23 11:04:41 +00:00
|
|
|
fn main() {
|
2018-01-31 10:09:00 +00:00
|
|
|
let version = make_imag_version!();
|
2016-05-18 17:06:05 +00:00
|
|
|
let rt = generate_runtime_setup("imag-link",
|
2018-01-31 10:09:00 +00:00
|
|
|
&version,
|
2018-10-30 17:40:53 +00:00
|
|
|
"Link entries",
|
2016-05-18 17:06:05 +00:00
|
|
|
build_ui);
|
2017-11-12 12:41:26 +00:00
|
|
|
if rt.cli().is_present("check-consistency") {
|
|
|
|
let exit_code = match rt.store().check_link_consistency() {
|
|
|
|
Ok(_) => {
|
|
|
|
info!("Store is consistent");
|
|
|
|
0
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
|
|
|
1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
::std::process::exit(exit_code);
|
|
|
|
}
|
2016-02-23 11:36:32 +00:00
|
|
|
|
2017-11-12 12:41:26 +00:00
|
|
|
let _ = rt.cli()
|
2016-02-23 11:36:32 +00:00
|
|
|
.subcommand_name()
|
|
|
|
.map(|name| {
|
|
|
|
match name {
|
2017-11-12 12:41:26 +00:00
|
|
|
"remove" => remove_linking(&rt),
|
2018-02-25 16:27:50 +00:00
|
|
|
"unlink" => unlink(&rt),
|
2017-11-12 12:41:26 +00:00
|
|
|
"list" => list_linkings(&rt),
|
2018-04-04 15:01:02 +00:00
|
|
|
other => {
|
|
|
|
debug!("Unknown command");
|
|
|
|
let _ = rt.handle_unknown_subcommand("imag-link", other, rt.cli())
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-04-04 15:01:02 +00:00
|
|
|
.code()
|
|
|
|
.map(::std::process::exit);
|
|
|
|
},
|
2016-02-23 11:36:32 +00:00
|
|
|
}
|
2017-11-12 12:41:26 +00:00
|
|
|
})
|
|
|
|
.or_else(|| {
|
|
|
|
if let (Some(from), Some(to)) = (rt.cli().value_of("from"), rt.cli().values_of("to")) {
|
|
|
|
Some(link_from_to(&rt, from, to))
|
|
|
|
} else {
|
|
|
|
warn_exit("No commandline call", 1)
|
|
|
|
}
|
|
|
|
})
|
2018-10-30 17:40:53 +00:00
|
|
|
.ok_or_else(|| Error::from(err_msg("No commandline call".to_owned())))
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2016-02-23 11:36:32 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 17:40:53 +00:00
|
|
|
fn get_entry_by_name<'a>(rt: &'a Runtime, name: &str) -> Result<Option<FileLockEntry<'a>>> {
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Getting: {:?}", name);
|
2019-02-11 21:47:44 +00:00
|
|
|
let result = StoreId::new(PathBuf::from(name)).and_then(|id| rt.store().get(id));
|
2018-03-18 13:27:45 +00:00
|
|
|
|
|
|
|
debug!(" => : {:?}", result);
|
|
|
|
result
|
2017-11-12 12:41:26 +00:00
|
|
|
}
|
2017-07-04 17:23:15 +00:00
|
|
|
|
2017-12-23 11:17:18 +00:00
|
|
|
fn link_from_to<'a, I>(rt: &'a Runtime, from: &'a str, to: I)
|
|
|
|
where I: Iterator<Item = &'a str>
|
|
|
|
{
|
2019-02-05 00:37:32 +00:00
|
|
|
let mut from_entry = match get_entry_by_name(rt, from).map_err_trace_exit_unwrap() {
|
2018-02-11 18:39:32 +00:00
|
|
|
Some(e) => e,
|
2018-03-18 13:27:45 +00:00
|
|
|
None => {
|
|
|
|
debug!("No 'from' entry");
|
|
|
|
warn_exit("No 'from' entry", 1)
|
|
|
|
},
|
2017-11-12 12:41:26 +00:00
|
|
|
};
|
2017-09-01 19:29:29 +00:00
|
|
|
|
2017-11-12 12:41:26 +00:00
|
|
|
for entry in to {
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Handling 'to' entry: {:?}", entry);
|
2019-02-05 00:37:32 +00:00
|
|
|
if !rt.store().get(PathBuf::from(entry)).map_err_trace_exit_unwrap().is_some() {
|
2017-11-12 12:41:26 +00:00
|
|
|
debug!("Linking externally: {:?} -> {:?}", from, entry);
|
2018-02-11 18:39:32 +00:00
|
|
|
let url = Url::parse(entry).unwrap_or_else(|e| {
|
|
|
|
error!("Error parsing URL: {:?}", e);
|
|
|
|
::std::process::exit(1);
|
|
|
|
});
|
|
|
|
|
2018-10-01 11:23:56 +00:00
|
|
|
let iter = from_entry
|
2017-11-12 12:41:26 +00:00
|
|
|
.add_external_link(rt.store(), url)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-10-01 11:23:56 +00:00
|
|
|
.into_iter();
|
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_all_touched(iter).unwrap_or_exit();
|
2017-11-12 12:41:26 +00:00
|
|
|
} else {
|
|
|
|
debug!("Linking internally: {:?} -> {:?}", from, entry);
|
2018-02-06 23:01:53 +00:00
|
|
|
|
2018-12-05 18:01:42 +00:00
|
|
|
let from_id = StoreId::new(PathBuf::from(from)).map_err_trace_exit_unwrap();
|
|
|
|
let entr_id = StoreId::new(PathBuf::from(entry)).map_err_trace_exit_unwrap();
|
2018-02-06 23:01:53 +00:00
|
|
|
|
|
|
|
if from_id == entr_id {
|
|
|
|
error!("Cannot link entry with itself. Exiting");
|
|
|
|
::std::process::exit(1)
|
|
|
|
}
|
|
|
|
|
2019-02-05 00:37:32 +00:00
|
|
|
let mut to_entry = match rt.store().get(entr_id).map_err_trace_exit_unwrap() {
|
2018-02-11 18:39:32 +00:00
|
|
|
Some(e) => e,
|
|
|
|
None => {
|
2017-11-12 12:41:26 +00:00
|
|
|
warn!("No 'to' entry: {}", entry);
|
|
|
|
::std::process::exit(1)
|
|
|
|
},
|
2017-09-01 19:29:29 +00:00
|
|
|
};
|
2017-11-12 12:41:26 +00:00
|
|
|
let _ = from_entry
|
|
|
|
.add_internal_link(&mut to_entry)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2018-10-01 11:23:56 +00:00
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(to_entry.get_location()).unwrap_or_exit();
|
2017-11-12 12:41:26 +00:00
|
|
|
}
|
2017-09-01 19:29:29 +00:00
|
|
|
|
2018-10-01 11:23:56 +00:00
|
|
|
|
2017-11-12 12:41:26 +00:00
|
|
|
info!("Ok: {} -> {}", from, entry);
|
2016-02-23 11:36:32 +00:00
|
|
|
}
|
2018-10-01 11:23:56 +00:00
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(from_entry.get_location()).unwrap_or_exit();
|
2017-11-12 12:41:26 +00:00
|
|
|
}
|
2017-07-04 16:27:01 +00:00
|
|
|
|
2017-11-12 12:41:26 +00:00
|
|
|
fn remove_linking(rt: &Runtime) {
|
2018-10-01 11:17:19 +00:00
|
|
|
let mut from = rt.cli()
|
2017-11-12 12:41:26 +00:00
|
|
|
.subcommand_matches("remove")
|
2018-10-01 11:17:19 +00:00
|
|
|
.unwrap() // safe, we know there is an "remove" subcommand
|
|
|
|
.value_of("from")
|
|
|
|
.map(PathBuf::from)
|
|
|
|
.map(|id| {
|
|
|
|
rt.store()
|
|
|
|
.get(id)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-10-01 11:17:19 +00:00
|
|
|
.ok_or_else(|| warn_exit("No 'from' entry", 1))
|
|
|
|
.unwrap() // safe by line above
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
2019-02-28 16:28:32 +00:00
|
|
|
rt.ids::<crate::ui::PathProvider>()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-10-01 11:17:19 +00:00
|
|
|
.into_iter()
|
|
|
|
.for_each(|id| match rt.store().get(id.clone()) {
|
|
|
|
Err(e) => trace_error(&e),
|
|
|
|
Ok(Some(mut to_entry)) => {
|
|
|
|
let _ = to_entry
|
|
|
|
.remove_internal_link(&mut from)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2018-10-01 11:23:56 +00:00
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(to_entry.get_location()).unwrap_or_exit();
|
2018-10-01 11:17:19 +00:00
|
|
|
},
|
|
|
|
Ok(None) => {
|
|
|
|
// looks like this is not an entry, but a filesystem URI and therefor an
|
|
|
|
// external link...?
|
|
|
|
if id.local().is_file() {
|
|
|
|
let pb = id.local().to_str().unwrap_or_else(|| {
|
|
|
|
warn!("Not StoreId and not a Path: {}", id);
|
|
|
|
::std::process::exit(1);
|
|
|
|
});
|
|
|
|
let url = Url::parse(pb).unwrap_or_else(|e| {
|
|
|
|
error!("Error parsing URL: {:?}", e);
|
|
|
|
::std::process::exit(1);
|
|
|
|
});
|
2019-02-05 00:37:32 +00:00
|
|
|
from.remove_external_link(rt.store(), url).map_err_trace_exit_unwrap();
|
2018-10-01 11:17:19 +00:00
|
|
|
info!("Ok: {}", id);
|
|
|
|
} else {
|
|
|
|
warn!("Entry not found: {:?}", id);
|
2016-02-23 11:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-12 12:41:26 +00:00
|
|
|
});
|
2018-10-01 11:23:56 +00:00
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(from.get_location()).unwrap_or_exit();
|
2016-02-23 11:04:41 +00:00
|
|
|
}
|
2016-02-23 11:06:28 +00:00
|
|
|
|
2018-02-25 16:27:50 +00:00
|
|
|
fn unlink(rt: &Runtime) {
|
2019-02-28 16:28:32 +00:00
|
|
|
rt.ids::<crate::ui::PathProvider>().map_err_trace_exit_unwrap().into_iter().for_each(|id| {
|
2018-10-01 11:17:19 +00:00
|
|
|
rt.store()
|
|
|
|
.get(id.clone())
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-10-01 11:17:19 +00:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
warn!("No entry for {}", id);
|
|
|
|
::std::process::exit(1)
|
|
|
|
})
|
|
|
|
.unlink(rt.store())
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2018-10-01 11:23:56 +00:00
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(&id).unwrap_or_exit();
|
2018-10-01 11:17:19 +00:00
|
|
|
});
|
2018-02-25 16:27:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 12:41:26 +00:00
|
|
|
fn list_linkings(rt: &Runtime) {
|
|
|
|
let cmd = rt.cli()
|
|
|
|
.subcommand_matches("list")
|
|
|
|
.unwrap(); // safed by clap
|
2016-04-12 15:49:23 +00:00
|
|
|
|
2018-02-14 22:32:23 +00:00
|
|
|
let list_externals = cmd.is_present("list-externals-too");
|
2018-03-23 14:58:45 +00:00
|
|
|
let list_plain = cmd.is_present("list-plain");
|
|
|
|
|
|
|
|
let mut tab = ::prettytable::Table::new();
|
|
|
|
tab.set_titles(row!["#", "Link"]);
|
2016-04-12 15:49:23 +00:00
|
|
|
|
2019-02-28 16:28:32 +00:00
|
|
|
rt.ids::<crate::ui::PathProvider>().map_err_trace_exit_unwrap().into_iter().for_each(|id| {
|
2018-10-01 11:17:19 +00:00
|
|
|
match rt.store().get(id.clone()) {
|
2017-11-12 12:41:26 +00:00
|
|
|
Ok(Some(entry)) => {
|
2019-02-05 00:37:32 +00:00
|
|
|
for (i, link) in entry.get_internal_links().map_err_trace_exit_unwrap().enumerate() {
|
2017-11-12 12:41:26 +00:00
|
|
|
let link = link
|
|
|
|
.to_str()
|
|
|
|
.map_warn_err(|e| format!("Failed to convert StoreId to string: {:?}", e))
|
|
|
|
.ok();
|
2016-04-12 15:49:23 +00:00
|
|
|
|
2017-11-12 12:41:26 +00:00
|
|
|
if let Some(link) = link {
|
2018-03-23 14:58:45 +00:00
|
|
|
if list_plain {
|
|
|
|
let _ = writeln!(rt.stdout(), "{: <3}: {}", i, link)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
} else {
|
|
|
|
tab.add_row(row![i, link]);
|
|
|
|
}
|
2017-11-12 12:41:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-12 15:49:23 +00:00
|
|
|
|
2017-11-12 12:41:26 +00:00
|
|
|
if list_externals {
|
2018-02-14 22:32:23 +00:00
|
|
|
entry.get_external_links(rt.store())
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-02-14 22:32:23 +00:00
|
|
|
.enumerate()
|
|
|
|
.for_each(|(i, link)| {
|
|
|
|
let link = link
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-02-14 22:32:23 +00:00
|
|
|
.into_string();
|
|
|
|
|
2018-03-23 14:58:45 +00:00
|
|
|
if list_plain {
|
|
|
|
let _ = writeln!(rt.stdout(), "{: <3}: {}", i, link)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
} else {
|
|
|
|
tab.add_row(row![i, link]);
|
|
|
|
}
|
2018-02-14 22:32:23 +00:00
|
|
|
})
|
2016-10-18 09:22:24 +00:00
|
|
|
}
|
2018-10-01 11:23:56 +00:00
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();
|
2018-10-01 11:23:56 +00:00
|
|
|
|
2017-11-12 12:41:26 +00:00
|
|
|
},
|
2018-10-01 11:17:19 +00:00
|
|
|
Ok(None) => warn!("Not found: {}", id),
|
2017-11-12 12:41:26 +00:00
|
|
|
Err(e) => trace_error(&e),
|
|
|
|
}
|
2018-10-01 11:23:56 +00:00
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(&id).unwrap_or_exit();
|
2018-10-01 11:17:19 +00:00
|
|
|
});
|
2018-03-23 14:58:45 +00:00
|
|
|
|
|
|
|
if !list_plain {
|
|
|
|
let out = rt.stdout();
|
|
|
|
let mut lock = out.lock();
|
|
|
|
tab.print(&mut lock)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
}
|
2016-04-12 15:49:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 23:46:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-12-23 11:17:18 +00:00
|
|
|
use super::link_from_to;
|
|
|
|
use super::remove_linking;
|
2017-06-03 23:46:01 +00:00
|
|
|
|
|
|
|
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;
|
2018-10-30 17:40:53 +00:00
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::Error;
|
2017-06-03 23:46:01 +00:00
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
use libimagstore::storeid::StoreId;
|
2018-10-30 17:40:53 +00:00
|
|
|
use libimagstore::store::{FileLockEntry, Entry};
|
2017-06-03 23:46:01 +00:00
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
fn setup_logging() {
|
|
|
|
let _ = ::env_logger::try_init();
|
|
|
|
}
|
|
|
|
|
2017-07-18 19:27:03 +00:00
|
|
|
make_mock_app! {
|
|
|
|
app "imag-link";
|
|
|
|
modulename mock;
|
2018-03-03 12:51:55 +00:00
|
|
|
version env!("CARGO_PKG_VERSION");
|
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
|
|
|
|
2018-10-30 17:40:53 +00:00
|
|
|
fn create_test_default_entry<'a, S: AsRef<OsStr>>(rt: &'a Runtime, name: S) -> Result<StoreId> {
|
2017-06-03 23:46:01 +00:00
|
|
|
let mut path = PathBuf::new();
|
|
|
|
path.set_file_name(name);
|
|
|
|
|
2018-12-05 18:01:42 +00:00
|
|
|
let default_entry = Entry::new(StoreId::new(PathBuf::from("")).unwrap())
|
2018-03-23 14:39:52 +00:00
|
|
|
.to_str()
|
|
|
|
.unwrap();
|
2017-09-19 16:31:28 +00:00
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Default entry constructed");
|
|
|
|
|
2018-12-05 18:01:42 +00:00
|
|
|
let id = StoreId::new(path)?;
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("StoreId constructed: {:?}", id);
|
|
|
|
|
2017-06-03 23:46:01 +00:00
|
|
|
let mut entry = rt.store().create(id.clone())?;
|
2018-03-18 13:27:45 +00:00
|
|
|
|
|
|
|
debug!("Entry constructed: {:?}", id);
|
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)
|
|
|
|
}
|
|
|
|
|
2018-10-30 17:40:53 +00:00
|
|
|
fn get_entry_links<'a>(entry: &'a FileLockEntry<'a>) -> Result<&'a Value> {
|
|
|
|
match entry.get_header().read(&"links.internal".to_owned()).map_err(Error::from)? {
|
|
|
|
Some(v) => Ok(v),
|
|
|
|
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() {
|
2018-03-18 13:27:45 +00:00
|
|
|
setup_logging();
|
2018-11-01 11:43:14 +00:00
|
|
|
let rt = generate_test_runtime(vec!["test1", "test2"])
|
2017-06-03 23:46:01 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Runtime created");
|
|
|
|
|
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
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Entries created");
|
|
|
|
|
2017-12-23 11:17:18 +00:00
|
|
|
link_from_to(&rt, "test1", vec!["test2"].into_iter());
|
2017-06-03 23:46:01 +00:00
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Linking done");
|
|
|
|
|
2017-06-03 23:46:01 +00:00
|
|
|
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();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Asserting");
|
|
|
|
|
2017-06-03 23:46:01 +00:00
|
|
|
assert_ne!(*test_links1, links_toml_value(vec![]));
|
|
|
|
assert_ne!(*test_links2, links_toml_value(vec![]));
|
2018-11-01 11:43:14 +00:00
|
|
|
|
|
|
|
debug!("Test finished")
|
2017-06-03 23:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_linking_links() {
|
2018-03-18 13:27:45 +00:00
|
|
|
setup_logging();
|
2018-11-01 11:43:14 +00:00
|
|
|
let rt = generate_test_runtime(vec!["test1", "test2"])
|
2017-06-03 23:46:01 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Runtime created");
|
|
|
|
|
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
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Test entries created");
|
|
|
|
|
2017-12-23 11:17:18 +00:00
|
|
|
link_from_to(&rt, "test1", vec!["test2"].into_iter());
|
2017-06-03 23:46:01 +00:00
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Linking done");
|
|
|
|
|
2017-06-03 23:46:01 +00:00
|
|
|
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();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Asserting");
|
|
|
|
|
2017-06-03 23:46:01 +00:00
|
|
|
assert_eq!(*test_links1, links_toml_value(vec!["test2"]));
|
|
|
|
assert_eq!(*test_links2, links_toml_value(vec!["test1"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multilinking() {
|
2018-03-18 13:27:45 +00:00
|
|
|
setup_logging();
|
2018-11-01 11:43:14 +00:00
|
|
|
let rt = generate_test_runtime(vec!["test1", "test2"])
|
2017-06-03 23:46:01 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Runtime created");
|
|
|
|
|
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
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Test entries created");
|
|
|
|
|
2017-12-23 11:17:18 +00:00
|
|
|
link_from_to(&rt, "test1", vec!["test2"].into_iter());
|
|
|
|
link_from_to(&rt, "test1", vec!["test2"].into_iter());
|
2017-06-03 23:46:01 +00:00
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Linking done");
|
|
|
|
|
2017-06-03 23:46:01 +00:00
|
|
|
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();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Asserting");
|
|
|
|
|
2017-06-03 23:46:01 +00:00
|
|
|
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() {
|
2018-03-18 13:27:45 +00:00
|
|
|
setup_logging();
|
2018-11-01 11:43:14 +00:00
|
|
|
let rt = generate_test_runtime(vec!["test1", "test2", "test3"])
|
2017-09-02 08:21:20 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Runtime created");
|
|
|
|
|
2017-09-02 08:21:20 +00:00
|
|
|
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();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Test entries created");
|
|
|
|
|
2017-12-23 11:17:18 +00:00
|
|
|
link_from_to(&rt, "test1", vec!["test2", "test3"].into_iter());
|
|
|
|
link_from_to(&rt, "test1", vec!["test2", "test3"].into_iter());
|
2017-09-02 08:21:20 +00:00
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Linking done");
|
|
|
|
|
2017-09-02 08:21:20 +00:00
|
|
|
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();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Asserting");
|
|
|
|
|
2017-09-02 08:21:20 +00:00
|
|
|
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() {
|
2018-03-18 13:27:45 +00:00
|
|
|
setup_logging();
|
2018-11-01 11:43:14 +00:00
|
|
|
let rt = generate_test_runtime(vec!["test1", "test2"])
|
2017-09-02 08:41:21 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Runtime created");
|
|
|
|
|
2017-09-02 08:41:21 +00:00
|
|
|
let test_id1 = create_test_default_entry(&rt, "test1").unwrap();
|
|
|
|
let test_id2 = create_test_default_entry(&rt, "test2").unwrap();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Test entries created");
|
|
|
|
|
2017-12-23 11:17:18 +00:00
|
|
|
link_from_to(&rt, "test1", vec!["test2"].into_iter());
|
2017-09-02 08:41:21 +00:00
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Linking done");
|
|
|
|
|
2017-12-23 11:17:18 +00:00
|
|
|
let rt = reset_test_runtime(vec!["remove", "test1", "test2"], rt)
|
2017-09-02 08:41:21 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2017-12-23 11:17:18 +00:00
|
|
|
remove_linking(&rt);
|
2017-09-02 08:41:21 +00:00
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Linking removed");
|
|
|
|
|
2017-09-02 08:41:21 +00:00
|
|
|
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();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Asserting");
|
|
|
|
|
2017-09-02 08:41:21 +00:00
|
|
|
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() {
|
2018-03-18 13:27:45 +00:00
|
|
|
setup_logging();
|
2018-11-01 11:43:14 +00:00
|
|
|
let rt = generate_test_runtime(vec!["test1", "test2", "test3"])
|
2017-09-02 08:41:21 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Runtime created");
|
|
|
|
|
2017-09-02 08:41:21 +00:00
|
|
|
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();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Test entries created");
|
|
|
|
|
2017-12-23 11:17:18 +00:00
|
|
|
link_from_to(&rt, "test1", vec!["test2", "test3"].into_iter());
|
2017-09-02 08:41:21 +00:00
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("linking done");
|
|
|
|
|
2017-12-23 11:17:18 +00:00
|
|
|
let rt = reset_test_runtime(vec!["remove", "test1", "test2", "test3"], rt)
|
2017-09-02 08:41:21 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2017-12-23 11:17:18 +00:00
|
|
|
remove_linking(&rt);
|
2017-09-02 08:41:21 +00:00
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("linking removed");
|
|
|
|
|
2017-09-02 08:41:21 +00:00
|
|
|
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();
|
|
|
|
|
2018-03-18 13:27:45 +00:00
|
|
|
debug!("Asserting");
|
|
|
|
|
2017-09-02 08:41:21 +00:00
|
|
|
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
|
|
|
}
|