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

View File

@ -196,6 +196,33 @@ fn add_overlay_from_const_module_name_decl() {
assert_eq!(actual.out, "foo"); 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 // This one tests that the `nu_repl()` loop works correctly
#[test] #[test]
fn add_overlay_from_file_decl_cd() { fn add_overlay_from_file_decl_cd() {