Add Rust tests for internal linking

This commit is contained in:
Robert Ignat 2017-06-04 01:46:01 +02:00 committed by Matthias Beyer
parent 6f42e7d002
commit 60e3038874
2 changed files with 216 additions and 0 deletions

View file

@ -334,3 +334,176 @@ fn list_links_for_entry(store: &Store, entry: &mut FileLockEntry) {
.ok(); .ok();
} }
#[cfg(test)]
mod tests {
use handle_internal_linking;
use std::path::PathBuf;
use std::ffi::OsStr;
use clap::{App, ArgMatches};
use toml::value::Value;
use libimagrt::spec::CliSpec;
use libimagrt::runtime::Runtime;
use libimagrt::error::RuntimeError;
use libimagrt::configuration::{Configuration, GetConfiguration, InternalConfiguration,
Result as ConfigResult, ConfigErrorKind};
use libimagstore::storeid::StoreId;
use libimagstore::store::{Result as StoreResult, FileLockEntry};
static TOML_CONFIG_FILE: &[u8] = include_bytes!("../test_config.toml");
static DEFAULT_ENTRY: &str = "\
---\
[imag]\
links = []\
version = \"0.3.0\"\
---";
#[derive(Clone)]
struct MockLinkApp<'a> {
args: Vec<&'static str>,
inner: App<'a, 'a>,
}
impl<'a> MockLinkApp<'a> {
fn new(args: Vec<&'static str>) -> Self {
Self {
args,
inner: ::build_ui(Runtime::get_default_cli_builder("imag-link",
"0.3.0",
"Link entries test")),
}
}
}
impl<'a> CliSpec<'a> for MockLinkApp<'a> {
fn name(&self) -> &str {
self.inner.get_name()
}
fn matches(self) -> ArgMatches<'a> {
self.inner.get_matches_from(self.args)
}
}
impl<'a> GetConfiguration for MockLinkApp<'a> {
fn get_configuration(_: &PathBuf) -> ConfigResult<Configuration> {
::toml::de::from_slice(TOML_CONFIG_FILE)
.map_err(|_| {
ConfigErrorKind::TOMLParserError.into()
})
.map(Configuration::new)
}
}
impl<'a> InternalConfiguration for MockLinkApp<'a> {
fn enable_hooks() -> bool {
false
}
fn enable_logging() -> bool {
false
}
}
fn generate_test_runtime<'a>(mut args: Vec<&'static str>) -> Result<Runtime<'a>, RuntimeError> {
let mut cli_args = vec!["imag-link",
"--no-color",
"--config",
"./imagrc.toml",
"--rtp",
"/tmp"];
cli_args.append(&mut args);
let cli_app = MockLinkApp::new(cli_args);
Runtime::new(cli_app)
}
fn create_test_entry<'a, S: AsRef<OsStr>>(rt: &'a Runtime, name: S) -> StoreResult<StoreId> {
let mut path = PathBuf::new();
path.set_file_name(name);
let id = StoreId::new_baseless(path)?;
let mut entry = rt.store().create(id.clone())?;
entry.get_content_mut().push_str(DEFAULT_ENTRY);
Ok(id)
}
fn get_entry_links<'a>(entry: &'a FileLockEntry<'a>) -> Option<&'a Value> {
entry
.get_header()
.get("imag")
.and_then(|ih| ih.get("links"))
}
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() {
let rt = generate_test_runtime(vec!["internal", "add", "--from", "test1", "--to", "test2"])
.unwrap();
let test_id1 = create_test_entry(&rt, "test1").unwrap();
let test_id2 = create_test_entry(&rt, "test2").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_ne!(*test_links1, links_toml_value(vec![]));
assert_ne!(*test_links2, links_toml_value(vec![]));
}
#[test]
fn test_linking_links() {
let rt = generate_test_runtime(vec!["internal", "add", "--from", "test1", "--to", "test2"])
.unwrap();
let test_id1 = create_test_entry(&rt, "test1").unwrap();
let test_id2 = create_test_entry(&rt, "test2").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!["test2"]));
assert_eq!(*test_links2, links_toml_value(vec!["test1"]));
}
#[test]
fn test_multilinking() {
let rt = generate_test_runtime(vec!["internal", "add", "--from", "test1", "--to", "test2"])
.unwrap();
let test_id1 = create_test_entry(&rt, "test1").unwrap();
let test_id2 = create_test_entry(&rt, "test2").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();
assert_eq!(*test_links1, links_toml_value(vec!["test2"]));
assert_eq!(*test_links2, links_toml_value(vec!["test1"]));
}
}

View file

@ -0,0 +1,43 @@
[store]
implicit-create = true
store-unload-hook-aspects = []
pre-create-hook-aspects = []
post-create-hook-aspects = []
pre-move-hook-aspects = []
post-move-hook-aspects = []
pre-retrieve-hook-aspects = []
post-retrieve-hook-aspects = []
pre-update-hook-aspects = []
post-update-hook-aspects = []
pre-delete-hook-aspects = []
post-delete-hook-aspects = []
[store.aspects.debug]
parallel = false
[store.aspects.vcs]
parallel = false
[store.hooks.stdhook_debug]
aspect = "vcs"
[store.hooks.stdhook_git_update]
aspect = "vcs"
enabled = false
[store.hooks.stdhook_git_update.commit]
enabled = false
[store.hooks.stdhook_git_delete]
aspect = "vcs"
enabled = false
[store.hooks.stdhook_git_delete.commit]
enabled = false
[store.hooks.stdhook_git_storeunload]
aspect = "vcs"
enabled = false
[store.hooks.stdhook_git_storeunload.commit]
enabled = false