From 70520000d20e488ef09f3549dcf5f2041d127da5 Mon Sep 17 00:00:00 2001 From: Devyn Cairns Date: Sat, 6 Apr 2024 16:28:08 -0700 Subject: [PATCH] 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 - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `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 --- crates/nu-plugin-test-support/src/plugin_test.rs | 5 ++--- crates/nu-plugin-test-support/tests/hello/mod.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/crates/nu-plugin-test-support/src/plugin_test.rs b/crates/nu-plugin-test-support/src/plugin_test.rs index af4a52f4e5..18872af44d 100644 --- a/crates/nu-plugin-test-support/src/plugin_test.rs +++ b/crates/nu-plugin-test-support/src/plugin_test.rs @@ -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, ) -> Result { - 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)); diff --git a/crates/nu-plugin-test-support/tests/hello/mod.rs b/crates/nu-plugin-test-support/tests/hello/mod.rs index 803b9620e8..00886f1888 100644 --- a/crates/nu-plugin-test-support/tests/hello/mod.rs +++ b/crates/nu-plugin-test-support/tests/hello/mod.rs @@ -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(()) +}