Use nu-cmd-lang default context for plugin tests (#12434)

# Description
@ayax79 added `nu-cmd-lang` as a dep for `nu-plugin-test-support` in
order to get access to `let`. Since we have the dep anyway now, we might
as well just add all of the lang commands - there aren't very many of
them and it would be less confusing than only `let` working.

# User-Facing Changes
- Can use some more core nu language features in plugin tests, like
loops and `do`

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

# After Submitting
- [ ] Might need to change something about the plugin testing section of
the book, since I think it says something about there only being the
plugin command itself available
This commit is contained in:
Devyn Cairns 2024-04-06 16:28:08 -07:00 committed by GitHub
parent 01c79bbefc
commit 70520000d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 3 deletions

View File

@ -1,7 +1,7 @@
use std::{cmp::Ordering, convert::Infallible, sync::Arc};
use nu_ansi_term::Style;
use nu_cmd_lang::Let;
use nu_cmd_lang::create_default_context;
use nu_engine::eval_block;
use nu_parser::parse;
use nu_plugin::{Plugin, PluginCommand, PluginCustomValue, PluginSource};
@ -37,9 +37,8 @@ impl PluginTest {
name: &str,
plugin: Arc<impl Plugin + Send + 'static>,
) -> Result<PluginTest, ShellError> {
let mut engine_state = EngineState::new();
let mut engine_state = create_default_context();
let mut working_set = StateWorkingSet::new(&engine_state);
working_set.add_decl(Box::new(Let));
let reg_plugin = fake_register(&mut working_set, name, plugin)?;
let source = Arc::new(PluginSource::new(reg_plugin));

View File

@ -73,3 +73,16 @@ fn test_an_example_with_the_wrong_result() -> Result<(), ShellError> {
assert!(result.is_err());
Ok(())
}
#[test]
fn test_requiring_nu_cmd_lang_commands() -> Result<(), ShellError> {
use nu_protocol::Span;
let result = PluginTest::new("hello", HelloPlugin.into())?
.eval("do { let greeting = hello; $greeting }")?
.into_value(Span::test_data());
assert_eq!(Value::test_string("Hello, World!"), result);
Ok(())
}