2022-12-23 20:19:10 +01:00
|
|
|
mod test_bits;
|
2022-12-21 23:54:39 +01:00
|
|
|
mod test_cell_path;
|
2023-03-16 23:45:35 +01:00
|
|
|
mod test_commandline;
|
2021-12-25 20:39:42 +01:00
|
|
|
mod test_conditionals;
|
2023-11-04 21:18:57 +01:00
|
|
|
mod test_config;
|
2022-08-23 09:18:14 +02:00
|
|
|
mod test_config_path;
|
2021-12-25 20:39:42 +01:00
|
|
|
mod test_converters;
|
|
|
|
mod test_custom_commands;
|
|
|
|
mod test_engine;
|
|
|
|
mod test_env;
|
Parameter defaults to $nu.scope.commands (#9152)
(*third* try at posting this PR, #9104, like #9084, got polluted with
unrelated commits. I'm never going to pull from the github feature
branch again!)
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Show parameter defaults in scope command signature, where they're
available for display by help.
per https://github.com/nushell/nushell/issues/8928.
I found unexpected ramifications in one completer (NuHelpCompleter) and
plugins, which both use the flag-formatting routine from builtin help.
For the moment I made the minimum necessary changes to get the mainline
scenario to pass tests and run. But we should circle back on what to do
with plugins and help completer..
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
1. New `parameter_default` column to `signatures` table in
`$nu.scope.commands`
It is populated with whatever parameters can be defaulted: currently
positional args and named flags.
2. Built in help (both `help <command>` and `<command> --help` will
display the defaults
3. Help completer will display defaults for flags, but not for
positionals.
Example:
A custom command with some default parameters:
```
〉cat ~/work/dflts.nu
# sample function to show defaults in help
export def main [
arg1: string # mandatory positional
arg2:string=abc # optional positional
--switch # no default here
--named:int # named flag, no default
--other:string=def # flag
--hard:record<foo:int bar:string, bas:bool> # default can be compound type
= {foo:22, bar:"other worlds", bas:false}
] { {arg1: $arg1,
arg2: $arg2,
switch: $switch,
named: $named,
other: $other,
hard: $hard, }
}
〉use ~/work/dflts.nu
〉$nu.scope.commands | where name == 'dflts' | get signatures.0.any | reject short_flag description custom_completion
╭───┬────────────────┬────────────────┬──────────────────────────────────────────┬─────────────┬───────────────────────────╮
│ # │ parameter_name │ parameter_type │ syntax_shape │ is_optional │ parameter_default │
├───┼────────────────┼────────────────┼──────────────────────────────────────────┼─────────────┼───────────────────────────┤
│ 0 │ │ input │ any │ false │ │
│ 1 │ arg1 │ positional │ string │ false │ │
│ 2 │ arg2 │ positional │ string │ true │ abc │
│ 3 │ switch │ switch │ │ true │ │
│ 4 │ named │ named │ int │ true │ │
│ 5 │ other │ named │ string │ true │ def │
│ 6 │ hard │ named │ record<foo: int, bar: string, bas: bool> │ true │ ╭───────┬───────────────╮ │
│ │ │ │ │ │ │ foo │ 22 │ │
│ │ │ │ │ │ │ bar │ other worlds │ │
│ │ │ │ │ │ │ bas │ false │ │
│ │ │ │ │ │ ╰───────┴───────────────╯ │
│ 7 │ │ output │ any │ false │ │
╰───┴────────────────┴────────────────┴──────────────────────────────────────────┴─────────────┴───────────────────────────╯
〉help dflts
sample function to show defaults in help
Usage:
> dflts {flags} <arg1> (arg2)
Flags:
--switch - switch -- no default here
--named <Int> - named flag, typed, but no default
--other <String> - flag with default (default: 'def')
--hard <Record([("foo", Int), ("bar", String), ("bas", Boolean)])> - default can be compound type (default: {foo: 22, bar: 'other worlds', bas: false})
-h, --help - Display the help message for this command
Parameters:
arg1 <string>: mandatory positional
arg2 <string>: optional positional (optional, default: 'abc')
```
Compared to (relevant bits of) help output previously:
```
Flags:
-h, --help - Display the help message for this command
-, --switch - no default here
-, --named <int> - named flag, no default
-, --other <string> - flag
-, --hard <record<foo: int, bar: string, bas: bool>> - default can be compound type
Signatures:
<any> | dflts <string> <string> -> <any>
Parameters:
arg1 <string>: mandatory positional
(optional) arg2 <string>: optional positional
```
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> [x] toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-05-11 20:59:56 +02:00
|
|
|
mod test_help;
|
2021-12-25 20:39:42 +01:00
|
|
|
mod test_hiding;
|
2023-04-07 21:01:12 +02:00
|
|
|
mod test_ide;
|
2021-12-25 20:39:42 +01:00
|
|
|
mod test_iteration;
|
2022-04-17 12:39:56 +02:00
|
|
|
mod test_known_external;
|
2021-12-25 20:39:42 +01:00
|
|
|
mod test_math;
|
|
|
|
mod test_modules;
|
|
|
|
mod test_parser;
|
|
|
|
mod test_ranges;
|
2022-04-07 08:23:14 +02:00
|
|
|
mod test_regex;
|
2023-03-24 12:54:06 +01:00
|
|
|
mod test_signatures;
|
2023-11-22 22:10:08 +01:00
|
|
|
mod test_spread;
|
2023-04-05 22:44:59 +02:00
|
|
|
mod test_stdlib;
|
2021-12-25 20:39:42 +01:00
|
|
|
mod test_strings;
|
|
|
|
mod test_table_operations;
|
|
|
|
mod test_type_check;
|
|
|
|
|
2021-07-30 22:02:16 +02:00
|
|
|
use assert_cmd::prelude::*;
|
|
|
|
use pretty_assertions::assert_eq;
|
2022-04-11 20:18:46 +02:00
|
|
|
use std::collections::HashMap;
|
2021-07-30 22:02:16 +02:00
|
|
|
use std::io::Write;
|
|
|
|
use std::process::Command;
|
|
|
|
use tempfile::NamedTempFile;
|
2021-07-17 20:52:50 +02:00
|
|
|
|
2021-12-25 20:39:42 +01:00
|
|
|
pub type TestResult = Result<(), Box<dyn std::error::Error>>;
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2022-04-11 20:18:46 +02:00
|
|
|
pub fn run_test_with_env(input: &str, expected: &str, env: &HashMap<&str, &str>) -> TestResult {
|
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2023-01-09 06:53:52 +01:00
|
|
|
cmd.arg(name).envs(env);
|
2022-04-11 20:18:46 +02:00
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
writeln!(file, "{input}")?;
|
2022-04-11 20:18:46 +02:00
|
|
|
|
|
|
|
run_cmd_and_assert(cmd, expected)
|
|
|
|
}
|
|
|
|
|
2021-07-30 22:02:16 +02:00
|
|
|
#[cfg(test)]
|
2021-12-25 20:39:42 +01:00
|
|
|
pub fn run_test(input: &str, expected: &str) -> TestResult {
|
2021-07-30 22:02:16 +02:00
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
2023-04-17 00:24:56 +02:00
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
|
|
|
cmd.arg("--no-std-lib");
|
|
|
|
cmd.arg(name);
|
|
|
|
cmd.env(
|
|
|
|
"PWD",
|
|
|
|
std::env::current_dir().expect("Can't get current dir"),
|
|
|
|
);
|
|
|
|
|
|
|
|
writeln!(file, "{input}")?;
|
|
|
|
|
|
|
|
run_cmd_and_assert(cmd, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn run_test_std(input: &str, expected: &str) -> TestResult {
|
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2023-01-09 06:53:52 +01:00
|
|
|
cmd.arg(name);
|
2022-04-17 12:39:56 +02:00
|
|
|
cmd.env(
|
|
|
|
"PWD",
|
|
|
|
std::env::current_dir().expect("Can't get current dir"),
|
|
|
|
);
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
writeln!(file, "{input}")?;
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2022-04-11 20:18:46 +02:00
|
|
|
run_cmd_and_assert(cmd, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn run_cmd_and_assert(mut cmd: Command, expected: &str) -> TestResult {
|
2021-07-30 22:02:16 +02:00
|
|
|
let output = cmd.output()?;
|
|
|
|
|
2021-07-30 23:57:22 +02:00
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("stdout: {stdout}");
|
|
|
|
println!("stderr: {stderr}");
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2021-07-30 23:57:22 +02:00
|
|
|
assert!(output.status.success());
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2021-07-30 23:57:22 +02:00
|
|
|
assert_eq!(stdout.trim(), expected);
|
2021-07-30 22:02:16 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-22 19:24:47 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
pub fn run_test_contains(input: &str, expected: &str) -> TestResult {
|
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2023-04-17 00:24:56 +02:00
|
|
|
cmd.arg("--no-std-lib");
|
2022-01-22 19:24:47 +01:00
|
|
|
cmd.arg(name);
|
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
writeln!(file, "{input}")?;
|
2023-04-07 21:01:12 +02:00
|
|
|
|
|
|
|
let output = cmd.output()?;
|
|
|
|
|
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
|
|
|
|
println!("stdout: {stdout}");
|
|
|
|
println!("stderr: {stderr}");
|
|
|
|
|
2023-09-21 20:12:36 +02:00
|
|
|
println!("Expected output to contain: {expected}");
|
2023-04-07 21:01:12 +02:00
|
|
|
assert!(output.status.success());
|
|
|
|
|
|
|
|
assert!(stdout.contains(expected));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn test_ide_contains(input: &str, ide_commands: &[&str], expected: &str) -> TestResult {
|
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2023-04-17 00:24:56 +02:00
|
|
|
cmd.arg("--no-std-lib");
|
2023-04-07 21:01:12 +02:00
|
|
|
for ide_command in ide_commands {
|
|
|
|
cmd.arg(ide_command);
|
|
|
|
}
|
|
|
|
cmd.arg(name);
|
|
|
|
|
|
|
|
writeln!(file, "{input}")?;
|
2022-01-22 19:24:47 +01:00
|
|
|
|
|
|
|
let output = cmd.output()?;
|
|
|
|
|
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("stdout: {stdout}");
|
|
|
|
println!("stderr: {stderr}");
|
2023-09-21 20:12:36 +02:00
|
|
|
println!("Expected output to contain: {expected}");
|
2022-01-22 19:24:47 +01:00
|
|
|
|
|
|
|
assert!(output.status.success());
|
|
|
|
|
|
|
|
assert!(stdout.contains(expected));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-30 22:02:16 +02:00
|
|
|
#[cfg(test)]
|
2021-12-25 20:39:42 +01:00
|
|
|
pub fn fail_test(input: &str, expected: &str) -> TestResult {
|
2021-07-30 22:02:16 +02:00
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
let mut cmd = Command::cargo_bin("nu")?;
|
2023-04-17 00:24:56 +02:00
|
|
|
cmd.arg("--no-std-lib");
|
2021-07-30 22:02:16 +02:00
|
|
|
cmd.arg(name);
|
2022-01-05 01:26:01 +01:00
|
|
|
cmd.env(
|
|
|
|
"PWD",
|
|
|
|
std::env::current_dir().expect("Can't get current dir"),
|
|
|
|
);
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
writeln!(file, "{input}")?;
|
2021-07-30 22:02:16 +02:00
|
|
|
|
|
|
|
let output = cmd.output()?;
|
|
|
|
|
2021-08-10 07:08:10 +02:00
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("stdout: {stdout}");
|
|
|
|
println!("stderr: {stderr}");
|
2023-09-21 20:12:36 +02:00
|
|
|
println!("Expected error to contain: {expected}");
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2022-01-31 18:42:12 +01:00
|
|
|
assert!(!stderr.is_empty() && stderr.contains(expected));
|
2021-07-30 22:02:16 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|