Skip to content

Commit 93863d5

Browse files
committed
Introduce a dedicated AbortCheck type
1 parent 47b6f9c commit 93863d5

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

examples/rustc_basic.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#[cfg(feature = "rustc")]
2-
use std::sync::atomic::Ordering;
3-
#[cfg(feature = "rustc")]
42
use ui_test::{run_tests, Config};
53

64
#[cfg(feature = "rustc")]
75
#[cfg_attr(test, test)]
86
fn main() -> ui_test::color_eyre::Result<()> {
97
let config = Config::rustc("examples_tests/rustc_basic");
108
let abort_check = config.abort_check.clone();
11-
ctrlc::set_handler(move || abort_check.store(true, Ordering::Relaxed))?;
9+
ctrlc::set_handler(move || abort_check.abort())?;
1210

1311
// Compile all `.rs` files in the given directory (relative to your
1412
// Cargo.toml) and compare their output against the corresponding

examples/rustc_twice_with_different_flags.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#[cfg(feature = "rustc")]
2-
use std::sync::atomic::Ordering;
3-
#[cfg(feature = "rustc")]
42
use ui_test::{
53
default_file_filter, default_per_file_config, run_tests_generic, status_emitter, Config,
64
};
@@ -10,7 +8,7 @@ use ui_test::{
108
fn main() -> ui_test::color_eyre::Result<()> {
119
let config = Config::rustc("examples_tests/rustc_basic");
1210
let abort_check = config.abort_check.clone();
13-
ctrlc::set_handler(move || abort_check.store(true, Ordering::Relaxed))?;
11+
ctrlc::set_handler(move || abort_check.abort())?;
1412

1513
// Compile all `.rs` files in the given directory (relative to your
1614
// Cargo.toml) and compare their output against the corresponding

src/config.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,26 @@ pub struct Config {
6161
pub custom_comments: BTreeMap<&'static str, CommandParserFunc>,
6262
/// Custom diagnostic extractor (invoked on the output of tests)
6363
pub diagnostic_extractor: fn(&Path, &[u8]) -> Diagnostics,
64-
/// An atomic bool that can be set to `true` to abort all tests.
65-
/// Will not cancel child processes, but if set from a Ctrl+C handler,
66-
/// the pressing of Ctrl+C will already have cancelled child processes.
67-
pub abort_check: Arc<AtomicBool>,
64+
/// Handle to the global abort check.
65+
pub abort_check: AbortCheck,
66+
}
67+
68+
/// An atomic bool that can be set to `true` to abort all tests.
69+
/// Will not cancel child processes, but if set from a Ctrl+C handler,
70+
/// the pressing of Ctrl+C will already have cancelled child processes.
71+
#[derive(Clone, Debug, Default)]
72+
pub struct AbortCheck(Arc<AtomicBool>);
73+
74+
impl AbortCheck {
75+
/// Whether any test has been aborted.
76+
pub fn aborted(&self) -> bool {
77+
self.0.load(std::sync::atomic::Ordering::Relaxed)
78+
}
79+
80+
/// Inform everyone that an abort has been requested
81+
pub fn abort(&self) {
82+
self.0.store(true, std::sync::atomic::Ordering::Relaxed)
83+
}
6884
}
6985

7086
/// Function that performs the actual output conflict handling.
@@ -439,7 +455,7 @@ impl Config {
439455
}
440456

441457
pub(crate) fn aborted(&self) -> bool {
442-
self.abort_check.load(std::sync::atomic::Ordering::Relaxed)
458+
self.abort_check.aborted()
443459
}
444460
}
445461

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use std::panic::AssertUnwindSafe;
3333
use std::path::Path;
3434
#[cfg(feature = "rustc")]
3535
use std::process::Command;
36-
use std::sync::atomic::Ordering;
3736
use std::sync::Arc;
3837
use test_result::TestRun;
3938
pub use test_result::{Errored, TestOk};
@@ -323,7 +322,7 @@ pub fn run_tests_generic(
323322
},
324323
|finished_files_recv| {
325324
for run in finished_files_recv {
326-
let aborted = run.abort_check.load(Ordering::Relaxed);
325+
let aborted = run.abort_check.aborted();
327326
run.status.done(&run.result, aborted);
328327

329328
// Do not write summaries for cancelled tests
@@ -340,7 +339,7 @@ pub fn run_tests_generic(
340339
let mut aborted = false;
341340

342341
for run in results {
343-
aborted |= run.abort_check.load(Ordering::Relaxed);
342+
aborted |= run.abort_check.aborted();
344343
match run.result {
345344
Ok(TestOk::Ok) => succeeded += 1,
346345
Ok(TestOk::Ignored) => ignored += 1,

src/test_result.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Various data structures used for carrying information about test success or failure
22
3-
use crate::{status_emitter::TestStatus, Error};
3+
use crate::{status_emitter::TestStatus, AbortCheck, Error};
44
use color_eyre::eyre::Result;
5-
use std::sync::{atomic::AtomicBool, Arc};
65

76
/// The possible non-failure results a single test can have.
87
#[derive(Debug)]
@@ -52,5 +51,5 @@ pub struct TestRun {
5251
/// Usually created via `for_revsion` or `for_path`
5352
pub status: Box<dyn TestStatus>,
5453
/// Whether the run was aborted prematurely
55-
pub abort_check: Arc<AtomicBool>,
54+
pub abort_check: AbortCheck,
5655
}

tests/integrations/basic-fail/Cargo.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,9 @@ tests/actual_tests_bless/revisions.rs ... ok
490490
tests/actual_tests_bless/revisions.rs (revision `foo`) ... ok
491491
tests/actual_tests_bless/revisions.rs (revision `bar`) ... ok
492492
tests/actual_tests_bless/revisions_bad.rs ... ok
493+
tests/actual_tests_bless/revisions_filter.rs ... ok
493494
tests/actual_tests_bless/revisions_bad.rs (revision `foo`) ... ok
494495
tests/actual_tests_bless/revisions_bad.rs (revision `bar`) ... FAILED
495-
tests/actual_tests_bless/revisions_filter.rs ... ok
496496
tests/actual_tests_bless/revisions_filter.rs (revision `foo`) ... ignored (in-test comment)
497497
tests/actual_tests_bless/revisions_filter.rs (revision `bar`) ... ignored (in-test comment)
498498
tests/actual_tests_bless/revisions_filter2.rs ... ok

0 commit comments

Comments
 (0)