-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathmatchers.rs
More file actions
719 lines (662 loc) · 23.6 KB
/
matchers.rs
File metadata and controls
719 lines (662 loc) · 23.6 KB
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
use std::fmt;
use std::process::Output;
use std::str;
use crate::process::ProcessBuilder;
use hamcrest2::core::{MatchResult, Matcher};
use serde_json::Value;
#[derive(Clone)]
pub struct Execs {
expect_stdout: Option<String>,
expect_stderr: Option<String>,
expect_exit_code: Option<i32>,
expect_stdout_contains: Vec<String>,
expect_stderr_contains: Vec<String>,
expect_either_contains: Vec<String>,
expect_stdout_contains_n: Vec<(String, usize)>,
expect_stdout_not_contains: Vec<String>,
expect_stderr_not_contains: Vec<String>,
expect_stderr_unordered: Vec<String>,
expect_neither_contains: Vec<String>,
expect_json: Option<Vec<Value>>,
}
impl Execs {
/// Verify that stdout is equal to the given lines.
/// See `lines_match` for supported patterns.
pub fn with_stdout<S: ToString>(mut self, expected: S) -> Execs {
self.expect_stdout = Some(expected.to_string());
self
}
/// Verify that stderr is equal to the given lines.
/// See `lines_match` for supported patterns.
pub fn with_stderr<S: ToString>(mut self, expected: S) -> Execs {
self._with_stderr(&expected);
self
}
fn _with_stderr(&mut self, expected: &dyn ToString) {
self.expect_stderr = Some(expected.to_string());
}
/// Verify the exit code from the process.
pub fn with_status(mut self, expected: i32) -> Execs {
self.expect_exit_code = Some(expected);
self
}
/// Verify that stdout contains the given contiguous lines somewhere in
/// its output.
/// See `lines_match` for supported patterns.
pub fn with_stdout_contains<S: ToString>(mut self, expected: S) -> Execs {
self.expect_stdout_contains.push(expected.to_string());
self
}
/// Verify that stderr contains the given contiguous lines somewhere in
/// its output.
/// See `lines_match` for supported patterns.
pub fn with_stderr_contains<S: ToString>(mut self, expected: S) -> Execs {
self.expect_stderr_contains.push(expected.to_string());
self
}
/// Verify that either stdout or stderr contains the given contiguous
/// lines somewhere in its output.
/// See `lines_match` for supported patterns.
pub fn with_either_contains<S: ToString>(mut self, expected: S) -> Execs {
self.expect_either_contains.push(expected.to_string());
self
}
/// Verify that stdout contains the given contiguous lines somewhere in
/// its output, and should be repeated `number` times.
/// See `lines_match` for supported patterns.
pub fn with_stdout_contains_n<S: ToString>(mut self, expected: S, number: usize) -> Execs {
self.expect_stdout_contains_n
.push((expected.to_string(), number));
self
}
/// Verify that stdout does not contain the given contiguous lines.
/// See `lines_match` for supported patterns.
/// See note on `with_stderr_does_not_contain`.
pub fn with_stdout_does_not_contain<S: ToString>(mut self, expected: S) -> Execs {
self.expect_stdout_not_contains.push(expected.to_string());
self
}
/// Verify that stderr does not contain the given contiguous lines.
/// See `lines_match` for supported patterns.
///
/// Care should be taken when using this method because there is a
/// limitless number of possible things that *won't* appear. A typo means
/// your test will pass without verifying the correct behavior. If
/// possible, write the test first so that it fails, and then implement
/// your fix/feature to make it pass.
pub fn with_stderr_does_not_contain<S: ToString>(mut self, expected: S) -> Execs {
self.expect_stderr_not_contains.push(expected.to_string());
self
}
/// Verify that all of the stderr output is equal to the given lines,
/// ignoring the order of the lines.
/// See `lines_match` for supported patterns.
/// This is useful when checking the output of `cargo build -v` since
/// the order of the output is not always deterministic.
/// Recommend use `with_stderr_contains` instead unless you really want to
/// check *every* line of output.
///
/// Be careful when using patterns such as `[..]`, because you may end up
/// with multiple lines that might match, and this is not smart enough to
/// do anything like longest-match. For example, avoid something like:
/// [RUNNING] `rustc [..]
/// [RUNNING] `rustc --crate-name foo [..]
/// This will randomly fail if the other crate name is `bar`, and the
/// order changes.
pub fn with_stderr_unordered<S: ToString>(mut self, expected: S) -> Execs {
self.expect_stderr_unordered.push(expected.to_string());
self
}
/// Verify the JSON output matches the given JSON.
/// Typically used when testing cargo commands that emit JSON.
/// Each separate JSON object should be separated by a blank line.
/// Example:
/// assert_that(
/// p.cargo("metadata"),
/// execs().with_json(r#"
/// {"example": "abc"}
/// {"example": "def"}
/// "#)
/// );
/// Objects should match in the order given.
/// The order of arrays is ignored.
/// Strings support patterns described in `lines_match`.
/// Use `{...}` to match any object.
pub fn with_json(mut self, expected: &str) -> Execs {
self.expect_json = Some(
expected
.split("\n\n")
.map(|obj| obj.parse().unwrap())
.collect(),
);
self
}
fn match_output(&self, actual: &Output) -> MatchResult {
self.match_status(actual)
.and(self.match_stdout(actual))
.and(self.match_stderr(actual))
}
fn match_status(&self, actual: &Output) -> MatchResult {
match self.expect_exit_code {
None => Ok(()),
Some(code) if actual.status.code() == Some(code) => Ok(()),
Some(_) => Err(format!(
"exited with {}\n--- stdout\n{}\n--- stderr\n{}",
actual.status,
String::from_utf8_lossy(&actual.stdout),
String::from_utf8_lossy(&actual.stderr)
)),
}
}
fn match_stdout(&self, actual: &Output) -> MatchResult {
self.match_std(
self.expect_stdout.as_ref(),
&actual.stdout,
"stdout",
&actual.stderr,
MatchKind::Exact,
)?;
for expect in self.expect_stdout_contains.iter() {
self.match_std(
Some(expect),
&actual.stdout,
"stdout",
&actual.stderr,
MatchKind::Partial,
)?;
}
for expect in self.expect_stderr_contains.iter() {
self.match_std(
Some(expect),
&actual.stderr,
"stderr",
&actual.stdout,
MatchKind::Partial,
)?;
}
for &(ref expect, number) in self.expect_stdout_contains_n.iter() {
self.match_std(
Some(expect),
&actual.stdout,
"stdout",
&actual.stderr,
MatchKind::PartialN(number),
)?;
}
for expect in self.expect_stdout_not_contains.iter() {
self.match_std(
Some(expect),
&actual.stdout,
"stdout",
&actual.stderr,
MatchKind::NotPresent,
)?;
}
for expect in self.expect_stderr_not_contains.iter() {
self.match_std(
Some(expect),
&actual.stderr,
"stderr",
&actual.stdout,
MatchKind::NotPresent,
)?;
}
for expect in self.expect_stderr_unordered.iter() {
self.match_std(
Some(expect),
&actual.stderr,
"stderr",
&actual.stdout,
MatchKind::Unordered,
)?;
}
for expect in self.expect_neither_contains.iter() {
self.match_std(
Some(expect),
&actual.stdout,
"stdout",
&actual.stdout,
MatchKind::NotPresent,
)?;
self.match_std(
Some(expect),
&actual.stderr,
"stderr",
&actual.stderr,
MatchKind::NotPresent,
)?;
}
for expect in self.expect_either_contains.iter() {
let match_std = self.match_std(
Some(expect),
&actual.stdout,
"stdout",
&actual.stdout,
MatchKind::Partial,
);
let match_err = self.match_std(
Some(expect),
&actual.stderr,
"stderr",
&actual.stderr,
MatchKind::Partial,
);
if let (Err(_), Err(_)) = (match_std, match_err) {
return Err(format!(
"expected to find:\n\
{}\n\n\
did not find in either output.",
expect
));
}
}
if let Some(ref objects) = self.expect_json {
let stdout = str::from_utf8(&actual.stdout)
.map_err(|_| "stdout was not utf8 encoded".to_owned())?;
let lines = stdout
.lines()
.filter(|line| line.starts_with('{'))
.collect::<Vec<_>>();
if lines.len() != objects.len() {
return Err(format!(
"expected {} json lines, got {}, stdout:\n{}",
objects.len(),
lines.len(),
stdout
));
}
for (obj, line) in objects.iter().zip(lines) {
self.match_json(obj, line)?;
}
}
Ok(())
}
fn match_stderr(&self, actual: &Output) -> MatchResult {
self.match_std(
self.expect_stderr.as_ref(),
&actual.stderr,
"stderr",
&actual.stdout,
MatchKind::Exact,
)
}
fn match_std(
&self,
expected: Option<&String>,
actual: &[u8],
description: &str,
extra: &[u8],
kind: MatchKind,
) -> MatchResult {
let out = match expected {
Some(out) => out,
None => return Ok(()),
};
let actual = match str::from_utf8(actual) {
Err(..) => return Err(format!("{} was not utf8 encoded", description)),
Ok(actual) => actual,
};
// Let's not deal with \r\n vs \n on windows...
let actual = actual.replace('\r', "");
let actual = actual.replace('\t', "<tab>");
match kind {
MatchKind::Exact => {
let a = actual.lines();
let e = out.lines();
let diffs = self.diff_lines(a, e, false);
if diffs.is_empty() {
Ok(())
} else {
Err(format!(
"differences:\n\
{}\n\n\
other output:\n\
`{}`",
diffs.join("\n"),
String::from_utf8_lossy(extra)
))
}
}
MatchKind::Partial => {
let mut a = actual.lines();
let e = out.lines();
let mut diffs = self.diff_lines(a.clone(), e.clone(), true);
#[allow(clippy::while_let_on_iterator)]
while let Some(..) = a.next() {
let a = self.diff_lines(a.clone(), e.clone(), true);
if a.len() < diffs.len() {
diffs = a;
}
}
if diffs.is_empty() {
Ok(())
} else {
Err(format!(
"expected to find:\n\
{}\n\n\
did not find in output:\n\
{}",
out, actual
))
}
}
MatchKind::PartialN(number) => {
let mut a = actual.lines();
let e = out.lines();
let mut matches = 0;
loop {
if self.diff_lines(a.clone(), e.clone(), true).is_empty() {
matches += 1;
}
if a.next().is_none() {
break;
}
}
if matches == number {
Ok(())
} else {
Err(format!(
"expected to find {} occurrences:\n\
{}\n\n\
did not find in output:\n\
{}",
number, out, actual
))
}
}
MatchKind::NotPresent => {
let mut a = actual.lines();
let e = out.lines();
let mut diffs = self.diff_lines(a.clone(), e.clone(), true);
#[allow(clippy::while_let_on_iterator)]
while let Some(..) = a.next() {
let a = self.diff_lines(a.clone(), e.clone(), true);
if a.len() < diffs.len() {
diffs = a;
}
}
if diffs.is_empty() {
Err(format!(
"expected not to find:\n\
{}\n\n\
but found in output:\n\
{}",
out, actual
))
} else {
Ok(())
}
}
MatchKind::Unordered => {
let mut a = actual.lines().collect::<Vec<_>>();
let e = out.lines();
for e_line in e {
match a.iter().position(|a_line| lines_match(e_line, a_line)) {
Some(index) => a.remove(index),
None => {
return Err(format!(
"Did not find expected line:\n\
{}\n\
Remaining available output:\n\
{}\n",
e_line,
a.join("\n")
));
}
};
}
if !a.is_empty() {
Err(format!(
"Output included extra lines:\n\
{}\n",
a.join("\n")
))
} else {
Ok(())
}
}
}
}
fn match_json(&self, expected: &Value, line: &str) -> MatchResult {
let actual = match line.parse() {
Err(e) => return Err(format!("invalid json, {}:\n`{}`", e, line)),
Ok(actual) => actual,
};
match find_mismatch(expected, &actual) {
Some((expected_part, actual_part)) => Err(format!(
"JSON mismatch\nExpected:\n{}\nWas:\n{}\nExpected part:\n{}\nActual part:\n{}\n",
serde_json::to_string_pretty(expected).unwrap(),
serde_json::to_string_pretty(&actual).unwrap(),
serde_json::to_string_pretty(expected_part).unwrap(),
serde_json::to_string_pretty(actual_part).unwrap(),
)),
None => Ok(()),
}
}
fn diff_lines<'a>(
&self,
actual: str::Lines<'a>,
expected: str::Lines<'a>,
partial: bool,
) -> Vec<String> {
let actual = actual.take(if partial {
expected.clone().count()
} else {
usize::MAX
});
zip_all(actual, expected)
.enumerate()
.filter_map(|(i, (a, e))| match (a, e) {
(Some(a), Some(e)) => {
if lines_match(e, a) {
None
} else {
Some(format!("{:3} - |{}|\n + |{}|\n", i, e, a))
}
}
(Some(a), None) => Some(format!("{:3} -\n + |{}|\n", i, a)),
(None, Some(e)) => Some(format!("{:3} - |{}|\n +\n", i, e)),
(None, None) => panic!("Cannot get here"),
})
.collect()
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum MatchKind {
Exact,
Partial,
PartialN(usize),
NotPresent,
Unordered,
}
/// Compare a line with an expected pattern.
/// - Use `[..]` as a wildcard to match 0 or more characters on the same line
/// (similar to `.*` in a regex).
/// - Use `[EXE]` to optionally add `.exe` on Windows (empty string on other
/// platforms).
/// - There is a wide range of macros (such as `[COMPILING]` or `[WARNING]`)
/// to match cargo's "status" output and allows you to ignore the alignment.
/// See `substitute_macros` for a complete list of macros.
pub fn lines_match(expected: &str, actual: &str) -> bool {
// Let's not deal with / vs \ (windows...)
let expected = expected.replace('\\', "/");
let mut actual: &str = &actual.replace('\\', "/");
let expected = substitute_macros(&expected);
for (i, part) in expected.split("[..]").enumerate() {
match actual.find(part) {
Some(j) => {
if i == 0 && j != 0 {
return false;
}
actual = &actual[j + part.len()..];
}
None => return false,
}
}
actual.is_empty() || expected.ends_with("[..]")
}
#[test]
fn lines_match_works() {
assert!(lines_match("a b", "a b"));
assert!(lines_match("a[..]b", "a b"));
assert!(lines_match("a[..]", "a b"));
assert!(lines_match("[..]", "a b"));
assert!(lines_match("[..]b", "a b"));
assert!(!lines_match("[..]b", "c"));
assert!(!lines_match("b", "c"));
assert!(!lines_match("b", "cb"));
}
// Compares JSON object for approximate equality.
// You can use `[..]` wildcard in strings (useful for OS dependent things such
// as paths). You can use a `"{...}"` string literal as a wildcard for
// arbitrary nested JSON (useful for parts of object emitted by other programs
// (e.g. rustc) rather than Cargo itself). Arrays are sorted before comparison.
fn find_mismatch<'a>(expected: &'a Value, actual: &'a Value) -> Option<(&'a Value, &'a Value)> {
use serde_json::Value::*;
match (expected, actual) {
(Number(l), Number(r)) if l == r => None,
(Bool(l), Bool(r)) if l == r => None,
(String(l), String(r)) if lines_match(l, r) => None,
(Array(l), Array(r)) => {
if l.len() != r.len() {
return Some((expected, actual));
}
let mut l = l.iter().collect::<Vec<_>>();
let mut r = r.iter().collect::<Vec<_>>();
l.retain(
|l| match r.iter().position(|r| find_mismatch(l, r).is_none()) {
Some(i) => {
r.remove(i);
false
}
None => true,
},
);
if !l.is_empty() {
assert!(!r.is_empty());
Some((l[0], r[0]))
} else {
assert_eq!(r.len(), 0);
None
}
}
(Object(l), Object(r)) => {
let same_keys = l.len() == r.len() && l.keys().all(|k| r.contains_key(k));
if !same_keys {
return Some((expected, actual));
}
l.values()
.zip(r.values())
.find_map(|(l, r)| find_mismatch(l, r))
}
(Null, Null) => None,
// magic string literal "{...}" acts as wildcard for any sub-JSON
(String(l), _) if l == "{...}" => None,
_ => Some((expected, actual)),
}
}
struct ZipAll<I1: Iterator, I2: Iterator> {
first: I1,
second: I2,
}
impl<T, I1: Iterator<Item = T>, I2: Iterator<Item = T>> Iterator for ZipAll<I1, I2> {
type Item = (Option<T>, Option<T>);
fn next(&mut self) -> Option<(Option<T>, Option<T>)> {
let first = self.first.next();
let second = self.second.next();
match (first, second) {
(None, None) => None,
(a, b) => Some((a, b)),
}
}
}
fn zip_all<T, I1: Iterator<Item = T>, I2: Iterator<Item = T>>(a: I1, b: I2) -> ZipAll<I1, I2> {
ZipAll {
first: a,
second: b,
}
}
impl fmt::Display for Execs {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "execs")
}
}
impl fmt::Debug for Execs {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "execs")
}
}
impl Matcher<ProcessBuilder> for Execs {
fn matches(&self, mut process: ProcessBuilder) -> MatchResult {
self.matches(&mut process)
}
}
impl<'a> Matcher<&'a mut ProcessBuilder> for Execs {
fn matches(&self, process: &'a mut ProcessBuilder) -> MatchResult {
println!("running {}", process);
let res = process.exec_with_output();
match res {
Ok(out) => self.match_output(&out),
Err(err) => {
if let Some(out) = &err.output {
return self.match_output(out);
}
Err(format!("could not exec process {}: {}", process, err))
}
}
}
}
impl Matcher<Output> for Execs {
fn matches(&self, output: Output) -> MatchResult {
self.match_output(&output)
}
}
pub fn execs() -> Execs {
Execs {
expect_stdout: None,
expect_stderr: None,
expect_exit_code: Some(0),
expect_stdout_contains: Vec::new(),
expect_stderr_contains: Vec::new(),
expect_either_contains: Vec::new(),
expect_stdout_contains_n: Vec::new(),
expect_stdout_not_contains: Vec::new(),
expect_stderr_not_contains: Vec::new(),
expect_stderr_unordered: Vec::new(),
expect_neither_contains: Vec::new(),
expect_json: None,
}
}
fn substitute_macros(input: &str) -> String {
let macros = [
("[RUNNING]", " Running"),
("[COMPILING]", " Compiling"),
("[CHECKING]", " Checking"),
("[CREATED]", " Created"),
("[FINISHED]", " Finished"),
("[ERROR]", "error:"),
("[WARNING]", "warning:"),
("[DOCUMENTING]", " Documenting"),
("[FRESH]", " Fresh"),
("[UPDATING]", " Updating"),
("[ADDING]", " Adding"),
("[REMOVING]", " Removing"),
("[DOCTEST]", " Doc-tests"),
("[PACKAGING]", " Packaging"),
("[DOWNLOADING]", " Downloading"),
("[UPLOADING]", " Uploading"),
("[VERIFYING]", " Verifying"),
("[ARCHIVING]", " Archiving"),
("[INSTALLING]", " Installing"),
("[REPLACING]", " Replacing"),
("[UNPACKING]", " Unpacking"),
("[SUMMARY]", " Summary"),
("[FIXING]", " Fixing"),
("[EXE]", if cfg!(windows) { ".exe" } else { "" }),
];
let mut result = input.to_owned();
for &(pat, subst) in ¯os {
result = result.replace(pat, subst)
}
result
}