nushell/crates/nu-command/tests/commands/error_make.rs
Antoine Stevan 05ff7a9925
FIX: do not allow *start > end* in error make spans (#8570)
This should close #8567.

# Description
this PR throws an error when `start > end` in the most complete branch
of `ErrorMake::run`, i.e. when `$.msg`, `$.label.text`, `$.label.start`
and `$.label.end` are defined.

i've also added a `error_start_bigger_than_end_should_fail` test to
check that it does indeed return the right error.

# User-Facing Changes
no more crash when manipulating span bounds and a clear error, e.g.
```bash
>_ error make {msg: "msg" label: {text: "text" start: 1010 end: 1000}}
Error:
  × invalid error format.
   ╭─[entry #3:1:1]
 1 │ error make {msg: "msg" label: {text: "text" start: 1010 end: 1000}}
   ·                               ──────────────────┬─────────────────
   ·                                                 ╰── `$.label.start` is stricly bigger than `$.label.end`
   ╰────
  help: 1010 > 1000
```
or
```bash
>_ error make {
:::     msg: "msg"
:::     label: {
:::         text: "text"
:::         start: ($nu.scope.engine_state.source_bytes - 90)
:::         end: ($nu.scope.engine_state.source_bytes - 100)
:::     }
::: }
Error:
  × invalid error format.
   ╭─[entry #4:2:1]
 2 │         msg: "msg"
 3 │ ╭─▶     label: {
 4 │ │           text: "text"
 5 │ │           start: ($nu.scope.engine_state.source_bytes - 90)
 6 │ │           end: ($nu.scope.engine_state.source_bytes - 100)
 7 │ ├─▶     }
   · ╰──── `$.label.start` is stricly bigger than `$.label.end`
 8 │     }
   ╰────
  help: 204525 > 204515
```

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🔴 `toolkit test`

# After Submitting
```
$nothing
```
2023-03-23 20:31:06 +01:00

42 lines
888 B
Rust

use nu_test_support::{nu, pipeline};
#[test]
fn error_label_works() {
let actual = nu!(
cwd: ".", pipeline(
r#"
error make {msg:foo label:{text:unseen}}
"#
));
assert!(actual.err.contains("unseen"));
assert!(actual.err.contains("╰──"));
}
#[test]
fn no_span_if_unspanned() {
let actual = nu!(
cwd: ".", pipeline(
r#"
error make -u {msg:foo label:{text:unseen}}
"#
));
assert!(!actual.err.contains("unseen"));
}
#[test]
fn error_start_bigger_than_end_should_fail() {
let actual = nu!(
cwd: ".", pipeline(
r#"
error make {msg: foo label: {text: bar start 456 end 123}}
"#
));
assert!(!actual.err.contains("invalid error format"));
assert!(!actual
.err
.contains("`$.label.start` should be smaller than `$.label.end`"));
}