avoid panic when using from nuon (#7533)

# Description

Fixes #5996 

Just found a relative easy way to fix the issue

# User-Facing Changes

```
❯ open $nu.plugin-path | from nuon
Error:
  × error when loading nuon text
   ╭─[entry #36:1:1]
 1 │ open $nu.plugin-path | from nuon
   ·                        ────┬────
   ·                            ╰── could not load nuon text
   ╰────

Error:
  × Error when loading


❯ open $nu.config-path | from nuon
Error:
  × error when loading nuon text
   ╭─[entry #37:1:1]
 1 │ open $nu.config-path | from nuon
   ·                        ────┬────
   ·                            ╰── could not load nuon text
   ╰────

Error:
  × error when loading
```

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` 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.
This commit is contained in:
WindSoilder 2022-12-22 08:42:23 +08:00 committed by GitHub
parent ec08e4bc6d
commit 046e46b962
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 11 deletions

View file

@ -56,7 +56,7 @@ impl Command for FromNuon {
fn run(
&self,
_engine_state: &EngineState,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
@ -64,19 +64,12 @@ impl Command for FromNuon {
let head = call.head;
let (string_input, metadata) = input.collect_string_strict(head)?;
let engine_state = EngineState::new();
let engine_state = engine_state.clone();
let mut working_set = StateWorkingSet::new(&engine_state);
let _ = working_set.add_file("nuon file".to_string(), string_input.as_bytes());
let mut error = None;
// Most of the 'work' in converting from Nuon is simply pushing it through the Nu parser.
let (lexed, err) = nu_parser::lex(string_input.as_bytes(), 0, &[b'\n', b'\r'], &[], true);
error = error.or(err);
let (mut block, err) = nu_parser::parse_block(&mut working_set, &lexed, true, &[], false);
let (mut block, err) =
nu_parser::parse(&mut working_set, None, string_input.as_bytes(), false, &[]);
error = error.or(err);
if let Some(pipeline) = block.pipelines.get(1) {

View file

@ -340,6 +340,14 @@ fn quotes_some_strings_necessarily() {
assert_eq!(actual.out, "list<string>");
}
#[test]
fn read_code_should_fail_rather_than_panic() {
let actual = nu!(cwd: "tests/fixtures/formats", pipeline(
r#"open code.nu | from nuon"#
));
assert!(actual.err.contains("error when parsing"))
}
proptest! {
#[test]
fn to_nuon_from_nuon(c: char) {

1
tests/fixtures/formats/code.nu vendored Normal file
View file

@ -0,0 +1 @@
register