Skip to content

Commit ed27485

Browse files
committed
refactor(test_runner): replace MainError with ad-hoc anyhow error
1 parent 3e0bd0b commit ed27485

File tree

1 file changed

+9
-27
lines changed

1 file changed

+9
-27
lines changed

src/r3_test_runner/src/main.rs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
#![feature(decl_macro)] // `macro`
66
#![feature(pin_macro)] // `core::pin::pin!`
77
#![warn(must_not_suspend)]
8-
use anyhow::Context;
8+
use anyhow::{bail, Context};
99
use clap::Parser;
1010
use std::{env, path::Path};
11-
use thiserror::Error;
1211

1312
mod driverinterface;
1413
mod selection;
@@ -31,23 +30,6 @@ async fn main() {
3130
}
3231
}
3332

34-
// TODO: Top-level error enum is useless; replace it with `anyhow`
35-
#[derive(Error, Debug)]
36-
enum MainError {
37-
#[error("Could not initialize the test driver interface.")]
38-
TestDriver(#[source] driverinterface::TestDriverNewError),
39-
#[error("Could not connect to the target.")]
40-
ConnectTarget(#[source] anyhow::Error),
41-
#[error("Could not build the test '{0}'.")]
42-
BuildTest(String, #[source] driverinterface::TestDriverRunError),
43-
#[error("Could not run the test '{0}'.")]
44-
RunTest(String, #[source] driverinterface::TestDriverRunError),
45-
#[error("Test failed.")]
46-
TestFail,
47-
#[error("The target architecture '{0}' is invalid or unsupported.")]
48-
BadTarget(targets::Arch),
49-
}
50-
5133
/// R3 test runner
5234
#[derive(Parser)]
5335
struct Opt {
@@ -161,9 +143,9 @@ async fn main_inner() -> anyhow::Result<()> {
161143
let target_arch = opt.target_arch.unwrap_or_else(|| opt.target.target_arch());
162144
log::debug!("target_arch = {}", target_arch);
163145

164-
let target_arch_opt = target_arch
165-
.build_opt()
166-
.ok_or(MainError::BadTarget(target_arch))?;
146+
let target_arch_opt = target_arch.build_opt().with_context(|| {
147+
format!("The target architecture '{target_arch}' is invalid or unsupported.")
148+
})?;
167149
log::debug!("target_arch_opt = {:?}", target_arch_opt);
168150

169151
// Initialize the test driver interface
@@ -175,7 +157,7 @@ async fn main_inner() -> anyhow::Result<()> {
175157
opt.additional_rustflags.unwrap_or_default(),
176158
)
177159
.await
178-
.map_err(MainError::TestDriver)?;
160+
.context("Could not initialize the test driver interface.")?;
179161

180162
// Select tests
181163
let test_source = selection::TestSource {
@@ -215,7 +197,7 @@ async fn main_inner() -> anyhow::Result<()> {
215197
opt.target
216198
.connect()
217199
.await
218-
.map_err(MainError::ConnectTarget)?,
200+
.context("Could not connect to the target.")?,
219201
)
220202
};
221203

@@ -243,7 +225,7 @@ async fn main_inner() -> anyhow::Result<()> {
243225
},
244226
)
245227
.await
246-
.map_err(|e| MainError::BuildTest(full_test_name.clone(), e))?;
228+
.with_context(|| format!("Could not build the test '{full_test_name}'."))?;
247229

248230
// Run the specified program
249231
let mut test_result = if opt.exec.is_empty() {
@@ -278,7 +260,7 @@ async fn main_inner() -> anyhow::Result<()> {
278260
test_result = test_driver
279261
.run(test_run, &mut **debug_probe)
280262
.await
281-
.map_err(|e| MainError::RunTest(full_test_name, e))?;
263+
.with_context(|| format!("Could not run the test '{full_test_name}'."))?;
282264
}
283265
}
284266

@@ -316,7 +298,7 @@ async fn main_inner() -> anyhow::Result<()> {
316298
}
317299
}
318300

319-
return Err(MainError::TestFail.into());
301+
bail!("Test failed.");
320302
}
321303

322304
assert!(tests_skipped_to_fail_fast.is_empty());

0 commit comments

Comments
 (0)