-
Notifications
You must be signed in to change notification settings - Fork 8
/
readme.rs
968 lines (851 loc) · 35.8 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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
//! This module contains a small bash interpreter, capable of interpreting simple pipes
//! of programs such as `echo 'Hello World!' | some-program --some-option | some-program
//! arg1 arg2`. It knows how to process and forward stdin and stdout, and will assert
//! commands run terminate as expected, with stdout as expected.
//!
//! There is also a small facility for tracking files, enabling usage of `cat some-file`
//! as stdin for later processes.
//!
//! The interpreter can be used to run some binary under test on provided input and test
//! against expected output. In this case, the source for all these elements is a
//! Markdown document.
#[cfg(all(test, feature = "all"))]
mod tests {
use core::{fmt, panic};
use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::io::Write;
use std::mem::ManuallyDrop;
use std::rc::Rc;
use assert_cmd::Command;
use comrak::nodes::{NodeCodeBlock, NodeValue};
use comrak::{parse_document, Arena, ComrakOptions};
use fancy_regex::Regex;
use itertools::Itertools;
use nom::branch::alt;
use nom::bytes::complete::{escaped, tag, take_until, take_while1};
use nom::character::complete::{
alpha1 as ascii_alpha1, alphanumeric1 as ascii_alphanumeric1, anychar, char, line_ending,
none_of, space0, space1,
};
use nom::combinator::{cut, eof, map, opt, recognize};
use nom::error::ParseError;
use nom::multi::{many0, many1, many_till, separated_list1};
use nom::sequence::{delimited, preceded, tuple};
use nom::{Finish, IResult};
use pretty_assertions::assert_eq;
use tempfile::NamedTempFile;
use unescape::unescape;
const PROGRAM_NAME: &str = env!("CARGO_PKG_NAME");
const DOCUMENT: &str = include_str!("../README.md");
/// 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),
}
}
const 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) | Self::Cat(inv) | Self::Self_(inv) => inv.stdout.clone(),
}
}
/// This sets an option to forcibly ignore any provided stdin.
///
/// Even though a command might run with `None` for its stdin, the binary under
/// test might heuristically check for a readable stdin and somehow detect it.
/// This might be a quirk in the command execution from [`Command`]... no idea.
/// For that scenario, we need a hacky method to disable stdin for good.
fn force_ignore_stdin(&mut self) {
match self {
Self::Self_(inv) => inv
.opts
.push(Opt::Long("stdin-override-to".into(), "false".into())),
_ => panic!("Forcing stdin ignore only applicable to `self` program"),
}
}
}
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 = Self::cargo_bin(name).expect("Should be able to find binary");
for flag in &inv.flags {
cmd.arg(flag.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.args([format!("-{name}"), value]);
}
Opt::Long(name, value) => {
cmd.args([format!("--{name}"), value]);
}
}
}
for arg in &inv.args {
cmd.arg(arg.to_string());
}
if let Some(stdin) = inv.stdin {
cmd.write_stdin(stdin);
}
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,
/// Is this not actually a pipe, but a single, standalone program?
///
/// Checking for the length of `programs` might not suffice, as programs are
/// dropped from it as it's processed.
standalone: 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 to kick things off (unless there is only one `standalone` program in
/// the pipe). The passed `stdout` is the expected output of the last program,
/// aka the entire pipe. All programs in the middle (after *second*, before
/// last), which can be an unbounded number, are later on fed their stdin
/// dynamically, from the previous program's output, with their stdout becoming
/// the next piped program's stdin, and so on. These stdins and stdouts are not
/// set here.
///
/// [`Program::Cat`] invocations may need access to [`Snippets`], so those are
/// provided as well; pipes may also be *expected* to fail.
///
/// For example, a chain might look like:
///
/// ```text
/// echo 'hello' | butest -a | butest -b | butest --cee | butest -d
/// ^^^^^^^^^^^^ ^^^^^^^^^
/// produces is fed ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^
/// initial stdin to these are provided last one
/// stdin kick their stdin *later* in the
/// things on, and produce their pipe:
/// off stdout dynamically too its
/// stdout is
/// set here,
/// its stdin
/// provided
/// dynamically
/// ```
///
/// with some binary under test (`butest`).
fn assemble(
chain: impl Iterator<Item = Program>,
stdout: Option<String>,
snippets: &Snippets,
should_fail: bool,
) -> Result<Self, &'static str> {
let mut programs = chain.collect::<VecDeque<_>>();
eprintln!("Will assemble programs: {programs:?}");
// There's a trailing newline inserted which ruins/fails diffs.
let stdout = stdout.map(|s| s.trim_end().to_string());
let mut standalone = false;
let first = programs
.pop_front()
.ok_or("Should have at least one program in pipe")?;
let stdin = match &first {
Program::Echo(inv) => Some(
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")?;
Some(
snippets
.get(&file_name.0)
.ok_or("Snippet should be present")?
.original
.clone()
.ok_or("Snippet should have an original")?,
)
}
Program::Self_(..) => {
standalone = true;
None
}
};
// Set the *second*, if any, command's standard input.
match programs.front_mut() {
Some(Program::Echo(_) | Program::Cat(_)) => {
return Err("Stdin-generating program should not be in the middle of a pipe")
}
Some(Program::Self_(inv)) => {
inv.stdin = stdin;
}
None => {
// Nothing to do; assert flag was set already.
assert!(standalone);
}
}
// Set the expected standard output of the *entire* pipe, aka the last
// program's standard output.
let mut first = first;
match programs.back_mut() {
Some(Program::Echo(_) | Program::Cat(_)) => {
return Err("Stdin-generating program should not be at the end of a pipe")
}
Some(Program::Self_(inv)) => {
inv.stdout = if should_fail {
// No stdout needed if command fails anyway
None
} else if let Program::Cat(inv) = first {
Some(
snippets
.get(&inv.args.first().expect("Cat should have an argument").0)
.expect("Cat invocation needs a snippet")
.output
.clone()
.unwrap_or_else(|| {
stdout.expect(
"Snippet for cat has no output, so stdout is required",
)
}),
)
} else {
Some(stdout.expect("Stdout should be given for non-`cat`-fed program"))
}
}
None => {
match &mut first {
// There is no 'last program': we have a stand-alone one.
Program::Echo(_) | Program::Cat(_) => {
return Err("Illegal standalone program")
}
Program::Self_(inv) => {
inv.stdout = Some(
stdout.expect("Stdout should be given for standalone program"),
);
}
};
assert!(standalone, "Should have been set before.");
// Put it back!
programs.push_back(first);
}
}
assert!(!programs.is_empty());
Ok(Self {
programs,
should_fail,
standalone,
})
}
}
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<'a>(
input: &'a str,
snippets: &Snippets,
) -> IResult<&'a 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. Be careful about `$` signs
// *inside* of expected stdout: only `\n$` is a new prompt.
let (input, (stdout_chars, start_of_next_input)) =
many_till(anychar, alt((preceded(line_ending, tag("$")), eof)))(input)?;
assert!(
start_of_next_input == "$" || start_of_next_input.is_empty(),
"Unexpected parsing result: {start_of_next_input} (remaining input: {})",
input.escape_debug()
);
let stdout = if stdout_chars.is_empty() {
None
} else {
Some(stdout_chars.into_iter().join(""))
};
eprintln!("Remaining input: {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, O, E>(inner: F) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
where
F: 'a + Fn(&'a str) -> IResult<&'a str, O, E>,
E: ParseError<&'a str>,
{
delimited(space0, inner, space0)
}
/// Parses a single, whole program invocation.
#[allow(clippy::too_many_lines)] // :( many hard-coded values
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.
//
// Failure to add values here will result in bizarre and
// wrong order of arguments. For example, if `--some-option
// some_value` is given and `--some-option` is not
// registered here, `--some-option` will be understood as a
// *flag*, and `some_value` as a *positional argument*.
tag("--"),
alt((
// ⚠️ Careful: all `--<lang>-query` options need to come
// first; otherwise, the shorter `--<lang>` options eat
// them and results turn bad (complaining that `-query`
// is not a valid value). Generally, parsing is greedy,
// so shorter values will short-circuit, potentially
// incorrectly. That's why lines are generally sorted
// longest to shortest, to avoid confusion.
//
// Parsing is brittle here :-(
alt((
tag("typescript-query"),
tag("csharp-query"),
tag("python-query"),
tag("rust-query"),
tag("hcl-query"),
tag("go-query"),
tag("c-query"),
)),
//
alt((
tag("typescript"),
tag("csharp"),
tag("python"),
tag("rust"),
tag("hcl"),
tag("go"),
tag("c"),
)),
// Misc. flags used in the docs
alt((tag("glob"), tag("stdin-override-to"), tag("threads"))),
// Shorthands
alt((
tag("tsx"),
tag("hcl"),
tag("cs"),
tag("py"),
tag("rs"),
tag("ts"),
tag("go"),
tag("tf"),
tag("c"),
// tag("h"), // Breaks `--help` and isn't used
)),
)),
),
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,
space0,
),
),
)),
|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(
// File paths.
recognize(tuple((
many1(alt((ascii_alphanumeric1, tag("/"), tag("-"), tag("_")))),
char('.'),
ascii_alphanumeric1,
))),
|file_path: &str| {
inv.borrow_mut().args.push(file_path.into());
file_path.to_owned()
},
),
))),
))(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<'a>(
input: &'a str,
snippets: &Snippets,
) -> IResult<&'a str, Vec<PipedPrograms>> {
many1(|input| parse_piped_programs_with_prompt_and_output(input, snippets))(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)
}
/// Supported Operating systems.
#[derive(Debug, Clone, PartialEq, Eq)]
enum OS {
Linux,
#[allow(clippy::enum_variant_names)]
MacOS,
Windows,
}
impl TryFrom<String> for OS {
type Error = &'static str;
/// Convert from a value as returned by [`std::env::consts::OS`].
fn try_from(value: String) -> Result<Self, Self::Error> {
match value.as_str() {
"linux" => Ok(Self::Linux),
"macos" => Ok(Self::MacOS),
"windows" => Ok(Self::Windows),
// Just a double check to ensure parsing of options went right.
_ => Err("unknown OS"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
struct BlockOptions {
/// The file name to associate with this code block, if any.
filename: Option<String>,
/// Skip when running on any of these operating systems.
skip_os: Option<Vec<OS>>,
}
impl<S> From<S> for BlockOptions
where
S: AsRef<str>,
{
fn from(value: S) -> Self {
let string = value.as_ref();
let mut options: HashMap<&str, String> = string
.split(';')
.map(|pair| {
pair.split_once('=')
.unwrap_or_else(|| panic!("need a value for key-value pair: {}", pair))
})
.map(|(k, v)| (k, v.to_string()))
.collect();
let get_many = |s: String| s.split(',').map(ToOwned::to_owned).collect_vec();
let res = Self {
filename: options.remove("file"),
skip_os: options.remove("skip-os").map(get_many).map(|oss| {
oss.into_iter()
.map(|os| os.try_into().expect("any used OS to be known"))
.collect_vec()
}),
};
assert!(options.is_empty(), "unknown keys in options: {options:?}");
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(DOCUMENT, |ncb| {
if let Some((_language, options)) = ncb.info.split_once(' ') {
let mut snippet = Snippet::default();
let options: BlockOptions = options.into();
if let BlockOptions {
filename: Some(mut file_name),
..
} = options
{
file_name = if let Some(stripped_file_name) = file_name.strip_prefix("output-")
{
snippet.output = Some(ncb.literal.trim_end().to_owned());
stripped_file_name.to_owned()
} else {
snippet.original = Some(ncb.literal.trim_end().to_owned());
file_name
};
if let Some(other) = snippets.remove(&file_name) {
snippet = snippet.join(other);
}
snippets.insert(file_name, snippet);
}
}
});
eprintln!("Snippets: {snippets:#?}");
snippets
}
fn get_readme_program_pipes(snippets: &Snippets) -> Vec<PipedPrograms> {
let mut pipes = Vec::new();
map_on_markdown_codeblocks(DOCUMENT, |ncb| {
let (language, options): (&str, Option<BlockOptions>) = ncb
.info
.split_once(' ')
.map_or_else(|| (ncb.info.as_str(), None), |(l, o)| (l, Some(o.into())));
if let Some(BlockOptions {
skip_os: Some(oss), ..
}) = options
{
let curr_os: OS = std::env::consts::OS
.to_owned()
.try_into()
.expect("any used OS to be known");
if oss.contains(&curr_os) {
eprintln!("Skipping OS: {curr_os:?}");
return;
}
}
if language == "console" || language == "bash" {
let (_, commands) = parse_code_blocks(&ncb.literal, &snippets.clone())
.finish()
.expect("Anything in `console` should be parseable as a command");
pipes.extend(commands);
}
});
eprintln!("Piped programs: {pipes:?}");
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 _helper = TestHinter;
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;
let standalone = pipe.standalone;
for mut program in pipe {
if standalone {
program.force_ignore_stdin();
}
let program = program; // de-mut
let mut cmd = Command::try_from(program.clone())
.expect("Should be able to convert invocation to cmd to run");
// We're testing and need determinism. This hard-codes a flag!
cmd.arg("--sorted");
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()
};
let mut observed_stdout = String::from_utf8(assertion.get_output().stdout.clone())
.expect("Stdout should be given as UTF-8");
observed_stdout = {
let s = observed_stdout.trim_end();
s.to_owned()
};
if let Some(expected_stdout) = program.stdout().clone() {
if cfg!(target_os = "windows") {
observed_stdout = fix_windows_output(observed_stdout);
}
if observed_stdout != expected_stdout {
// Write to files for easier inspection
let (mut obs_f, mut exp_f) = (
ManuallyDrop::new(NamedTempFile::new().unwrap()),
ManuallyDrop::new(NamedTempFile::new().unwrap()),
);
obs_f.write_all(observed_stdout.as_bytes()).unwrap();
exp_f.write_all(expected_stdout.as_bytes()).unwrap();
// Now panic as usual, for the usual output
assert_eq!(
// Get some more readable output diff compared to
// `assert::Command`'s `stdout()` function, for which diffing
// whitespace is very hard.
expected_stdout,
observed_stdout,
"Output differs; for inspection see observed stdout at '{}', expected stdout at '{}'",
obs_f.path().display(),
exp_f.path().display()
);
// Temporary files remain, they're not dropped.
unreachable!();
}
}
// Pipe stdout to stdin of next run...
previous_stdin = Some(observed_stdout);
}
}
}
fn fix_windows_output(mut input: String) -> String {
input = unixfy_file_paths(&input);
input = remove_exe_suffix(&input);
input
}
fn remove_exe_suffix(input: &str) -> String {
input.replace(
concat!(env!("CARGO_PKG_NAME"), ".exe"),
env!("CARGO_PKG_NAME"),
)
}
/// The document under test might contain hard-coded Unix file paths. When running
/// under Windows, where `\` might be printed as the path separator, tests will
/// break. So hack strings which look like paths to spell `/` instead of `\` as
/// their path separator.
fn unixfy_file_paths(input: &str) -> String {
// Pattern for Windows-style file paths:
//
// ```text
// No Match: /usr/bin/local
// No Match: bin/local
// Matches: test\files
// Matches: test\some\more\files
// Matches: test
// Matches: test\some\file.py
//```
//
// https://regex101.com/r/NURPe2/1
let pattern = Regex::new(r"^([a-z]+\\)*[a-z]+(\.[a-z]+)?$").unwrap();
let mut res = input
.lines()
.map(|s| {
if pattern.is_match(s).unwrap() {
let res = s.replace('\\', "/");
eprintln!("Replaced Windows path-like string: {s} -> {res}");
res
} else {
s.to_owned()
}
})
.collect::<Vec<String>>()
.join("\n");
if input.ends_with('\n') {
res.push('\n');
}
res
}
struct TestHinter;
impl Drop for TestHinter {
fn drop(&mut self) {
if std::thread::panicking() {
println!("\n==============================================");
println!("💡 README test failed!");
println!("Did you update the `srgn --help` output?");
println!("If no, run `./scripts/update-readme.py README.md` and try again.");
println!("==============================================\n");
}
}
}
}