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
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 {
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 {})
}
}