Add and use new Signals struct (#13314)

# Description
This PR introduces a new `Signals` struct to replace our adhoc passing
around of `ctrlc: Option<Arc<AtomicBool>>`. Doing so has a few benefits:
- We can better enforce when/where resetting or triggering an interrupt
is allowed.
- Consolidates `nu_utils::ctrl_c::was_pressed` and other ad-hoc
re-implementations into a single place: `Signals::check`.
- This allows us to add other types of signals later if we want. E.g.,
exiting or suspension.
- Similarly, we can more easily change the underlying implementation if
we need to in the future.
- Places that used to have a `ctrlc` of `None` now use
`Signals::empty()`, so we can double check these usages for correctness
in the future.
This commit is contained in:
Ian Manske
2024-07-07 22:29:01 +00:00
committed by GitHub
parent c6b6b1b7a8
commit 399a7c8836
246 changed files with 1332 additions and 1234 deletions

View File

@ -8,8 +8,8 @@
//! use nu_plugin::*;
//! use nu_plugin_test_support::PluginTest;
//! use nu_protocol::{
//! Example, IntoInterruptiblePipelineData, LabeledError, PipelineData, ShellError, Signature,
//! Span, Type, Value,
//! Example, IntoInterruptiblePipelineData, LabeledError, PipelineData, ShellError, Signals,
//! Signature, Span, Type, Value,
//! };
//!
//! struct LowercasePlugin;
@ -60,7 +60,7 @@
//! // Errors in a stream should be returned as values.
//! .unwrap_or_else(|err| Value::error(err, span))
//! },
//! None,
//! &Signals::empty(),
//! )?)
//! }
//! }
@ -83,7 +83,7 @@
//!
//! // #[test]
//! fn test_lowercase() -> Result<(), ShellError> {
//! let input = vec![Value::test_string("FooBar")].into_pipeline_data(Span::test_data(), None);
//! let input = vec![Value::test_string("FooBar")].into_pipeline_data(Span::test_data(), Signals::empty());
//! let output = PluginTest::new("lowercase", LowercasePlugin.into())?
//! .eval_with("lowercase", input)?
//! .into_value(Span::test_data())?;

View File

@ -11,7 +11,7 @@ use nu_protocol::{
debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet},
report_error_new, CustomValue, Example, IntoSpanned as _, LabeledError, PipelineData,
ShellError, Span, Value,
ShellError, Signals, Span, Value,
};
use crate::{diff::diff_by_line, fake_register::fake_register};
@ -85,13 +85,13 @@ impl PluginTest {
///
/// ```rust,no_run
/// # use nu_plugin_test_support::PluginTest;
/// # use nu_protocol::{ShellError, Span, Value, IntoInterruptiblePipelineData};
/// # use nu_protocol::{IntoInterruptiblePipelineData, ShellError, Signals, Span, Value};
/// # use nu_plugin::*;
/// # fn test(MyPlugin: impl Plugin + Send + 'static) -> Result<(), ShellError> {
/// let result = PluginTest::new("my_plugin", MyPlugin.into())?
/// .eval_with(
/// "my-command",
/// vec![Value::test_int(42)].into_pipeline_data(Span::test_data(), None)
/// vec![Value::test_int(42)].into_pipeline_data(Span::test_data(), Signals::empty())
/// )?
/// .into_value(Span::test_data())?;
/// assert_eq!(Value::test_string("42"), result);
@ -151,7 +151,7 @@ impl PluginTest {
Err(err) => Value::error(err, value.span()),
}
},
None,
&Signals::empty(),
)?
};
@ -171,7 +171,7 @@ impl PluginTest {
Err(err) => Value::error(err, value.span()),
}
},
None,
&Signals::empty(),
)
}
}

View File

@ -1,8 +1,8 @@
use nu_plugin::*;
use nu_plugin_test_support::PluginTest;
use nu_protocol::{
Example, IntoInterruptiblePipelineData, LabeledError, PipelineData, ShellError, Signature,
Span, Type, Value,
Example, IntoInterruptiblePipelineData, LabeledError, PipelineData, ShellError, Signals,
Signature, Span, Type, Value,
};
struct LowercasePlugin;
@ -53,7 +53,7 @@ impl PluginCommand for Lowercase {
// Errors in a stream should be returned as values.
.unwrap_or_else(|err| Value::error(err, span))
},
None,
&Signals::empty(),
)?)
}
}
@ -72,7 +72,8 @@ impl Plugin for LowercasePlugin {
fn test_lowercase_using_eval_with() -> Result<(), ShellError> {
let result = PluginTest::new("lowercase", LowercasePlugin.into())?.eval_with(
"lowercase",
vec![Value::test_string("HeLlO wOrLd")].into_pipeline_data(Span::test_data(), None),
vec![Value::test_string("HeLlO wOrLd")]
.into_pipeline_data(Span::test_data(), Signals::empty()),
)?;
assert_eq!(