Skip to content

Commit

Permalink
avoid panic when using from nuon (nushell#7533)
Browse files Browse the repository at this point in the history
# Description

Fixes nushell#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 nushell#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 nushell#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.
  • Loading branch information
WindSoilder authored Dec 22, 2022
1 parent ec08e4b commit 046e46b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
15 changes: 4 additions & 11 deletions crates/nu-command/src/formats/from/nuon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,20 @@ impl Command for FromNuon {

fn run(
&self,
_engine_state: &EngineState,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
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) {
Expand Down
8 changes: 8 additions & 0 deletions crates/nu-command/tests/format_conversions/nuon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/formats/code.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
register

0 comments on commit 046e46b

Please sign in to comment.