cnf_lib/test/mod.rs
1// Copyright (C) 2023 Andreas Hartmann <hartan@7x.de>
2// GNU General Public License v3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5//! Shared utilities for testing parts of `cnf`.
6pub mod assert;
7pub mod mock;
8
9pub mod prelude {
10 //! Essential includes for tests.
11 pub use crate::environment::ExecutionError;
12 pub use crate::provider::search_in;
13 pub use crate::test::mock::Mock;
14 #[cfg(unix)]
15 pub use std::os::unix::process::ExitStatusExt;
16 #[cfg(windows)]
17 pub use std::os::windows::process::ExitStatusExt;
18 pub use std::process::ExitStatus;
19 pub use std::sync::Arc;
20
21 pub use super::quick_test;
22 pub use crate::test;
23 pub use crate::test::assert;
24}
25
26/// Convenient generation of query results.
27///
28/// Takes an implementor of [`IsProvider`](crate::provider::IsProvider) as first argument and an
29/// aribtrary amount of `Result<String, ExecutionError>` as further arguments. The second parameter
30/// and all following are given as mock input to the `Provider` impl and are retrieved by
31/// subsequent calls to e.g. [`Environment::output_of()`](crate::env::Environment::output_of()) in
32/// the order specified.
33///
34/// The search term passed in the test is 'my-test-app', in case your application has to match
35/// against it.
36#[macro_export]
37macro_rules! quick_test {
38 ($provider:expr, $( $value:expr ),+) => {
39 tokio::runtime::Runtime::new().unwrap().block_on(async {
40 let provider = Arc::new(Provider::from($provider));
41 let mock = Mock::new();
42 $(
43 mock.push_raw($value);
44 )+
45
46 search_in(provider, "my-test-app", mock.to_env()).await
47 })
48 };
49}
50pub use quick_test;
51
52/// Insert a bunch of default tests.
53///
54/// - `ExecutionError::NotFound` should resolve to `ProviderError::Requirements`
55#[macro_export]
56macro_rules! default_tests {
57 ($provider:expr) => {
58 #[test]
59 fn exec_not_found_is_requirements_error() {
60 let query = $crate::test::quick_test!(
61 $provider,
62 Err(ExecutionError::NotFound("foo".to_string()))
63 );
64
65 assert::is_err!(query);
66 assert::err::requirements!(query);
67 }
68 };
69}
70pub use default_tests;