Add const support for all overlay commands (#7720)

This commit is contained in:
Jakub Žádník 2023-01-12 00:18:06 +02:00 committed by GitHub
parent 1ae9157985
commit 57cf805e12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 20 deletions

View File

@ -2315,16 +2315,16 @@ pub fn parse_overlay_new(
};
let (overlay_name, _) = if let Some(expr) = call.positional_nth(0) {
if let Some(s) = expr.as_string() {
(s, expr.span)
} else {
return (
garbage_pipeline(spans),
Some(ParseError::UnknownState(
"internal error: Module name not a string".into(),
expr.span,
)),
);
match eval_constant(working_set, expr) {
Ok(val) => match value_as_string(val, expr.span) {
Ok(s) => (s, expr.span),
Err(err) => {
return (garbage_pipeline(spans), Some(err));
}
},
Err(err) => {
return (garbage_pipeline(spans), Some(err));
}
}
} else {
return (
@ -2721,16 +2721,16 @@ pub fn parse_overlay_hide(
};
let (overlay_name, overlay_name_span) = if let Some(expr) = call.positional_nth(0) {
if let Some(s) = expr.as_string() {
(s, expr.span)
} else {
return (
garbage_pipeline(spans),
Some(ParseError::UnknownState(
"internal error: Module name not a string".into(),
expr.span,
)),
);
match eval_constant(working_set, expr) {
Ok(val) => match value_as_string(val, expr.span) {
Ok(s) => (s, expr.span),
Err(err) => {
return (garbage_pipeline(spans), Some(err));
}
},
Err(err) => {
return (garbage_pipeline(spans), Some(err));
}
}
} else {
(

View File

@ -196,6 +196,33 @@ fn add_overlay_from_const_module_name_decl() {
assert_eq!(actual.out, "foo");
}
#[test]
fn new_overlay_from_const_name() {
let inp = &[
r#"const mod = 'spam'"#,
r#"overlay new $mod"#,
r#"overlay list | last"#,
];
let actual = nu!(cwd: "tests/overlays", pipeline(&inp.join("; ")));
assert_eq!(actual.out, "spam");
}
#[test]
fn hide_overlay_from_const_name() {
let inp = &[
r#"const mod = 'spam'"#,
r#"overlay new $mod"#,
r#"overlay hide $mod"#,
r#"overlay list | str join ' '"#,
];
let actual = nu!(cwd: "tests/overlays", pipeline(&inp.join("; ")));
assert!(!actual.out.contains("spam"));
}
// This one tests that the `nu_repl()` loop works correctly
#[test]
fn add_overlay_from_file_decl_cd() {