summaryrefslogtreecommitdiffstats
path: root/lib/etc/libimagutil/src/testing.rs
blob: 81e83678df462a744f3ebe2e6de688047ba818eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2020 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
//

/// 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;
        with ui builder function $buildui:path;
    }=> {
        mod $module {
            use clap::{App, ArgMatches};
            use libimagrt::spec::CliSpec;
            use libimagrt::runtime::Runtime;
            use libimagrt::configuration::InternalConfiguration;
            use toml::Value;
            use failure::Error;

            #[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: ($buildui)(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
                }
            }

            #[allow(unused)]
            pub fn generate_minimal_test_config() -> Option<Value> {
                ::toml::de::from_str("[store]\nimplicit-create=true").ok()
            }

            #[allow(unused)]
            pub fn generate_test_runtime<'a>(mut args: Vec<&'static str>) -> Result<Runtime<'a>, Error> {
                let mut cli_args = vec![$appname];
                cli_args.append(&mut args);
                debug!("Generating test runtime with args = {:?}", cli_args);

                let cli_app = MockLinkApp::new(cli_args);
                Runtime::with_configuration(cli_app, generate_minimal_test_config())
            }

            #[allow(unused)]
            pub fn reset_test_runtime<'a>(mut args: Vec<&'static str>, old_runtime: Runtime)
                -> Result<Runtime<'a>, Error>
            {
                let mut cli_args = vec![$appname];
                cli_args.append(&mut args);
                debug!("New arguments: {:?}", cli_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()))
            }
        }
    };

}