2024-02-28 15:05:09 +00:00
|
|
|
use nu_test_support::fs::Stub::EmptyFile;
|
2020-05-07 11:03:43 +00:00
|
|
|
use nu_test_support::nu;
|
2024-02-28 15:05:09 +00:00
|
|
|
use nu_test_support::playground::Playground;
|
2023-04-08 18:52:37 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2020-01-16 09:05:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shows_error_for_command_not_found() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("ferris_is_not_here.exe");
|
2020-01-16 09:05:53 +00:00
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(!actual.err.is_empty());
|
2020-01-16 09:05:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:39:11 +00:00
|
|
|
#[test]
|
|
|
|
fn shows_error_for_command_not_found_in_pipeline() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("ferris_is_not_here.exe | echo done");
|
2020-06-29 17:39:11 +00:00
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(!actual.err.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ignore] // jt: we can't test this using the -c workaround currently
|
2020-03-17 18:13:38 +00:00
|
|
|
#[test]
|
|
|
|
fn automatically_change_directory() {
|
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
|
|
|
|
Playground::setup("cd_test_5_1", |dirs, sandbox| {
|
2020-04-27 01:22:01 +00:00
|
|
|
sandbox.mkdir("autodir");
|
2020-03-17 18:13:38 +00:00
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-03-17 18:13:38 +00:00
|
|
|
autodir
|
2021-05-12 01:01:48 +00:00
|
|
|
echo (pwd)
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-03-17 18:13:38 +00:00
|
|
|
);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert!(actual.out.ends_with("autodir"));
|
2020-03-17 18:13:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
// FIXME: jt: we don't currently support autocd in testing
|
|
|
|
#[ignore]
|
2020-04-27 01:22:01 +00:00
|
|
|
#[test]
|
|
|
|
fn automatically_change_directory_with_trailing_slash_and_same_name_as_command() {
|
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
|
|
|
|
Playground::setup("cd_test_5_1", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("cd");
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-04-27 01:22:01 +00:00
|
|
|
cd/
|
2020-10-26 06:55:52 +00:00
|
|
|
pwd
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-04-27 01:22:01 +00:00
|
|
|
);
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert!(actual.out.ends_with("cd"));
|
2020-04-27 01:22:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-25 23:06:17 +00:00
|
|
|
#[test]
|
|
|
|
fn pass_dot_as_external_arguments() {
|
|
|
|
let actual = nu!("nu --testbin cococo .");
|
|
|
|
|
|
|
|
assert_eq!(actual.out, ".");
|
|
|
|
}
|
|
|
|
|
2020-04-30 04:54:07 +00:00
|
|
|
#[test]
|
|
|
|
fn correctly_escape_external_arguments() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("^nu --testbin cococo '$0'");
|
2020-04-30 04:54:07 +00:00
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "$0");
|
2020-04-30 04:54:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 18:05:08 +00:00
|
|
|
#[test]
|
|
|
|
fn escape_also_escapes_equals() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("^MYFOONAME=MYBARVALUE");
|
2023-03-16 18:05:08 +00:00
|
|
|
|
Rewrite run_external.rs (#12921)
This PR is a complete rewrite of `run_external.rs`. The main goal of the
rewrite is improving readability, but it also fixes some bugs related to
argument handling and the PATH variable (fixes
https://github.com/nushell/nushell/issues/6011).
I'll discuss some technical details to make reviewing easier.
## Argument handling
Quoting arguments for external commands is hard. Like, *really* hard.
We've had more than a dozen issues and PRs dedicated to quoting
arguments (see Appendix) but the current implementation is still buggy.
Here's a demonstration of the buggy behavior:
```nu
let foo = "'bar'"
^touch $foo # This creates a file named `bar`, but it should be `'bar'`
^touch ...[ "'bar'" ] # Same
```
I'll describe how this PR deals with argument handling.
First, we'll introduce the concept of **bare strings**. Bare strings are
**string literals** that are either **unquoted** or **quoted by
backticks** [^1]. Strings within a list literal are NOT considered bare
strings, even if they are unquoted or quoted by backticks.
When a bare string is used as an argument to external process, we need
to perform tilde-expansion, glob-expansion, and inner-quotes-removal, in
that order. "Inner-quotes-removal" means transforming from
`--option="value"` into `--option=value`.
## `.bat` files and CMD built-ins
On Windows, `.bat` files and `.cmd` files are considered executable, but
they need `CMD.exe` as the interpreter. The Rust standard library
supports running `.bat` files directly and will spawn `CMD.exe` under
the hood (see
[documentation](https://doc.rust-lang.org/std/process/index.html#windows-argument-splitting)).
However, other extensions are not supported [^2].
Nushell also supports a selected number of CMD built-ins. The problem
with CMD is that it uses a different set of quoting rules. Correctly
quoting for CMD requires using
[Command::raw_arg()](https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html#tymethod.raw_arg)
and manually quoting CMD special characters, on top of quoting from the
Nushell side. ~~I decided that this is too complex and chose to reject
special characters in CMD built-ins instead [^3]. Hopefully this will
not affact real-world use cases.~~ I've implemented escaping that works
reasonably well.
## `which-support` feature
The `which` crate is now a hard dependency of `nu-command`, making the
`which-support` feature essentially useless. The `which` crate is
already a hard dependency of `nu-cli`, and we should consider removing
the `which-support` feature entirely.
## Appendix
Here's a list of quoting-related issues and PRs in rough chronological
order.
* https://github.com/nushell/nushell/issues/4609
* https://github.com/nushell/nushell/issues/4631
* https://github.com/nushell/nushell/issues/4601
* https://github.com/nushell/nushell/pull/5846
* https://github.com/nushell/nushell/issues/5978
* https://github.com/nushell/nushell/pull/6014
* https://github.com/nushell/nushell/issues/6154
* https://github.com/nushell/nushell/pull/6161
* https://github.com/nushell/nushell/issues/6399
* https://github.com/nushell/nushell/pull/6420
* https://github.com/nushell/nushell/pull/6426
* https://github.com/nushell/nushell/issues/6465
* https://github.com/nushell/nushell/issues/6559
* https://github.com/nushell/nushell/pull/6560
[^1]: The idea that backtick-quoted strings act like bare strings was
introduced by Kubouch and briefly mentioned in [the language
reference](https://www.nushell.sh/lang-guide/chapters/strings_and_text.html#backtick-quotes).
[^2]: The documentation also said "running .bat scripts in this way may
be removed in the future and so should not be relied upon", which is
another reason to move away from this. But again, quoting for CMD is
hard.
[^3]: If anyone wants to try, the best resource I found on the topic is
[this](https://daviddeley.com/autohotkey/parameters/parameters.htm).
2024-05-23 02:05:27 +00:00
|
|
|
assert!(actual
|
|
|
|
.err
|
|
|
|
.contains("Command `MYFOONAME=MYBARVALUE` not found"));
|
2023-03-16 18:05:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 00:20:07 +00:00
|
|
|
#[test]
|
|
|
|
fn execute_binary_in_string() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-04-11 18:18:46 +00:00
|
|
|
let cmd = "nu"
|
|
|
|
^$"($cmd)" --testbin cococo "$0"
|
2021-06-14 00:20:07 +00:00
|
|
|
"#);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "$0");
|
|
|
|
}
|
|
|
|
|
2022-03-18 06:59:28 +00:00
|
|
|
#[test]
|
|
|
|
fn single_quote_dollar_external() {
|
2024-01-05 03:40:56 +00:00
|
|
|
let actual = nu!("let author = 'JT'; nu --testbin cococo $'foo=($author)'");
|
2022-03-18 06:59:28 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo=JT");
|
|
|
|
}
|
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
#[test]
|
|
|
|
fn redirects_custom_command_external() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("def foo [] { nu --testbin cococo foo bar }; foo | str length");
|
IO and redirection overhaul (#11934)
# Description
The PR overhauls how IO redirection is handled, allowing more explicit
and fine-grain control over `stdout` and `stderr` output as well as more
efficient IO and piping.
To summarize the changes in this PR:
- Added a new `IoStream` type to indicate the intended destination for a
pipeline element's `stdout` and `stderr`.
- The `stdout` and `stderr` `IoStream`s are stored in the `Stack` and to
avoid adding 6 additional arguments to every eval function and
`Command::run`. The `stdout` and `stderr` streams can be temporarily
overwritten through functions on `Stack` and these functions will return
a guard that restores the original `stdout` and `stderr` when dropped.
- In the AST, redirections are now directly part of a `PipelineElement`
as a `Option<Redirection>` field instead of having multiple different
`PipelineElement` enum variants for each kind of redirection. This
required changes to the parser, mainly in `lite_parser.rs`.
- `Command`s can also set a `IoStream` override/redirection which will
apply to the previous command in the pipeline. This is used, for
example, in `ignore` to allow the previous external command to have its
stdout redirected to `Stdio::null()` at spawn time. In contrast, the
current implementation has to create an os pipe and manually consume the
output on nushell's side. File and pipe redirections (`o>`, `e>`, `e>|`,
etc.) have precedence over overrides from commands.
This PR improves piping and IO speed, partially addressing #10763. Using
the `throughput` command from that issue, this PR gives the following
speedup on my setup for the commands below:
| Command | Before (MB/s) | After (MB/s) | Bash (MB/s) |
| --------------------------- | -------------:| ------------:|
-----------:|
| `throughput o> /dev/null` | 1169 | 52938 | 54305 |
| `throughput \| ignore` | 840 | 55438 | N/A |
| `throughput \| null` | Error | 53617 | N/A |
| `throughput \| rg 'x'` | 1165 | 3049 | 3736 |
| `(throughput) \| rg 'x'` | 810 | 3085 | 3815 |
(Numbers above are the median samples for throughput)
This PR also paves the way to refactor our `ExternalStream` handling in
the various commands. For example, this PR already fixes the following
code:
```nushell
^sh -c 'echo -n "hello "; sleep 0; echo "world"' | find "hello world"
```
This returns an empty list on 0.90.1 and returns a highlighted "hello
world" on this PR.
Since the `stdout` and `stderr` `IoStream`s are available to commands
when they are run, then this unlocks the potential for more convenient
behavior. E.g., the `find` command can disable its ansi highlighting if
it detects that the output `IoStream` is not the terminal. Knowing the
output streams will also allow background job output to be redirected
more easily and efficiently.
# User-Facing Changes
- External commands returned from closures will be collected (in most
cases):
```nushell
1..2 | each {|_| nu -c "print a" }
```
This gives `["a", "a"]` on this PR, whereas this used to print "a\na\n"
and then return an empty list.
```nushell
1..2 | each {|_| nu -c "print -e a" }
```
This gives `["", ""]` and prints "a\na\n" to stderr, whereas this used
to return an empty list and print "a\na\n" to stderr.
- Trailing new lines are always trimmed for external commands when
piping into internal commands or collecting it as a value. (Failure to
decode the output as utf-8 will keep the trailing newline for the last
binary value.) In the current nushell version, the following three code
snippets differ only in parenthesis placement, but they all also have
different outputs:
1. `1..2 | each { ^echo a }`
```
a
a
╭────────────╮
│ empty list │
╰────────────╯
```
2. `1..2 | each { (^echo a) }`
```
╭───┬───╮
│ 0 │ a │
│ 1 │ a │
╰───┴───╯
```
3. `1..2 | (each { ^echo a })`
```
╭───┬───╮
│ 0 │ a │
│ │ │
│ 1 │ a │
│ │ │
╰───┴───╯
```
But in this PR, the above snippets will all have the same output:
```
╭───┬───╮
│ 0 │ a │
│ 1 │ a │
╰───┴───╯
```
- All existing flags on `run-external` are now deprecated.
- File redirections now apply to all commands inside a code block:
```nushell
(nu -c "print -e a"; nu -c "print -e b") e> test.out
```
This gives "a\nb\n" in `test.out` and prints nothing. The same result
would happen when printing to stdout and using a `o>` file redirection.
- External command output will (almost) never be ignored, and ignoring
output must be explicit now:
```nushell
(^echo a; ^echo b)
```
This prints "a\nb\n", whereas this used to print only "b\n". This only
applies to external commands; values and internal commands not in return
position will not print anything (e.g., `(echo a; echo b)` still only
prints "b").
- `complete` now always captures stderr (`do` is not necessary).
# After Submitting
The language guide and other documentation will need to be updated.
2024-03-14 20:51:55 +00:00
|
|
|
assert_eq!(actual.out, "7");
|
2021-04-21 20:54:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 02:35:59 +00:00
|
|
|
#[test]
|
|
|
|
fn passes_binary_data_between_externals() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/fixtures/formats", "nu --testbin meowb sample.db | nu --testbin relay | hash sha256");
|
2022-03-27 02:35:59 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"2f5050e7eea415c1f3d80b5d93355efd15043ec9157a2bb167a9e73f2ae651f2"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:40:41 +00:00
|
|
|
#[test]
|
|
|
|
fn command_not_found_error_suggests_search_term() {
|
|
|
|
// 'distinct' is not a command, but it is a search term for 'uniq'
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("ls | distinct");
|
2022-08-07 18:40:41 +00:00
|
|
|
assert!(actual.err.contains("uniq"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn command_not_found_error_suggests_typo_fix() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("benchmark { echo 'foo'}");
|
2023-02-11 18:57:48 +00:00
|
|
|
assert!(actual.err.contains("timeit"));
|
2022-08-07 18:40:41 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 12:21:08 +00:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
#[test]
|
|
|
|
fn command_not_found_error_recognizes_non_executable_file() {
|
|
|
|
let actual = nu!("./Cargo.toml");
|
2024-11-15 04:27:26 +00:00
|
|
|
assert!(actual.err.contains(
|
|
|
|
"refers to a file that is not executable. Did you forget to set execute permissions?"
|
|
|
|
));
|
2024-08-12 12:21:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:33:05 +00:00
|
|
|
#[test]
|
2023-08-18 17:45:33 +00:00
|
|
|
fn command_not_found_error_shows_not_found_1() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2023-04-13 17:33:05 +00:00
|
|
|
export extern "foo" [];
|
|
|
|
foo
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
Rewrite run_external.rs (#12921)
This PR is a complete rewrite of `run_external.rs`. The main goal of the
rewrite is improving readability, but it also fixes some bugs related to
argument handling and the PATH variable (fixes
https://github.com/nushell/nushell/issues/6011).
I'll discuss some technical details to make reviewing easier.
## Argument handling
Quoting arguments for external commands is hard. Like, *really* hard.
We've had more than a dozen issues and PRs dedicated to quoting
arguments (see Appendix) but the current implementation is still buggy.
Here's a demonstration of the buggy behavior:
```nu
let foo = "'bar'"
^touch $foo # This creates a file named `bar`, but it should be `'bar'`
^touch ...[ "'bar'" ] # Same
```
I'll describe how this PR deals with argument handling.
First, we'll introduce the concept of **bare strings**. Bare strings are
**string literals** that are either **unquoted** or **quoted by
backticks** [^1]. Strings within a list literal are NOT considered bare
strings, even if they are unquoted or quoted by backticks.
When a bare string is used as an argument to external process, we need
to perform tilde-expansion, glob-expansion, and inner-quotes-removal, in
that order. "Inner-quotes-removal" means transforming from
`--option="value"` into `--option=value`.
## `.bat` files and CMD built-ins
On Windows, `.bat` files and `.cmd` files are considered executable, but
they need `CMD.exe` as the interpreter. The Rust standard library
supports running `.bat` files directly and will spawn `CMD.exe` under
the hood (see
[documentation](https://doc.rust-lang.org/std/process/index.html#windows-argument-splitting)).
However, other extensions are not supported [^2].
Nushell also supports a selected number of CMD built-ins. The problem
with CMD is that it uses a different set of quoting rules. Correctly
quoting for CMD requires using
[Command::raw_arg()](https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html#tymethod.raw_arg)
and manually quoting CMD special characters, on top of quoting from the
Nushell side. ~~I decided that this is too complex and chose to reject
special characters in CMD built-ins instead [^3]. Hopefully this will
not affact real-world use cases.~~ I've implemented escaping that works
reasonably well.
## `which-support` feature
The `which` crate is now a hard dependency of `nu-command`, making the
`which-support` feature essentially useless. The `which` crate is
already a hard dependency of `nu-cli`, and we should consider removing
the `which-support` feature entirely.
## Appendix
Here's a list of quoting-related issues and PRs in rough chronological
order.
* https://github.com/nushell/nushell/issues/4609
* https://github.com/nushell/nushell/issues/4631
* https://github.com/nushell/nushell/issues/4601
* https://github.com/nushell/nushell/pull/5846
* https://github.com/nushell/nushell/issues/5978
* https://github.com/nushell/nushell/pull/6014
* https://github.com/nushell/nushell/issues/6154
* https://github.com/nushell/nushell/pull/6161
* https://github.com/nushell/nushell/issues/6399
* https://github.com/nushell/nushell/pull/6420
* https://github.com/nushell/nushell/pull/6426
* https://github.com/nushell/nushell/issues/6465
* https://github.com/nushell/nushell/issues/6559
* https://github.com/nushell/nushell/pull/6560
[^1]: The idea that backtick-quoted strings act like bare strings was
introduced by Kubouch and briefly mentioned in [the language
reference](https://www.nushell.sh/lang-guide/chapters/strings_and_text.html#backtick-quotes).
[^2]: The documentation also said "running .bat scripts in this way may
be removed in the future and so should not be relied upon", which is
another reason to move away from this. But again, quoting for CMD is
hard.
[^3]: If anyone wants to try, the best resource I found on the topic is
[this](https://daviddeley.com/autohotkey/parameters/parameters.htm).
2024-05-23 02:05:27 +00:00
|
|
|
assert!(actual.err.contains("Command `foo` not found"));
|
2023-04-13 17:33:05 +00:00
|
|
|
}
|
|
|
|
|
Make external command substitution works friendly(like fish shell, trailing ending newlines) (#7156)
# Description
As title, when execute external sub command, auto-trimming end
new-lines, like how fish shell does.
And if the command is executed directly like: `cat tmp`, the result
won't change.
Fixes: #6816
Fixes: #3980
Note that although nushell works correctly by directly replace output of
external command to variable(or other places like string interpolation),
it's not friendly to user, and users almost want to use `str trim` to
trim trailing newline, I think that's why fish shell do this
automatically.
If the pr is ok, as a result, no more `str trim -r` is required when
user is writing scripts which using external commands.
# User-Facing Changes
Before:
<img width="523" alt="img"
src="https://user-images.githubusercontent.com/22256154/202468810-86b04dbb-c147-459a-96a5-e0095eeaab3d.png">
After:
<img width="505" alt="img"
src="https://user-images.githubusercontent.com/22256154/202468599-7b537488-3d6b-458e-9d75-d85780826db0.png">
# 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 --features=extra -- -D warnings -D
clippy::unwrap_used -A clippy::needless_collect` to check that you're
using the standard code style
- `cargo test --workspace --features=extra` to check that all tests pass
# 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.
2022-11-23 03:51:57 +00:00
|
|
|
#[test]
|
|
|
|
fn command_substitution_wont_output_extra_newline() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2024-04-16 11:08:58 +00:00
|
|
|
with-env { FOO: "bar" } { echo $"prefix (nu --testbin echo_env FOO) suffix" }
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
Make external command substitution works friendly(like fish shell, trailing ending newlines) (#7156)
# Description
As title, when execute external sub command, auto-trimming end
new-lines, like how fish shell does.
And if the command is executed directly like: `cat tmp`, the result
won't change.
Fixes: #6816
Fixes: #3980
Note that although nushell works correctly by directly replace output of
external command to variable(or other places like string interpolation),
it's not friendly to user, and users almost want to use `str trim` to
trim trailing newline, I think that's why fish shell do this
automatically.
If the pr is ok, as a result, no more `str trim -r` is required when
user is writing scripts which using external commands.
# User-Facing Changes
Before:
<img width="523" alt="img"
src="https://user-images.githubusercontent.com/22256154/202468810-86b04dbb-c147-459a-96a5-e0095eeaab3d.png">
After:
<img width="505" alt="img"
src="https://user-images.githubusercontent.com/22256154/202468599-7b537488-3d6b-458e-9d75-d85780826db0.png">
# 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 --features=extra -- -D warnings -D
clippy::unwrap_used -A clippy::needless_collect` to check that you're
using the standard code style
- `cargo test --workspace --features=extra` to check that all tests pass
# 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.
2022-11-23 03:51:57 +00:00
|
|
|
assert_eq!(actual.out, "prefix bar suffix");
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2024-04-16 11:08:58 +00:00
|
|
|
with-env { FOO: "bar" } { (nu --testbin echo_env FOO) }
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
Make external command substitution works friendly(like fish shell, trailing ending newlines) (#7156)
# Description
As title, when execute external sub command, auto-trimming end
new-lines, like how fish shell does.
And if the command is executed directly like: `cat tmp`, the result
won't change.
Fixes: #6816
Fixes: #3980
Note that although nushell works correctly by directly replace output of
external command to variable(or other places like string interpolation),
it's not friendly to user, and users almost want to use `str trim` to
trim trailing newline, I think that's why fish shell do this
automatically.
If the pr is ok, as a result, no more `str trim -r` is required when
user is writing scripts which using external commands.
# User-Facing Changes
Before:
<img width="523" alt="img"
src="https://user-images.githubusercontent.com/22256154/202468810-86b04dbb-c147-459a-96a5-e0095eeaab3d.png">
After:
<img width="505" alt="img"
src="https://user-images.githubusercontent.com/22256154/202468599-7b537488-3d6b-458e-9d75-d85780826db0.png">
# 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 --features=extra -- -D warnings -D
clippy::unwrap_used -A clippy::needless_collect` to check that you're
using the standard code style
- `cargo test --workspace --features=extra` to check that all tests pass
# 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.
2022-11-23 03:51:57 +00:00
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
}
|
|
|
|
|
Make pipe redirections consistent, add `err>|` etc. forms (#13334)
# Description
Fixes the lexer to recognize `out>|`, `err>|`, `out+err>|`, etc.
Previously only the short-style forms were recognized, which was
inconsistent with normal file redirections.
I also integrated it all more into the normal lex path by checking `|`
in a special way, which should be more performant and consistent, and
cleans up the code a bunch.
Closes #13331.
# User-Facing Changes
- Adds `out>|` (error), `err>|`, `out+err>|`, `err+out>|` as recognized
forms of the pipe redirection.
# Tests + Formatting
All passing. Added tests for the new forms.
# After Submitting
- [ ] release notes
2024-07-10 23:16:22 +00:00
|
|
|
#[rstest::rstest]
|
|
|
|
#[case("err>|")]
|
|
|
|
#[case("e>|")]
|
|
|
|
fn basic_err_pipe_works(#[case] redirection: &str) {
|
|
|
|
let actual = nu!(
|
|
|
|
r#"with-env { FOO: "bar" } { nu --testbin echo_env_stderr FOO {redirection} str length }"#
|
|
|
|
.replace("{redirection}", redirection)
|
|
|
|
);
|
IO and redirection overhaul (#11934)
# Description
The PR overhauls how IO redirection is handled, allowing more explicit
and fine-grain control over `stdout` and `stderr` output as well as more
efficient IO and piping.
To summarize the changes in this PR:
- Added a new `IoStream` type to indicate the intended destination for a
pipeline element's `stdout` and `stderr`.
- The `stdout` and `stderr` `IoStream`s are stored in the `Stack` and to
avoid adding 6 additional arguments to every eval function and
`Command::run`. The `stdout` and `stderr` streams can be temporarily
overwritten through functions on `Stack` and these functions will return
a guard that restores the original `stdout` and `stderr` when dropped.
- In the AST, redirections are now directly part of a `PipelineElement`
as a `Option<Redirection>` field instead of having multiple different
`PipelineElement` enum variants for each kind of redirection. This
required changes to the parser, mainly in `lite_parser.rs`.
- `Command`s can also set a `IoStream` override/redirection which will
apply to the previous command in the pipeline. This is used, for
example, in `ignore` to allow the previous external command to have its
stdout redirected to `Stdio::null()` at spawn time. In contrast, the
current implementation has to create an os pipe and manually consume the
output on nushell's side. File and pipe redirections (`o>`, `e>`, `e>|`,
etc.) have precedence over overrides from commands.
This PR improves piping and IO speed, partially addressing #10763. Using
the `throughput` command from that issue, this PR gives the following
speedup on my setup for the commands below:
| Command | Before (MB/s) | After (MB/s) | Bash (MB/s) |
| --------------------------- | -------------:| ------------:|
-----------:|
| `throughput o> /dev/null` | 1169 | 52938 | 54305 |
| `throughput \| ignore` | 840 | 55438 | N/A |
| `throughput \| null` | Error | 53617 | N/A |
| `throughput \| rg 'x'` | 1165 | 3049 | 3736 |
| `(throughput) \| rg 'x'` | 810 | 3085 | 3815 |
(Numbers above are the median samples for throughput)
This PR also paves the way to refactor our `ExternalStream` handling in
the various commands. For example, this PR already fixes the following
code:
```nushell
^sh -c 'echo -n "hello "; sleep 0; echo "world"' | find "hello world"
```
This returns an empty list on 0.90.1 and returns a highlighted "hello
world" on this PR.
Since the `stdout` and `stderr` `IoStream`s are available to commands
when they are run, then this unlocks the potential for more convenient
behavior. E.g., the `find` command can disable its ansi highlighting if
it detects that the output `IoStream` is not the terminal. Knowing the
output streams will also allow background job output to be redirected
more easily and efficiently.
# User-Facing Changes
- External commands returned from closures will be collected (in most
cases):
```nushell
1..2 | each {|_| nu -c "print a" }
```
This gives `["a", "a"]` on this PR, whereas this used to print "a\na\n"
and then return an empty list.
```nushell
1..2 | each {|_| nu -c "print -e a" }
```
This gives `["", ""]` and prints "a\na\n" to stderr, whereas this used
to return an empty list and print "a\na\n" to stderr.
- Trailing new lines are always trimmed for external commands when
piping into internal commands or collecting it as a value. (Failure to
decode the output as utf-8 will keep the trailing newline for the last
binary value.) In the current nushell version, the following three code
snippets differ only in parenthesis placement, but they all also have
different outputs:
1. `1..2 | each { ^echo a }`
```
a
a
╭────────────╮
│ empty list │
╰────────────╯
```
2. `1..2 | each { (^echo a) }`
```
╭───┬───╮
│ 0 │ a │
│ 1 │ a │
╰───┴───╯
```
3. `1..2 | (each { ^echo a })`
```
╭───┬───╮
│ 0 │ a │
│ │ │
│ 1 │ a │
│ │ │
╰───┴───╯
```
But in this PR, the above snippets will all have the same output:
```
╭───┬───╮
│ 0 │ a │
│ 1 │ a │
╰───┴───╯
```
- All existing flags on `run-external` are now deprecated.
- File redirections now apply to all commands inside a code block:
```nushell
(nu -c "print -e a"; nu -c "print -e b") e> test.out
```
This gives "a\nb\n" in `test.out` and prints nothing. The same result
would happen when printing to stdout and using a `o>` file redirection.
- External command output will (almost) never be ignored, and ignoring
output must be explicit now:
```nushell
(^echo a; ^echo b)
```
This prints "a\nb\n", whereas this used to print only "b\n". This only
applies to external commands; values and internal commands not in return
position will not print anything (e.g., `(echo a; echo b)` still only
prints "b").
- `complete` now always captures stderr (`do` is not necessary).
# After Submitting
The language guide and other documentation will need to be updated.
2024-03-14 20:51:55 +00:00
|
|
|
assert_eq!(actual.out, "3");
|
2024-02-08 17:30:46 +00:00
|
|
|
}
|
|
|
|
|
Make pipe redirections consistent, add `err>|` etc. forms (#13334)
# Description
Fixes the lexer to recognize `out>|`, `err>|`, `out+err>|`, etc.
Previously only the short-style forms were recognized, which was
inconsistent with normal file redirections.
I also integrated it all more into the normal lex path by checking `|`
in a special way, which should be more performant and consistent, and
cleans up the code a bunch.
Closes #13331.
# User-Facing Changes
- Adds `out>|` (error), `err>|`, `out+err>|`, `err+out>|` as recognized
forms of the pipe redirection.
# Tests + Formatting
All passing. Added tests for the new forms.
# After Submitting
- [ ] release notes
2024-07-10 23:16:22 +00:00
|
|
|
#[rstest::rstest]
|
|
|
|
#[case("out+err>|")]
|
|
|
|
#[case("err+out>|")]
|
|
|
|
#[case("o+e>|")]
|
|
|
|
#[case("e+o>|")]
|
|
|
|
fn basic_outerr_pipe_works(#[case] redirection: &str) {
|
2024-02-08 17:30:46 +00:00
|
|
|
let actual = nu!(
|
Make pipe redirections consistent, add `err>|` etc. forms (#13334)
# Description
Fixes the lexer to recognize `out>|`, `err>|`, `out+err>|`, etc.
Previously only the short-style forms were recognized, which was
inconsistent with normal file redirections.
I also integrated it all more into the normal lex path by checking `|`
in a special way, which should be more performant and consistent, and
cleans up the code a bunch.
Closes #13331.
# User-Facing Changes
- Adds `out>|` (error), `err>|`, `out+err>|`, `err+out>|` as recognized
forms of the pipe redirection.
# Tests + Formatting
All passing. Added tests for the new forms.
# After Submitting
- [ ] release notes
2024-07-10 23:16:22 +00:00
|
|
|
r#"with-env { FOO: "bar" } { nu --testbin echo_env_mixed out-err FOO FOO {redirection} str length }"#
|
|
|
|
.replace("{redirection}", redirection)
|
2024-02-08 17:30:46 +00:00
|
|
|
);
|
IO and redirection overhaul (#11934)
# Description
The PR overhauls how IO redirection is handled, allowing more explicit
and fine-grain control over `stdout` and `stderr` output as well as more
efficient IO and piping.
To summarize the changes in this PR:
- Added a new `IoStream` type to indicate the intended destination for a
pipeline element's `stdout` and `stderr`.
- The `stdout` and `stderr` `IoStream`s are stored in the `Stack` and to
avoid adding 6 additional arguments to every eval function and
`Command::run`. The `stdout` and `stderr` streams can be temporarily
overwritten through functions on `Stack` and these functions will return
a guard that restores the original `stdout` and `stderr` when dropped.
- In the AST, redirections are now directly part of a `PipelineElement`
as a `Option<Redirection>` field instead of having multiple different
`PipelineElement` enum variants for each kind of redirection. This
required changes to the parser, mainly in `lite_parser.rs`.
- `Command`s can also set a `IoStream` override/redirection which will
apply to the previous command in the pipeline. This is used, for
example, in `ignore` to allow the previous external command to have its
stdout redirected to `Stdio::null()` at spawn time. In contrast, the
current implementation has to create an os pipe and manually consume the
output on nushell's side. File and pipe redirections (`o>`, `e>`, `e>|`,
etc.) have precedence over overrides from commands.
This PR improves piping and IO speed, partially addressing #10763. Using
the `throughput` command from that issue, this PR gives the following
speedup on my setup for the commands below:
| Command | Before (MB/s) | After (MB/s) | Bash (MB/s) |
| --------------------------- | -------------:| ------------:|
-----------:|
| `throughput o> /dev/null` | 1169 | 52938 | 54305 |
| `throughput \| ignore` | 840 | 55438 | N/A |
| `throughput \| null` | Error | 53617 | N/A |
| `throughput \| rg 'x'` | 1165 | 3049 | 3736 |
| `(throughput) \| rg 'x'` | 810 | 3085 | 3815 |
(Numbers above are the median samples for throughput)
This PR also paves the way to refactor our `ExternalStream` handling in
the various commands. For example, this PR already fixes the following
code:
```nushell
^sh -c 'echo -n "hello "; sleep 0; echo "world"' | find "hello world"
```
This returns an empty list on 0.90.1 and returns a highlighted "hello
world" on this PR.
Since the `stdout` and `stderr` `IoStream`s are available to commands
when they are run, then this unlocks the potential for more convenient
behavior. E.g., the `find` command can disable its ansi highlighting if
it detects that the output `IoStream` is not the terminal. Knowing the
output streams will also allow background job output to be redirected
more easily and efficiently.
# User-Facing Changes
- External commands returned from closures will be collected (in most
cases):
```nushell
1..2 | each {|_| nu -c "print a" }
```
This gives `["a", "a"]` on this PR, whereas this used to print "a\na\n"
and then return an empty list.
```nushell
1..2 | each {|_| nu -c "print -e a" }
```
This gives `["", ""]` and prints "a\na\n" to stderr, whereas this used
to return an empty list and print "a\na\n" to stderr.
- Trailing new lines are always trimmed for external commands when
piping into internal commands or collecting it as a value. (Failure to
decode the output as utf-8 will keep the trailing newline for the last
binary value.) In the current nushell version, the following three code
snippets differ only in parenthesis placement, but they all also have
different outputs:
1. `1..2 | each { ^echo a }`
```
a
a
╭────────────╮
│ empty list │
╰────────────╯
```
2. `1..2 | each { (^echo a) }`
```
╭───┬───╮
│ 0 │ a │
│ 1 │ a │
╰───┴───╯
```
3. `1..2 | (each { ^echo a })`
```
╭───┬───╮
│ 0 │ a │
│ │ │
│ 1 │ a │
│ │ │
╰───┴───╯
```
But in this PR, the above snippets will all have the same output:
```
╭───┬───╮
│ 0 │ a │
│ 1 │ a │
╰───┴───╯
```
- All existing flags on `run-external` are now deprecated.
- File redirections now apply to all commands inside a code block:
```nushell
(nu -c "print -e a"; nu -c "print -e b") e> test.out
```
This gives "a\nb\n" in `test.out` and prints nothing. The same result
would happen when printing to stdout and using a `o>` file redirection.
- External command output will (almost) never be ignored, and ignoring
output must be explicit now:
```nushell
(^echo a; ^echo b)
```
This prints "a\nb\n", whereas this used to print only "b\n". This only
applies to external commands; values and internal commands not in return
position will not print anything (e.g., `(echo a; echo b)` still only
prints "b").
- `complete` now always captures stderr (`do` is not necessary).
# After Submitting
The language guide and other documentation will need to be updated.
2024-03-14 20:51:55 +00:00
|
|
|
assert_eq!(actual.out, "7");
|
2024-02-08 17:30:46 +00:00
|
|
|
}
|
|
|
|
|
2024-02-28 15:05:09 +00:00
|
|
|
#[test]
|
|
|
|
fn dont_run_glob_if_pass_variable_to_external() {
|
|
|
|
Playground::setup("dont_run_glob", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[
|
2024-02-28 15:05:09 +00:00
|
|
|
EmptyFile("jt_likes_cake.txt"),
|
|
|
|
EmptyFile("andres_likes_arepas.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(cwd: dirs.test(), r#"let f = "*.txt"; nu --testbin nonu $f"#);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "*.txt");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_glob_if_pass_variable_to_external() {
|
|
|
|
Playground::setup("run_glob_on_external", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[
|
2024-02-28 15:05:09 +00:00
|
|
|
EmptyFile("jt_likes_cake.txt"),
|
|
|
|
EmptyFile("andres_likes_arepas.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(cwd: dirs.test(), r#"let f = "*.txt"; nu --testbin nonu ...(glob $f)"#);
|
|
|
|
|
|
|
|
assert!(actual.out.contains("jt_likes_cake.txt"));
|
|
|
|
assert!(actual.out.contains("andres_likes_arepas.txt"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-28 02:38:57 +00:00
|
|
|
#[test]
|
|
|
|
fn subexpression_does_not_implicitly_capture() {
|
|
|
|
let actual = nu!("(nu --testbin cococo); null");
|
|
|
|
assert_eq!(actual.out, "cococo");
|
|
|
|
}
|
|
|
|
|
2020-01-16 09:05:53 +00:00
|
|
|
mod it_evaluation {
|
|
|
|
use super::nu;
|
2020-02-11 23:25:56 +00:00
|
|
|
use nu_test_support::fs::Stub::{EmptyFile, FileWithContent, FileWithContentToBeTrimmed};
|
2020-01-16 09:05:53 +00:00
|
|
|
use nu_test_support::{pipeline, playground::Playground};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn takes_rows_of_nu_value_strings() {
|
|
|
|
Playground::setup("it_argument_test_1", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[
|
2023-03-15 05:54:55 +00:00
|
|
|
EmptyFile("jt_likes_cake.txt"),
|
2020-01-16 09:05:53 +00:00
|
|
|
EmptyFile("andres_likes_arepas.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-01-16 09:05:53 +00:00
|
|
|
ls
|
|
|
|
| sort-by name
|
|
|
|
| get name
|
2022-02-17 11:40:24 +00:00
|
|
|
| each { |it| nu --testbin cococo $it }
|
2022-02-02 20:59:01 +00:00
|
|
|
| get 1
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-01-16 09:05:53 +00:00
|
|
|
));
|
|
|
|
|
2023-03-15 05:54:55 +00:00
|
|
|
assert_eq!(actual.out, "jt_likes_cake.txt");
|
2020-01-16 09:05:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn takes_rows_of_nu_value_lines() {
|
|
|
|
Playground::setup("it_argument_test_2", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[FileWithContentToBeTrimmed(
|
2020-01-16 09:05:53 +00:00
|
|
|
"nu_candies.txt",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-01-16 09:05:53 +00:00
|
|
|
AndrásWithKitKatzz
|
|
|
|
AndrásWithKitKatz
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2020-01-16 09:05:53 +00:00
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-01-16 09:05:53 +00:00
|
|
|
open nu_candies.txt
|
|
|
|
| lines
|
2022-02-17 11:40:24 +00:00
|
|
|
| each { |it| nu --testbin chop $it}
|
2022-02-02 20:59:01 +00:00
|
|
|
| get 1
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-01-16 09:05:53 +00:00
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "AndrásWithKitKat");
|
2020-01-16 09:05:53 +00:00
|
|
|
})
|
|
|
|
}
|
2020-02-11 23:25:56 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-03-10 22:35:15 +00:00
|
|
|
fn can_properly_buffer_lines_externally() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-03-13 21:46:40 +00:00
|
|
|
nu --testbin repeater c 8197 | lines | length
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-03-10 22:35:15 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "1");
|
|
|
|
}
|
|
|
|
#[test]
|
2020-02-11 23:25:56 +00:00
|
|
|
fn supports_fetching_given_a_column_path_to_it() {
|
|
|
|
Playground::setup("it_argument_test_3", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[FileWithContent(
|
2020-02-11 23:25:56 +00:00
|
|
|
"sample.toml",
|
|
|
|
r#"
|
|
|
|
nu_party_venue = "zion"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-02-11 23:25:56 +00:00
|
|
|
open sample.toml
|
2022-02-02 20:59:01 +00:00
|
|
|
| nu --testbin cococo $in.nu_party_venue
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-02-11 23:25:56 +00:00
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "zion");
|
2020-02-11 23:25:56 +00:00
|
|
|
})
|
|
|
|
}
|
2020-01-16 09:05:53 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 15:37:48 +00:00
|
|
|
mod stdin_evaluation {
|
2020-05-07 11:03:43 +00:00
|
|
|
use super::nu;
|
2020-02-10 15:37:48 +00:00
|
|
|
use nu_test_support::pipeline;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_panic_with_no_newline_in_stream() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
|
|
|
r#"
|
2024-05-04 15:00:44 +00:00
|
|
|
nu --testbin nonu "where's the nuline?" | length
|
2020-02-10 15:37:48 +00:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.err, "");
|
2020-02-10 15:37:48 +00:00
|
|
|
}
|
2020-03-01 17:19:09 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_block_indefinitely() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let stdout = nu!(pipeline(
|
|
|
|
"
|
2022-02-02 20:59:01 +00:00
|
|
|
( nu --testbin iecho yes
|
|
|
|
| nu --testbin chop
|
|
|
|
| nu --testbin chop
|
|
|
|
| lines
|
2022-09-28 22:08:17 +00:00
|
|
|
| first )
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-05-07 11:03:43 +00:00
|
|
|
))
|
|
|
|
.out;
|
2020-03-01 17:19:09 +00:00
|
|
|
|
|
|
|
assert_eq!(stdout, "y");
|
|
|
|
}
|
2020-02-10 15:37:48 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 19:14:49 +00:00
|
|
|
mod external_words {
|
|
|
|
use super::nu;
|
2021-06-08 20:59:53 +00:00
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
use nu_test_support::{pipeline, playground::Playground};
|
2024-05-09 23:50:31 +00:00
|
|
|
|
2020-01-24 19:14:49 +00:00
|
|
|
#[test]
|
|
|
|
fn relaxed_external_words() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-05-18 03:52:56 +00:00
|
|
|
nu --testbin cococo joturner@foo.bar.baz
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-01-24 19:14:49 +00:00
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "joturner@foo.bar.baz");
|
2020-01-24 19:14:49 +00:00
|
|
|
}
|
2020-07-02 23:29:28 +00:00
|
|
|
|
2024-05-09 23:50:31 +00:00
|
|
|
#[test]
|
|
|
|
fn raw_string_as_external_argument() {
|
|
|
|
let actual = nu!("nu --testbin cococo r#'asdf'#");
|
|
|
|
assert_eq!(actual.out, "asdf");
|
|
|
|
}
|
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
//FIXME: jt: limitation in testing - can't use single ticks currently
|
|
|
|
#[ignore]
|
2020-07-02 23:29:28 +00:00
|
|
|
#[test]
|
|
|
|
fn no_escaping_for_single_quoted_strings() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2020-07-02 23:29:28 +00:00
|
|
|
nu --testbin cococo 'test "things"'
|
|
|
|
"#);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "test \"things\"");
|
|
|
|
}
|
2021-06-08 20:59:53 +00:00
|
|
|
|
2021-06-28 10:16:03 +00:00
|
|
|
#[rstest::rstest]
|
|
|
|
#[case("sample.toml", r#""sample.toml""#)]
|
|
|
|
#[case("a sample file.toml", r#""a sample file.toml""#)]
|
2022-02-02 20:59:01 +00:00
|
|
|
//FIXME: jt: we don't currently support single ticks in tests
|
|
|
|
//#[case("quote'mark.toml", r#""quote'mark.toml""#)]
|
2021-06-28 10:16:03 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
not(target_os = "windows"),
|
|
|
|
case(r#"quote"mark.toml"#, r#"$"quote(char double_quote)mark.toml""#)
|
|
|
|
)]
|
|
|
|
#[cfg_attr(not(target_os = "windows"), case("?mark.toml", r#""?mark.toml""#))]
|
|
|
|
#[cfg_attr(not(target_os = "windows"), case("*.toml", r#""*.toml""#))]
|
|
|
|
#[cfg_attr(not(target_os = "windows"), case("*.toml", "*.toml"))]
|
|
|
|
#[case("$ sign.toml", r#""$ sign.toml""#)]
|
|
|
|
fn external_arg_with_special_characters(#[case] path: &str, #[case] nu_path_argument: &str) {
|
2021-06-08 20:59:53 +00:00
|
|
|
Playground::setup("external_arg_with_quotes", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[FileWithContent(
|
2021-06-28 10:16:03 +00:00
|
|
|
path,
|
2021-06-08 20:59:53 +00:00
|
|
|
r#"
|
|
|
|
nu_party_venue = "zion"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 17:07:20 +00:00
|
|
|
&format!("
|
2023-01-30 01:37:54 +00:00
|
|
|
nu --testbin meow {nu_path_argument} | from toml | get nu_party_venue
|
2023-07-12 17:07:20 +00:00
|
|
|
")
|
2021-06-08 20:59:53 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "zion");
|
|
|
|
})
|
|
|
|
}
|
2020-01-24 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 06:55:07 +00:00
|
|
|
mod nu_commands {
|
2022-06-20 14:05:11 +00:00
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
|
2020-02-10 06:55:07 +00:00
|
|
|
use super::nu;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn echo_internally_externally() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2024-04-09 22:27:46 +00:00
|
|
|
nu -n -c "echo 'foo'"
|
2020-02-10 06:55:07 +00:00
|
|
|
"#);
|
|
|
|
|
2023-01-09 05:53:52 +00:00
|
|
|
assert_eq!(actual.out, "foo");
|
2020-02-10 06:55:07 +00:00
|
|
|
}
|
2022-04-21 03:31:52 +00:00
|
|
|
|
2022-06-20 14:05:11 +00:00
|
|
|
#[test]
|
|
|
|
fn failed_with_proper_exit_code() {
|
|
|
|
Playground::setup("external failed", |dirs, _sandbox| {
|
|
|
|
let actual = nu!(cwd: dirs.test(), r#"
|
2024-04-09 22:27:46 +00:00
|
|
|
nu -n -c "cargo build | complete | get exit_code"
|
2022-06-20 14:05:11 +00:00
|
|
|
"#);
|
|
|
|
|
|
|
|
// cargo for non rust project's exit code is 101.
|
2023-01-09 05:53:52 +00:00
|
|
|
assert_eq!(actual.out, "101")
|
2022-06-20 14:05:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-21 03:31:52 +00:00
|
|
|
#[test]
|
|
|
|
fn better_arg_quoting() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2024-04-09 22:27:46 +00:00
|
|
|
nu -n -c "\# '"
|
2022-04-21 03:31:52 +00:00
|
|
|
"#);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
}
|
2022-06-06 10:19:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn command_list_arg_test() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2024-04-09 22:27:46 +00:00
|
|
|
nu ...['-n' '-c' 'version']
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-06-06 10:19:06 +00:00
|
|
|
|
|
|
|
assert!(actual.out.contains("version"));
|
|
|
|
assert!(actual.out.contains("rust_version"));
|
|
|
|
assert!(actual.out.contains("rust_channel"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn command_cell_path_arg_test() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2024-04-09 22:27:46 +00:00
|
|
|
nu ...([ '-n' '-c' 'version' ])
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-06-06 10:19:06 +00:00
|
|
|
|
|
|
|
assert!(actual.out.contains("version"));
|
|
|
|
assert!(actual.out.contains("rust_version"));
|
|
|
|
assert!(actual.out.contains("rust_channel"));
|
|
|
|
}
|
2020-02-10 06:55:07 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 16:49:45 +00:00
|
|
|
mod nu_script {
|
|
|
|
use super::nu;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_nu_script() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/fixtures/formats", "
|
2024-04-09 22:27:46 +00:00
|
|
|
nu -n script.nu
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-02-10 16:49:45 +00:00
|
|
|
|
2023-01-09 05:53:52 +00:00
|
|
|
assert_eq!(actual.out, "done");
|
2020-02-10 16:49:45 +00:00
|
|
|
}
|
2020-02-14 05:24:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_nu_script_multiline() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(cwd: "tests/fixtures/formats", "
|
2024-04-09 22:27:46 +00:00
|
|
|
nu -n script_multiline.nu
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-02-14 05:24:18 +00:00
|
|
|
|
2023-01-09 05:53:52 +00:00
|
|
|
assert_eq!(actual.out, "23");
|
2020-02-14 05:24:18 +00:00
|
|
|
}
|
2020-02-10 16:49:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 09:05:53 +00:00
|
|
|
mod tilde_expansion {
|
|
|
|
use super::nu;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn as_home_directory_when_passed_as_argument_and_begins_with_tilde() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-05-18 03:52:56 +00:00
|
|
|
nu --testbin cococo ~
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-01-16 09:05:53 +00:00
|
|
|
|
2021-03-26 08:26:57 +00:00
|
|
|
assert!(!actual.out.contains('~'));
|
2020-01-16 09:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_expand_when_passed_as_argument_and_does_not_start_with_tilde() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2020-05-18 03:52:56 +00:00
|
|
|
nu --testbin cococo "1~1"
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2020-01-16 09:05:53 +00:00
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "1~1");
|
2020-01-16 09:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-22 21:41:34 +00:00
|
|
|
|
|
|
|
mod external_command_arguments {
|
|
|
|
use super::nu;
|
|
|
|
use nu_test_support::fs::Stub::EmptyFile;
|
|
|
|
use nu_test_support::{pipeline, playground::Playground};
|
|
|
|
#[test]
|
|
|
|
fn expands_table_of_primitives_to_positional_arguments() {
|
|
|
|
Playground::setup(
|
|
|
|
"expands_table_of_primitives_to_positional_arguments",
|
|
|
|
|dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[
|
2023-03-15 05:54:55 +00:00
|
|
|
EmptyFile("jt_likes_cake.txt"),
|
2020-07-22 21:41:34 +00:00
|
|
|
EmptyFile("andres_likes_arepas.txt"),
|
|
|
|
EmptyFile("ferris_not_here.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
Allow spreading arguments to commands (#11289)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Finishes implementing https://github.com/nushell/nushell/issues/10598,
which asks for a spread operator in lists, in records, and when calling
commands.
# 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.
-->
This PR will allow spreading arguments to commands (both internal and
external). It will also deprecate spreading arguments automatically when
passing to external commands.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
- Users will be able to use `...` to spread arguments to custom/builtin
commands that have rest parameters or allow unknown arguments, or to any
external command
- If a custom command doesn't have a rest parameter and it doesn't allow
unknown arguments either, the spread operator will not be allowed
- Passing lists to external commands without `...` will work for now but
will cause a deprecation warning saying that it'll stop working in 0.91
(is 2 versions enough time?)
Here's a function to help with demonstrating some behavior:
```nushell
> def foo [ a, b, c?, d?, ...rest ] { [$a $b $c $d $rest] | to nuon }
```
You can pass a list of arguments to fill in the `rest` parameter using
`...`:
```nushell
> foo 1 2 3 4 ...[5 6]
[1, 2, 3, 4, [5, 6]]
```
If you don't use `...`, the list `[5 6]` will be treated as a single
argument:
```nushell
> foo 1 2 3 4 [5 6] # Note the double [[]]
[1, 2, 3, 4, [[5, 6]]]
```
You can omit optional parameters before the spread arguments:
```nushell
> foo 1 2 3 ...[4 5] # d is omitted here
[1, 2, 3, null, [4, 5]]
```
If you have multiple lists, you can spread them all:
```nushell
> foo 1 2 3 ...[4 5] 6 7 ...[8] ...[]
[1, 2, 3, null, [4, 5, 6, 7, 8]]
```
Here's the kind of error you get when you try to spread arguments to a
command with no rest parameter:
![image](https://github.com/nushell/nushell/assets/45539777/93faceae-00eb-4e59-ac3f-17f98436e6e4)
And this is the warning you get when you pass a list to an external now
(without `...`):
![image](https://github.com/nushell/nushell/assets/45539777/d368f590-201e-49fb-8b20-68476ced415e)
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` 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
> toolkit check pr
> ```
-->
Added tests to cover the following cases:
- Spreading arguments to a command that doesn't have a rest parameter
(unexpected spread argument error)
- Spreading arguments to a command that doesn't have a rest parameter
*but* there's also a missing positional argument (missing positional
error)
- Spreading arguments to a command that doesn't have a rest parameter
but does allow unknown arguments, such as `exec` (allowed)
- Spreading a list literal containing arguments of the wrong type (parse
error)
- Spreading a non-list value, both to internal and external commands
- Having named arguments in the middle of rest arguments
- `explain`ing a command call that spreads its arguments
# 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.
-->
# Examples
Suppose you have multiple tables:
```nushell
let people = [[id name age]; [0 alice 100] [1 bob 200] [2 eve 300]]
let evil_twins = [[id name age]; [0 ecila 100] [-1 bob 200] [-2 eve 300]]
```
Maybe you often find yourself needing to merge multiple tables and want
a utility to do that. You could write a function like this:
```nushell
def merge_all [ ...tables ] { $tables | reduce { |it, acc| $acc | merge $it } }
```
Then you can use it like this:
```nushell
> merge_all ...([$people $evil_twins] | each { |$it| $it | select name age })
╭───┬───────┬─────╮
│ # │ name │ age │
├───┼───────┼─────┤
│ 0 │ ecila │ 100 │
│ 1 │ bob │ 200 │
│ 2 │ eve │ 300 │
╰───┴───────┴─────╯
```
Except they had duplicate columns, so now you first want to suffix every
column with a number to tell you which table the column came from. You
can make a command for that:
```nushell
def select_and_merge [ --cols: list<string>, ...tables ] {
let renamed_tables = $tables
| enumerate
| each { |it|
$it.item | select $cols | rename ...($cols | each { |col| $col + ($it.index | into string) })
};
merge_all ...$renamed_tables
}
```
And call it like this:
```nushell
> select_and_merge --cols [name age] $people $evil_twins
╭───┬───────┬──────┬───────┬──────╮
│ # │ name0 │ age0 │ name1 │ age1 │
├───┼───────┼──────┼───────┼──────┤
│ 0 │ alice │ 100 │ ecila │ 100 │
│ 1 │ bob │ 200 │ bob │ 200 │
│ 2 │ eve │ 300 │ eve │ 300 │
╰───┴───────┴──────┴───────┴──────╯
```
---
Suppose someone's made a command to search for APT packages:
```nushell
# The main command
def search-pkgs [
--install # Whether to install any packages it finds
log_level: int # Pretend it's a good idea to make this a required positional parameter
exclude?: list<string> # Packages to exclude
repositories?: list<string> # Which repositories to look in (searches in all if not given)
...pkgs # Package names to search for
] {
{ install: $install, log_level: $log_level, exclude: ($exclude | to nuon), repositories: ($repositories | to nuon), pkgs: ($pkgs | to nuon) }
}
```
It has a lot of parameters to configure it, so you might make your own
helper commands to wrap around it for specific cases. Here's one
example:
```nushell
# Only look for packages locally
def search-pkgs-local [
--install # Whether to install any packages it finds
log_level: int
exclude?: list<string> # Packages to exclude
...pkgs # Package names to search for
] {
# All required and optional positional parameters are given
search-pkgs --install=$install $log_level [] ["<local URI or something>"] ...$pkgs
}
```
And you can run it like this:
```nushell
> search-pkgs-local --install=false 5 ...["python2.7" "vim"]
╭──────────────┬──────────────────────────────╮
│ install │ false │
│ log_level │ 5 │
│ exclude │ [] │
│ repositories │ ["<local URI or something>"] │
│ pkgs │ ["python2.7", vim] │
╰──────────────┴──────────────────────────────╯
```
One thing I realized when writing this was that if we decide to not
allow passing optional arguments using the spread operator, then you can
(mis?)use the spread operator to skip optional parameters. Here, I
didn't want to give `exclude` explicitly, so I used a spread operator to
pass the packages to install. Without it, I would've needed to do
`search-pkgs-local --install=false 5 [] "python2.7" "vim"` (explicitly
pass `[]` (or `null`, in the general case) to `exclude`). There are
probably more idiomatic ways to do this, but I just thought it was
something interesting.
If you're a virologist of the [xkcd](https://xkcd.com/350/) kind,
another helper command you might make is this:
```nushell
# Install any packages it finds
def live-dangerously [ ...pkgs ] {
# One optional argument was given (exclude), while another was not (repositories)
search-pkgs 0 [] ...$pkgs --install # Flags can go after spread arguments
}
```
Running it:
```nushell
> live-dangerously "git" "*vi*" # *vi* because I don't feel like typing out vim and neovim
╭──────────────┬─────────────╮
│ install │ true │
│ log_level │ 0 │
│ exclude │ [] │
│ repositories │ null │
│ pkgs │ [git, *vi*] │
╰──────────────┴─────────────╯
```
Here's an example that uses the spread operator more than once within
the same command call:
```nushell
let extras = [ chrome firefox python java git ]
def search-pkgs-curated [ ...pkgs ] {
(search-pkgs
1
[emacs]
["example.com", "foo.com"]
vim # A must for everyone!
...($pkgs | filter { |p| not ($p | str contains "*") }) # Remove packages with globs
python # Good tool to have
...$extras
--install=false
python3) # I forget, did I already put Python in extras?
}
```
Running it:
```nushell
> search-pkgs-curated "git" "*vi*"
╭──────────────┬───────────────────────────────────────────────────────────────────╮
│ install │ false │
│ log_level │ 1 │
│ exclude │ [emacs] │
│ repositories │ [example.com, foo.com] │
│ pkgs │ [vim, git, python, chrome, firefox, python, java, git, "python3"] │
╰──────────────┴───────────────────────────────────────────────────────────────────╯
```
2023-12-28 07:43:20 +00:00
|
|
|
nu --testbin cococo ...(ls | get name)
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-07-22 21:41:34 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
2023-03-15 05:54:55 +00:00
|
|
|
"andres_likes_arepas.txt ferris_not_here.txt jt_likes_cake.txt"
|
2020-07-22 21:41:34 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2021-06-17 19:59:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proper_subexpression_paths_in_external_args() {
|
|
|
|
Playground::setup(
|
|
|
|
"expands_table_of_primitives_to_positional_arguments",
|
|
|
|
|dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[
|
2023-03-15 05:54:55 +00:00
|
|
|
EmptyFile("jt_likes_cake.txt"),
|
2021-06-17 19:59:58 +00:00
|
|
|
EmptyFile("andres_likes_arepas.txt"),
|
|
|
|
EmptyFile("ferris_not_here.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2021-06-17 19:59:58 +00:00
|
|
|
nu --testbin cococo (ls | sort-by name | get name).1
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2021-06-17 19:59:58 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "ferris_not_here.txt");
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2021-06-25 20:14:54 +00:00
|
|
|
|
2021-07-06 18:14:48 +00:00
|
|
|
#[cfg(not(windows))]
|
2021-06-25 20:14:54 +00:00
|
|
|
#[test]
|
|
|
|
fn string_interpolation_with_an_external_command() {
|
|
|
|
Playground::setup(
|
|
|
|
"string_interpolation_with_an_external_command",
|
|
|
|
|dirs, sandbox| {
|
|
|
|
sandbox.mkdir("cd");
|
|
|
|
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[EmptyFile("cd/jt_likes_cake.txt")]);
|
2021-06-25 20:14:54 +00:00
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
2022-02-02 20:59:01 +00:00
|
|
|
nu --testbin cococo $"(pwd)/cd"
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.out.contains("cd"));
|
2021-06-25 20:14:54 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2021-11-29 15:46:42 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn semicolons_are_sanitized_before_passing_to_subshell() {
|
2024-01-05 03:40:56 +00:00
|
|
|
let actual = nu!("nu --testbin cococo \"a;b\"");
|
2021-11-29 15:46:42 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "a;b");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ampersands_are_sanitized_before_passing_to_subshell() {
|
2024-01-05 03:40:56 +00:00
|
|
|
let actual = nu!("nu --testbin cococo \"a&b\"");
|
2021-11-29 15:46:42 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "a&b");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
#[test]
|
|
|
|
fn subcommands_are_sanitized_before_passing_to_subshell() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("nu --testbin cococo \"$(ls)\"");
|
2021-11-29 15:46:42 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "$(ls)");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
#[test]
|
|
|
|
fn shell_arguments_are_sanitized_even_if_coming_from_other_commands() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("nu --testbin cococo (echo \"a;&$(hello)\")");
|
2021-11-29 15:46:42 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "a;&$(hello)");
|
|
|
|
}
|
2024-06-06 03:03:34 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remove_quotes_in_shell_arguments() {
|
|
|
|
let actual = nu!("nu --testbin cococo expression='-r -w'");
|
|
|
|
assert_eq!(actual.out, "expression=-r -w");
|
|
|
|
let actual = nu!(r#"nu --testbin cococo expression="-r -w""#);
|
|
|
|
assert_eq!(actual.out, "expression=-r -w");
|
|
|
|
let actual = nu!("nu --testbin cococo expression='-r -w'");
|
|
|
|
assert_eq!(actual.out, "expression=-r -w");
|
|
|
|
let actual = nu!(r#"nu --testbin cococo expression="-r\" -w""#);
|
|
|
|
assert_eq!(actual.out, r#"expression=-r" -w"#);
|
|
|
|
let actual = nu!(r#"nu --testbin cococo expression='-r\" -w'"#);
|
|
|
|
assert_eq!(actual.out, r#"expression=-r\" -w"#);
|
|
|
|
}
|
2020-07-22 21:41:34 +00:00
|
|
|
}
|
Error on non-zero exit statuses (#13515)
# Description
This PR makes it so that non-zero exit codes and termination by signal
are treated as a normal `ShellError`. Currently, these are silent
errors. That is, if an external command fails, then it's code block is
aborted, but the parent block can sometimes continue execution. E.g.,
see #8569 and this example:
```nushell
[1 2] | each { ^false }
```
Before this would give:
```
╭───┬──╮
│ 0 │ │
│ 1 │ │
╰───┴──╯
```
Now, this shows an error:
```
Error: nu::shell::eval_block_with_input
× Eval block failed with pipeline input
╭─[entry #1:1:2]
1 │ [1 2] | each { ^false }
· ┬
· ╰── source value
╰────
Error: nu::shell::non_zero_exit_code
× External command had a non-zero exit code
╭─[entry #1:1:17]
1 │ [1 2] | each { ^false }
· ──┬──
· ╰── exited with code 1
╰────
```
This PR fixes #12874, fixes #5960, fixes #10856, and fixes #5347. This
PR also partially addresses #10633 and #10624 (only the last command of
a pipeline is currently checked). It looks like #8569 is already fixed,
but this PR will make sure it is definitely fixed (fixes #8569).
# User-Facing Changes
- Non-zero exit codes and termination by signal now cause an error to be
thrown.
- The error record value passed to a `catch` block may now have an
`exit_code` column containing the integer exit code if the error was due
to an external command.
- Adds new config values, `display_errors.exit_code` and
`display_errors.termination_signal`, which determine whether an error
message should be printed in the respective error cases. For
non-interactive sessions, these are set to `true`, and for interactive
sessions `display_errors.exit_code` is false (via the default config).
# Tests
Added a few tests.
# After Submitting
- Update docs and book.
- Future work:
- Error if other external commands besides the last in a pipeline exit
with a non-zero exit code. Then, deprecate `do -c` since this will be
the default behavior everywhere.
- Add a better mechanism for exit codes and deprecate
`$env.LAST_EXIT_CODE` (it's buggy).
2024-09-07 06:44:26 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exit_code_stops_execution_closure() {
|
|
|
|
let actual = nu!("[1 2] | each {|x| nu -c $'exit ($x)'; print $x }");
|
|
|
|
assert!(actual.out.is_empty());
|
|
|
|
assert!(actual.err.contains("exited with code 1"));
|
|
|
|
}
|
|
|
|
|
2024-10-14 14:57:30 +00:00
|
|
|
// TODO: need to add tests under display_error.exit_code = true
|
Error on non-zero exit statuses (#13515)
# Description
This PR makes it so that non-zero exit codes and termination by signal
are treated as a normal `ShellError`. Currently, these are silent
errors. That is, if an external command fails, then it's code block is
aborted, but the parent block can sometimes continue execution. E.g.,
see #8569 and this example:
```nushell
[1 2] | each { ^false }
```
Before this would give:
```
╭───┬──╮
│ 0 │ │
│ 1 │ │
╰───┴──╯
```
Now, this shows an error:
```
Error: nu::shell::eval_block_with_input
× Eval block failed with pipeline input
╭─[entry #1:1:2]
1 │ [1 2] | each { ^false }
· ┬
· ╰── source value
╰────
Error: nu::shell::non_zero_exit_code
× External command had a non-zero exit code
╭─[entry #1:1:17]
1 │ [1 2] | each { ^false }
· ──┬──
· ╰── exited with code 1
╰────
```
This PR fixes #12874, fixes #5960, fixes #10856, and fixes #5347. This
PR also partially addresses #10633 and #10624 (only the last command of
a pipeline is currently checked). It looks like #8569 is already fixed,
but this PR will make sure it is definitely fixed (fixes #8569).
# User-Facing Changes
- Non-zero exit codes and termination by signal now cause an error to be
thrown.
- The error record value passed to a `catch` block may now have an
`exit_code` column containing the integer exit code if the error was due
to an external command.
- Adds new config values, `display_errors.exit_code` and
`display_errors.termination_signal`, which determine whether an error
message should be printed in the respective error cases. For
non-interactive sessions, these are set to `true`, and for interactive
sessions `display_errors.exit_code` is false (via the default config).
# Tests
Added a few tests.
# After Submitting
- Update docs and book.
- Future work:
- Error if other external commands besides the last in a pipeline exit
with a non-zero exit code. Then, deprecate `do -c` since this will be
the default behavior everywhere.
- Add a better mechanism for exit codes and deprecate
`$env.LAST_EXIT_CODE` (it's buggy).
2024-09-07 06:44:26 +00:00
|
|
|
#[test]
|
|
|
|
fn exit_code_stops_execution_custom_command() {
|
|
|
|
let actual = nu!("def cmd [] { nu -c 'exit 42'; 'ok1' }; cmd; print 'ok2'");
|
|
|
|
assert!(actual.out.is_empty());
|
2024-10-14 14:57:30 +00:00
|
|
|
assert!(!actual.err.contains("exited with code 42"));
|
Error on non-zero exit statuses (#13515)
# Description
This PR makes it so that non-zero exit codes and termination by signal
are treated as a normal `ShellError`. Currently, these are silent
errors. That is, if an external command fails, then it's code block is
aborted, but the parent block can sometimes continue execution. E.g.,
see #8569 and this example:
```nushell
[1 2] | each { ^false }
```
Before this would give:
```
╭───┬──╮
│ 0 │ │
│ 1 │ │
╰───┴──╯
```
Now, this shows an error:
```
Error: nu::shell::eval_block_with_input
× Eval block failed with pipeline input
╭─[entry #1:1:2]
1 │ [1 2] | each { ^false }
· ┬
· ╰── source value
╰────
Error: nu::shell::non_zero_exit_code
× External command had a non-zero exit code
╭─[entry #1:1:17]
1 │ [1 2] | each { ^false }
· ──┬──
· ╰── exited with code 1
╰────
```
This PR fixes #12874, fixes #5960, fixes #10856, and fixes #5347. This
PR also partially addresses #10633 and #10624 (only the last command of
a pipeline is currently checked). It looks like #8569 is already fixed,
but this PR will make sure it is definitely fixed (fixes #8569).
# User-Facing Changes
- Non-zero exit codes and termination by signal now cause an error to be
thrown.
- The error record value passed to a `catch` block may now have an
`exit_code` column containing the integer exit code if the error was due
to an external command.
- Adds new config values, `display_errors.exit_code` and
`display_errors.termination_signal`, which determine whether an error
message should be printed in the respective error cases. For
non-interactive sessions, these are set to `true`, and for interactive
sessions `display_errors.exit_code` is false (via the default config).
# Tests
Added a few tests.
# After Submitting
- Update docs and book.
- Future work:
- Error if other external commands besides the last in a pipeline exit
with a non-zero exit code. Then, deprecate `do -c` since this will be
the default behavior everywhere.
- Add a better mechanism for exit codes and deprecate
`$env.LAST_EXIT_CODE` (it's buggy).
2024-09-07 06:44:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 14:57:30 +00:00
|
|
|
// TODO: need to add tests under display_error.exit_code = true
|
Error on non-zero exit statuses (#13515)
# Description
This PR makes it so that non-zero exit codes and termination by signal
are treated as a normal `ShellError`. Currently, these are silent
errors. That is, if an external command fails, then it's code block is
aborted, but the parent block can sometimes continue execution. E.g.,
see #8569 and this example:
```nushell
[1 2] | each { ^false }
```
Before this would give:
```
╭───┬──╮
│ 0 │ │
│ 1 │ │
╰───┴──╯
```
Now, this shows an error:
```
Error: nu::shell::eval_block_with_input
× Eval block failed with pipeline input
╭─[entry #1:1:2]
1 │ [1 2] | each { ^false }
· ┬
· ╰── source value
╰────
Error: nu::shell::non_zero_exit_code
× External command had a non-zero exit code
╭─[entry #1:1:17]
1 │ [1 2] | each { ^false }
· ──┬──
· ╰── exited with code 1
╰────
```
This PR fixes #12874, fixes #5960, fixes #10856, and fixes #5347. This
PR also partially addresses #10633 and #10624 (only the last command of
a pipeline is currently checked). It looks like #8569 is already fixed,
but this PR will make sure it is definitely fixed (fixes #8569).
# User-Facing Changes
- Non-zero exit codes and termination by signal now cause an error to be
thrown.
- The error record value passed to a `catch` block may now have an
`exit_code` column containing the integer exit code if the error was due
to an external command.
- Adds new config values, `display_errors.exit_code` and
`display_errors.termination_signal`, which determine whether an error
message should be printed in the respective error cases. For
non-interactive sessions, these are set to `true`, and for interactive
sessions `display_errors.exit_code` is false (via the default config).
# Tests
Added a few tests.
# After Submitting
- Update docs and book.
- Future work:
- Error if other external commands besides the last in a pipeline exit
with a non-zero exit code. Then, deprecate `do -c` since this will be
the default behavior everywhere.
- Add a better mechanism for exit codes and deprecate
`$env.LAST_EXIT_CODE` (it's buggy).
2024-09-07 06:44:26 +00:00
|
|
|
#[test]
|
|
|
|
fn exit_code_stops_execution_for_loop() {
|
|
|
|
let actual = nu!("for x in [0 1] { nu -c 'exit 42'; print $x }");
|
|
|
|
assert!(actual.out.is_empty());
|
2024-10-14 14:57:30 +00:00
|
|
|
assert!(!actual.err.contains("exited with code 42"));
|
Error on non-zero exit statuses (#13515)
# Description
This PR makes it so that non-zero exit codes and termination by signal
are treated as a normal `ShellError`. Currently, these are silent
errors. That is, if an external command fails, then it's code block is
aborted, but the parent block can sometimes continue execution. E.g.,
see #8569 and this example:
```nushell
[1 2] | each { ^false }
```
Before this would give:
```
╭───┬──╮
│ 0 │ │
│ 1 │ │
╰───┴──╯
```
Now, this shows an error:
```
Error: nu::shell::eval_block_with_input
× Eval block failed with pipeline input
╭─[entry #1:1:2]
1 │ [1 2] | each { ^false }
· ┬
· ╰── source value
╰────
Error: nu::shell::non_zero_exit_code
× External command had a non-zero exit code
╭─[entry #1:1:17]
1 │ [1 2] | each { ^false }
· ──┬──
· ╰── exited with code 1
╰────
```
This PR fixes #12874, fixes #5960, fixes #10856, and fixes #5347. This
PR also partially addresses #10633 and #10624 (only the last command of
a pipeline is currently checked). It looks like #8569 is already fixed,
but this PR will make sure it is definitely fixed (fixes #8569).
# User-Facing Changes
- Non-zero exit codes and termination by signal now cause an error to be
thrown.
- The error record value passed to a `catch` block may now have an
`exit_code` column containing the integer exit code if the error was due
to an external command.
- Adds new config values, `display_errors.exit_code` and
`display_errors.termination_signal`, which determine whether an error
message should be printed in the respective error cases. For
non-interactive sessions, these are set to `true`, and for interactive
sessions `display_errors.exit_code` is false (via the default config).
# Tests
Added a few tests.
# After Submitting
- Update docs and book.
- Future work:
- Error if other external commands besides the last in a pipeline exit
with a non-zero exit code. Then, deprecate `do -c` since this will be
the default behavior everywhere.
- Add a better mechanism for exit codes and deprecate
`$env.LAST_EXIT_CODE` (it's buggy).
2024-09-07 06:44:26 +00:00
|
|
|
}
|
2024-10-31 15:13:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_dont_run_subcommand_if_surrounded_with_quote() {
|
|
|
|
let actual = nu!("nu --testbin cococo `(echo aa)`");
|
|
|
|
assert_eq!(actual.out, "(echo aa)");
|
|
|
|
let actual = nu!("nu --testbin cococo \"(echo aa)\"");
|
|
|
|
assert_eq!(actual.out, "(echo aa)");
|
|
|
|
let actual = nu!("nu --testbin cococo '(echo aa)'");
|
|
|
|
assert_eq!(actual.out, "(echo aa)");
|
|
|
|
}
|