|
1 | | -use output; |
2 | | -use std::ffi::OsString; |
3 | | - |
4 | | -const ERROR_PREFIX: &'static str = "CLI assertion failed"; |
5 | | - |
6 | | -fn format_cmd(cmd: &[OsString]) -> String { |
7 | | - let result: Vec<String> = cmd.iter() |
8 | | - .map(|s| s.to_string_lossy().into_owned()) |
9 | | - .collect(); |
10 | | - result.join(" ") |
11 | | -} |
12 | | - |
13 | | -error_chain! { |
14 | | - links { |
15 | | - Output(output::Error, output::ErrorKind); |
16 | | - } |
17 | | - foreign_links { |
18 | | - Io(::std::io::Error); |
19 | | - Fmt(::std::fmt::Error); |
20 | | - } |
21 | | - errors { |
22 | | - SpawnFailed(cmd: Vec<OsString>) { |
23 | | - description("Spawn failed") |
24 | | - display( |
25 | | - "{}: (command `{}` failed to run)", |
26 | | - ERROR_PREFIX, |
27 | | - format_cmd(cmd), |
28 | | - ) |
29 | | - } |
30 | | - AssertionFailed(cmd: Vec<OsString>) { |
31 | | - description("Assertion failed") |
32 | | - display( |
33 | | - "{}: (command `{}` failed)", |
34 | | - ERROR_PREFIX, |
35 | | - format_cmd(cmd), |
36 | | - ) |
37 | | - } |
38 | | - StatusMismatch(expected: bool, out: String, err: String) { |
39 | | - description("Wrong status") |
40 | | - display( |
41 | | - "Expected to {}\nstatus={}\nstdout=```{}```\nstderr=```{}```", |
42 | | - expected = if *expected { "succeed" } else { "fail" }, |
43 | | - got = if *expected { "failed" } else { "succeeded" }, |
44 | | - out = out, |
45 | | - err = err, |
46 | | - ) |
47 | | - } |
48 | | - ExitCodeMismatch( |
49 | | - expected: Option<i32>, |
50 | | - got: Option<i32>, |
51 | | - out: String, |
52 | | - err: String |
53 | | - ) { |
54 | | - description("Wrong exit code") |
55 | | - display( |
56 | | - "Expected exit code to be `{expected:?}`)\n\ |
57 | | - exit code=`{code:?}`\n\ |
58 | | - stdout=```{stdout}```\n\ |
59 | | - stderr=```{stderr}```", |
60 | | - expected=expected, |
61 | | - code=got, |
62 | | - stdout=out, |
63 | | - stderr=err, |
64 | | - ) |
| 1 | +use std::fmt; |
| 2 | +use std::result; |
| 3 | + |
| 4 | +use failure; |
| 5 | + |
| 6 | +pub type Result<T> = result::Result<T, failure::Error>; |
| 7 | + |
| 8 | +#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)] |
| 9 | +pub enum AssertionKind { |
| 10 | + #[fail(display = "Spawn failed.")] Spawn, |
| 11 | + #[fail(display = "Status mismatch.")] StatusMismatch, |
| 12 | + #[fail(display = "Exit code mismatch.")] ExitCodeMismatch, |
| 13 | + #[fail(display = "Output mismatch.")] OutputMismatch, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug)] |
| 17 | +pub struct AssertionError { |
| 18 | + inner: failure::Context<AssertionKind>, |
| 19 | +} |
| 20 | + |
| 21 | +impl AssertionError { |
| 22 | + pub fn new(kind: AssertionKind) -> Self { |
| 23 | + Self { inner: kind.into() } |
| 24 | + } |
| 25 | + |
| 26 | + pub fn kind(&self) -> AssertionKind { |
| 27 | + *self.inner.get_context() |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl failure::Fail for AssertionError { |
| 32 | + fn cause(&self) -> Option<&failure::Fail> { |
| 33 | + self.inner.cause() |
| 34 | + } |
| 35 | + |
| 36 | + fn backtrace(&self) -> Option<&failure::Backtrace> { |
| 37 | + self.inner.backtrace() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl fmt::Display for AssertionError { |
| 42 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 43 | + writeln!(f, "CLI Assertion Error: {}", self.inner) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl From<AssertionKind> for AssertionError { |
| 48 | + fn from(kind: AssertionKind) -> AssertionError { |
| 49 | + AssertionError { |
| 50 | + inner: failure::Context::new(kind), |
65 | 51 | } |
66 | 52 | } |
67 | 53 | } |
| 54 | + |
| 55 | +impl From<failure::Context<AssertionKind>> for AssertionError { |
| 56 | + fn from(inner: failure::Context<AssertionKind>) -> AssertionError { |
| 57 | + AssertionError { inner: inner } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Debug)] |
| 62 | +pub struct KeyValueDisplay<D> |
| 63 | +where |
| 64 | + D: fmt::Display + Send + Sync + 'static, |
| 65 | +{ |
| 66 | + key: &'static str, |
| 67 | + context: D, |
| 68 | +} |
| 69 | + |
| 70 | +impl<D> KeyValueDisplay<D> |
| 71 | +where |
| 72 | + D: fmt::Display + Send + Sync + 'static, |
| 73 | +{ |
| 74 | + pub fn new(key: &'static str, context: D) -> Self { |
| 75 | + Self { key, context } |
| 76 | + } |
| 77 | + |
| 78 | + pub fn key(&self) -> &str { |
| 79 | + self.key |
| 80 | + } |
| 81 | + |
| 82 | + pub fn context(&self) -> &D { |
| 83 | + &self.context |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl<D> fmt::Display for KeyValueDisplay<D> |
| 88 | +where |
| 89 | + D: fmt::Display + Send + Sync + 'static, |
| 90 | +{ |
| 91 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 92 | + write!(f, "{}={}", self.key, self.context) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[derive(Debug)] |
| 97 | +pub struct DebugDisplay<D> |
| 98 | +where |
| 99 | + D: fmt::Debug + Send + Sync + 'static, |
| 100 | +{ |
| 101 | + context: D, |
| 102 | +} |
| 103 | + |
| 104 | +impl<D> DebugDisplay<D> |
| 105 | +where |
| 106 | + D: fmt::Debug + Send + Sync + 'static, |
| 107 | +{ |
| 108 | + pub fn new(context: D) -> Self { |
| 109 | + Self { context } |
| 110 | + } |
| 111 | + |
| 112 | + pub fn context(&self) -> &D { |
| 113 | + &self.context |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl<D> fmt::Display for DebugDisplay<D> |
| 118 | +where |
| 119 | + D: fmt::Debug + Send + Sync + 'static, |
| 120 | +{ |
| 121 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 122 | + write!(f, "{:?}", self.context) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +#[derive(Debug)] |
| 127 | +pub struct QuotedDisplay<D> |
| 128 | +where |
| 129 | + D: fmt::Display + Send + Sync + 'static, |
| 130 | +{ |
| 131 | + context: D, |
| 132 | +} |
| 133 | + |
| 134 | +impl<D> QuotedDisplay<D> |
| 135 | +where |
| 136 | + D: fmt::Display + Send + Sync + 'static, |
| 137 | +{ |
| 138 | + pub fn new(context: D) -> Self { |
| 139 | + Self { context } |
| 140 | + } |
| 141 | + |
| 142 | + pub fn context(&self) -> &D { |
| 143 | + &self.context |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +impl<D> fmt::Display for QuotedDisplay<D> |
| 148 | +where |
| 149 | + D: fmt::Display + Send + Sync + 'static, |
| 150 | +{ |
| 151 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 152 | + write!(f, "```{}```", self.context) |
| 153 | + } |
| 154 | +} |
0 commit comments