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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! A framework to format and check examples.
//!
//! The help text for subcommands includes examples. That's great.
//! But, it is even better when they are tested. This module defines
//! data structures to describe the examples, mechanisms to format the
//! examples, and infrastructure to execute the examples.
use std::collections::BTreeMap;
use clap::builder::IntoResettable;
use clap::builder::Resettable;
/// A command that is executed by the integration test, but not shown
/// in the manual pages.
pub struct Setup<'a> {
pub command: &'a [ &'a str ],
}
/// Builds up setup actions in an extensible way.
pub struct SetupBuilder<'a> {
setup: Setup<'a>,
}
impl<'a> SetupBuilder<'a> {
/// Returns a new setup builder.
const fn new() -> Self {
SetupBuilder {
setup: Setup {
command: &[],
}
}
}
/// Provides the command as slice.
///
/// It'd be nice to provide a per-argument interface, but that
/// requires some ingenuity for it to stay const.
pub const fn command(mut self, command: &'a [&'a str]) -> Self {
self.setup.command = command;
self
}
/// Finishes building the setup action.
pub const fn build(self) -> Action<'a> {
assert!(! self.setup.command.is_empty());
Action::Setup(self.setup)
}
}
/// A command that is executed by the integration test, and shown in
/// the manual pages.
pub struct Example<'a> {
// A human-readable comment.
pub comment: &'a str,
pub command: &'a [ &'a str ],
pub hide: &'a [ &'a str ],
}
/// Builds up example actions in an extensible way.
pub struct ExampleBuilder<'a> {
example: Example<'a>,
}
impl<'a> ExampleBuilder<'a> {
/// Returns a new example builder.
const fn new() -> Self {
ExampleBuilder {
example: Example {
comment: "",
command: &[],
hide: &[],
},
}
}
/// Provides the comment.
///
/// It'd be nice to provide a per-argument interface, but that
/// requires some ingenuity for it to stay const.
pub const fn comment(mut self, comment: &'a str) -> Self {
self.example.comment = comment;
self
}
/// Provides the command as slice.
///
/// It'd be nice to provide a per-argument interface, but that
/// requires some ingenuity for it to stay const.
pub const fn command(mut self, command: &'a [&'a str]) -> Self {
self.example.command = command;
self
}
/// Hides the parameters in the output
///
/// Skip these parameters when generating human readable output
#[allow(unused)]
pub const fn hide(mut self, hide: &'a [&'a str]) -> Self {
self.example.hide = hide;
self
}
/// Finishes building the example action.
///
/// The example will be executed by the test.
pub const fn build(self) -> Action<'a> {
assert!(! self.example.comment.is_empty());
assert!(! self.example.command.is_empty());
Action::Example(self.example)
}
/// Finishes building the example action, marking it for syntax
/// checking only.
///
/// The example will not be executed by the test, but the syntax
/// will be checked using our command line parser.
pub const fn syntax_check(self) -> Action<'a> {
assert!(! self.example.comment.is_empty());
assert!(! self.example.command.is_empty());
Action::SyntaxCheck(self.example)
}
}
/// An action to execute.
#[allow(dead_code)]
pub enum Action<'a> {
/// A command that is executed by the integration test, but not
/// shown in the manual pages.
Setup(Setup<'a>),
/// A command that is syntax check (but not run) by the
/// integration test, and shown in the manual pages.
SyntaxCheck(Example<'a>),
/// A command that is executed by the integration test, and shown
/// in the manual pages.
Example(Example<'a>),
}
impl<'a> Action<'a> {
/// Creates a setup action.
pub const fn setup() -> SetupBuilder<'a> {
SetupBuilder::new()
}
/// Creates an example action.
pub const fn example() -> ExampleBuilder<'a> {
ExampleBuilder::new()
}
/// Return the action's command, if any.
#[allow(dead_code)]
pub fn command(&self) -> Option<&'a [ &'a str ]> {
match self {
Action::Setup(Setup { command, .. }) => Some(command),
Action::SyntaxCheck(Example { command, .. }) => Some(command),
Action::Example(Example { command, .. }) => Some(command),
}
}
}
/// A sequence of actions to execute.
pub struct Actions<'a> {
pub actions: &'a [Action<'a>],
}
impl<'a> IntoResettable<clap::builder::StyledStr> for Actions<'a> {
fn into_resettable(self) -> Resettable<clap::builder::StyledStr> {
// Default width when we aren't connected to a terminal.
let default_width = 72;
// We prefix lines with either `# `, `$ `, or ` `.
const PREFIX_WIDTH: usize = 2;
let terminal_size = terminal_size::terminal_size();
let width = if let Some((width, _height)) = terminal_size {
let width = width.0 as usize;
if width < 40 {
// If the terminal is too narrow, then give up and use
// the default.
default_width
} else {
std::cmp::max(40, width - PREFIX_WIDTH)
}
} else {
default_width
};
let mut lines = vec![ "Examples:".to_string() ];
lines.extend(self.actions
.iter()
.filter_map(|action| {
let example = match action {
Action::SyntaxCheck(example) => example,
Action::Example(example) => example,
// Don't show it.
Action::Setup(_) => return None,
};
let comment = textwrap::indent(
&textwrap::wrap(example.comment, width).join("\n"),
"# ");
// Our manpage generate complains if an
// example is too long:
//
// warning: Command in example exceeds 64 chars:
//
// or
//
// warning: Continuation in example exceeds 57 chars:
let command = wrap_command(&example.command,
&example.hide,
"", width.min(64),
" ", width.min(57));
Some(format!("{}\n{}", comment, command))
}));
let text = lines.join("\n\n").into();
Resettable::Value(text)
}
}
/// Wraps the given command to width, adding continuation backslashes.
///
/// The first line is prefixed with `indent` and wrapped `to_width`,
/// any continuations are prefixed with `continuation_indent` and
/// wrapped to `continuation_width`.
pub fn wrap_command<S: AsRef<str>>(command: &[S],
hide: &[S],
indent: &str,
to_width: usize,
continuation_indent: &str,
continuation_width: usize)
-> String
{
let prompt = platform! {
unix => { "$" },
windows => { ">" },
};
let mut hide
= BTreeMap::from_iter(hide.iter().map(|s| (s.as_ref(), false)));
let result = command
.iter()
.filter(|&item| {
// Remove all of the items in command which are also in
// hide.
if let Some(used) = hide.get_mut(item.as_ref()) {
*used = true;
// Don't show it.
false
} else {
// Show it.
true
}
})
.fold(vec![format!("{}{}", indent, prompt)], |mut s, arg| {
let first = s.len() == 1;
let arg = arg.as_ref();
if arg == "|" {
let last = s.last_mut().expect("have one");
*last = format!("{} \\", last);
s.push(format!(" {}", arg));
return s;
}
// Quote the argument, if necessary.
let quote = |arg: &str| -> String {
if arg.contains(&[
'\"',
]) {
format!("'{}'", arg)
} else if arg.chars().any(char::is_whitespace)
|| arg.contains(&[
'`', '#', '$', '&', '*', '(', ')',
'\\', '|', '[', ']', '{', '}',
';', '\'', '<', '>', '?', '!',
])
{
format!("\"{}\"", arg)
} else {
arg.to_string()
}
};
// If we have --foo=bar, then only but bar in quotes.
let mut quoted = None;
if arg.starts_with("--") {
if let Some(i) = arg.find('=') {
if arg[0..i].chars().all(|c| {
c.is_alphanumeric() || c == '-'
})
{
quoted = Some(format!("{}={}",
&arg[..i],
quote(&arg[i + 1..])));
}
}
}
let arg = if let Some(quoted) = quoted {
quoted
} else {
quote(arg)
};
let last = s.last_mut().expect("have one");
let last_chars = last.chars().count();
let arg_chars = arg.chars().count();
let max_width = if first { to_width } else { continuation_width };
if last_chars + 1 + arg_chars <= max_width {
*last = format!("{} {}", last, arg);
} else {
*last = format!("{} \\", last);
s.push(format!("{}{}", continuation_indent, arg));
}
s
})
.join("\n");
#[cfg(debug_assertions)]
for (arg, used) in hide.into_iter() {
if ! used {
panic!("Example `{}` includes an argument to hide (`{}`), but the \
argument wasn't used by the example!",
command.iter()
.map(|arg| arg.as_ref().to_string())
.collect::<Vec<String>>()
.join(" "),
arg);
}
}
result
}
macro_rules! test_examples {
($ident:ident, $actions:expr) => {
#[test]
fn $ident() {
use std::path::PathBuf;
use tempfile::TempDir;
use assert_cmd::Command;
let fixtures = PathBuf::from(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/data/examples"));
let tmp_dir = TempDir::new().unwrap();
let options = fs_extra::dir::CopyOptions::new()
.content_only(true);
fs_extra::dir::copy(&fixtures, &tmp_dir, &options)
.expect(&format!("Copying {:?} to {:?}",
fixtures, &tmp_dir));
// Create an empty policy configuration file. We use this
// instead of the system-wide policy configuration file,
// which might be more strict than what our test vectors
// expect.
let policy = tmp_dir.path().join("empty-policy.toml");
std::fs::write(&policy, "").unwrap();
let home = tmp_dir.path().join("home");
let cert_store = tmp_dir.path().join("cert-store");
let key_store = tmp_dir.path().join("key-store");
eprintln!("Testing example from {}:{}", file!(), line!());
for (i, action) in $actions.actions.into_iter().enumerate() {
let command = if let Some(command) = action.command() {
command
} else {
continue;
};
if let Action::SyntaxCheck(_) = &action {
// Just syntax check it.
eprintln!("Syntax checking: {:?}", command);
use clap::Parser;
if let Err(err) = $crate::cli::SqCommand::try_parse_from(command.iter()) {
eprintln!("example:{}:{}: checking example #{}: {}",
file!(), line!(), i + 1, err);
panic!("syntax checking example failed");
}
continue;
}
// Handle pipelines by tracking intermediate results.
let mut intermediate = None;
for command in command.split(|p| *p == "|") {
eprintln!("Executing: {:?}", command);
let mut cmd = Command::cargo_bin(command[0]).unwrap();
cmd.current_dir(&tmp_dir)
.env("RUST_BACKTRACE", "1")
.env("RUST_LOG", "trace")
.env("SEQUOIA_CRYPTO_POLICY", &policy)
.env("SEQUOIA_HOME", &home)
.env("SEQUOIA_CERT_STORE", &cert_store)
.env("SEQUOIA_KEY_STORE", &key_store)
.arg("--batch")
.args(&command[1..]);
if let Some(prev) = intermediate {
cmd.write_stdin(prev);
}
let res = cmd.assert();
intermediate = Some(res.get_output().stdout.clone());
if let Err(err) = res.try_success() {
eprintln!("example:{}:{}: executing example #{}: {}",
file!(), line!(), i + 1, err);
panic!("executing example failed");
}
}
}
}
};
}