2022-08-31 22:32:56 +02:00
|
|
|
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
|
|
|
use nu_test_support::playground::Playground;
|
2023-07-12 19:07:20 +02:00
|
|
|
use nu_test_support::{nu, nu_repl_code};
|
2023-04-08 20:52:37 +02:00
|
|
|
use pretty_assertions::assert_eq;
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_overlay() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"foo",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "foo");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 09:38:21 +02:00
|
|
|
#[test]
|
|
|
|
fn add_overlay_as_new_name() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam as spam_new",
|
|
|
|
"foo",
|
2022-09-13 09:38:21 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-09-13 09:38:21 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
2022-08-12 20:06:51 +02:00
|
|
|
#[test]
|
|
|
|
fn add_overlay_twice() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"overlay use spam",
|
|
|
|
"foo",
|
2022-08-12 20:06:51 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-12 20:06:51 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_prefixed_overlay() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use --prefix spam",
|
|
|
|
"spam foo",
|
2022-08-12 20:06:51 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-12 20:06:51 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_prefixed_overlay_twice() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use --prefix spam",
|
|
|
|
"overlay use --prefix spam",
|
|
|
|
"spam foo",
|
2022-08-12 20:06:51 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-12 20:06:51 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_prefixed_overlay_mismatch_1() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use --prefix spam",
|
|
|
|
"overlay use spam",
|
2022-08-12 20:06:51 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-12 20:06:51 +02:00
|
|
|
|
|
|
|
assert!(actual.err.contains("exists with a prefix"));
|
|
|
|
// Why doesn't the REPL test work with the previous expected output
|
|
|
|
assert!(actual_repl.err.contains("overlay_prefix_mismatch"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_prefixed_overlay_mismatch_2() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"overlay use --prefix spam",
|
2022-08-12 20:06:51 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-12 20:06:51 +02:00
|
|
|
|
|
|
|
assert!(actual.err.contains("exists without a prefix"));
|
|
|
|
// Why doesn't the REPL test work with the previous expected output
|
|
|
|
assert!(actual_repl.err.contains("overlay_prefix_mismatch"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prefixed_overlay_keeps_custom_decl() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use --prefix spam",
|
2022-08-12 20:06:51 +02:00
|
|
|
r#"def bar [] { "bar" }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay hide --keep-custom spam",
|
|
|
|
"bar",
|
2022-08-12 20:06:51 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-12 20:06:51 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
assert_eq!(actual_repl.out, "bar");
|
|
|
|
}
|
|
|
|
|
2022-05-07 21:39:22 +02:00
|
|
|
#[test]
|
|
|
|
fn add_overlay_env() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"module spam { export-env { $env.FOO = "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"$env.FOO",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "foo");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
2022-08-12 20:06:51 +02:00
|
|
|
#[test]
|
|
|
|
fn add_prefixed_overlay_env_no_prefix() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"module spam { export-env { $env.FOO = "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use --prefix spam",
|
|
|
|
"$env.FOO",
|
2022-08-12 20:06:51 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-12 20:06:51 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
2022-05-07 21:39:22 +02:00
|
|
|
#[test]
|
|
|
|
fn add_overlay_from_file_decl() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["overlay use samples/spam.nu", "foo"];
|
2022-05-08 15:09:39 +02:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
2022-12-21 23:21:03 +01:00
|
|
|
#[test]
|
|
|
|
fn add_overlay_from_const_file_decl() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["const file = 'samples/spam.nu'", "overlay use $file", "foo"];
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_overlay_from_const_module_name_decl() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"const mod = 'spam'",
|
|
|
|
"overlay use $mod",
|
|
|
|
"foo",
|
2022-12-21 23:21:03 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
}
|
|
|
|
|
2023-01-11 23:18:06 +01:00
|
|
|
#[test]
|
|
|
|
fn new_overlay_from_const_name() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"const mod = 'spam'",
|
|
|
|
"overlay new $mod",
|
|
|
|
"overlay list | last",
|
2023-01-11 23:18:06 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-11 23:18:06 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hide_overlay_from_const_name() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"const mod = 'spam'",
|
|
|
|
"overlay new $mod",
|
|
|
|
"overlay hide $mod",
|
|
|
|
"overlay list | str join ' '",
|
2023-01-11 23:18:06 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-11 23:18:06 +01:00
|
|
|
|
|
|
|
assert!(!actual.out.contains("spam"));
|
|
|
|
}
|
|
|
|
|
2022-05-08 15:09:39 +02:00
|
|
|
// This one tests that the `nu_repl()` loop works correctly
|
|
|
|
#[test]
|
|
|
|
fn add_overlay_from_file_decl_cd() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["cd samples", "overlay use spam.nu", "foo"];
|
2022-05-08 15:09:39 +02:00
|
|
|
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
2022-05-08 15:09:39 +02:00
|
|
|
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_overlay_from_file_alias() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["overlay use samples/spam.nu", "bar"];
|
2022-05-08 15:09:39 +02:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "bar");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "bar");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_overlay_from_file_env() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["overlay use samples/spam.nu", "$env.BAZ"];
|
2022-05-08 15:09:39 +02:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "baz");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "baz");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_overlay_scoped() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"do { overlay use spam }",
|
|
|
|
"foo",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-08 15:09:39 +02:00
|
|
|
|
|
|
|
assert!(!actual.err.is_empty());
|
|
|
|
#[cfg(windows)]
|
2023-01-15 03:03:32 +01:00
|
|
|
assert_ne!(actual_repl.out, "foo");
|
2022-05-08 15:09:39 +02:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
assert!(!actual_repl.err.is_empty());
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_overlay_from_module() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
2022-05-08 15:09:39 +02:00
|
|
|
r#"module spam { export def foo [] { "bar" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"foo",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "bar");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "bar");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_overlay_from_module_env() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"module spam { export-env { $env.FOO = "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"module spam { export-env { $env.FOO = "bar" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"$env.FOO",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "bar");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "bar");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 17:36:42 +02:00
|
|
|
#[test]
|
|
|
|
fn overlay_use_do_not_eval_twice() {
|
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"module spam { export-env { $env.FOO = "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"$env.FOO = "bar""#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay hide spam",
|
|
|
|
"overlay use spam",
|
|
|
|
"$env.FOO",
|
2022-09-04 17:36:42 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-09-04 17:36:42 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
assert_eq!(actual_repl.out, "bar");
|
|
|
|
}
|
|
|
|
|
2022-05-07 21:39:22 +02:00
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"overlay hide spam",
|
|
|
|
"foo",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert!(!actual.err.is_empty());
|
2022-05-08 15:09:39 +02:00
|
|
|
#[cfg(windows)]
|
2023-01-15 03:03:32 +01:00
|
|
|
assert_ne!(actual_repl.out, "foo");
|
2022-05-08 15:09:39 +02:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
assert!(!actual_repl.err.is_empty());
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_last_overlay() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"overlay hide",
|
|
|
|
"foo",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert!(!actual.err.is_empty());
|
2022-05-08 15:09:39 +02:00
|
|
|
#[cfg(windows)]
|
2023-01-15 03:03:32 +01:00
|
|
|
assert_ne!(actual_repl.out, "foo");
|
2022-05-08 15:09:39 +02:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
assert!(!actual_repl.err.is_empty());
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_scoped() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"do { overlay hide spam }",
|
|
|
|
"foo",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "foo");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_env() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"module spam { export-env { $env.FOO = "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"overlay hide spam",
|
|
|
|
"$env.FOO",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
2022-12-10 18:23:34 +01:00
|
|
|
assert!(actual.err.contains("not_found"));
|
|
|
|
assert!(actual_repl.err.contains("not_found"));
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_scoped_env() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
2023-06-30 21:57:51 +02:00
|
|
|
r#"module spam { export-env { $env.FOO = "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"do { overlay hide spam }",
|
|
|
|
"$env.FOO",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "foo");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_default_overlay() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["overlay list | last"];
|
2022-05-08 15:09:39 +02:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "zero");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "zero");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_last_overlay() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"overlay list | last",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "spam");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "spam");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_overlay_scoped() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"do { overlay list | last }",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "spam");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "spam");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_discard_decl() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
2022-05-08 15:09:39 +02:00
|
|
|
r#"def bagr [] { "bagr" }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay hide spam",
|
|
|
|
"bagr",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert!(!actual.err.is_empty());
|
2022-05-08 15:09:39 +02:00
|
|
|
#[cfg(windows)]
|
2023-01-15 03:03:32 +01:00
|
|
|
assert_ne!(actual_repl.out, "bagr");
|
2022-05-08 15:09:39 +02:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
assert!(!actual_repl.err.is_empty());
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_discard_alias() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# 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.
2023-02-27 08:44:05 +01:00
|
|
|
r#"alias bagr = echo "bagr""#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay hide spam",
|
|
|
|
"bagr",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert!(!actual.err.is_empty());
|
2022-05-08 15:09:39 +02:00
|
|
|
#[cfg(windows)]
|
2023-01-15 03:03:32 +01:00
|
|
|
assert_ne!(actual_repl.out, "bagr");
|
2022-05-08 15:09:39 +02:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
assert!(!actual_repl.err.is_empty());
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_discard_env() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
|
|
|
"$env.BAGR = 'bagr'",
|
|
|
|
"overlay hide spam",
|
|
|
|
"$env.BAGR",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
2022-12-10 18:23:34 +01:00
|
|
|
assert!(actual.err.contains("not_found"));
|
|
|
|
assert!(actual_repl.err.contains("not_found"));
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
2022-05-24 23:22:17 +02:00
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_keep_decl() {
|
2022-05-24 23:22:17 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
2022-05-24 23:22:17 +02:00
|
|
|
r#"def bagr [] { "bagr" }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay hide --keep-custom spam",
|
|
|
|
"bagr",
|
2022-05-24 23:22:17 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
2022-05-24 23:22:17 +02:00
|
|
|
|
|
|
|
assert!(actual.out.contains("bagr"));
|
|
|
|
assert!(actual_repl.out.contains("bagr"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_keep_alias() {
|
2022-05-24 23:22:17 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
|
|
|
"alias bagr = echo 'bagr'",
|
|
|
|
"overlay hide --keep-custom spam",
|
|
|
|
"bagr",
|
2022-05-24 23:22:17 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
2022-05-24 23:22:17 +02:00
|
|
|
|
|
|
|
assert!(actual.out.contains("bagr"));
|
|
|
|
assert!(actual_repl.out.contains("bagr"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_dont_keep_env() {
|
2022-05-24 23:22:17 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
|
|
|
"$env.BAGR = 'bagr'",
|
|
|
|
"overlay hide --keep-custom spam",
|
|
|
|
"$env.BAGR",
|
2022-05-24 23:22:17 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-24 23:22:17 +02:00
|
|
|
|
2022-12-10 18:23:34 +01:00
|
|
|
assert!(actual.err.contains("not_found"));
|
|
|
|
assert!(actual_repl.err.contains("not_found"));
|
2022-05-24 23:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_dont_keep_overwritten_decl() {
|
2022-05-24 23:22:17 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
|
|
|
"def foo [] { 'bar' }",
|
|
|
|
"overlay hide --keep-custom spam",
|
|
|
|
"foo",
|
2022-05-24 23:22:17 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-24 23:22:17 +02:00
|
|
|
|
|
|
|
assert!(!actual.err.is_empty());
|
|
|
|
#[cfg(windows)]
|
2023-01-15 03:03:32 +01:00
|
|
|
assert_ne!(actual_repl.out, "bagr");
|
2022-05-24 23:22:17 +02:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
assert!(!actual_repl.err.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_dont_keep_overwritten_alias() {
|
2022-05-24 23:22:17 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
|
|
|
"alias bar = echo `baz`",
|
|
|
|
"overlay hide --keep-custom spam",
|
|
|
|
"bar",
|
2022-05-24 23:22:17 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-24 23:22:17 +02:00
|
|
|
|
|
|
|
assert!(!actual.err.is_empty());
|
|
|
|
#[cfg(windows)]
|
2023-01-15 03:03:32 +01:00
|
|
|
assert_ne!(actual_repl.out, "bagr");
|
2022-05-24 23:22:17 +02:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
assert!(!actual_repl.err.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_dont_keep_overwritten_env() {
|
2022-05-24 23:22:17 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
|
|
|
"$env.BAZ = 'bagr'",
|
|
|
|
"overlay hide --keep-custom spam",
|
|
|
|
"$env.BAZ",
|
2022-05-24 23:22:17 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-24 23:22:17 +02:00
|
|
|
|
2022-12-10 18:23:34 +01:00
|
|
|
assert!(actual.err.contains("not_found"));
|
|
|
|
assert!(actual_repl.err.contains("not_found"));
|
2022-05-24 23:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_keep_decl_in_latest_overlay() {
|
2022-05-24 23:22:17 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
|
|
|
"def bagr [] { 'bagr' }",
|
|
|
|
"module eggs { }",
|
|
|
|
"overlay use eggs",
|
|
|
|
"overlay hide --keep-custom spam",
|
|
|
|
"bagr",
|
2022-05-24 23:22:17 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
2022-05-24 23:22:17 +02:00
|
|
|
|
|
|
|
assert!(actual.out.contains("bagr"));
|
|
|
|
assert!(actual_repl.out.contains("bagr"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_keep_alias_in_latest_overlay() {
|
2022-05-24 23:22:17 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
|
|
|
"alias bagr = echo 'bagr'",
|
|
|
|
"module eggs { }",
|
|
|
|
"overlay use eggs",
|
|
|
|
"overlay hide --keep-custom spam",
|
|
|
|
"bagr",
|
2022-05-24 23:22:17 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
2022-05-24 23:22:17 +02:00
|
|
|
|
|
|
|
assert!(actual.out.contains("bagr"));
|
|
|
|
assert!(actual_repl.out.contains("bagr"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn hide_overlay_dont_keep_env_in_latest_overlay() {
|
2022-05-24 23:22:17 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
|
|
|
"$env.BAGR = 'bagr'",
|
|
|
|
"module eggs { }",
|
|
|
|
"overlay use eggs",
|
|
|
|
"overlay hide --keep-custom spam",
|
|
|
|
"$env.BAGR",
|
2022-05-24 23:22:17 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-24 23:22:17 +02:00
|
|
|
|
2022-12-10 18:23:34 +01:00
|
|
|
assert!(actual.err.contains("not_found"));
|
|
|
|
assert!(actual_repl.err.contains("not_found"));
|
2022-05-24 23:22:17 +02:00
|
|
|
}
|
|
|
|
|
2022-05-07 21:39:22 +02:00
|
|
|
#[test]
|
|
|
|
fn preserve_overrides() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
2022-05-08 15:09:39 +02:00
|
|
|
r#"def foo [] { "new-foo" }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay hide spam",
|
|
|
|
"overlay use spam",
|
|
|
|
"foo",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "new-foo");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "new-foo");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reset_overrides() {
|
2022-05-08 15:09:39 +02:00
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use samples/spam.nu",
|
2022-05-08 15:09:39 +02:00
|
|
|
r#"def foo [] { "new-foo" }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay hide spam",
|
|
|
|
"overlay use samples/spam.nu",
|
|
|
|
"foo",
|
2022-05-08 15:09:39 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
2022-05-07 21:39:22 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
2022-05-08 15:09:39 +02:00
|
|
|
assert_eq!(actual_repl.out, "foo");
|
2022-05-07 21:39:22 +02:00
|
|
|
}
|
2022-05-26 16:47:04 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_new() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["overlay new spam", "overlay list | last"];
|
2022-05-26 16:47:04 +02:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-05-26 16:47:04 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
assert_eq!(actual_repl.out, "spam");
|
|
|
|
}
|
2022-07-11 22:58:28 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_keep_pwd() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay new spam",
|
|
|
|
"cd samples",
|
|
|
|
"overlay hide --keep-env [ PWD ] spam",
|
|
|
|
"$env.PWD | path basename",
|
2022-07-11 22:58:28 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-07-29 22:42:00 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
2022-07-11 22:58:28 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "samples");
|
|
|
|
assert_eq!(actual_repl.out, "samples");
|
|
|
|
}
|
2022-08-13 16:28:18 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_wrong_rename_type() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["module spam {}", "overlay use spam as { echo foo }"];
|
2022-08-13 16:28:18 +02:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-08-13 16:28:18 +02:00
|
|
|
|
|
|
|
assert!(actual.err.contains("parse_mismatch"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_add_renamed() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam as eggs --prefix",
|
|
|
|
"eggs foo",
|
2022-08-13 16:28:18 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-13 16:28:18 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
2022-12-21 23:21:03 +01:00
|
|
|
#[test]
|
|
|
|
fn overlay_add_renamed_const() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"const name = 'spam'",
|
|
|
|
"const new_name = 'eggs'",
|
|
|
|
"overlay use $name as $new_name --prefix",
|
|
|
|
"eggs foo",
|
2022-12-21 23:21:03 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
2022-08-13 16:28:18 +02:00
|
|
|
#[test]
|
|
|
|
fn overlay_add_renamed_from_file() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["overlay use samples/spam.nu as eggs --prefix", "eggs foo"];
|
2022-08-13 16:28:18 +02:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/overlays", &inp.join("; "));
|
2022-08-13 16:28:18 +02:00
|
|
|
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_cant_rename_existing_overlay() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"overlay hide spam",
|
|
|
|
"overlay use spam as eggs",
|
2022-08-13 16:28:18 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-13 16:28:18 +02:00
|
|
|
|
|
|
|
assert!(actual.err.contains("cant_add_overlay_help"));
|
|
|
|
assert!(actual_repl.err.contains("cant_add_overlay_help"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_can_add_renamed_overlay() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam as eggs --prefix",
|
|
|
|
"overlay use spam --prefix",
|
|
|
|
"(spam foo) + (eggs foo)",
|
2022-08-13 16:28:18 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-13 16:28:18 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foofoo");
|
|
|
|
assert_eq!(actual_repl.out, "foofoo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn overlay_hide_renamed_overlay() {
|
2022-08-13 16:28:18 +02:00
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam as eggs",
|
|
|
|
"overlay hide eggs",
|
|
|
|
"foo",
|
2022-08-13 16:28:18 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-13 16:28:18 +02:00
|
|
|
|
2022-12-30 16:44:37 +01:00
|
|
|
assert!(actual.err.contains("external_command"));
|
|
|
|
assert!(actual_repl.err.contains("external_command"));
|
2022-08-13 16:28:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-10 22:20:31 +01:00
|
|
|
fn overlay_hide_and_add_renamed_overlay() {
|
2022-08-13 16:28:18 +02:00
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam as eggs",
|
|
|
|
"overlay hide eggs",
|
|
|
|
"overlay use eggs",
|
|
|
|
"foo",
|
2022-08-13 16:28:18 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-13 16:28:18 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
2022-08-27 00:32:19 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_export_env() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"module spam { export-env { $env.FOO = 'foo' } }",
|
|
|
|
"overlay use spam",
|
|
|
|
"$env.FOO",
|
2022-08-27 00:32:19 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-27 00:32:19 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_export_env_hide() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"$env.FOO = 'foo'",
|
|
|
|
"module spam { export-env { hide-env FOO } }",
|
|
|
|
"overlay use spam",
|
|
|
|
"$env.FOO",
|
2022-08-27 00:32:19 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-08-27 00:32:19 +02:00
|
|
|
|
2022-12-10 18:23:34 +01:00
|
|
|
assert!(actual.err.contains("not_found"));
|
|
|
|
assert!(actual_repl.err.contains("not_found"));
|
2022-08-27 00:32:19 +02:00
|
|
|
}
|
2022-08-31 22:32:56 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_do_cd() {
|
|
|
|
Playground::setup("overlay_use_do_cd", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("test1/test2")
|
2024-05-04 02:53:15 +02:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-08-31 22:32:56 +02:00
|
|
|
"test1/test2/spam.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
export-env { cd test1/test2 }
|
2023-07-12 19:07:20 +02:00
|
|
|
",
|
2022-08-31 22:32:56 +02:00
|
|
|
)]);
|
|
|
|
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use test1/test2/spam.nu",
|
|
|
|
"$env.PWD | path basename",
|
2022-08-31 22:32:56 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-08-31 22:32:56 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "test2");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_do_cd_file_relative() {
|
|
|
|
Playground::setup("overlay_use_do_cd_file_relative", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("test1/test2")
|
2024-05-04 02:53:15 +02:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-08-31 22:32:56 +02:00
|
|
|
"test1/test2/spam.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
export-env { cd ($env.FILE_PWD | path join '..') }
|
2023-07-12 19:07:20 +02:00
|
|
|
",
|
2022-08-31 22:32:56 +02:00
|
|
|
)]);
|
|
|
|
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use test1/test2/spam.nu",
|
|
|
|
"$env.PWD | path basename",
|
2022-08-31 22:32:56 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-08-31 22:32:56 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "test1");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_dont_cd_overlay() {
|
|
|
|
Playground::setup("overlay_use_dont_cd_overlay", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("test1/test2")
|
2024-05-04 02:53:15 +02:00
|
|
|
.with_files(&[FileWithContentToBeTrimmed(
|
2022-08-31 22:32:56 +02:00
|
|
|
"test1/test2/spam.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
export-env {
|
|
|
|
overlay new spam
|
|
|
|
cd test1/test2
|
|
|
|
overlay hide spam
|
|
|
|
}
|
2023-07-12 19:07:20 +02:00
|
|
|
",
|
2022-08-31 22:32:56 +02:00
|
|
|
)]);
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["source-env test1/test2/spam.nu", "$env.PWD | path basename"];
|
2022-08-31 22:32:56 +02:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
|
2022-08-31 22:32:56 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "overlay_use_dont_cd_overlay");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-09-04 17:36:42 +02:00
|
|
|
fn overlay_use_find_scoped_module() {
|
2022-08-31 22:32:56 +02:00
|
|
|
Playground::setup("overlay_use_find_module_scoped", |dirs, _| {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = "
|
2022-08-31 22:32:56 +02:00
|
|
|
do {
|
2022-09-04 17:36:42 +02:00
|
|
|
module spam { }
|
2022-08-31 22:32:56 +02:00
|
|
|
|
|
|
|
overlay use spam
|
2022-09-04 17:36:42 +02:00
|
|
|
overlay list | last
|
2022-08-31 22:32:56 +02:00
|
|
|
}
|
2023-07-12 19:07:20 +02:00
|
|
|
";
|
2022-08-31 22:32:56 +02:00
|
|
|
|
2022-09-04 17:36:42 +02:00
|
|
|
let actual = nu!(cwd: dirs.test(), inp);
|
2022-08-31 22:32:56 +02:00
|
|
|
|
2022-09-04 17:36:42 +02:00
|
|
|
assert_eq!(actual.out, "spam");
|
2022-08-31 22:32:56 +02:00
|
|
|
})
|
|
|
|
}
|
2022-09-04 19:32:06 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_preserve_hidden_env_1() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay new spam",
|
|
|
|
"$env.FOO = 'foo'",
|
|
|
|
"overlay new eggs",
|
|
|
|
"$env.FOO = 'bar'",
|
|
|
|
"hide-env FOO",
|
|
|
|
"overlay use eggs",
|
|
|
|
"$env.FOO",
|
2022-09-04 19:32:06 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-09-04 19:32:06 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_preserve_hidden_env_2() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay new spam",
|
|
|
|
"$env.FOO = 'foo'",
|
|
|
|
"overlay hide spam",
|
|
|
|
"overlay new eggs",
|
|
|
|
"$env.FOO = 'bar'",
|
|
|
|
"hide-env FOO",
|
|
|
|
"overlay hide eggs",
|
|
|
|
"overlay use spam",
|
|
|
|
"overlay use eggs",
|
|
|
|
"$env.FOO",
|
2022-09-04 19:32:06 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-09-04 19:32:06 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_reset_hidden_env() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay new spam",
|
|
|
|
"$env.FOO = 'foo'",
|
|
|
|
"overlay new eggs",
|
|
|
|
"$env.FOO = 'bar'",
|
|
|
|
"hide-env FOO",
|
|
|
|
"module eggs { export-env { $env.FOO = 'bar' } }",
|
|
|
|
"overlay use eggs",
|
|
|
|
"$env.FOO",
|
2022-09-04 19:32:06 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-09-04 19:32:06 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
assert_eq!(actual_repl.out, "bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ignore = "TODO: For this to work, we'd need to make predecls respect overlays"]
|
|
|
|
#[test]
|
|
|
|
fn overlay_preserve_hidden_decl() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay new spam",
|
|
|
|
"def foo [] { 'foo' }",
|
|
|
|
"overlay new eggs",
|
|
|
|
"def foo [] { 'bar' }",
|
|
|
|
"hide foo",
|
|
|
|
"overlay use eggs",
|
|
|
|
"foo",
|
2022-09-04 19:32:06 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-09-04 19:32:06 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# 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.
2023-02-27 08:44:05 +01:00
|
|
|
#[ignore = "TODO: For this to work, we'd need to make predecls respect overlays"]
|
2022-09-04 19:32:06 +02:00
|
|
|
#[test]
|
|
|
|
fn overlay_preserve_hidden_alias() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay new spam",
|
|
|
|
"alias foo = echo 'foo'",
|
|
|
|
"overlay new eggs",
|
|
|
|
"alias foo = echo 'bar'",
|
|
|
|
"hide foo",
|
|
|
|
"overlay use eggs",
|
|
|
|
"foo",
|
2022-09-04 19:32:06 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-09-04 19:32:06 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
2022-09-16 09:27:12 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_trim_single_quote() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use 'spam'",
|
|
|
|
"overlay list | last ",
|
2022-09-16 09:27:12 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-09-16 09:27:12 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_trim_single_quote_hide() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use 'spam'",
|
|
|
|
"overlay hide spam ",
|
|
|
|
"foo",
|
2022-09-16 09:27:12 +02:00
|
|
|
];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-09-16 09:27:12 +02:00
|
|
|
|
|
|
|
assert!(!actual.err.is_empty());
|
|
|
|
#[cfg(windows)]
|
2023-01-15 03:03:32 +01:00
|
|
|
assert_ne!(actual_repl.out, "foo");
|
2022-09-16 09:27:12 +02:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
assert!(!actual_repl.err.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_trim_double_quote() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
|
|
|
r#"overlay use "spam" "#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay list | last ",
|
2022-09-16 09:27:12 +02:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-09-16 09:27:12 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_trim_double_quote_hide() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def foo [] { "foo" } }"#,
|
|
|
|
r#"overlay use "spam" "#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay hide spam ",
|
|
|
|
"foo",
|
2022-09-16 09:27:12 +02:00
|
|
|
];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2022-09-16 09:27:12 +02:00
|
|
|
|
|
|
|
assert!(!actual.err.is_empty());
|
|
|
|
#[cfg(windows)]
|
2023-01-15 03:03:32 +01:00
|
|
|
assert_ne!(actual_repl.out, "foo");
|
2022-09-16 09:27:12 +02:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
assert!(!actual_repl.err.is_empty());
|
|
|
|
}
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_and_restore_older_env_vars() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"module spam {
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
export-env {
|
|
|
|
let old_baz = $env.BAZ;
|
2023-06-30 21:57:51 +02:00
|
|
|
$env.BAZ = $old_baz + 'baz'
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
}
|
2023-07-12 19:07:20 +02:00
|
|
|
}",
|
|
|
|
"$env.BAZ = 'baz'",
|
|
|
|
"overlay use spam",
|
|
|
|
"overlay hide spam",
|
|
|
|
"$env.BAZ = 'new-baz'",
|
|
|
|
"overlay use --reload spam",
|
|
|
|
"$env.BAZ",
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "new-bazbaz");
|
|
|
|
assert_eq!(actual_repl.out, "new-bazbaz");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_and_reload() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"module spam {
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
export def foo [] { 'foo' };
|
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# 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.
2023-02-27 08:44:05 +01:00
|
|
|
export alias fooalias = echo 'foo';
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
export-env {
|
2023-06-30 21:57:51 +02:00
|
|
|
$env.FOO = 'foo'
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
}
|
2023-07-12 19:07:20 +02:00
|
|
|
}",
|
|
|
|
"overlay use spam",
|
|
|
|
"def foo [] { 'newfoo' }",
|
|
|
|
"alias fooalias = echo 'newfoo'",
|
|
|
|
"$env.FOO = 'newfoo'",
|
|
|
|
"overlay use --reload spam",
|
|
|
|
"$'(foo)(fooalias)($env.FOO)'",
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foofoofoo");
|
|
|
|
assert_eq!(actual_repl.out, "foofoofoo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_and_reolad_keep_custom() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay new spam",
|
|
|
|
"def foo [] { 'newfoo' }",
|
|
|
|
"alias fooalias = echo 'newfoo'",
|
|
|
|
"$env.FOO = 'newfoo'",
|
|
|
|
"overlay use --reload spam",
|
|
|
|
"$'(foo)(fooalias)($env.FOO)'",
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# 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.
2022-11-24 23:45:24 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "newfoonewfoonewfoo");
|
|
|
|
assert_eq!(actual_repl.out, "newfoonewfoonewfoo");
|
|
|
|
}
|
2023-01-22 20:34:15 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_main() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def main [] { "spam" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"spam",
|
2023-01-22 20:34:15 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 20:34:15 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_main_prefix() {
|
|
|
|
let inp = &[
|
|
|
|
r#"module spam { export def main [] { "spam" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam --prefix",
|
|
|
|
"spam",
|
2023-01-22 20:34:15 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 20:34:15 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_main_def_env() {
|
|
|
|
let inp = &[
|
2023-10-02 20:13:31 +02:00
|
|
|
r#"module spam { export def --env main [] { $env.SPAM = "spam" } }"#,
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay use spam",
|
|
|
|
"spam",
|
|
|
|
"$env.SPAM",
|
2023-01-22 20:34:15 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 20:34:15 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_main_def_known_external() {
|
|
|
|
// note: requires installed cargo
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"module cargo { export extern main [] }",
|
|
|
|
"overlay use cargo",
|
|
|
|
"cargo --version",
|
2023-01-22 20:34:15 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 20:34:15 +01:00
|
|
|
|
|
|
|
assert!(actual.out.contains("cargo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_main_not_exported() {
|
|
|
|
let inp = &[
|
2023-12-08 13:08:38 +01:00
|
|
|
r#"module foo { def main [] { "foo" } }"#,
|
|
|
|
"overlay use foo",
|
|
|
|
"foo",
|
2023-01-22 20:34:15 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2023-01-22 20:34:15 +01:00
|
|
|
|
|
|
|
assert!(actual.err.contains("external_command"));
|
|
|
|
}
|
2023-03-10 22:20:31 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_overlay_hide() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"overlay new spam",
|
|
|
|
"def foo [] { 'foo' }",
|
|
|
|
"overlay new eggs",
|
|
|
|
"alias oh = overlay hide",
|
|
|
|
"oh spam",
|
|
|
|
"foo",
|
2023-03-10 22:20:31 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2023-03-10 22:20:31 +01:00
|
|
|
|
|
|
|
assert!(actual.err.contains("external_command"));
|
|
|
|
assert!(actual_repl.err.contains("external_command"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_overlay_use() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"module spam { export def foo [] { 'foo' } }",
|
|
|
|
"alias ou = overlay use",
|
|
|
|
"ou spam",
|
|
|
|
"foo",
|
2023-03-10 22:20:31 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2023-03-10 22:20:31 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert_eq!(actual_repl.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_overlay_new() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"alias on = overlay new",
|
|
|
|
"on spam",
|
|
|
|
"on eggs",
|
|
|
|
"overlay list | last",
|
2023-03-10 22:20:31 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
let actual_repl = nu!(nu_repl_code(inp));
|
2023-03-10 22:20:31 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "eggs");
|
|
|
|
assert_eq!(actual_repl.out, "eggs");
|
|
|
|
}
|
2023-05-03 13:08:54 +02:00
|
|
|
|
2023-05-06 20:39:54 +02:00
|
|
|
#[test]
|
|
|
|
fn overlay_use_module_dir() {
|
|
|
|
let import = "overlay use samples/spam";
|
|
|
|
|
|
|
|
let inp = &[import, "spam"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
|
|
|
|
let inp = &[import, "foo"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
|
|
|
|
let inp = &[import, "bar"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
|
|
|
|
let inp = &[import, "foo baz"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "foobaz");
|
|
|
|
|
|
|
|
let inp = &[import, "bar baz"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "barbaz");
|
|
|
|
|
|
|
|
let inp = &[import, "baz"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "spambaz");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overlay_use_module_dir_prefix() {
|
|
|
|
let import = "overlay use samples/spam --prefix";
|
|
|
|
|
|
|
|
let inp = &[import, "spam"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
|
|
|
|
let inp = &[import, "spam foo"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
|
|
|
|
let inp = &[import, "spam bar"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
|
|
|
|
let inp = &[import, "spam foo baz"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "foobaz");
|
|
|
|
|
|
|
|
let inp = &[import, "spam bar baz"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "barbaz");
|
|
|
|
|
|
|
|
let inp = &[import, "spam baz"];
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/modules", &inp.join("; "));
|
2023-05-06 20:39:54 +02:00
|
|
|
assert_eq!(actual.out, "spambaz");
|
|
|
|
}
|
|
|
|
|
2023-05-03 13:08:54 +02:00
|
|
|
#[test]
|
|
|
|
fn overlay_help_no_error() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!("overlay hide -h");
|
2023-05-03 13:08:54 +02:00
|
|
|
assert!(actual.err.is_empty());
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!("overlay new -h");
|
2023-05-03 13:08:54 +02:00
|
|
|
assert!(actual.err.is_empty());
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!("overlay use -h");
|
2023-05-03 13:08:54 +02:00
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
}
|