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
//! [`Error`], the one error every icedwright call returns, and the harness's own errors.
use std::fmt;
use iced_core::window;
use icedwright_types::VirtualDuration;
use crate::effects::{TaskState, UntypedTaskId};
/// What every harness call returns.
pub type Result<T = (), E = Error> = std::result::Result<T, E>;
/// Every way an icedwright call can fail. Its `Debug` output is the full report, because
/// libtest prints a returned error with `{:?}`.
#[derive(thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// The harness itself.
#[error(transparent)]
Harness(#[from] HarnessError),
/// A configuration or environment value.
#[error(transparent)]
Config(#[from] ConfigError),
/// An assertion or actionability check.
#[error(transparent)]
Check(#[from] icedwright_check::CheckError),
/// An action that was not sent, or did not take.
#[error(transparent)]
Action(#[from] crate::actions::ActionError),
/// An expectation that failed, with its full report.
#[error(transparent)]
Expect(#[from] crate::expect::ExpectError),
/// A locator used directly, such as a selector string that did not parse.
#[error(transparent)]
Locator(#[from] icedwright_locator::Error),
/// The semantic tree's YAML or paths.
#[error(transparent)]
Semantics(#[from] icedwright_semantics::Error),
/// Writing or reading a trace.
#[error(transparent)]
Trace(#[from] icedwright_trace::TraceError),
/// The remote protocol.
#[error(transparent)]
Protocol(#[from] icedwright_protocol::ProtocolError),
/// A frame whose bytes do not match its size.
#[error(transparent)]
Frame(#[from] icedwright_types::FrameError),
/// A scale factor out of range.
#[error(transparent)]
ScaleFactor(#[from] icedwright_types::InvalidScaleFactor),
/// A ratio out of range.
#[error(transparent)]
Ratio(#[from] icedwright_types::InvalidRatio),
/// A key chord that did not parse.
#[error(transparent)]
Chord(#[from] icedwright_types::ChordError),
/// A role name that did not parse.
#[error(transparent)]
Role(#[from] icedwright_types::ParseRoleError),
/// A role that did not match the one asked for.
#[error(transparent)]
RoleMismatch(#[from] icedwright_types::RoleMismatch),
/// A state name that did not parse.
#[error(transparent)]
State(#[from] icedwright_types::ParseStateError),
/// The file system.
#[error(transparent)]
Io(#[from] std::io::Error),
/// JSON.
#[error(transparent)]
Json(#[from] serde_json::Error),
/// A semantic or visual snapshot, or the fonts they depend on.
#[error(transparent)]
Snapshot(#[from] crate::snapshot::SnapshotError),
/// Anything else, from [`Error::other`].
#[error("{0}")]
Other(#[source] Box<dyn std::error::Error + Send + Sync>),
}
impl Error {
/// Wraps any error, such as an app's own, keeping it as the source.
pub fn other(error: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::Other(Box::new(error))
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")?;
let mut source = std::error::Error::source(self);
if matches!(self, Error::Other(_)) {
source = source.and_then(std::error::Error::source);
}
while let Some(cause) = source {
write!(f, "\n caused by: {cause}")?;
source = cause.source();
}
Ok(())
}
}
/// Why the harness could not take a step.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum HarnessError {
/// A harness was built inside another tokio runtime.
#[error(
"icedwright runs its own runtime; use #[test] or #[icedwright::test], not #[tokio::test]"
)]
InsideRuntime,
/// The app asked to exit, so no later step can run.
#[error("the app has exited")]
Exited,
/// No window with this id is open.
#[error("no window {0:?}")]
NoSuchWindow(window::Id),
/// A user cannot resize this window: its settings say it is not resizable.
#[error("window {0:?} is not resizable; use force_size for a tiling window manager")]
NotResizable(window::Id),
/// The step needs a window and none is open.
#[error("no window is open")]
NoWindow,
/// The headless renderer could not be created.
#[error("the tiny-skia renderer could not be created for a window")]
Renderer,
/// The app kept doing work at one instant without settling.
#[error("the app never settled after {turns} turns; busiest tasks: {busiest:?}")]
#[non_exhaustive]
NeverIdle {
/// The turn bound that was reached.
turns: u32,
/// The tasks polled most, busiest first.
busiest: Vec<UntypedTaskId>,
},
/// Nothing made progress for the whole hang window.
#[error("the process hung: no progress for {since_progress:?} during {step}. {cause}")]
#[non_exhaustive]
Hung {
/// The step that was running.
step: String,
/// How long nothing had progressed, in real time.
since_progress: std::time::Duration,
/// What the harness was waiting for.
cause: HangCause,
},
/// A task panicked while the harness polled it.
#[error("task {task} panicked: {message}")]
#[non_exhaustive]
TaskPanicked {
/// The task.
task: UntypedTaskId,
/// The panic message.
message: String,
},
/// iced was built with a time backend other than tokio.
#[error(
"iced's time backend is {executor}, not tokio; add `iced = {{ version = \"0.14\", features = [\"tokio\"] }}` to [dev-dependencies]"
)]
#[non_exhaustive]
NonTokioTimeBackend {
/// The backend's type name.
executor: &'static str,
},
/// A component declared a timer on the real clock.
#[error("{component} runs on real time, so its ticks would depend on the machine")]
#[non_exhaustive]
RealTimer {
/// The component.
component: &'static str,
},
/// A runtime action that has no headless meaning.
#[error("{0} is not supported headlessly")]
UnsupportedAction(&'static str),
/// A named task started that a `deny_unrouted` rule covers, with no route or hold.
#[error("task {task} ({name}) started at {location} with no route or hold")]
#[non_exhaustive]
Unrouted {
/// The task.
task: UntypedTaskId,
/// Its name.
name: &'static str,
/// Where it was named.
location: String,
},
/// The task cannot be acted on any more.
#[error("task {task} is {state:?}")]
#[non_exhaustive]
TaskGone {
/// The task.
task: UntypedTaskId,
/// Its state.
state: TaskState,
},
/// No task of the name was held before the timeout.
#[error("no held {name} task within {timeout}; tasks that did start: {started:?}")]
#[non_exhaustive]
NoTask {
/// The name waited for.
name: &'static str,
/// The virtual timeout.
timeout: VirtualDuration,
/// The names of named tasks that did start, newest first.
started: Vec<&'static str>,
},
/// A fake was called with nothing to answer.
#[error("fake {fake} had no answer for {request}")]
#[non_exhaustive]
Unanswered {
/// The fake's name.
fake: &'static str,
/// The request, printed.
request: String,
},
/// No task with this id was started by this harness.
#[error("no task {0}")]
UnknownTask(UntypedTaskId),
/// A route or resolution made for another name reached a task.
#[error("task {task} was answered with a value made for {name}")]
#[non_exhaustive]
RejectedPayload {
/// The task.
task: UntypedTaskId,
/// The name the value was made for.
name: &'static str,
},
/// Held tasks were still pending when the test finished.
#[error("held tasks were still pending at the end of the test: {0:?}")]
HeldAtEnd(Vec<UntypedTaskId>),
}
/// What a hung step was waiting for.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum HangCause {
/// A call into app code.
AppCall {
/// The call.
what: &'static str,
},
/// Blocking work on the harness's one blocking thread.
BlockingWork {
/// The task waiting for it, when known.
job: Option<UntypedTaskId>,
},
/// Declared external sources, during a wait with a fixed real-time ceiling.
ExternalSources {
/// The declared sources.
sources: Vec<String>,
},
/// Studio's headed socket, which stayed open but stopped taking or acknowledging steps.
HeadedStudio,
}
impl fmt::Display for HangCause {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
HangCause::AppCall { what } => write!(f, "The app call {what} did not return."),
HangCause::BlockingWork { .. } => f.write_str(
"A spawn_blocking job never finished. The harness runs blocking work on one \
thread, so a long-lived worker holds it: run the worker on the app's own \
thread and declare it as an external source, or put it behind the app's own \
trait and fake it in tests.",
),
HangCause::ExternalSources { sources } => write!(
f,
"The wait reached its fixed real-time ceiling while waiting on the declared \
external sources {sources:?}."
),
HangCause::HeadedStudio => f.write_str(
"Studio's headed socket stayed open but neither took the step nor acknowledged \
it. Whatever listens on the socket has stopped answering: restart \
`icedwright-studio headed`, or run without ICEDWRIGHT_HEADED.",
),
}
}
}
/// A configuration value that cannot be used.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ConfigError {
/// An environment variable whose value does not parse.
#[error("{var}={value:?} is not valid; expected {expected}")]
#[non_exhaustive]
Invalid {
/// The variable.
var: &'static str,
/// What it held.
value: String,
/// The accepted forms.
expected: &'static str,
},
}
impl ConfigError {
pub(crate) fn invalid(
var: &'static str,
value: impl Into<String>,
expected: &'static str,
) -> Self {
Self::Invalid {
var,
value: value.into(),
expected,
}
}
}