/
readme.rs
627 lines (547 loc) · 21.7 KB
/
readme.rs
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#[cfg(all(test, feature = "all"))]
mod tests {
use assert_cmd::Command;
use comrak::{
nodes::{NodeCodeBlock, NodeValue},
parse_document, Arena, ComrakOptions,
};
use core::fmt;
use nom::{
branch::alt,
bytes::complete::{escaped, is_not, tag, take_until, take_while1},
character::complete::{
alpha1 as ascii_alpha1, alphanumeric1 as ascii_alphanumeric1, char, line_ending,
none_of, space0, space1,
},
combinator::{cut, map, opt},
error::ParseError,
multi::{many0, many1, separated_list1},
sequence::{delimited, preceded, tuple},
Finish, IResult,
};
use std::{
cell::RefCell,
collections::{HashMap, VecDeque},
rc::Rc,
};
use unescape::unescape;
const PROGRAM_NAME: &str = env!("CARGO_PKG_NAME");
/// A flag, either short or long.
///
/// Does not have a value, e.g. `--flag` or `-f`.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Flag {
Short(char),
Long(String),
}
impl From<char> for Flag {
fn from(c: char) -> Self {
Self::Short(c)
}
}
impl From<&str> for Flag {
fn from(s: &str) -> Self {
Self::Long(s.to_string())
}
}
impl fmt::Display for Flag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Short(c) => write!(f, "-{}", c),
Self::Long(s) => write!(f, "--{}", s),
}
}
}
/// An option, either short or long.
///
/// Has a value, e.g. `--option value` or `-o value`.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Opt {
#[allow(dead_code)] // Not used yet
Short(char, String),
Long(String, String),
}
impl From<(&str, &str)> for Opt {
fn from((s, v): (&str, &str)) -> Self {
Self::Long(s.to_string(), v.to_string())
}
}
/// A positional argument.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Arg(String);
impl From<&str> for Arg {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl fmt::Display for Arg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
/// A collected, whole invocation of a program, including all bits and pieces
/// required for running *except* the program name itself.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
struct Invocation {
flags: Vec<Flag>,
opts: Vec<Opt>,
args: Vec<Arg>,
//
stdin: Option<String>,
stdout: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Program {
/// The `echo` program, used to generate stdin for the program under test.
Echo(Invocation),
/// The `cat` program, used to generate stdin for the program under test.
Cat(Invocation),
/// The binary under test itself.
Self_(Invocation),
}
impl Program {
fn from_name(name: &str, mut invocation: Invocation) -> Self {
match name {
"echo" => {
if invocation.flags.contains(&Flag::Short('e')) {
invocation.args = invocation
.args
.into_iter()
.map(|a| Arg(unescape(a.0.as_str()).expect("Invalid escape sequence")))
.collect();
}
Self::Echo(invocation)
}
"cat" => Self::Cat(invocation),
PROGRAM_NAME => Self::Self_(invocation),
_ => panic!("Unsupported program name: {}", name),
}
}
fn name(&self) -> &str {
match self {
Self::Echo(_) => "echo",
Self::Cat(_) => "cat",
Self::Self_(_) => PROGRAM_NAME,
}
}
fn stdout(&self) -> Option<String> {
match self {
Self::Echo(inv) => inv.stdout.clone(),
Self::Cat(inv) => inv.stdout.clone(),
Self::Self_(inv) => inv.stdout.clone(),
}
}
}
impl TryFrom<Program> for Command {
type Error = &'static str;
fn try_from(prog: Program) -> Result<Self, Self::Error> {
let name = prog.name().to_string();
match prog {
Program::Echo(_) | Program::Cat(_) => {
Err("Cannot be run, only used to generate stdin")
}
Program::Self_(inv) => {
let mut cmd = Command::cargo_bin(name).expect("Should be able to find binary");
for flag in inv.flags {
cmd.arg(flag.to_string());
}
for arg in inv.args {
cmd.arg(arg.to_string());
}
for opt in inv.opts {
match opt {
Opt::Short(name, value) => {
// Push these separately, as `arg` will escape the
// value, and something like `--option value` will be
// taken as a single arg, breaking the test.
cmd.arg(format!("-{name}"));
cmd.arg(value);
}
Opt::Long(name, value) => {
cmd.arg(format!("--{name}"));
cmd.arg(value);
}
}
}
// Empty string will be overwritten later on anyway. This saves a bunch
// of code later.
cmd.write_stdin(inv.stdin.unwrap_or_default());
Ok(cmd)
}
}
}
}
/// Multiple commands can be piped together.
#[derive(Debug, Clone, PartialEq, Eq)]
struct PipedPrograms {
/// The program forming the pipe.
programs: VecDeque<Program>,
/// The expected outcome of the *entire* pipe. Any failure anywhere in the pipe
/// should cause overall failure (like `pipefail`).
should_fail: bool,
}
impl PipedPrograms {
/// Assembles a list of programs into a pipe.
///
/// The first program is specially treated, and needs to be able to produce some
/// stdin. The passed `stdout` is the expected output of the last program, aka
/// the entire pipe.
fn assemble(
chain: impl Iterator<Item = Program>,
stdout: Option<&str>,
snippets: Snippets,
should_fail: bool,
) -> Result<Self, &str> {
let mut programs = chain.collect::<VecDeque<_>>();
let first = programs
.pop_front()
.ok_or("Should have at least one program in pipe")?;
let stdin = match &first {
Program::Echo(inv) => inv
.args
.first()
.ok_or("Echo should have an argument")?
.to_string(),
Program::Cat(inv) => {
let file_name = inv.args.first().ok_or("Cat should have an argument")?;
snippets
.get(&file_name.0)
.ok_or("Snippet should be present")?
.original
.clone()
.ok_or("Snippet should have an original")?
}
_ => return Err("First command should be able to produce stdin."),
};
match programs
.front_mut()
.expect("No second program to assemble with")
{
Program::Echo(_) | Program::Cat(_) => {
return Err("Stdin-generating program should not be in the middle of a pipe")
}
Program::Self_(inv) => {
inv.stdin = Some(stdin);
}
}
match programs
.back_mut()
.expect("No last program to assemble with")
{
Program::Echo(_) | Program::Cat(_) => {
return Err("Stdin-generating program should not be at the end of a pipe")
}
Program::Self_(inv) => {
inv.stdout = if should_fail {
// No stdout needed if command fails anyway
None
} else if let Program::Cat(inv) = first {
assert!(
stdout.is_none(),
"Cat output should be given as extra snippet, not inline"
);
Some(
snippets
.get(&inv.args.first().expect("Cat should have an argument").0)
.ok_or("Snippet should be present")?
.output
.clone()
.expect("Snippet should have an output"),
)
} else {
Some(
stdout
.expect("Stdout should be given for non-`cat`-fed program")
// No patience for hard-to-diff, fiddly, invisible
// whitespace issues, even though it would be "more
// correct"
.trim_end()
.to_string(),
)
}
}
}
Ok(Self {
programs,
should_fail,
})
}
}
impl IntoIterator for PipedPrograms {
type Item = Program;
type IntoIter = std::collections::vec_deque::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.programs.into_iter()
}
}
/// Parses a code block such as:
///
/// ```console
/// $ echo 'some input' | program --flag arg1 arg2 | program2 arg1 # Optional comment
/// some output
/// ```
///
/// into a proper [`CommandUnderTest`], such that `program` can be run with those
/// found arguments. Note this routine's job is *not* to deal with the backticks or
/// the `console` language tag, but rather to parse the command and its output (so
/// anything in between). If applied multiple times, blocks such as these can be
/// detected:
///
/// ```console
/// $ echo 'some input' | program --flag arg1 arg2 # Optional comment
/// some output
/// $ echo 'some other input' | program arg1
/// some other output
/// ```
fn parse_piped_programs_with_prompt_and_output(
input: &str,
snippets: Snippets,
) -> IResult<&str, PipedPrograms> {
let prompt = '$';
let (input, _) = opt(char(prompt))(input)?;
let (input, _) = space0(input)?;
let (input, programs) = parse_piped_programs(input)?;
eprintln!("Parsed programs: {:#?}", programs);
// Advance to end; this eats optional comments and trailing whitespace.
let (input, tail) = take_until("\n")(input)?;
let should_fail = tail.contains("will fail");
let (input, _) = line_ending(input)?;
// Parse stdout; anything up to the next prompt.
let (input, stdout) = opt(is_not(prompt.to_string().as_str()))(input)?;
eprintln!("Parsed stdout: {:#?}", stdout);
Ok((
input,
PipedPrograms::assemble(programs.into_iter(), stdout, snippets, should_fail)
.expect("Should be able to assemble"),
))
}
/// https://docs.rs/nom/7.1.3/nom/recipes/index.html#wrapper-combinators-that-eat-whitespace-before-and-after-a-parser
/// A combinator that takes a parser `inner` and produces a parser that also consumes both leading and
/// trailing whitespace, returning the output of `inner`.
fn maybe_ws<'a, F: 'a, O, E: ParseError<&'a str>>(
inner: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
where
F: Fn(&'a str) -> IResult<&'a str, O, E>,
{
delimited(space0, inner, space0)
}
/// Parses a single, whole program invocation.
fn parse_program(input: &str) -> IResult<&str, Program> {
// Interior mutability is fine, as the different closures aliasing this run
// sequentially, never at once (is using this and `map` of `nom` an
// anti-pattern? works quite well...)
let inv = Rc::new(RefCell::new(Invocation::default()));
let (input, (name, _flags, _args)) = tuple((
maybe_ws(ascii_alpha1),
many0(alt((
map(
tuple((
preceded(
// Long options. Hard-coded, as otherwise it's undecidable
// whether an option is supposed to have a value or not
// (just a flag). Alternatively, import `clap::Cli` here and
// `try_get_matches` with it, but cannot/don't want to
// expose (`pub`) that.
tag("--"),
alt((
tag("csharp-query"),
tag("csharp"),
tag("go-query"),
tag("go"),
tag("python-query"),
tag("python"),
tag("rust-query"),
tag("rust"),
tag("typescript-query"),
tag("typescript"),
)),
),
cut(
// `cut`: should we get here, and not succeed, parsing has
// to fail entirely. Else we continue with bad data.
delimited(
space1,
// Quoting always is technically overkill, but much
// simpler and safer
parse_quoted,
space1,
),
),
)),
|findings| {
inv.borrow_mut().opts.push(findings.into());
findings
},
),
map(
tuple((
preceded(
// Long flags, like `--long-flag`. No values. Can contain
// hyphens itself.
tag("--"),
take_while1(|c: char| c == '-' || c.is_ascii_alphanumeric()),
),
space0,
)),
|findings: (&str, &str)| {
let (flag, _space) = findings;
inv.borrow_mut().flags.push(flag.into());
findings
},
),
map(
tuple((
// Short flags, like `-s`, but also `-sGu`. No values.
preceded(char('-'), ascii_alphanumeric1),
space0,
)),
|found: (&str, &str)| {
let (flag, _space) = found;
flag.chars()
.for_each(|c| inv.borrow_mut().flags.push(c.into()));
found
},
),
))),
many0(alt((
map(
// Regular, quoted positional args
maybe_ws(parse_quoted),
|s: &str| {
inv.borrow_mut().args.push(s.into());
// Owned because type needs to align with other list members
s.to_owned()
},
),
map(
// There's also file names, which cannot occur quoted in shell
// contexts
tuple((ascii_alphanumeric1, char('.'), ascii_alphanumeric1)),
|parts: (&str, char, &str)| {
let (stem, sep, suffix) = parts;
let file_name = format!("{}{}{}", stem, sep, suffix);
inv.borrow_mut().args.push(file_name.as_str().into());
file_name
},
),
))),
))(input)?;
let (input, _) = space0(input)?;
let program = Program::from_name(name, inv.borrow().clone());
Ok((input, program))
}
fn parse_piped_programs(input: &str) -> IResult<&str, Vec<Program>> {
separated_list1(tag("|"), parse_program)(input)
}
/// Parses multiple pairs of 'command and output' into a list of them.
fn parse_code_blocks(input: &str, snippets: Snippets) -> IResult<&str, Vec<PipedPrograms>> {
many1(|input| parse_piped_programs_with_prompt_and_output(input, snippets.clone()))(input)
}
/// https://stackoverflow.com/a/58907488/11477374
fn parse_quoted(input: &str) -> IResult<&str, &str> {
let esc = escaped(none_of(r"'"), '\\', tag("'"));
let esc_or_empty = alt((esc, tag("")));
let res = delimited(tag("'"), esc_or_empty, tag("'"))(input)?;
Ok(res)
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
struct Snippet {
original: Option<String>,
output: Option<String>,
}
impl Snippet {
fn join(self, other: Self) -> Self {
Self {
original: Some(
self.original
.or(other.original)
.expect("After joining, snippet should have an original"),
),
output: Some(
self.output
.or(other.output)
.expect("After joining, snippet should have an output"),
),
}
}
}
type Snippets = HashMap<String, Snippet>;
fn get_readme_snippets() -> Snippets {
let mut snippets = HashMap::new();
map_on_markdown_codeblocks(include_str!("../README.md"), |ncb| {
if let Some((_language, mut file_name)) = ncb.info.split_once(' ') {
let mut snippet = Snippet::default();
file_name = match file_name.strip_prefix("output-") {
Some(stripped_file_name) => {
snippet.output = Some(ncb.literal);
stripped_file_name
}
None => {
snippet.original = Some(ncb.literal);
file_name
}
};
if let Some((_, other)) = snippets.remove_entry(file_name) {
snippet = snippet.join(other);
}
snippets.insert(file_name.to_owned(), snippet);
}
});
eprintln!("Snippets: {:#?}", snippets);
snippets
}
fn get_readme_program_pipes(snippets: Snippets) -> Vec<PipedPrograms> {
let mut pipes = Vec::new();
map_on_markdown_codeblocks(include_str!("../README.md"), |ncb| {
if ncb.info == "console" || ncb.info == "bash" {
let (_, commands) = parse_code_blocks(&ncb.literal, snippets.clone())
.finish()
.expect("Anything in `console` should be parseable as a command");
pipes.extend(commands);
}
});
pipes
}
fn map_on_markdown_codeblocks(markdown: &str, mut f: impl FnMut(NodeCodeBlock)) {
let arena = Arena::new();
let root = parse_document(&arena, markdown, &ComrakOptions::default());
root.descendants().for_each(|node| {
let value = node.to_owned().data.borrow().value.clone();
if let NodeValue::CodeBlock(ncb) = value {
f(ncb);
}
});
}
#[test]
fn test_readme_code_blocks() {
let snippets = get_readme_snippets();
let pipes = get_readme_program_pipes(snippets);
for pipe in pipes {
let mut previous_stdin = None;
let should_fail = pipe.should_fail;
for program in pipe {
let mut cmd = Command::try_from(program.clone())
.expect("Should be able to convert invocation to cmd to run");
if let Some(previous_stdin) = previous_stdin {
cmd.write_stdin(previous_stdin);
}
eprintln!("Running command: {:?}", cmd);
let mut assertion = cmd.assert();
assertion = if should_fail {
assertion.failure()
} else {
assertion.success()
};
if let Some(stdout) = program.stdout().clone() {
assertion.stdout(stdout);
}
// Pipe stdout to stdin of next run...
previous_stdin = Some(
String::from_utf8(cmd.assert().get_output().stdout.clone())
.expect("Stdout should be given as UTF-8"),
);
}
}
}
}