|
| 1 | +use std::ffi; |
1 | 2 | use std::fmt; |
2 | 3 | use std::result; |
3 | 4 |
|
4 | 5 | use failure; |
5 | 6 |
|
6 | 7 | pub type Result<T> = result::Result<T, failure::Error>; |
7 | 8 |
|
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, |
| 9 | +fn format_cmd(cmd: &[ffi::OsString]) -> String { |
| 10 | + let result: Vec<String> = cmd.iter() |
| 11 | + .map(|s| s.to_string_lossy().into_owned()) |
| 12 | + .collect(); |
| 13 | + result.join(" ") |
14 | 14 | } |
15 | 15 |
|
16 | | -#[derive(Debug)] |
17 | | -pub struct AssertionError { |
18 | | - inner: failure::Context<AssertionKind>, |
| 16 | +#[derive(Fail, Debug)] |
| 17 | +pub struct SpawnError { |
| 18 | + cmd: Vec<ffi::OsString>, |
19 | 19 | } |
20 | 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() |
| 21 | +impl SpawnError { |
| 22 | + pub fn new(cmd: Vec<ffi::OsString>) -> Self { |
| 23 | + Self { cmd } |
38 | 24 | } |
39 | 25 | } |
40 | 26 |
|
41 | | -impl fmt::Display for AssertionError { |
| 27 | +impl fmt::Display for SpawnError { |
42 | 28 | 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), |
51 | | - } |
| 29 | + write!(f, "Failed to run `{}`", format_cmd(&self.cmd)) |
52 | 30 | } |
53 | 31 | } |
54 | 32 |
|
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, |
| 33 | +#[derive(Fail, Debug)] |
| 34 | +pub struct AssertionError { |
| 35 | + cmd: Vec<ffi::OsString>, |
68 | 36 | } |
69 | 37 |
|
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 |
| 38 | +impl AssertionError { |
| 39 | + pub fn new(cmd: Vec<ffi::OsString>) -> Self { |
| 40 | + Self { cmd } |
84 | 41 | } |
85 | 42 | } |
86 | 43 |
|
87 | | -impl<D> fmt::Display for KeyValueDisplay<D> |
88 | | -where |
89 | | - D: fmt::Display + Send + Sync + 'static, |
90 | | -{ |
| 44 | +impl fmt::Display for AssertionError { |
91 | 45 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
92 | | - write!(f, "{}={}", self.key, self.context) |
| 46 | + write!(f, "Assertion failed for `{}`", format_cmd(&self.cmd)) |
93 | 47 | } |
94 | 48 | } |
95 | 49 |
|
96 | | -#[derive(Debug)] |
97 | | -pub struct DebugDisplay<D> |
98 | | -where |
99 | | - D: fmt::Debug + Send + Sync + 'static, |
100 | | -{ |
101 | | - context: D, |
| 50 | +#[derive(Fail, Debug)] |
| 51 | +pub struct StatusError { |
| 52 | + unexpected: bool, |
| 53 | + stdout: Vec<u8>, |
| 54 | + stderr: Vec<u8>, |
102 | 55 | } |
103 | 56 |
|
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 |
| 57 | +impl StatusError { |
| 58 | + pub fn new(unexpected: bool, stdout: Vec<u8>, stderr: Vec<u8>) -> Self { |
| 59 | + Self { |
| 60 | + unexpected, |
| 61 | + stdout, |
| 62 | + stderr, |
| 63 | + } |
114 | 64 | } |
115 | 65 | } |
116 | 66 |
|
117 | | -impl<D> fmt::Display for DebugDisplay<D> |
118 | | -where |
119 | | - D: fmt::Debug + Send + Sync + 'static, |
120 | | -{ |
| 67 | +impl fmt::Display for StatusError { |
121 | 68 | 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 |
| 69 | + let out = String::from_utf8_lossy(&self.stdout); |
| 70 | + let err = String::from_utf8_lossy(&self.stderr); |
| 71 | + writeln!( |
| 72 | + f, |
| 73 | + "Unexpected {} return status", |
| 74 | + if self.unexpected { |
| 75 | + "success" |
| 76 | + } else { |
| 77 | + "failure" |
| 78 | + } |
| 79 | + )?; |
| 80 | + writeln!(f, "stdout=```{}```", out)?; |
| 81 | + write!(f, "stderr=```{}```", err) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[derive(Fail, Debug)] |
| 86 | +pub struct ExitCodeError { |
| 87 | + expected: Option<i32>, |
| 88 | + got: Option<i32>, |
| 89 | + stdout: Vec<u8>, |
| 90 | + stderr: Vec<u8>, |
| 91 | +} |
| 92 | + |
| 93 | +impl ExitCodeError { |
| 94 | + pub fn new(expected: Option<i32>, got: Option<i32>, stdout: Vec<u8>, stderr: Vec<u8>) -> Self { |
| 95 | + Self { |
| 96 | + expected, |
| 97 | + got, |
| 98 | + stdout, |
| 99 | + stderr, |
| 100 | + } |
144 | 101 | } |
145 | 102 | } |
146 | 103 |
|
147 | | -impl<D> fmt::Display for QuotedDisplay<D> |
148 | | -where |
149 | | - D: fmt::Display + Send + Sync + 'static, |
150 | | -{ |
| 104 | +impl fmt::Display for ExitCodeError { |
151 | 105 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
152 | | - write!(f, "```{}```", self.context) |
| 106 | + let out = String::from_utf8_lossy(&self.stdout); |
| 107 | + let err = String::from_utf8_lossy(&self.stderr); |
| 108 | + writeln!(f, "expected={:?}", self.expected)?; |
| 109 | + writeln!(f, "got={:?}", self.got)?; |
| 110 | + writeln!(f, "stdout=```{}```", out)?; |
| 111 | + write!(f, "stderr=```{}```", err) |
153 | 112 | } |
154 | 113 | } |
0 commit comments