Skip to content

Commit fda4657

Browse files
committed
feat(cases): add a boolean flag to return Error in case the binary was not resolved
1 parent 4fcd57a commit fda4657

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

crates/trycmd/src/cases.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub struct TestCases {
77
bins: std::cell::RefCell<crate::BinRegistry>,
88
substitutions: std::cell::RefCell<snapbox::Redactions>,
99
has_run: std::cell::Cell<bool>,
10+
fail_on_unresolved_bin: std::cell::Cell<bool>,
1011
}
1112

1213
impl TestCases {
@@ -169,6 +170,17 @@ impl TestCases {
169170
Ok(self)
170171
}
171172

173+
/// Set rule to whether to fail on unresolved bin.
174+
///
175+
/// If this flag is set to `true`, the test will fail if a bin cannot be resolved.
176+
/// By default, this is set to `false`.
177+
///
178+
/// When set to `false`, the test will not fail if a bin cannot be resolved, but the case will just be ignored.
179+
pub fn set_fail_on_unresolved_bin(&self, fail: bool) -> &Self {
180+
self.fail_on_unresolved_bin.set(fail);
181+
self
182+
}
183+
172184
/// Run tests
173185
///
174186
/// This will happen on `drop` if not done explicitly
@@ -178,7 +190,11 @@ impl TestCases {
178190
let mode = parse_mode(std::env::var_os("TRYCMD").as_deref());
179191
mode.initialize().unwrap();
180192

181-
let runner = self.runner.borrow_mut().prepare();
193+
let runner = self
194+
.runner
195+
.borrow_mut()
196+
.prepare()
197+
.fail_on_unresolved_bin(self.fail_on_unresolved_bin.get());
182198
runner.run(&mode, &self.bins.borrow(), &self.substitutions.borrow());
183199
}
184200
}

crates/trycmd/src/runner.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,24 @@ use snapbox::IntoData;
2020
#[derive(Debug)]
2121
pub(crate) struct Runner {
2222
cases: Vec<Case>,
23+
fail_on_unresolved_bin: bool,
2324
}
2425

2526
impl Runner {
2627
pub(crate) fn new() -> Self {
2728
Self {
2829
cases: Default::default(),
30+
fail_on_unresolved_bin: false,
2931
}
3032
}
3133

34+
/// Set the rule value for `fail_on_unresolved_bin`
35+
pub(crate) fn fail_on_unresolved_bin(mut self, fail: bool) -> Self {
36+
self.fail_on_unresolved_bin = fail;
37+
38+
self
39+
}
40+
3241
pub(crate) fn case(&mut self, case: Case) {
3342
self.cases.push(case);
3443
}
@@ -49,7 +58,7 @@ impl Runner {
4958
.cases
5059
.par_iter()
5160
.flat_map(|c| {
52-
let results = c.run(mode, bins, substitutions);
61+
let results = c.run(mode, bins, substitutions, self.fail_on_unresolved_bin);
5362

5463
let stderr = stderr();
5564
let mut stderr = stderr.lock();
@@ -159,6 +168,7 @@ impl Case {
159168
mode: &Mode,
160169
bins: &crate::BinRegistry,
161170
substitutions: &snapbox::Redactions,
171+
fail_on_unresolved_bin: bool,
162172
) -> Vec<Result<Output, Output>> {
163173
if self.expected == Some(crate::schema::CommandStatus::Skipped) {
164174
let output = Output::sequence(self.path.clone());
@@ -235,7 +245,13 @@ impl Case {
235245
step.expected_status = Some(crate::schema::CommandStatus::Skipped);
236246
}
237247

238-
let step_status = self.run_step(step, cwd.as_deref(), bins, &substitutions);
248+
let step_status = self.run_step(
249+
step,
250+
cwd.as_deref(),
251+
bins,
252+
&substitutions,
253+
fail_on_unresolved_bin,
254+
);
239255
if fs_context.is_mutable() && step_status.is_err() && *mode == Mode::Fail {
240256
prior_step_failed = true;
241257
}
@@ -324,6 +340,7 @@ impl Case {
324340
cwd: Option<&std::path::Path>,
325341
bins: &crate::BinRegistry,
326342
substitutions: &snapbox::Redactions,
343+
fail_on_unresolved_bin: bool,
327344
) -> Result<Output, Output> {
328345
let output = if let Some(id) = step.id.clone() {
329346
Output::step(self.path.clone(), id)
@@ -355,10 +372,14 @@ impl Case {
355372

356373
match &step.bin {
357374
Some(crate::schema::Bin::Path(_)) => {}
358-
Some(crate::schema::Bin::Name(_name)) => {
375+
Some(crate::schema::Bin::Name(name)) => {
359376
// Unhandled by resolve
360-
snapbox::debug!("bin={:?} not found", _name);
377+
snapbox::debug!("bin={:?} not found", name);
361378
assert_eq!(output.spawn.status, SpawnStatus::Skipped);
379+
380+
if fail_on_unresolved_bin {
381+
return Err(output.error(format!("bin={name:?} not found").into()));
382+
}
362383
return Ok(output);
363384
}
364385
Some(crate::schema::Bin::Error(_)) => {}
@@ -367,6 +388,9 @@ impl Case {
367388
Some(crate::schema::Bin::Ignore) => {
368389
// Unhandled by resolve
369390
assert_eq!(output.spawn.status, SpawnStatus::Skipped);
391+
if fail_on_unresolved_bin {
392+
return Err(output.error("bin not found".into()));
393+
}
370394
return Ok(output);
371395
}
372396
}

0 commit comments

Comments
 (0)