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.<var> =`. 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.
This commit is contained in:
TrMen 2023-06-25 14:43:48 +02:00 committed by GitHub
parent 14daa93a19
commit b74d508c0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -20,6 +20,7 @@ impl Command for Use {
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
Signature::build("use") Signature::build("use")
.input_output_types(vec![(Type::Nothing, Type::Nothing)]) .input_output_types(vec![(Type::Nothing, Type::Nothing)])
.allow_variants_without_examples(true)
.required("module", SyntaxShape::String, "Module or module file") .required("module", SyntaxShape::String, "Module or module file")
.rest( .rest(
"members", "members",
@ -134,7 +135,7 @@ This command is a parser keyword. For details, check:
}, },
Example { Example {
description: "Define a custom command that participates in the environment in a module and call it", 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")), result: Some(Value::test_string("BAZ")),
}, },
Example { 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 {})
}
}

View File

@ -14,7 +14,8 @@ mod test_examples {
check_example_input_and_output_types_match_command_signature, check_example_input_and_output_types_match_command_signature,
}; };
use crate::{ 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::{ use nu_protocol::{
engine::{Command, EngineState, StateWorkingSet}, engine::{Command, EngineState, StateWorkingSet},
@ -69,10 +70,12 @@ mod test_examples {
working_set.add_decl(Box::new(Break)); working_set.add_decl(Box::new(Break));
working_set.add_decl(Box::new(Collect)); working_set.add_decl(Box::new(Collect));
working_set.add_decl(Box::new(Def)); 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(Describe));
working_set.add_decl(Box::new(Echo)); working_set.add_decl(Box::new(Echo));
working_set.add_decl(Box::new(ExportCommand)); working_set.add_decl(Box::new(ExportCommand));
working_set.add_decl(Box::new(ExportDef)); 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(If));
working_set.add_decl(Box::new(Let)); working_set.add_decl(Box::new(Let));
working_set.add_decl(Box::new(Module)); working_set.add_decl(Box::new(Module));
@ -85,6 +88,8 @@ mod test_examples {
working_set.render() working_set.render()
}; };
engine_state.add_env_var("PWD".to_string(), Value::test_string("."));
engine_state engine_state
.merge_delta(delta) .merge_delta(delta)
.expect("Error merging delta"); .expect("Error merging delta");