From b74d508c0bdf2fb60e21ae77f11c12eb418862cf Mon Sep 17 00:00:00 2001 From: TrMen <31260183+TrMen@users.noreply.github.com> Date: Sun, 25 Jun 2023 14:43:48 +0200 Subject: [PATCH] Test examples of `use` (#9032) # Description Add `test_examples()` to `use` that verifies its examples. For that, add the necessary definitions and the `PWD` to the `nu-cmd-lang` test support. Note: `let-env` is a `nu-command` and thus not accessible from the `nu-cmd-lang` tests. So replace `let-env` with `$env. =`. This should be fine, since `let-env` is supposed to be removed before 1.0 anyway. # User-Facing Changes Nothing # Question for reviewer Is there any particular reason why so many core commands (parser keywords) don't verify their examples with tests? E.g. `break`, `hide`, `overlay use`, etc. (I think most of them?). If there is no particular reason, I'd make a follow up PR to enable tests for as many of them as possible. --- crates/nu-cmd-lang/src/core_commands/use_.rs | 13 ++++++++++++- crates/nu-cmd-lang/src/example_test.rs | 7 ++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/nu-cmd-lang/src/core_commands/use_.rs b/crates/nu-cmd-lang/src/core_commands/use_.rs index 41f4c4893a..edeb53d3d7 100644 --- a/crates/nu-cmd-lang/src/core_commands/use_.rs +++ b/crates/nu-cmd-lang/src/core_commands/use_.rs @@ -20,6 +20,7 @@ impl Command for Use { fn signature(&self) -> nu_protocol::Signature { Signature::build("use") .input_output_types(vec![(Type::Nothing, Type::Nothing)]) + .allow_variants_without_examples(true) .required("module", SyntaxShape::String, "Module or module file") .rest( "members", @@ -134,7 +135,7 @@ This command is a parser keyword. For details, check: }, Example { description: "Define a custom command that participates in the environment in a module and call it", - example: r#"module foo { export def-env bar [] { let-env FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#, + example: r#"module foo { export def-env bar [] { $env.FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#, result: Some(Value::test_string("BAZ")), }, Example { @@ -160,3 +161,13 @@ This command is a parser keyword. For details, check: ] } } + +#[cfg(test)] +mod test { + #[test] + fn test_examples() { + use super::Use; + use crate::test_examples; + test_examples(Use {}) + } +} diff --git a/crates/nu-cmd-lang/src/example_test.rs b/crates/nu-cmd-lang/src/example_test.rs index 0d1e13b1ab..336cf38a1a 100644 --- a/crates/nu-cmd-lang/src/example_test.rs +++ b/crates/nu-cmd-lang/src/example_test.rs @@ -14,7 +14,8 @@ mod test_examples { check_example_input_and_output_types_match_command_signature, }; use crate::{ - Break, Collect, Def, Describe, Echo, ExportCommand, ExportDef, If, Let, Module, Mut, Use, + Break, Collect, Def, DefEnv, Describe, Echo, ExportCommand, ExportDef, ExportDefEnv, If, + Let, Module, Mut, Use, }; use nu_protocol::{ engine::{Command, EngineState, StateWorkingSet}, @@ -69,10 +70,12 @@ mod test_examples { working_set.add_decl(Box::new(Break)); working_set.add_decl(Box::new(Collect)); working_set.add_decl(Box::new(Def)); + working_set.add_decl(Box::new(DefEnv)); working_set.add_decl(Box::new(Describe)); working_set.add_decl(Box::new(Echo)); working_set.add_decl(Box::new(ExportCommand)); working_set.add_decl(Box::new(ExportDef)); + working_set.add_decl(Box::new(ExportDefEnv)); working_set.add_decl(Box::new(If)); working_set.add_decl(Box::new(Let)); working_set.add_decl(Box::new(Module)); @@ -85,6 +88,8 @@ mod test_examples { working_set.render() }; + engine_state.add_env_var("PWD".to_string(), Value::test_string(".")); + engine_state .merge_delta(delta) .expect("Error merging delta");