nushell/tests/plugins/stress_internals.rs
Devyn Cairns c06ef201b7
Local socket mode and foreground terminal control for plugins (#12448)
# Description

Adds support for running plugins using local socket communication
instead of stdio. This will be an optional thing that not all plugins
have to support.

This frees up stdio for use to make plugins that use stdio to create
terminal UIs, cc @amtoine, @fdncred.

This uses the [`interprocess`](https://crates.io/crates/interprocess)
crate (298 stars, MIT license, actively maintained), which seems to be
the best option for cross-platform local socket support in Rust. On
Windows, a local socket name is provided. On Unixes, it's a path. The
socket name is kept to a relatively small size because some operating
systems have pretty strict limits on the whole path (~100 chars), so on
macOS for example we prefer `/tmp/nu.{pid}.{hash64}.sock` where the hash
includes the plugin filename and timestamp to be unique enough.

This also adds an API for moving plugins in and out of the foreground
group, which is relevant for Unixes where direct terminal control
depends on that.

TODO:

- [x] Generate local socket path according to OS conventions
- [x] Add support for passing `--local-socket` to the plugin executable
instead of `--stdio`, and communicating over that instead
- [x] Test plugins that were broken, including
[amtoine/nu_plugin_explore](https://github.com/amtoine/nu_plugin_explore)
- [x] Automatically upgrade to using local sockets when supported,
falling back if it doesn't work, transparently to the user without any
visible error messages
  - Added protocol feature: `LocalSocket`
- [x] Reset preferred mode to `None` on `register`
- [x] Allow plugins to detect whether they're running on a local socket
and can use stdio freely, so that TUI plugins can just produce an error
message otherwise
  - Implemented via `EngineInterface::is_using_stdio()`
- [x] Clean up foreground state when plugin command exits on the engine
side too, not just whole plugin
- [x] Make sure tests for failure cases work as intended
  - `nu_plugin_stress_internals` added

# User-Facing Changes
- TUI plugins work
- Non-Rust plugins could optionally choose to use this
- This might behave differently, so will need to test it carefully
across different operating systems

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting
- [ ] Document local socket option in plugin contrib docs
- [ ] Document how to do a terminal UI plugin in plugin contrib docs
- [ ] Document: `EnterForeground` engine call
- [ ] Document: `LeaveForeground` engine call
- [ ] Document: `LocalSocket` protocol feature
2024-04-15 18:28:18 +00:00

124 lines
3.4 KiB
Rust

use nu_test_support::nu_with_plugins;
fn ensure_stress_env_vars_unset() {
for (key, _) in std::env::vars_os() {
if key.to_string_lossy().starts_with("STRESS_") {
panic!("Test is running in a dirty environment: {key:?} is set");
}
}
}
#[test]
fn test_stdio() {
ensure_stress_env_vars_unset();
let result = nu_with_plugins!(
cwd: ".",
plugin: ("nu_plugin_stress_internals"),
"stress_internals"
);
assert!(result.status.success());
assert!(result.out.contains("local_socket_path: None"));
}
#[test]
fn test_local_socket() {
ensure_stress_env_vars_unset();
let result = nu_with_plugins!(
cwd: ".",
envs: vec![
("STRESS_ADVERTISE_LOCAL_SOCKET", "1"),
],
plugin: ("nu_plugin_stress_internals"),
"stress_internals"
);
assert!(result.status.success());
// Should be run once in stdio mode
assert!(result.err.contains("--stdio"));
// And then in local socket mode
assert!(result.err.contains("--local-socket"));
assert!(result.out.contains("local_socket_path: Some"));
}
#[test]
fn test_failing_local_socket_fallback() {
ensure_stress_env_vars_unset();
let result = nu_with_plugins!(
cwd: ".",
envs: vec![
("STRESS_ADVERTISE_LOCAL_SOCKET", "1"),
("STRESS_REFUSE_LOCAL_SOCKET", "1"),
],
plugin: ("nu_plugin_stress_internals"),
"stress_internals"
);
assert!(result.status.success());
// Count the number of times we do stdio/local socket
let mut count_stdio = 0;
let mut count_local_socket = 0;
for line in result.err.split('\n') {
if line.contains("--stdio") {
count_stdio += 1;
}
if line.contains("--local-socket") {
count_local_socket += 1;
}
}
// Should be run once in local socket mode
assert_eq!(1, count_local_socket, "count of --local-socket");
// Should be run twice in stdio mode, due to the fallback
assert_eq!(2, count_stdio, "count of --stdio");
// In the end it should not be running in local socket mode, but should succeed
assert!(result.out.contains("local_socket_path: None"));
}
#[test]
fn test_exit_early_stdio() {
ensure_stress_env_vars_unset();
let result = nu_with_plugins!(
cwd: ".",
envs: vec![
("STRESS_EXIT_EARLY", "1"),
],
plugin: ("nu_plugin_stress_internals"),
"stress_internals"
);
assert!(!result.status.success());
assert!(result.err.contains("--stdio"));
}
#[test]
fn test_exit_early_local_socket() {
ensure_stress_env_vars_unset();
let result = nu_with_plugins!(
cwd: ".",
envs: vec![
("STRESS_ADVERTISE_LOCAL_SOCKET", "1"),
("STRESS_EXIT_EARLY", "1"),
],
plugin: ("nu_plugin_stress_internals"),
"stress_internals"
);
assert!(!result.status.success());
assert!(result.err.contains("--local-socket"));
}
#[test]
fn test_wrong_version() {
ensure_stress_env_vars_unset();
let result = nu_with_plugins!(
cwd: ".",
envs: vec![
("STRESS_WRONG_VERSION", "1"),
],
plugin: ("nu_plugin_stress_internals"),
"stress_internals"
);
assert!(!result.status.success());
assert!(result.err.contains("version"));
assert!(result.err.contains("0.0.0"));
}