From 046e46b962b6538b3b4b25f846a6dee48f65c1f7 Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Thu, 22 Dec 2022 08:42:23 +0800 Subject: [PATCH] avoid panic when using from nuon (#7533) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 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. --- crates/nu-command/src/formats/from/nuon.rs | 15 ++++----------- .../nu-command/tests/format_conversions/nuon.rs | 8 ++++++++ tests/fixtures/formats/code.nu | 1 + 3 files changed, 13 insertions(+), 11 deletions(-) create mode 100644 tests/fixtures/formats/code.nu diff --git a/crates/nu-command/src/formats/from/nuon.rs b/crates/nu-command/src/formats/from/nuon.rs index 8e20f92668..68b2c016a9 100644 --- a/crates/nu-command/src/formats/from/nuon.rs +++ b/crates/nu-command/src/formats/from/nuon.rs @@ -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) { diff --git a/crates/nu-command/tests/format_conversions/nuon.rs b/crates/nu-command/tests/format_conversions/nuon.rs index 29a381ab3b..8226a3f0f2 100644 --- a/crates/nu-command/tests/format_conversions/nuon.rs +++ b/crates/nu-command/tests/format_conversions/nuon.rs @@ -340,6 +340,14 @@ fn quotes_some_strings_necessarily() { assert_eq!(actual.out, "list"); } +#[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) { diff --git a/tests/fixtures/formats/code.nu b/tests/fixtures/formats/code.nu new file mode 100644 index 0000000000..26263cf31e --- /dev/null +++ b/tests/fixtures/formats/code.nu @@ -0,0 +1 @@ +register \ No newline at end of file