2022-12-22 14:46:55 +00:00
|
|
|
|
use nu_test_support::{nu, nu_repl_code};
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn filesize_metric_true() {
|
|
|
|
|
let code = &[
|
2023-06-30 19:57:51 +00:00
|
|
|
|
r#"$env.config = { filesize: { metric: true, format:"mb" } }"#,
|
2022-12-22 14:46:55 +00:00
|
|
|
|
r#"20mib | into string"#,
|
|
|
|
|
];
|
2023-07-17 16:43:51 +00:00
|
|
|
|
let actual = nu!(nu_repl_code(code));
|
2022-12-22 14:46:55 +00:00
|
|
|
|
assert_eq!(actual.out, "21.0 MB");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn filesize_metric_false() {
|
|
|
|
|
let code = &[
|
2023-06-30 19:57:51 +00:00
|
|
|
|
r#"$env.config = { filesize: { metric: false, format:"mib" } }"#,
|
2022-12-22 14:46:55 +00:00
|
|
|
|
r#"20mib | into string"#,
|
|
|
|
|
];
|
2023-07-17 16:43:51 +00:00
|
|
|
|
let actual = nu!(nu_repl_code(code));
|
2022-12-22 14:46:55 +00:00
|
|
|
|
assert_eq!(actual.out, "20.0 MiB");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn filesize_metric_overrides_format() {
|
|
|
|
|
let code = &[
|
2023-06-30 19:57:51 +00:00
|
|
|
|
r#"$env.config = { filesize: { metric: false, format:"mb" } }"#,
|
2022-12-22 14:46:55 +00:00
|
|
|
|
r#"20mib | into string"#,
|
|
|
|
|
];
|
2023-07-17 16:43:51 +00:00
|
|
|
|
let actual = nu!(nu_repl_code(code));
|
2022-12-22 14:46:55 +00:00
|
|
|
|
assert_eq!(actual.out, "20.0 MiB");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn filesize_format_auto_metric_true() {
|
|
|
|
|
let code = &[
|
2023-06-30 19:57:51 +00:00
|
|
|
|
r#"$env.config = { filesize: { metric: true, format:"auto" } }"#,
|
2022-12-22 14:46:55 +00:00
|
|
|
|
r#"[2mb 2gb 2tb] | into string | to nuon"#,
|
|
|
|
|
];
|
2023-07-17 16:43:51 +00:00
|
|
|
|
let actual = nu!(nu_repl_code(code));
|
2022-12-22 14:46:55 +00:00
|
|
|
|
assert_eq!(actual.out, r#"["2.0 MB", "2.0 GB", "2.0 TB"]"#);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn filesize_format_auto_metric_false() {
|
|
|
|
|
let code = &[
|
2023-06-30 19:57:51 +00:00
|
|
|
|
r#"$env.config = { filesize: { metric: false, format:"auto" } }"#,
|
2022-12-22 14:46:55 +00:00
|
|
|
|
r#"[2mb 2gb 2tb] | into string | to nuon"#,
|
|
|
|
|
];
|
2023-07-17 16:43:51 +00:00
|
|
|
|
let actual = nu!(nu_repl_code(code));
|
2022-12-22 14:46:55 +00:00
|
|
|
|
assert_eq!(actual.out, r#"["1.9 MiB", "1.9 GiB", "1.8 TiB"]"#);
|
|
|
|
|
}
|
2023-08-27 11:54:15 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn fancy_default_errors() {
|
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
|
|
|
|
let code = nu_repl_code(&[
|
Add `auto` option for `config.use_ansi_coloring` (#14647)
<!--
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!
-->
# 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.
-->
In this PR I continued the idea of #11494, it added an `auto` option to
the ansi coloring config option, I did this too but in a more simple
approach.
So I added a new enum `UseAnsiColoring` with the three values `True`,
`False` and `Auto`. When that value is set to `auto`, the default value,
it will use `std::io::stdout().is_terminal()` to decided whether to use
ansi coloring. This allows to dynamically decide whether to print ansi
color codes or not, [cargo does it the same
way](https://github.com/rust-lang/cargo/blob/652623b779c88fe44afede28bf7f1c9c07812511/src/bin/cargo/main.rs#L72).
`True` and `False` act as overrides to the `is_terminal` check. So with
that PR it is possible to force ansi colors on the `table` command or
automatically remove them from the miette errors if no terminal is used.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Terminal users shouldn't be affected by this change as the default value
was `true` and `is_terminal` returns for terminals `true` (duh).
Non-terminal users, that use `nu` in some embedded way or the engine
implemented in some other way (like my jupyter kernel) will now have by
default no ansi coloring and need to enable it manually if their
environment allows it.
# 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 toolkit.nu; toolkit test stdlib"` 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
> ```
-->
The test for fancy errors expected ansi codes, since tests aren't run
"in terminal", the ansi codes got stripped away.
I added a line that forced ansi colors above it. I'm not sure if that
should be the case or if we should test against no ansi colors.
- :green_circle: `toolkit fmt`
- :green_circle: `toolkit clippy`
- :green_circle: `toolkit test`
- :green_circle: `toolkit test stdlib`
# 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.
-->
This should resolve #11464 and partially #11847. This also closes
#11494.
2024-12-26 17:00:01 +00:00
|
|
|
|
"$env.config.use_ansi_coloring = true",
|
2023-08-27 11:54:15 +00:00
|
|
|
|
r#"def force_error [x] {
|
|
|
|
|
error make {
|
|
|
|
|
msg: "oh no!"
|
|
|
|
|
label: {
|
|
|
|
|
text: "here's the error"
|
2023-11-03 15:09:33 +00:00
|
|
|
|
span: (metadata $x).span
|
2023-08-27 11:54:15 +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
|
|
|
|
r#"force_error "My error""#,
|
|
|
|
|
]);
|
|
|
|
|
|
2024-09-10 02:44:04 +00:00
|
|
|
|
let actual = nu!(format!("try {{ {code} }}"));
|
2023-08-27 11:54:15 +00:00
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
actual.err,
|
Add `auto` option for `config.use_ansi_coloring` (#14647)
<!--
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!
-->
# 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.
-->
In this PR I continued the idea of #11494, it added an `auto` option to
the ansi coloring config option, I did this too but in a more simple
approach.
So I added a new enum `UseAnsiColoring` with the three values `True`,
`False` and `Auto`. When that value is set to `auto`, the default value,
it will use `std::io::stdout().is_terminal()` to decided whether to use
ansi coloring. This allows to dynamically decide whether to print ansi
color codes or not, [cargo does it the same
way](https://github.com/rust-lang/cargo/blob/652623b779c88fe44afede28bf7f1c9c07812511/src/bin/cargo/main.rs#L72).
`True` and `False` act as overrides to the `is_terminal` check. So with
that PR it is possible to force ansi colors on the `table` command or
automatically remove them from the miette errors if no terminal is used.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Terminal users shouldn't be affected by this change as the default value
was `true` and `is_terminal` returns for terminals `true` (duh).
Non-terminal users, that use `nu` in some embedded way or the engine
implemented in some other way (like my jupyter kernel) will now have by
default no ansi coloring and need to enable it manually if their
environment allows it.
# 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 toolkit.nu; toolkit test stdlib"` 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
> ```
-->
The test for fancy errors expected ansi codes, since tests aren't run
"in terminal", the ansi codes got stripped away.
I added a line that forced ansi colors above it. I'm not sure if that
should be the case or if we should test against no ansi colors.
- :green_circle: `toolkit fmt`
- :green_circle: `toolkit clippy`
- :green_circle: `toolkit test`
- :green_circle: `toolkit test stdlib`
# 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.
-->
This should resolve #11464 and partially #11847. This also closes
#11494.
2024-12-26 17:00:01 +00:00
|
|
|
|
"Error: \n \u{1b}[31m×\u{1b}[0m oh no!\n ╭─[\u{1b}[36;1;4mline2:1:13\u{1b}[0m]\n \u{1b}[2m1\u{1b}[0m │ force_error \"My error\"\n · \u{1b}[35;1m ─────┬────\u{1b}[0m\n · \u{1b}[35;1m╰── \u{1b}[35;1mhere's the error\u{1b}[0m\u{1b}[0m\n ╰────\n\n"
|
2023-08-27 11:54:15 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn narratable_errors() {
|
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
|
|
|
|
let code = nu_repl_code(&[
|
2023-08-27 11:54:15 +00:00
|
|
|
|
r#"$env.config = { error_style: "plain" }"#,
|
|
|
|
|
r#"def force_error [x] {
|
|
|
|
|
error make {
|
|
|
|
|
msg: "oh no!"
|
|
|
|
|
label: {
|
|
|
|
|
text: "here's the error"
|
2023-11-03 15:09:33 +00:00
|
|
|
|
span: (metadata $x).span
|
2023-08-27 11:54:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}"#,
|
|
|
|
|
r#"force_error "my error""#,
|
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-09-10 02:44:04 +00:00
|
|
|
|
let actual = nu!(format!("try {{ {code} }}"));
|
2023-08-27 11:54:15 +00:00
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
actual.err,
|
|
|
|
|
r#"Error: oh no!
|
|
|
|
|
Diagnostic severity: error
|
|
|
|
|
Begin snippet for line2 starting at line 1, column 1
|
|
|
|
|
|
|
|
|
|
snippet line 1: force_error "my error"
|
|
|
|
|
label at line 1, columns 13 to 22: here's the error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"#,
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-01-15 08:59:47 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn plugins() {
|
|
|
|
|
let code = &[
|
|
|
|
|
r#"$env.config = { plugins: { nu_plugin_config: { key: value } } }"#,
|
|
|
|
|
r#"$env.config.plugins"#,
|
|
|
|
|
];
|
|
|
|
|
let actual = nu!(nu_repl_code(code));
|
|
|
|
|
assert_eq!(actual.out, r#"{nu_plugin_config: {key: value}}"#);
|
|
|
|
|
}
|