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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//! A testing library that makes it easy(ish) to add intentional errors to a program
//!
//! When testing error-handling codepaths, it is often useful to programmatically tell parts of the
//! code to fail. This crate provides the [`failspot!()`][failspot] macro, which can be used
//! to mark a spot in the codepath where an intentional failure can be toggled on and off
//! from testing code.
//!
//! Adding it to code is fairly simple:
//!
//! ```no_run
//! # use {failspot::failspot, std::{error::Error, fs, io, path::Path}};
//! # failspot::failspot_name! { pub enum FailSpotName { FailDataRead } }
//! # fn main() {
//! # read_data_file("/tmp/foo".as_ref());
//! # }
//! fn read_data_file(path: &Path) -> Result<Vec<u8>, Box<dyn Error>> {
//! // `FailDataRead` is a variant of an enum that was declared in our crate
//! // `bail` returns `Err` and does type conversion on the error type
//! failspot!(FailDataRead bail(io::Error::other("failed due to test config")));
//! Ok(fs::read(path)?)
//! }
//! ```
//!
//! The [`failspot_name!()`][failspot_name] macro is used to declare an enum that can be used
//! to name a failspot. Its syntax is identical to a regular enum declaration:
//!
//! ```no_run
//! # use failspot::failspot_name;
//! failspot_name! {
//! pub enum FailSpotName {
//! StuffWentWrong,
//! }
//! }
//! ```
//!
//! # Syntaxes
//!
//! The [`failspot!()`][failspot] macro has **four main syntaxes**. Each takes either a
//! **long form** or a **short form**.
//!
//! The **long form** explicitly contains the path to the enum containing the failspot names. This
//! is useful if there are multiple enums with different failspot names, or if the enum is only
//! accessible at a non-standard name or path.
//!
//! If the enum can be reached at the name `crate::FailSpotName` (either because it was declared in
//! the crate root or re-exported there), the **short form** versions of these macros
//! can be used. The examples below make this clear:
//!
//! ## "if-else" syntax (long form)
//!
//! Useful when standard if-else behavior is desired:
//!
//! ```
//! # use {failspot::failspot};
//! # pub mod my_module {
//! # failspot::failspot_name! { pub enum MyFailName { FailDataRead } }
//! # }
//! # fn main() {
//! // In "long form", the entire path to the enum must be spelled out
//! let _enabled = failspot!(if <crate::my_module::MyFailName>::FailDataRead {
//! println!("Data read failure enabled");
//! true
//! } else {
//! println!("Data read failure disabled");
//! false
//! });
//! # }
//! ```
//!
//! When the `enabled` feature is not on, the compiler will see the second block verbatim.
//!
//! ### The same code with the short-form
//!
//! If an enum named `FailSpotName` is reachable at the crate root, like this:
//!
//! ```
//! # use failspot::failspot_name;
//! // lib.rs
//! failspot_name! {
//! pub enum FailSpotName {
//! FailDataRead,
//! }
//! }
//! ```
//!
//! The short form can be used, like this:
//!
//! ```
//! # use failspot::{failspot, failspot_name};
//! # failspot_name! { pub enum FailSpotName { FailDataRead } }
//! # fn main() {
//! let _enabled = failspot!(if FailDataRead {
//! println!("Data read failure enabled");
//! true
//! } else {
//! println!("Data read failure disabled");
//! false
//! });
//! # }
//! ```
//!
//! ## "quick expression" syntax
//!
//! Useful when a short block of syntax should be evaluated when the failspot is enabled:
//!
//! ```
//! # use {failspot::failspot};
//! # failspot::failspot_name! { pub enum FailSpotName { FailDataRead } }
//! # fn main() {
//! failspot!(FailDataRead println!("Data read failure enabled"));
//!
//! // Also good for panicking
//! failspot!(FailDataRead panic!());
//!
//! // Or for just early returning
//! failspot!(FailDataRead return);
//!
//! // Multiple statements can be run, but use sparingly as things start to get ugly.
//! failspot!(FailDataRead println!("Data read failure enabled"); return);
//! # }
//! ```
//!
//! When the `enabled` feature is not on, the macro will evaluate to the empty block, `{}`.
//!
//! ## "bail" syntax
//!
//! Useful for returning an `Err` with error-type conversion:
//!
//! ```
//! # use {failspot::failspot, std::error::Error};
//! # failspot::failspot_name! { pub enum FailSpotName { FailDataRead } }
//! fn main() -> Result<(), Box<dyn Error>> {
//! failspot!(FailDataRead bail(std::io::Error::other("Data read failure enabled")));
//! Ok(())
//! }
//! ```
//!
//! When the `enabled` feature is not on, the macro will evaluate to the empty block, `{}`.
//!
//! ## "bool" syntax
//!
//! Useful to just evaluate whether the failspot is enabled or not:
//!
//! ```
//! # use {failspot::failspot, std::error::Error};
//! # failspot::failspot_name! { pub enum FailSpotName { FailDataRead } }
//! # fn main() {
//! # let bytes_to_read = 5;
//! let fail_the_read = bytes_to_read > 5000 || failspot!(FailDataRead);
//! # }
//! ```
//!
//! When the `enabled` feature is not on, the macro will evaluate to the token `false`.
/// Declares a spot that can trigger intentional failures
///
/// See the [crate-level docs][crate] for details.
};
=> ;
=> ;
=> ;
=> ;
=> ;
=> ;
=> }
/// Declares a spot that can trigger intentional failures
///
/// See the [crate-level docs][crate] for details.
};
=> ;
=> ;
=> ;
=> ;
=> ;
=> ;
=> }
/// Declares an enum that can be used as a name for a [failspot]
///
/// When feature `enabled` is off, this macro does nothing.
///
/// See the [crate-level docs][crate] for details.
} => }
/// Declares an enum that can be used as a name for a [failspot]
///
/// When feature `enabled` is off, this macro does nothing.
///
/// See the [crate-level docs][crate] for details.
;
;
pub use flagset;