Fix to nuon --serialize of closure (#15357)

# Description
Closes #15351

Adds quotes that were missed in #14698 with the proper escaping.


# User-Facing Changes
`to nuon --serialize` will now produce a quoted string instead of
illegal nuon when given a closure

# Tests + Formatting
Reenable the `to nuon` rejection of closures in the base state test.
Added test for quoting.
This commit is contained in:
Stefan Holderbach 2025-03-20 17:50:36 +01:00 committed by GitHub
parent 2ea2a904e8
commit 7a6cfa24fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 4 deletions

View File

@ -307,7 +307,6 @@ fn from_nuon_datetime() {
}
#[test]
#[ignore]
fn to_nuon_errs_on_closure() {
let actual = nu!(pipeline(
r#"
@ -316,7 +315,19 @@ fn to_nuon_errs_on_closure() {
"#
));
assert!(actual.err.contains("can't convert closure to NUON"));
assert!(actual.err.contains("not deserializable"));
}
#[test]
fn to_nuon_closure_coerced_to_quoted_string() {
let actual = nu!(pipeline(
r#"
{|| to nuon}
| to nuon --serialize
"#
));
assert_eq!(actual.out, "\"{|| to nuon}\"");
}
#[test]

View File

@ -99,7 +99,9 @@ fn value_to_string(
}
Value::Closure { val, .. } => {
if serialize_types {
Ok(val.coerce_into_string(engine_state, span)?.to_string())
Ok(escape_quote_string(
&val.coerce_into_string(engine_state, span)?,
))
} else {
Err(ShellError::UnsupportedInput {
msg: "closures are currently not deserializable (use --serialize to serialize as a string)".into(),

View File

@ -108,7 +108,7 @@ fn literal_closure() {
#[test]
fn literal_closure_to_nuon() {
test_eval("{||} | to nuon --serialize", Eq("{||}"))
test_eval("{||} | to nuon --serialize", Eq("\"{||}\""))
}
#[test]