Skip to content

Commit 7e2c81d

Browse files
committed
Add a test
Includes adding facility for checking that output does not contain a string.
1 parent 02625ba commit 7e2c81d

File tree

2 files changed

+97
-29
lines changed

2 files changed

+97
-29
lines changed

tests/cargotest/support/mod.rs

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ pub struct Execs {
263263
expect_exit_code: Option<i32>,
264264
expect_stdout_contains: Vec<String>,
265265
expect_stderr_contains: Vec<String>,
266+
expect_stdout_not_contains: Vec<String>,
267+
expect_stderr_not_contains: Vec<String>,
266268
expect_json: Option<Vec<Json>>,
267269
}
268270

@@ -292,6 +294,16 @@ impl Execs {
292294
self
293295
}
294296

297+
pub fn with_stdout_does_not_contain<S: ToString>(mut self, expected: S) -> Execs {
298+
self.expect_stdout_not_contains.push(expected.to_string());
299+
self
300+
}
301+
302+
pub fn with_stderr_does_not_contain<S: ToString>(mut self, expected: S) -> Execs {
303+
self.expect_stderr_not_contains.push(expected.to_string());
304+
self
305+
}
306+
295307
pub fn with_json(mut self, expected: &str) -> Execs {
296308
self.expect_json = Some(expected.split("\n\n").map(|obj| {
297309
Json::from_str(obj).unwrap()
@@ -321,14 +333,22 @@ impl Execs {
321333

322334
fn match_stdout(&self, actual: &Output) -> ham::MatchResult {
323335
self.match_std(self.expect_stdout.as_ref(), &actual.stdout,
324-
"stdout", &actual.stderr, false)?;
336+
"stdout", &actual.stderr, MatchKind::Exact)?;
325337
for expect in self.expect_stdout_contains.iter() {
326338
self.match_std(Some(expect), &actual.stdout, "stdout",
327-
&actual.stderr, true)?;
339+
&actual.stderr, MatchKind::Partial)?;
328340
}
329341
for expect in self.expect_stderr_contains.iter() {
330342
self.match_std(Some(expect), &actual.stderr, "stderr",
331-
&actual.stdout, true)?;
343+
&actual.stdout, MatchKind::Partial)?;
344+
}
345+
for expect in self.expect_stdout_not_contains.iter() {
346+
self.match_std(Some(expect), &actual.stdout, "stdout",
347+
&actual.stderr, MatchKind::NotPresent)?;
348+
}
349+
for expect in self.expect_stderr_not_contains.iter() {
350+
self.match_std(Some(expect), &actual.stderr, "stderr",
351+
&actual.stdout, MatchKind::NotPresent)?;
332352
}
333353

334354
if let Some(ref objects) = self.expect_json {
@@ -349,12 +369,12 @@ impl Execs {
349369

350370
fn match_stderr(&self, actual: &Output) -> ham::MatchResult {
351371
self.match_std(self.expect_stderr.as_ref(), &actual.stderr,
352-
"stderr", &actual.stdout, false)
372+
"stderr", &actual.stdout, MatchKind::Exact)
353373
}
354374

355375
fn match_std(&self, expected: Option<&String>, actual: &[u8],
356376
description: &str, extra: &[u8],
357-
partial: bool) -> ham::MatchResult {
377+
kind: MatchKind) -> ham::MatchResult {
358378
let out = match expected {
359379
Some(out) => out,
360380
None => return ham::success(),
@@ -368,33 +388,46 @@ impl Execs {
368388
let actual = actual.replace("\r", "");
369389
let actual = actual.replace("\t", "<tab>");
370390

371-
let mut a = actual.lines();
372-
let e = out.lines();
373-
374-
if partial {
375-
let mut diffs = self.diff_lines(a.clone(), e.clone(), partial);
376-
while let Some(..) = a.next() {
377-
let a = self.diff_lines(a.clone(), e.clone(), partial);
378-
if a.len() < diffs.len() {
379-
diffs = a;
391+
match kind {
392+
MatchKind::Exact => {
393+
let a = actual.lines();
394+
let e = out.lines();
395+
396+
let diffs = self.diff_lines(a, e, false);
397+
ham::expect(diffs.is_empty(),
398+
format!("differences:\n\
399+
{}\n\n\
400+
other output:\n\
401+
`{}`", diffs.join("\n"),
402+
String::from_utf8_lossy(extra)))
403+
}
404+
MatchKind::Partial => {
405+
let mut a = actual.lines();
406+
let e = out.lines();
407+
408+
let mut diffs = self.diff_lines(a.clone(), e.clone(), true);
409+
while let Some(..) = a.next() {
410+
let a = self.diff_lines(a.clone(), e.clone(), true);
411+
if a.len() < diffs.len() {
412+
diffs = a;
413+
}
380414
}
415+
ham::expect(diffs.is_empty(),
416+
format!("expected to find:\n\
417+
{}\n\n\
418+
did not find in output:\n\
419+
{}", out,
420+
actual))
421+
}
422+
MatchKind::NotPresent => {
423+
ham::expect(!actual.contains(out),
424+
format!("expected not to find:\n\
425+
{}\n\n\
426+
but found in output:\n\
427+
{}", out,
428+
actual))
381429
}
382-
ham::expect(diffs.is_empty(),
383-
format!("expected to find:\n\
384-
{}\n\n\
385-
did not find in output:\n\
386-
{}", out,
387-
actual))
388-
} else {
389-
let diffs = self.diff_lines(a, e, partial);
390-
ham::expect(diffs.is_empty(),
391-
format!("differences:\n\
392-
{}\n\n\
393-
other output:\n\
394-
`{}`", diffs.join("\n"),
395-
String::from_utf8_lossy(extra)))
396430
}
397-
398431
}
399432

400433
fn match_json(&self, expected: &Json, line: &str) -> ham::MatchResult {
@@ -441,6 +474,13 @@ impl Execs {
441474
}
442475
}
443476

477+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
478+
enum MatchKind {
479+
Exact,
480+
Partial,
481+
NotPresent,
482+
}
483+
444484
pub fn lines_match(expected: &str, mut actual: &str) -> bool {
445485
let expected = substitute_macros(expected);
446486
for (i, part) in expected.split("[..]").enumerate() {
@@ -589,6 +629,8 @@ pub fn execs() -> Execs {
589629
expect_exit_code: None,
590630
expect_stdout_contains: Vec::new(),
591631
expect_stderr_contains: Vec::new(),
632+
expect_stdout_not_contains: Vec::new(),
633+
expect_stderr_not_contains: Vec::new(),
592634
expect_json: None,
593635
}
594636
}

tests/check.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,29 @@ fn build_check() {
212212
assert_that(foo.cargo_process("check"),
213213
execs().with_status(0));
214214
}
215+
216+
// Checks that where a project has both a lib and a bin, the lib is only checked
217+
// not built.
218+
#[test]
219+
fn issue_3418() {
220+
if !is_nightly() {
221+
return;
222+
}
223+
224+
let foo = project("foo")
225+
.file("Cargo.toml", r#"
226+
[package]
227+
name = "foo"
228+
version = "0.1.0"
229+
authors = []
230+
231+
[dependencies]
232+
"#)
233+
.file("src/lib.rs", "")
234+
.file("src/main.rs", "fn main() {}");
235+
foo.build();
236+
237+
assert_that(foo.cargo_process("check").arg("-v"),
238+
execs().with_status(0)
239+
.with_stderr_does_not_contain("--crate-type lib"));
240+
}

0 commit comments

Comments
 (0)