| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
/// Platform-independent helper for constructing a `PathBuf` from individual elements |
| 7 |
#[macro_export] |
| 8 |
macro_rules! path_concat { |
| 9 |
($e:expr, ..$n:expr) => {{ |
| 10 |
use std::path::PathBuf; |
| 11 |
let n = $n; |
| 12 |
let mut pb = PathBuf::new(); |
| 13 |
for _ in 0..n { |
| 14 |
pb.push($e); |
| 15 |
} |
| 16 |
pb.to_str().unwrap().to_owned() |
| 17 |
}}; |
| 18 |
($($e:expr),*) => {{ |
| 19 |
use std::path::PathBuf; |
| 20 |
let mut pb = PathBuf::new(); |
| 21 |
$( |
| 22 |
pb.push($e); |
| 23 |
)* |
| 24 |
pb.to_str().unwrap().to_owned() |
| 25 |
}}; |
| 26 |
} |
| 27 |
|
| 28 |
/// Deduce the name of the test binary from the test filename. |
| 29 |
/// |
| 30 |
/// e.g.: `tests/by-util/test_cat.rs` -> `cat` |
| 31 |
#[macro_export] |
| 32 |
macro_rules! util_name { |
| 33 |
() => { |
| 34 |
module_path!() |
| 35 |
.split("_") |
| 36 |
.nth(1) |
| 37 |
.and_then(|s| s.split("::").next()) |
| 38 |
.expect("no test name") |
| 39 |
}; |
| 40 |
} |
| 41 |
|
| 42 |
/// Convenience macro for acquiring a [`UCommand`] builder. |
| 43 |
/// |
| 44 |
/// Returns the following: |
| 45 |
/// - a [`UCommand`] builder for invoking the binary to be tested |
| 46 |
/// |
| 47 |
/// This macro is intended for quick, single-call tests. For more complex tests |
| 48 |
/// that require multiple invocations of the tested binary, see [`TestScenario`] |
| 49 |
/// |
| 50 |
/// [`UCommand`]: crate::util::UCommand |
| 51 |
/// [`TestScenario`]: crate::util::TestScenario |
| 52 |
#[macro_export] |
| 53 |
macro_rules! new_ucmd { |
| 54 |
() => { |
| 55 |
::uutests::util::TestScenario::new(::uutests::util_name!()).ucmd() |
| 56 |
}; |
| 57 |
} |
| 58 |
|
| 59 |
/// Convenience macro for acquiring a [`UCommand`] builder and a test path. |
| 60 |
/// |
| 61 |
/// Returns a tuple containing the following: |
| 62 |
/// - an [`AtPath`] that points to a unique temporary test directory |
| 63 |
/// - a [`UCommand`] builder for invoking the binary to be tested |
| 64 |
/// |
| 65 |
/// This macro is intended for quick, single-call tests. For more complex tests |
| 66 |
/// that require multiple invocations of the tested binary, see [`TestScenario`] |
| 67 |
/// |
| 68 |
/// [`UCommand`]: crate::util::UCommand |
| 69 |
/// [`AtPath`]: crate::util::AtPath |
| 70 |
/// [`TestScenario`]: crate::util::TestScenario |
| 71 |
#[macro_export] |
| 72 |
macro_rules! at_and_ucmd { |
| 73 |
() => {{ |
| 74 |
let ts = ::uutests::util::TestScenario::new(::uutests::util_name!()); |
| 75 |
(ts.fixtures.clone(), ts.ucmd()) |
| 76 |
}}; |
| 77 |
} |
| 78 |
|
| 79 |
/// Convenience macro for acquiring a [`TestScenario`] with its test path. |
| 80 |
/// |
| 81 |
/// Returns a tuple containing the following: |
| 82 |
/// - a [`TestScenario`] for invoking commands |
| 83 |
/// - an [`AtPath`] that points to a unique temporary test directory |
| 84 |
/// |
| 85 |
/// [`AtPath`]: crate::util::AtPath |
| 86 |
/// [`TestScenario`]: crate::util::TestScenario |
| 87 |
#[macro_export] |
| 88 |
macro_rules! at_and_ts { |
| 89 |
() => {{ |
| 90 |
let ts = ::uutests::util::TestScenario::new(::uutests::util_name!()); |
| 91 |
(ts.fixtures.clone(), ts) |
| 92 |
}}; |
| 93 |
} |
| 94 |
|
| 95 |
/// If `common::util::expected_result` returns an error, i.e. the `util` in `$PATH` doesn't |
| 96 |
/// include a coreutils version string or the version is too low, |
| 97 |
/// this macro can be used to automatically skip the test and print the reason. |
| 98 |
#[macro_export] |
| 99 |
macro_rules! unwrap_or_return { |
| 100 |
( $e:expr ) => { |
| 101 |
match $e { |
| 102 |
Ok(x) => x, |
| 103 |
Err(e) => { |
| 104 |
println!("test skipped: {e}"); |
| 105 |
return; |
| 106 |
} |
| 107 |
} |
| 108 |
}; |
| 109 |
} |