2017-07-18 19:27:03 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2018-02-07 01:48:53 +00:00
|
|
|
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-07-18 19:27:03 +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
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Generator helper macro for mock app (for testing only)
|
|
|
|
///
|
|
|
|
/// Requires the following crates in scope:
|
|
|
|
///
|
|
|
|
/// * std
|
|
|
|
/// * libimagrt
|
|
|
|
/// * clap
|
|
|
|
///
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! make_mock_app {
|
|
|
|
{
|
|
|
|
app $appname:expr;
|
|
|
|
module $module:ident;
|
|
|
|
version $version:expr;
|
|
|
|
} => {
|
|
|
|
make_mock_app! {
|
|
|
|
app $appname;
|
|
|
|
module $module;
|
|
|
|
version $version;
|
|
|
|
with help "This is a mocking app";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
app $appname:expr;
|
|
|
|
modulename $module:ident;
|
|
|
|
version $version:expr;
|
|
|
|
with help $help:expr;
|
|
|
|
}=> {
|
|
|
|
mod $module {
|
|
|
|
use clap::{App, ArgMatches};
|
|
|
|
use libimagrt::spec::CliSpec;
|
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
use libimagrt::error::RuntimeError;
|
2017-10-28 17:19:46 +00:00
|
|
|
use libimagrt::configuration::InternalConfiguration;
|
|
|
|
use toml::Value;
|
2017-07-18 19:27:03 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct MockLinkApp<'a> {
|
|
|
|
args: Vec<&'static str>,
|
|
|
|
inner: App<'a, 'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> MockLinkApp<'a> {
|
|
|
|
fn new(args: Vec<&'static str>) -> Self {
|
|
|
|
MockLinkApp {
|
|
|
|
args: args,
|
|
|
|
inner: ::build_ui(Runtime::get_default_cli_builder($appname, $version, $help)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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> InternalConfiguration for MockLinkApp<'a> {
|
|
|
|
fn enable_logging(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn use_inmemory_fs(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 20:01:40 +00:00
|
|
|
#[allow(unused)]
|
2017-10-28 17:19:46 +00:00
|
|
|
pub fn generate_minimal_test_config() -> Option<Value> {
|
|
|
|
::toml::de::from_str("[store]\nimplicit-create=true").ok()
|
2017-07-18 19:27:03 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 20:01:40 +00:00
|
|
|
#[allow(unused)]
|
2017-07-18 19:27:03 +00:00
|
|
|
pub fn generate_test_runtime<'a>(mut args: Vec<&'static str>) -> Result<Runtime<'a>, RuntimeError> {
|
2018-10-07 13:25:15 +00:00
|
|
|
let mut cli_args = vec![$appname];
|
2017-07-18 19:27:03 +00:00
|
|
|
|
|
|
|
cli_args.append(&mut args);
|
|
|
|
|
|
|
|
let cli_app = MockLinkApp::new(cli_args);
|
|
|
|
Runtime::with_configuration(cli_app, generate_minimal_test_config())
|
|
|
|
}
|
2017-09-02 08:29:10 +00:00
|
|
|
|
2017-09-03 20:01:40 +00:00
|
|
|
#[allow(unused)]
|
2017-09-02 08:29:10 +00:00
|
|
|
pub fn reset_test_runtime<'a>(mut args: Vec<&'static str>, old_runtime: Runtime)
|
|
|
|
-> Result<Runtime<'a>, RuntimeError>
|
|
|
|
{
|
|
|
|
let mut cli_args = vec![$appname, "--rtp", "/tmp"];
|
|
|
|
|
|
|
|
cli_args.append(&mut args);
|
|
|
|
|
|
|
|
let cli_app = MockLinkApp::new(cli_args);
|
|
|
|
Runtime::with_configuration(cli_app, generate_minimal_test_config())
|
|
|
|
.map(|rt| rt.with_store(old_runtime.extract_store()))
|
|
|
|
}
|
2017-07-18 19:27:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|