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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#[macro_use]
pub mod core;
pub mod enc;
pub mod errors;
pub mod net;
pub mod sys;
pub mod unit;
pub use crate::errors::FuError;
pub use crate::errors::FuResult;
pub mod prelude {
pub use super::{
cfgblock,
core::*,
defer,
enc::{gzip, tar},
errors::*,
function,
net::{self, agent},
sys::{self, exec, ext::*, user},
unit::{self, time},
};
pub use blake2;
pub use gory::*;
pub use lazy_static::*;
pub use libc;
pub use regex::Regex;
pub use std::{
ffi::{OsStr, OsString},
fmt,
fs::{self, File, OpenOptions},
io::{self, prelude::*, BufRead, BufReader},
os::unix::fs::{MetadataExt, PermissionsExt},
path::{Path, PathBuf},
str,
};
}
pub mod assert {
pub use super::{assert_dir, assert_exists, assert_file, assert_no_dir, assert_no_exists, assert_no_file, assert_remove, assert_remove_all, assert_setup, core::assert::*, create_test_setup_func, prelude::*};
}
#[macro_export]
macro_rules! defer {
($($tokens:tt)*) => {
let _defer = defer(|| { $($tokens)* });
};
}
#[macro_export]
macro_rules! function {
() => {{
fn _f() {}
fn type_of<T>(_: T) -> &'static str {
std::any::type_name::<T>()
}
let fqn = type_of(_f);
let fqn = &fqn[..fqn.len() - 4];
match fqn.rfind(':') {
Some(i) => &fqn[i + 1..],
None => &fqn,
}
}};
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
use std::cell::Cell;
#[test]
fn test_defer_macro() {
let obj = Cell::new(1);
defer!(obj.set(2));
assert_eq!(1, obj.get());
}
#[test]
fn test_function_macro() {
fn indirect_func_name() -> &'static str {
function!()
}
assert_eq!(function!(), "test_function_macro");
assert_eq!(indirect_func_name(), "indirect_func_name");
}
}