2020-01-16 09:05:53 +00:00
|
|
|
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
2020-05-16 07:25:18 +00:00
|
|
|
use nu_test_support::playground::Playground;
|
2023-07-12 17:07:20 +00:00
|
|
|
use nu_test_support::{nu, pipeline};
|
2023-04-08 18:52:37 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2020-01-16 09:05:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn takes_rows_of_nu_value_strings_and_pipes_it_to_stdin_of_external() {
|
2023-11-29 22:21:34 +00:00
|
|
|
let sample = r#"
|
|
|
|
[[name, rusty_luck, origin];
|
|
|
|
[Jason, 1, Canada],
|
|
|
|
[JT, 1, "New Zealand"],
|
|
|
|
[Andrés, 1, Ecuador],
|
|
|
|
[AndKitKatz, 1, "Estados Unidos"]]
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let actual = nu!(pipeline(&format!(
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2023-11-29 22:21:34 +00:00
|
|
|
{sample}
|
2020-05-25 22:50:54 +00:00
|
|
|
| get origin
|
2023-11-29 22:21:34 +00:00
|
|
|
| each {{ |it| nu --testbin cococo $it | nu --testbin chop }}
|
2022-02-02 20:59:01 +00:00
|
|
|
| get 2
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2023-11-29 22:21:34 +00:00
|
|
|
)));
|
2020-01-16 09:05:53 +00:00
|
|
|
|
2023-11-29 22:21:34 +00:00
|
|
|
// chop will remove the last escaped double quote from \"Estados Unidos\"
|
|
|
|
assert_eq!(actual.out, "Ecuado");
|
2020-01-16 09:05:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 20:18:29 +00:00
|
|
|
#[test]
|
|
|
|
fn treats_dot_dot_as_path_not_range() {
|
|
|
|
Playground::setup("dot_dot_dir", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[FileWithContentToBeTrimmed(
|
2021-04-19 20:18:29 +00:00
|
|
|
"nu_times.csv",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2021-04-19 20:18:29 +00:00
|
|
|
name,rusty_luck,origin
|
|
|
|
Jason,1,Canada
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2021-04-19 20:18:29 +00:00
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2021-04-19 20:18:29 +00:00
|
|
|
mkdir temp;
|
|
|
|
cd temp;
|
2023-03-16 22:53:46 +00:00
|
|
|
print (open ../nu_times.csv).name.0 | table;
|
2021-04-19 20:18:29 +00:00
|
|
|
cd ..;
|
|
|
|
rmdir temp
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2021-04-19 20:18:29 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
// chop will remove the last escaped double quote from \"Estados Unidos\"
|
|
|
|
assert_eq!(actual.out, "Jason");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-02 07:49:14 +00:00
|
|
|
#[test]
|
2021-06-07 08:08:35 +00:00
|
|
|
fn subexpression_properly_redirects() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-09-11 08:48:27 +00:00
|
|
|
echo (nu --testbin cococo "hello") | str join
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2020-06-27 21:04:57 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello");
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:25:18 +00:00
|
|
|
#[test]
|
2021-06-07 08:08:35 +00:00
|
|
|
fn argument_subexpression() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-02-17 11:40:24 +00:00
|
|
|
echo "foo" | each { |it| echo (echo $it) }
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2020-05-16 07:25:18 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
}
|
|
|
|
|
2022-11-10 20:05:34 +00:00
|
|
|
#[test]
|
|
|
|
fn for_loop() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-11-10 20:05:34 +00:00
|
|
|
for i in 1..3 { print $i }
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-11-10 20:05:34 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "123");
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:25:18 +00:00
|
|
|
#[test]
|
2021-06-07 08:08:35 +00:00
|
|
|
fn subexpression_handles_dot() {
|
|
|
|
Playground::setup("subexpression_handles_dot", |dirs, sandbox| {
|
2024-05-04 00:53:15 +00:00
|
|
|
sandbox.with_files(&[FileWithContentToBeTrimmed(
|
2020-05-16 07:25:18 +00:00
|
|
|
"nu_times.csv",
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-05-16 07:25:18 +00:00
|
|
|
name,rusty_luck,origin
|
|
|
|
Jason,1,Canada
|
2023-03-15 05:54:55 +00:00
|
|
|
JT,1,New Zealand
|
2020-05-16 07:25:18 +00:00
|
|
|
Andrés,1,Ecuador
|
|
|
|
AndKitKatz,1,Estados Unidos
|
2023-07-12 17:07:20 +00:00
|
|
|
",
|
2020-05-16 07:25:18 +00:00
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2021-05-12 01:01:48 +00:00
|
|
|
echo (open nu_times.csv)
|
2020-05-16 07:25:18 +00:00
|
|
|
| get name
|
2022-02-17 11:40:24 +00:00
|
|
|
| each { |it| nu --testbin chop $it }
|
2022-02-02 20:59:01 +00:00
|
|
|
| get 3
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
2020-05-16 07:25:18 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "AndKitKat");
|
2023-11-29 22:21:34 +00:00
|
|
|
});
|
2020-05-16 07:25:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 19:27:26 +00:00
|
|
|
#[test]
|
|
|
|
fn string_interpolation_with_it() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-02-17 11:40:24 +00:00
|
|
|
echo "foo" | each { |it| echo $"($it)" }
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2020-05-19 19:27:26 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_interpolation_with_it_column_path() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-02-17 11:40:24 +00:00
|
|
|
echo [[name]; [sammie]] | each { |it| echo $"($it.name)" } | get 0
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2020-05-19 19:27:26 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "sammie");
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:20:29 +00:00
|
|
|
#[test]
|
|
|
|
fn string_interpolation_shorthand_overlap() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2021-05-20 22:55:38 +00:00
|
|
|
$"3 + 4 = (3 + 4)"
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-05-12 04:20:29 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "3 + 4 = 7");
|
|
|
|
}
|
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
// FIXME: jt - we don't currently have a way to escape the single ticks easily
|
|
|
|
#[ignore]
|
2021-05-20 22:55:38 +00:00
|
|
|
#[test]
|
|
|
|
fn string_interpolation_and_paren() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2021-05-20 22:55:38 +00:00
|
|
|
$"a paren is ('(')"
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-05-20 22:55:38 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "a paren is (");
|
|
|
|
}
|
|
|
|
|
2021-07-30 00:07:34 +00:00
|
|
|
#[test]
|
|
|
|
fn string_interpolation_with_unicode() {
|
|
|
|
//カ = U+30AB : KATAKANA LETTER KA
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2021-07-30 00:07:34 +00:00
|
|
|
$"カ"
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-07-30 00:07:34 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "カ");
|
|
|
|
}
|
|
|
|
|
2021-01-08 23:53:59 +00:00
|
|
|
#[test]
|
2020-12-18 07:53:49 +00:00
|
|
|
fn run_custom_command() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-05-12 01:01:48 +00:00
|
|
|
def add-me [x y] { $x + $y}; add-me 10 5
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-12-18 07:53:49 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "15");
|
|
|
|
}
|
|
|
|
|
2020-12-21 07:36:59 +00:00
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_flag() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-09-05 14:41:06 +00:00
|
|
|
def foo [--bar:number] { if ($bar | is-empty) { echo "empty" } else { echo $bar } }; foo --bar 10
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2020-12-21 07:36:59 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "10");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_flag_missing() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-09-05 14:41:06 +00:00
|
|
|
def foo [--bar:number] { if ($bar | is-empty) { echo "empty" } else { echo $bar } }; foo
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2020-12-21 07:36:59 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "empty");
|
|
|
|
}
|
|
|
|
|
2020-12-23 07:43:56 +00:00
|
|
|
#[test]
|
|
|
|
fn run_custom_subcommand() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-09-11 08:48:27 +00:00
|
|
|
def "str double" [x] { echo $x $x | str join }; str double bob
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2020-12-23 07:43:56 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "bobbob");
|
|
|
|
}
|
|
|
|
|
2021-01-01 06:23:54 +00:00
|
|
|
#[test]
|
|
|
|
fn run_inner_custom_command() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-01-01 06:23:54 +00:00
|
|
|
def outer [x] { def inner [y] { echo $y }; inner $x }; outer 10
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-01-01 06:23:54 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "10");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_broken_inner_custom_command() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-01-01 06:23:54 +00:00
|
|
|
def outer [x] { def inner [y] { echo $y }; inner $x }; inner 10
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-01-01 06:23:54 +00:00
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(!actual.err.is_empty());
|
2021-01-01 06:23:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 21:28:32 +00:00
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_rest() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-02-02 20:59:01 +00:00
|
|
|
def rest-me [...rest: string] { echo $rest.1 $rest.0}; rest-me "hello" "world" | to json --raw
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-01-22 21:28:32 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, r#"["world","hello"]"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_rest_and_arg() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-02-02 20:59:01 +00:00
|
|
|
def rest-me-with-arg [name: string, ...rest: string] { echo $rest.1 $rest.0 $name}; rest-me-with-arg "hello" "world" "yay" | to json --raw
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-01-22 21:28:32 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, r#"["yay","world","hello"]"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_rest_and_flag() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-02-02 20:59:01 +00:00
|
|
|
def rest-me-with-flag [--name: string, ...rest: string] { echo $rest.1 $rest.0 $name}; rest-me-with-flag "hello" "world" --name "yay" | to json --raw
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-01-22 21:28:32 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, r#"["world","hello","yay"]"#);
|
|
|
|
}
|
|
|
|
|
2021-04-12 02:37:36 +00:00
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_empty_rest() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2023-02-22 16:18:33 +00:00
|
|
|
def rest-me-with-empty-rest [...rest: string] { $rest }; rest-me-with-empty-rest | is-empty
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-04-12 02:37:36 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
assert_eq!(actual.err, "");
|
2021-04-12 02:37:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
//FIXME: jt: blocked on https://github.com/nushell/engine-q/issues/912
|
|
|
|
#[ignore]
|
2021-08-26 17:58:53 +00:00
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_rest_other_name() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2021-08-26 17:58:53 +00:00
|
|
|
def say-hello [
|
|
|
|
greeting:string,
|
|
|
|
...names:string # All of the names
|
|
|
|
] {
|
2022-09-11 08:48:27 +00:00
|
|
|
echo $"($greeting), ($names | sort-by | str join)"
|
2021-08-26 17:58:53 +00:00
|
|
|
}
|
|
|
|
say-hello Salutations E D C A B
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-08-26 17:58:53 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
assert_eq!(actual.out, "Salutations, ABCDE");
|
|
|
|
assert_eq!(actual.err, "");
|
2021-08-26 17:58:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 05:58:32 +00:00
|
|
|
#[test]
|
|
|
|
fn alias_a_load_env() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-02 20:59:01 +00:00
|
|
|
def activate-helper [] { {BOB: SAM} }; alias activate = load-env (activate-helper); activate; $env.BOB
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-05-26 05:58:32 +00:00
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
assert_eq!(actual.out, "SAM");
|
2021-05-26 05:58:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 07:53:49 +00:00
|
|
|
#[test]
|
2021-05-20 04:26:54 +00:00
|
|
|
fn let_variable() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-01-04 23:30:55 +00:00
|
|
|
let x = 5
|
|
|
|
let y = 12
|
2021-05-12 01:01:48 +00:00
|
|
|
$x + $y
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-12-18 07:53:49 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "17");
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:44:21 +00:00
|
|
|
#[test]
|
2021-05-20 04:26:54 +00:00
|
|
|
fn let_doesnt_leak() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-01-04 23:30:55 +00:00
|
|
|
do { let x = 5 }; echo $x
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-01-03 06:44:21 +00:00
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(actual.err.contains("variable not found"));
|
2021-01-03 06:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 06:25:03 +00:00
|
|
|
#[test]
|
2023-06-30 19:57:51 +00:00
|
|
|
fn mutate_env_variable() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
$env.TESTENVVAR = "hello world"
|
2022-02-02 20:59:01 +00:00
|
|
|
echo $env.TESTENVVAR
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2020-12-19 06:25:03 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello world");
|
|
|
|
}
|
|
|
|
|
Allow environment variables to be hidden (#3950)
* Allow environment variables to be hidden
This change allows environment variables in Nushell to have a value of
`Nothing`, which can be set by the user by passing `$nothing` to
`let-env` and friends.
Environment variables with a value of Nothing behave as if they are not
set at all. This allows a user to shadow the value of an environment
variable in a parent scope, effectively removing it from their current
scope. This was not possible before, because a scope can not affect its
parent scopes.
This is a workaround for issues like #3920.
Additionally, this allows a user to simultaneously set, change and
remove multiple environment variables via `load-env`. Any environment
variables set to $nothing will be hidden and thus act as if they are
removed. This simplifies working with virtual environments, which rely
on setting multiple environment variables, including PATH, to specific
values, and remove/change them on deactivation.
One surprising behavior is that an environment variable set to $nothing
will act as if it is not set when querying it (via $nu.env.X), but it is
still possible to remove it entirely via `unlet-env`. If the same
environment variable is present in the parent scope, the value in the
parent scope will be visible to the user. This might be surprising
behavior to users who are not familiar with the implementation details.
An additional corner case is the the shorthand form of `with-env` does
not work with this feature. Using `X=$nothing` will set $nu.env.X to the
string "$nothing". The long-form works as expected: `with-env [X
$nothing] {...}`.
* Remove unused import
* Allow all primitives to be convert to strings
2021-08-26 13:15:58 +00:00
|
|
|
#[test]
|
2023-06-30 19:57:51 +00:00
|
|
|
fn mutate_env_hides_variable() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
$env.TESTENVVAR = "hello world"
|
2023-03-16 22:53:46 +00:00
|
|
|
print $env.TESTENVVAR
|
2022-08-13 09:55:06 +00:00
|
|
|
hide-env TESTENVVAR
|
2023-03-16 22:53:46 +00:00
|
|
|
print $env.TESTENVVAR
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
Allow environment variables to be hidden (#3950)
* Allow environment variables to be hidden
This change allows environment variables in Nushell to have a value of
`Nothing`, which can be set by the user by passing `$nothing` to
`let-env` and friends.
Environment variables with a value of Nothing behave as if they are not
set at all. This allows a user to shadow the value of an environment
variable in a parent scope, effectively removing it from their current
scope. This was not possible before, because a scope can not affect its
parent scopes.
This is a workaround for issues like #3920.
Additionally, this allows a user to simultaneously set, change and
remove multiple environment variables via `load-env`. Any environment
variables set to $nothing will be hidden and thus act as if they are
removed. This simplifies working with virtual environments, which rely
on setting multiple environment variables, including PATH, to specific
values, and remove/change them on deactivation.
One surprising behavior is that an environment variable set to $nothing
will act as if it is not set when querying it (via $nu.env.X), but it is
still possible to remove it entirely via `unlet-env`. If the same
environment variable is present in the parent scope, the value in the
parent scope will be visible to the user. This might be surprising
behavior to users who are not familiar with the implementation details.
An additional corner case is the the shorthand form of `with-env` does
not work with this feature. Using `X=$nothing` will set $nu.env.X to the
string "$nothing". The long-form works as expected: `with-env [X
$nothing] {...}`.
* Remove unused import
* Allow all primitives to be convert to strings
2021-08-26 13:15:58 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello world");
|
2022-12-10 17:23:34 +00:00
|
|
|
assert!(actual.err.contains("not_found"));
|
Allow environment variables to be hidden (#3950)
* Allow environment variables to be hidden
This change allows environment variables in Nushell to have a value of
`Nothing`, which can be set by the user by passing `$nothing` to
`let-env` and friends.
Environment variables with a value of Nothing behave as if they are not
set at all. This allows a user to shadow the value of an environment
variable in a parent scope, effectively removing it from their current
scope. This was not possible before, because a scope can not affect its
parent scopes.
This is a workaround for issues like #3920.
Additionally, this allows a user to simultaneously set, change and
remove multiple environment variables via `load-env`. Any environment
variables set to $nothing will be hidden and thus act as if they are
removed. This simplifies working with virtual environments, which rely
on setting multiple environment variables, including PATH, to specific
values, and remove/change them on deactivation.
One surprising behavior is that an environment variable set to $nothing
will act as if it is not set when querying it (via $nu.env.X), but it is
still possible to remove it entirely via `unlet-env`. If the same
environment variable is present in the parent scope, the value in the
parent scope will be visible to the user. This might be surprising
behavior to users who are not familiar with the implementation details.
An additional corner case is the the shorthand form of `with-env` does
not work with this feature. Using `X=$nothing` will set $nu.env.X to the
string "$nothing". The long-form works as expected: `with-env [X
$nothing] {...}`.
* Remove unused import
* Allow all primitives to be convert to strings
2021-08-26 13:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-06-30 19:57:51 +00:00
|
|
|
fn mutate_env_hides_variable_in_parent_scope() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
$env.TESTENVVAR = "hello world"
|
2023-03-16 22:53:46 +00:00
|
|
|
print $env.TESTENVVAR
|
2022-02-02 20:59:01 +00:00
|
|
|
do {
|
2022-08-13 09:55:06 +00:00
|
|
|
hide-env TESTENVVAR
|
2023-03-16 22:53:46 +00:00
|
|
|
print $env.TESTENVVAR
|
2022-02-02 20:59:01 +00:00
|
|
|
}
|
2023-03-16 22:53:46 +00:00
|
|
|
print $env.TESTENVVAR
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2022-02-02 20:59:01 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello world");
|
2022-12-10 17:23:34 +00:00
|
|
|
assert!(actual.err.contains("not_found"));
|
Allow environment variables to be hidden (#3950)
* Allow environment variables to be hidden
This change allows environment variables in Nushell to have a value of
`Nothing`, which can be set by the user by passing `$nothing` to
`let-env` and friends.
Environment variables with a value of Nothing behave as if they are not
set at all. This allows a user to shadow the value of an environment
variable in a parent scope, effectively removing it from their current
scope. This was not possible before, because a scope can not affect its
parent scopes.
This is a workaround for issues like #3920.
Additionally, this allows a user to simultaneously set, change and
remove multiple environment variables via `load-env`. Any environment
variables set to $nothing will be hidden and thus act as if they are
removed. This simplifies working with virtual environments, which rely
on setting multiple environment variables, including PATH, to specific
values, and remove/change them on deactivation.
One surprising behavior is that an environment variable set to $nothing
will act as if it is not set when querying it (via $nu.env.X), but it is
still possible to remove it entirely via `unlet-env`. If the same
environment variable is present in the parent scope, the value in the
parent scope will be visible to the user. This might be surprising
behavior to users who are not familiar with the implementation details.
An additional corner case is the the shorthand form of `with-env` does
not work with this feature. Using `X=$nothing` will set $nu.env.X to the
string "$nothing". The long-form works as expected: `with-env [X
$nothing] {...}`.
* Remove unused import
* Allow all primitives to be convert to strings
2021-08-26 13:15:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 03:00:07 +00:00
|
|
|
#[test]
|
|
|
|
fn unlet_env_variable() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
$env.TEST_VAR = "hello world"
|
2022-08-13 09:55:06 +00:00
|
|
|
hide-env TEST_VAR
|
2022-02-02 20:59:01 +00:00
|
|
|
echo $env.TEST_VAR
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2022-12-10 17:23:34 +00:00
|
|
|
assert!(actual.err.contains("not_found"));
|
2021-06-19 03:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-03-17 22:35:50 +00:00
|
|
|
#[ignore]
|
2021-06-19 03:00:07 +00:00
|
|
|
fn unlet_nonexistent_variable() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-08-13 09:55:06 +00:00
|
|
|
hide-env NONEXISTENT_VARIABLE
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-02-02 20:59:01 +00:00
|
|
|
|
|
|
|
assert!(actual.err.contains("did not find"));
|
Allow environment variables to be hidden (#3950)
* Allow environment variables to be hidden
This change allows environment variables in Nushell to have a value of
`Nothing`, which can be set by the user by passing `$nothing` to
`let-env` and friends.
Environment variables with a value of Nothing behave as if they are not
set at all. This allows a user to shadow the value of an environment
variable in a parent scope, effectively removing it from their current
scope. This was not possible before, because a scope can not affect its
parent scopes.
This is a workaround for issues like #3920.
Additionally, this allows a user to simultaneously set, change and
remove multiple environment variables via `load-env`. Any environment
variables set to $nothing will be hidden and thus act as if they are
removed. This simplifies working with virtual environments, which rely
on setting multiple environment variables, including PATH, to specific
values, and remove/change them on deactivation.
One surprising behavior is that an environment variable set to $nothing
will act as if it is not set when querying it (via $nu.env.X), but it is
still possible to remove it entirely via `unlet-env`. If the same
environment variable is present in the parent scope, the value in the
parent scope will be visible to the user. This might be surprising
behavior to users who are not familiar with the implementation details.
An additional corner case is the the shorthand form of `with-env` does
not work with this feature. Using `X=$nothing` will set $nu.env.X to the
string "$nothing". The long-form works as expected: `with-env [X
$nothing] {...}`.
* Remove unused import
* Allow all primitives to be convert to strings
2021-08-26 13:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unlet_variable_in_parent_scope() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
$env.DEBUG = "1"
|
2023-03-16 22:53:46 +00:00
|
|
|
print $env.DEBUG
|
2022-02-02 20:59:01 +00:00
|
|
|
do {
|
2023-06-30 19:57:51 +00:00
|
|
|
$env.DEBUG = "2"
|
2023-03-16 22:53:46 +00:00
|
|
|
print $env.DEBUG
|
2022-08-13 09:55:06 +00:00
|
|
|
hide-env DEBUG
|
2023-03-16 22:53:46 +00:00
|
|
|
print $env.DEBUG
|
2022-02-02 20:59:01 +00:00
|
|
|
}
|
2023-03-16 22:53:46 +00:00
|
|
|
print $env.DEBUG
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
Allow environment variables to be hidden (#3950)
* Allow environment variables to be hidden
This change allows environment variables in Nushell to have a value of
`Nothing`, which can be set by the user by passing `$nothing` to
`let-env` and friends.
Environment variables with a value of Nothing behave as if they are not
set at all. This allows a user to shadow the value of an environment
variable in a parent scope, effectively removing it from their current
scope. This was not possible before, because a scope can not affect its
parent scopes.
This is a workaround for issues like #3920.
Additionally, this allows a user to simultaneously set, change and
remove multiple environment variables via `load-env`. Any environment
variables set to $nothing will be hidden and thus act as if they are
removed. This simplifies working with virtual environments, which rely
on setting multiple environment variables, including PATH, to specific
values, and remove/change them on deactivation.
One surprising behavior is that an environment variable set to $nothing
will act as if it is not set when querying it (via $nu.env.X), but it is
still possible to remove it entirely via `unlet-env`. If the same
environment variable is present in the parent scope, the value in the
parent scope will be visible to the user. This might be surprising
behavior to users who are not familiar with the implementation details.
An additional corner case is the the shorthand form of `with-env` does
not work with this feature. Using `X=$nothing` will set $nu.env.X to the
string "$nothing". The long-form works as expected: `with-env [X
$nothing] {...}`.
* Remove unused import
* Allow all primitives to be convert to strings
2021-08-26 13:15:58 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "1211");
|
2021-06-19 03:00:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:44:21 +00:00
|
|
|
#[test]
|
2023-06-30 19:57:51 +00:00
|
|
|
fn mutate_env_doesnt_leak() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
do { $env.xyz = "my message" }; echo $env.xyz
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-01-03 06:44:21 +00:00
|
|
|
|
2022-12-10 17:23:34 +00:00
|
|
|
assert!(actual.err.contains("not_found"));
|
2021-01-03 06:44:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 07:48:02 +00:00
|
|
|
#[test]
|
2023-06-30 19:57:51 +00:00
|
|
|
fn proper_shadow_mutate_env_aliases() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
$env.DEBUG = "true"; print $env.DEBUG | table; do { $env.DEBUG = "false"; print $env.DEBUG } | table; print $env.DEBUG
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-01-03 07:48:02 +00:00
|
|
|
assert_eq!(actual.out, "truefalsetrue");
|
|
|
|
}
|
|
|
|
|
2021-05-25 18:18:20 +00:00
|
|
|
#[test]
|
|
|
|
fn load_env_variable() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-02-02 20:59:01 +00:00
|
|
|
echo {TESTENVVAR: "hello world"} | load-env
|
|
|
|
echo $env.TESTENVVAR
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-05-25 18:18:20 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello world");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load_env_variable_arg() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-02-02 20:59:01 +00:00
|
|
|
load-env {TESTENVVAR: "hello world"}
|
|
|
|
echo $env.TESTENVVAR
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-05-25 18:18:20 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello world");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load_env_doesnt_leak() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2022-02-02 20:59:01 +00:00
|
|
|
do { echo { name: xyz, value: "my message" } | load-env }; echo $env.xyz
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-05-25 18:18:20 +00:00
|
|
|
|
2022-12-10 17:23:34 +00:00
|
|
|
assert!(actual.err.contains("not_found"));
|
2021-05-25 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proper_shadow_load_env_aliases() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
$env.DEBUG = "true"; print $env.DEBUG | table; do { echo {DEBUG: "false"} | load-env; print $env.DEBUG } | table; print $env.DEBUG
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-05-25 18:18:20 +00:00
|
|
|
assert_eq!(actual.out, "truefalsetrue");
|
|
|
|
}
|
|
|
|
|
2022-12-22 20:30:10 +00:00
|
|
|
//FIXME: jt: load-env can not currently hide variables because null no longer hides
|
2022-02-02 20:59:01 +00:00
|
|
|
#[ignore]
|
Allow environment variables to be hidden (#3950)
* Allow environment variables to be hidden
This change allows environment variables in Nushell to have a value of
`Nothing`, which can be set by the user by passing `$nothing` to
`let-env` and friends.
Environment variables with a value of Nothing behave as if they are not
set at all. This allows a user to shadow the value of an environment
variable in a parent scope, effectively removing it from their current
scope. This was not possible before, because a scope can not affect its
parent scopes.
This is a workaround for issues like #3920.
Additionally, this allows a user to simultaneously set, change and
remove multiple environment variables via `load-env`. Any environment
variables set to $nothing will be hidden and thus act as if they are
removed. This simplifies working with virtual environments, which rely
on setting multiple environment variables, including PATH, to specific
values, and remove/change them on deactivation.
One surprising behavior is that an environment variable set to $nothing
will act as if it is not set when querying it (via $nu.env.X), but it is
still possible to remove it entirely via `unlet-env`. If the same
environment variable is present in the parent scope, the value in the
parent scope will be visible to the user. This might be surprising
behavior to users who are not familiar with the implementation details.
An additional corner case is the the shorthand form of `with-env` does
not work with this feature. Using `X=$nothing` will set $nu.env.X to the
string "$nothing". The long-form works as expected: `with-env [X
$nothing] {...}`.
* Remove unused import
* Allow all primitives to be convert to strings
2021-08-26 13:15:58 +00:00
|
|
|
#[test]
|
|
|
|
fn load_env_can_hide_var_envs() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
$env.DEBUG = "1"
|
2022-02-02 20:59:01 +00:00
|
|
|
echo $env.DEBUG
|
2022-12-22 20:30:10 +00:00
|
|
|
load-env [[name, value]; [DEBUG null]]
|
2022-02-02 20:59:01 +00:00
|
|
|
echo $env.DEBUG
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
Allow environment variables to be hidden (#3950)
* Allow environment variables to be hidden
This change allows environment variables in Nushell to have a value of
`Nothing`, which can be set by the user by passing `$nothing` to
`let-env` and friends.
Environment variables with a value of Nothing behave as if they are not
set at all. This allows a user to shadow the value of an environment
variable in a parent scope, effectively removing it from their current
scope. This was not possible before, because a scope can not affect its
parent scopes.
This is a workaround for issues like #3920.
Additionally, this allows a user to simultaneously set, change and
remove multiple environment variables via `load-env`. Any environment
variables set to $nothing will be hidden and thus act as if they are
removed. This simplifies working with virtual environments, which rely
on setting multiple environment variables, including PATH, to specific
values, and remove/change them on deactivation.
One surprising behavior is that an environment variable set to $nothing
will act as if it is not set when querying it (via $nu.env.X), but it is
still possible to remove it entirely via `unlet-env`. If the same
environment variable is present in the parent scope, the value in the
parent scope will be visible to the user. This might be surprising
behavior to users who are not familiar with the implementation details.
An additional corner case is the the shorthand form of `with-env` does
not work with this feature. Using `X=$nothing` will set $nu.env.X to the
string "$nothing". The long-form works as expected: `with-env [X
$nothing] {...}`.
* Remove unused import
* Allow all primitives to be convert to strings
2021-08-26 13:15:58 +00:00
|
|
|
assert_eq!(actual.out, "1");
|
|
|
|
assert!(actual.err.contains("error"));
|
|
|
|
assert!(actual.err.contains("Unknown column"));
|
|
|
|
}
|
|
|
|
|
2022-12-22 20:30:10 +00:00
|
|
|
//FIXME: jt: load-env can not currently hide variables because null no longer hides
|
2022-02-02 20:59:01 +00:00
|
|
|
#[ignore]
|
Allow environment variables to be hidden (#3950)
* Allow environment variables to be hidden
This change allows environment variables in Nushell to have a value of
`Nothing`, which can be set by the user by passing `$nothing` to
`let-env` and friends.
Environment variables with a value of Nothing behave as if they are not
set at all. This allows a user to shadow the value of an environment
variable in a parent scope, effectively removing it from their current
scope. This was not possible before, because a scope can not affect its
parent scopes.
This is a workaround for issues like #3920.
Additionally, this allows a user to simultaneously set, change and
remove multiple environment variables via `load-env`. Any environment
variables set to $nothing will be hidden and thus act as if they are
removed. This simplifies working with virtual environments, which rely
on setting multiple environment variables, including PATH, to specific
values, and remove/change them on deactivation.
One surprising behavior is that an environment variable set to $nothing
will act as if it is not set when querying it (via $nu.env.X), but it is
still possible to remove it entirely via `unlet-env`. If the same
environment variable is present in the parent scope, the value in the
parent scope will be visible to the user. This might be surprising
behavior to users who are not familiar with the implementation details.
An additional corner case is the the shorthand form of `with-env` does
not work with this feature. Using `X=$nothing` will set $nu.env.X to the
string "$nothing". The long-form works as expected: `with-env [X
$nothing] {...}`.
* Remove unused import
* Allow all primitives to be convert to strings
2021-08-26 13:15:58 +00:00
|
|
|
#[test]
|
|
|
|
fn load_env_can_hide_var_envs_in_parent_scope() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2023-06-30 19:57:51 +00:00
|
|
|
$env.DEBUG = "1"
|
2022-02-02 20:59:01 +00:00
|
|
|
echo $env.DEBUG
|
|
|
|
do {
|
2022-12-22 20:30:10 +00:00
|
|
|
load-env [[name, value]; [DEBUG null]]
|
2022-02-02 20:59:01 +00:00
|
|
|
echo $env.DEBUG
|
|
|
|
}
|
|
|
|
echo $env.DEBUG
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
Allow environment variables to be hidden (#3950)
* Allow environment variables to be hidden
This change allows environment variables in Nushell to have a value of
`Nothing`, which can be set by the user by passing `$nothing` to
`let-env` and friends.
Environment variables with a value of Nothing behave as if they are not
set at all. This allows a user to shadow the value of an environment
variable in a parent scope, effectively removing it from their current
scope. This was not possible before, because a scope can not affect its
parent scopes.
This is a workaround for issues like #3920.
Additionally, this allows a user to simultaneously set, change and
remove multiple environment variables via `load-env`. Any environment
variables set to $nothing will be hidden and thus act as if they are
removed. This simplifies working with virtual environments, which rely
on setting multiple environment variables, including PATH, to specific
values, and remove/change them on deactivation.
One surprising behavior is that an environment variable set to $nothing
will act as if it is not set when querying it (via $nu.env.X), but it is
still possible to remove it entirely via `unlet-env`. If the same
environment variable is present in the parent scope, the value in the
parent scope will be visible to the user. This might be surprising
behavior to users who are not familiar with the implementation details.
An additional corner case is the the shorthand form of `with-env` does
not work with this feature. Using `X=$nothing` will set $nu.env.X to the
string "$nothing". The long-form works as expected: `with-env [X
$nothing] {...}`.
* Remove unused import
* Allow all primitives to be convert to strings
2021-08-26 13:15:58 +00:00
|
|
|
assert_eq!(actual.out, "11");
|
|
|
|
assert!(actual.err.contains("error"));
|
|
|
|
assert!(actual.err.contains("Unknown column"));
|
|
|
|
}
|
|
|
|
|
2021-01-03 07:48:02 +00:00
|
|
|
#[test]
|
2021-05-20 04:26:54 +00:00
|
|
|
fn proper_shadow_let_aliases() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2023-03-16 22:53:46 +00:00
|
|
|
let DEBUG = false; print $DEBUG | table; do { let DEBUG = true; print $DEBUG } | table; print $DEBUG
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-01-03 07:48:02 +00:00
|
|
|
assert_eq!(actual.out, "falsetruefalse");
|
|
|
|
}
|
|
|
|
|
2021-05-20 04:26:54 +00:00
|
|
|
#[test]
|
2021-05-21 07:04:27 +00:00
|
|
|
fn block_params_override() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-05-21 07:04:27 +00:00
|
|
|
[1, 2, 3] | each { |a| echo $it }
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(actual.err.contains("variable not found"));
|
2021-05-20 04:26:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 19:03:57 +00:00
|
|
|
#[test]
|
|
|
|
fn alias_reuse() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("alias foo = echo bob; foo; foo");
|
2022-03-18 19:03:57 +00:00
|
|
|
|
|
|
|
assert!(actual.out.contains("bob"));
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
}
|
|
|
|
|
2021-05-20 04:26:54 +00:00
|
|
|
#[test]
|
2021-05-21 07:04:27 +00:00
|
|
|
fn block_params_override_correct() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-02 20:59:01 +00:00
|
|
|
[1, 2, 3] | each { |a| echo $a } | to json --raw
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-05-21 07:04:27 +00:00
|
|
|
assert_eq!(actual.out, "[1,2,3]");
|
2021-05-20 04:26:54 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 05:14:51 +00:00
|
|
|
#[test]
|
|
|
|
fn hex_number() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-06-06 05:14:51 +00:00
|
|
|
0x10
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-06-06 05:14:51 +00:00
|
|
|
assert_eq!(actual.out, "16");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn binary_number() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-06-06 05:14:51 +00:00
|
|
|
0b10
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-06-06 05:14:51 +00:00
|
|
|
assert_eq!(actual.out, "2");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn octal_number() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-06-06 05:14:51 +00:00
|
|
|
0o10
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-06-06 05:14:51 +00:00
|
|
|
assert_eq!(actual.out, "8");
|
|
|
|
}
|
|
|
|
|
2021-04-21 02:31:54 +00:00
|
|
|
#[test]
|
Restrict closure expression to be something like `{|| ...}` (#8290)
# Description
As title, closes: #7921 closes: #8273
# User-Facing Changes
when define a closure without pipe, nushell will raise error for now:
```
❯ let x = {ss ss}
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #2:1:1]
1 │ let x = {ss ss}
· ───┬───
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`any`, `each`, `all`, `where` command accepts closure, it forces user
input closure like `{||`, or parse error will returned.
```
❯ {major:2, minor:1, patch:4} | values | each { into string }
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #4:1:1]
1 │ {major:2, minor:1, patch:4} | values | each { into string }
· ───────┬───────
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`with-env`, `do`, `def`, `try` are special, they still remain the same,
although it says that it accepts a closure, but they don't need to be
written like `{||`, it's more likely a block but can capture variable
outside of scope:
```
❯ def test [input] { echo [0 1 2] | do { do { echo $input } } }; test aaa
aaa
```
Just realize that It's a big breaking change, we need to update config
and scripts...
# 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-03-17 12:36:28 +00:00
|
|
|
fn run_dynamic_closures() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
Restrict closure expression to be something like `{|| ...}` (#8290)
# Description
As title, closes: #7921 closes: #8273
# User-Facing Changes
when define a closure without pipe, nushell will raise error for now:
```
❯ let x = {ss ss}
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #2:1:1]
1 │ let x = {ss ss}
· ───┬───
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`any`, `each`, `all`, `where` command accepts closure, it forces user
input closure like `{||`, or parse error will returned.
```
❯ {major:2, minor:1, patch:4} | values | each { into string }
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #4:1:1]
1 │ {major:2, minor:1, patch:4} | values | each { into string }
· ───────┬───────
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`with-env`, `do`, `def`, `try` are special, they still remain the same,
although it says that it accepts a closure, but they don't need to be
written like `{||`, it's more likely a block but can capture variable
outside of scope:
```
❯ def test [input] { echo [0 1 2] | do { do { echo $input } } }; test aaa
aaa
```
Just realize that It's a big breaking change, we need to update config
and scripts...
# 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-03-17 12:36:28 +00:00
|
|
|
let closure = {|| echo "holaaaa" }; do $closure
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-04-21 02:31:54 +00:00
|
|
|
assert_eq!(actual.out, "holaaaa");
|
|
|
|
}
|
|
|
|
|
2024-03-11 10:11:08 +00:00
|
|
|
#[test]
|
|
|
|
fn dynamic_closure_type_check() {
|
|
|
|
let actual = nu!(r#"let closure = {|x: int| echo $x}; do $closure "aa""#);
|
|
|
|
assert!(actual.err.contains("can't convert string to int"))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dynamic_closure_optional_arg() {
|
|
|
|
let actual = nu!(r#"let closure = {|x: int = 3| echo $x}; do $closure"#);
|
|
|
|
assert_eq!(actual.out, "3");
|
|
|
|
let actual = nu!(r#"let closure = {|x: int = 3| echo $x}; do $closure 10"#);
|
|
|
|
assert_eq!(actual.out, "10");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dynamic_closure_rest_args() {
|
|
|
|
let actual = nu!(r#"let closure = {|...args| $args | str join ""}; do $closure 1 2 3"#);
|
|
|
|
assert_eq!(actual.out, "123");
|
2024-05-30 13:29:46 +00:00
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
r#"let closure = {|required, ...args| $"($required), ($args | str join "")"}; do $closure 1 2 3"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "1, 23");
|
|
|
|
let actual = nu!(
|
|
|
|
r#"let closure = {|required, optional?, ...args| $"($required), ($optional), ($args | str join "")"}; do $closure 1 2 3"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "1, 2, 3");
|
2024-03-11 10:11:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:57:25 +00:00
|
|
|
#[test]
|
2021-06-07 08:08:35 +00:00
|
|
|
fn argument_subexpression_reports_errors() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("echo (ferris_is_not_here.exe)");
|
2020-05-19 12:57:25 +00:00
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(!actual.err.is_empty());
|
2020-05-19 12:57:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 09:05:53 +00:00
|
|
|
#[test]
|
|
|
|
fn can_process_one_row_from_internal_and_pipes_it_to_stdin_of_external() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#""nushelll" | nu --testbin chop"#);
|
2020-01-16 09:05:53 +00:00
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "nushell");
|
2020-01-16 09:05:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 05:27:10 +00:00
|
|
|
#[test]
|
|
|
|
fn bad_operator() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-05-24 05:27:10 +00:00
|
|
|
2 $ 2
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-05-24 05:27:10 +00:00
|
|
|
|
|
|
|
assert!(actual.err.contains("operator"));
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:14:13 +00:00
|
|
|
#[test]
|
|
|
|
fn index_out_of_bounds() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-01-22 23:14:13 +00:00
|
|
|
let foo = [1, 2, 3]; echo $foo.5
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-01-22 23:14:13 +00:00
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(actual.err.contains("too large"));
|
|
|
|
}
|
|
|
|
|
2021-06-09 17:17:45 +00:00
|
|
|
#[test]
|
2023-09-13 21:53:55 +00:00
|
|
|
fn negative_float_start() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-06-09 17:17:45 +00:00
|
|
|
-1.3 + 4
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-06-09 17:17:45 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "2.7");
|
|
|
|
}
|
2021-08-06 21:49:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_inside_of() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2021-08-06 21:49:37 +00:00
|
|
|
"bob" in "bobby"
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-08-06 21:49:37 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_not_inside_of() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2021-08-06 21:49:37 +00:00
|
|
|
"bob" not-in "bobby"
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2021-08-06 21:49:37 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "false");
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:14:13 +00:00
|
|
|
#[test]
|
|
|
|
fn index_row() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-02 20:59:01 +00:00
|
|
|
let foo = [[name]; [joe] [bob]]; echo $foo.1 | to json --raw
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-02-02 20:59:01 +00:00
|
|
|
|
2024-03-20 21:14:31 +00:00
|
|
|
assert_eq!(actual.out, r#"{"name":"bob"}"#);
|
2021-01-22 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn index_cell() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-01-22 23:14:13 +00:00
|
|
|
let foo = [[name]; [joe] [bob]]; echo $foo.name.1
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-01-22 23:14:13 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "bob");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn index_cell_alt() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-01-22 23:14:13 +00:00
|
|
|
let foo = [[name]; [joe] [bob]]; echo $foo.1.name
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-01-22 23:14:13 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "bob");
|
|
|
|
}
|
|
|
|
|
2021-04-15 19:07:06 +00:00
|
|
|
#[test]
|
|
|
|
fn not_echoing_ranges_without_numbers() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-04-15 19:07:06 +00:00
|
|
|
echo ..
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-04-15 19:07:06 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "..");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_echoing_exclusive_ranges_without_numbers() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-04-15 19:07:06 +00:00
|
|
|
echo ..<
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-04-15 19:07:06 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "..<");
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:07:53 +00:00
|
|
|
#[test]
|
|
|
|
fn echoing_ranges() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-06-19 02:02:01 +00:00
|
|
|
echo 1..3 | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-05-27 18:07:53 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "6");
|
|
|
|
}
|
|
|
|
|
2020-09-13 21:53:08 +00:00
|
|
|
#[test]
|
|
|
|
fn echoing_exclusive_ranges() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-09-13 21:53:08 +00:00
|
|
|
echo 1..<4 | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-09-13 21:53:08 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "6");
|
|
|
|
}
|
|
|
|
|
2020-08-30 04:55:33 +00:00
|
|
|
#[test]
|
|
|
|
fn table_literals1() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-09 10:58:54 +00:00
|
|
|
echo [[name age]; [foo 13]] | get age.0
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-08-30 04:55:33 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "13");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn table_literals2() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-08-30 04:55:33 +00:00
|
|
|
echo [[name age] ; [bob 13] [sally 20]] | get age | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-08-30 04:55:33 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "33");
|
|
|
|
}
|
|
|
|
|
2020-08-30 06:19:54 +00:00
|
|
|
#[test]
|
|
|
|
fn list_with_commas() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-08-30 06:19:54 +00:00
|
|
|
echo [1, 2, 3] | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-08-30 06:19:54 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "6");
|
|
|
|
}
|
|
|
|
|
2020-09-07 02:43:58 +00:00
|
|
|
#[test]
|
|
|
|
fn range_with_left_var() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-02 20:59:01 +00:00
|
|
|
({ size: 3}.size)..10 | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-09-07 02:43:58 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "52");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_with_right_var() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-02 20:59:01 +00:00
|
|
|
4..({ size: 30}.size) | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-09-07 02:43:58 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "459");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_with_open_left() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-09-07 02:43:58 +00:00
|
|
|
echo ..30 | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-09-07 02:43:58 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "465");
|
|
|
|
}
|
|
|
|
|
2020-09-13 21:53:08 +00:00
|
|
|
#[test]
|
|
|
|
fn exclusive_range_with_open_left() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-09-13 21:53:08 +00:00
|
|
|
echo ..<31 | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-09-13 21:53:08 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "465");
|
|
|
|
}
|
|
|
|
|
2020-09-07 02:43:58 +00:00
|
|
|
#[test]
|
|
|
|
fn range_with_open_right() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-09-07 02:43:58 +00:00
|
|
|
echo 5.. | first 10 | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-09-07 02:43:58 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "95");
|
|
|
|
}
|
|
|
|
|
2020-09-13 21:53:08 +00:00
|
|
|
#[test]
|
|
|
|
fn exclusive_range_with_open_right() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-09-13 21:53:08 +00:00
|
|
|
echo 5..< | first 10 | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-09-13 21:53:08 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "95");
|
|
|
|
}
|
|
|
|
|
2020-09-07 17:30:11 +00:00
|
|
|
#[test]
|
|
|
|
fn range_with_mixed_types() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-09-07 17:30:11 +00:00
|
|
|
echo 1..10.5 | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-09-07 17:30:11 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "55");
|
|
|
|
}
|
|
|
|
|
2021-01-29 22:35:18 +00:00
|
|
|
#[test]
|
|
|
|
fn filesize_math() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-02 20:59:01 +00:00
|
|
|
100 * 10kib
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-02-02 20:59:01 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "1000.0 KiB");
|
2021-02-10 02:31:12 +00:00
|
|
|
// why 1000.0 KB instead of 1.0 MB?
|
|
|
|
// looks like `byte.get_appropriate_unit(false)` behaves this way
|
2021-01-29 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesize_math2() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2021-05-12 01:01:48 +00:00
|
|
|
100 / 10kb
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2021-01-29 22:35:18 +00:00
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(actual.err.contains("doesn't support"));
|
2021-01-29 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesize_math3() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-02 20:59:01 +00:00
|
|
|
100kib / 10
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-02-02 20:59:01 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "10.0 KiB");
|
2021-01-29 22:35:18 +00:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn filesize_math4() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-02 20:59:01 +00:00
|
|
|
100kib * 5
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-02-02 20:59:01 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "500.0 KiB");
|
2021-02-10 02:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesize_math5() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-02 20:59:01 +00:00
|
|
|
1000 * 1kib
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-02-02 20:59:01 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "1000.0 KiB");
|
2021-02-10 02:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesize_math6() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-02 20:59:01 +00:00
|
|
|
1000 * 1mib
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-02-02 20:59:01 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "1000.0 MiB");
|
2021-02-10 02:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesize_math7() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2022-02-02 20:59:01 +00:00
|
|
|
1000 * 1gib
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2022-02-02 20:59:01 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "1000.0 GiB");
|
2021-01-29 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 21:53:08 +00:00
|
|
|
#[test]
|
|
|
|
fn exclusive_range_with_mixed_types() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-09-13 21:53:08 +00:00
|
|
|
echo 1..<10.5 | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-09-13 21:53:08 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "55");
|
|
|
|
}
|
|
|
|
|
2020-08-30 06:19:54 +00:00
|
|
|
#[test]
|
|
|
|
fn table_with_commas() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-08-30 06:19:54 +00:00
|
|
|
echo [[name, age, height]; [JT, 42, 185] [Unknown, 99, 99]] | get age | math sum
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-08-30 06:19:54 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "141");
|
|
|
|
}
|
|
|
|
|
2021-06-04 16:54:18 +00:00
|
|
|
#[test]
|
|
|
|
fn duration_overflow() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
|
|
|
"
|
2022-02-17 11:40:24 +00:00
|
|
|
ls | get modified | each { |it| $it + 10000000000000000day }
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
|
|
|
));
|
2021-06-04 16:54:18 +00:00
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(actual.err.contains("duration too large"));
|
2021-06-04 16:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn date_and_duration_overflow() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
|
|
|
"
|
2022-02-17 11:40:24 +00:00
|
|
|
ls | get modified | each { |it| $it + 1000000000day }
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
|
|
|
));
|
2021-06-04 16:54:18 +00:00
|
|
|
|
|
|
|
// assert_eq!(actual.err, "overflow");
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(actual.err.contains("duration too large"));
|
2021-06-04 16:54:18 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 00:31:01 +00:00
|
|
|
#[test]
|
|
|
|
fn pipeline_params_simple() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
|
|
|
"
|
2021-06-21 00:31:01 +00:00
|
|
|
echo 1 2 3 | $in.1 * $in.2
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
|
|
|
));
|
2021-06-21 00:31:01 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "6");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pipeline_params_inner() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
|
|
|
"
|
2021-06-21 00:31:01 +00:00
|
|
|
echo 1 2 3 | (echo $in.2 6 7 | $in.0 * $in.1 * $in.2)
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
|
|
|
));
|
2021-06-21 00:31:01 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "126");
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:50:24 +00:00
|
|
|
#[test]
|
|
|
|
fn better_table_lex() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
|
|
|
"
|
2021-06-25 05:50:24 +00:00
|
|
|
let table = [
|
|
|
|
[name, size];
|
|
|
|
[small, 7]
|
|
|
|
[medium, 10]
|
|
|
|
[large, 12]
|
|
|
|
];
|
|
|
|
$table.1.size
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
|
|
|
));
|
2021-06-25 05:50:24 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "10");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn better_subexpr_lex() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
|
|
|
"
|
2021-06-25 05:50:24 +00:00
|
|
|
(echo boo
|
|
|
|
sam | str length | math sum)
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
|
|
|
));
|
2021-06-25 05:50:24 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "6");
|
|
|
|
}
|
|
|
|
|
2021-06-25 21:09:06 +00:00
|
|
|
#[test]
|
|
|
|
fn subsubcommand() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
2021-06-25 21:09:06 +00:00
|
|
|
r#"
|
|
|
|
def "aws s3 rb" [url] { $url + " loaded" }; aws s3 rb localhost
|
2023-07-12 17:07:20 +00:00
|
|
|
"#
|
|
|
|
));
|
2021-06-25 21:09:06 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "localhost loaded");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn manysubcommand() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
2021-06-25 21:09:06 +00:00
|
|
|
r#"
|
|
|
|
def "aws s3 rb ax vf qqqq rrrr" [url] { $url + " loaded" }; aws s3 rb ax vf qqqq rrrr localhost
|
2023-07-12 17:07:20 +00:00
|
|
|
"#
|
|
|
|
));
|
2021-06-25 21:09:06 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "localhost loaded");
|
|
|
|
}
|
|
|
|
|
2021-07-07 19:21:02 +00:00
|
|
|
#[test]
|
|
|
|
fn nothing_string_1() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
2021-07-07 19:21:02 +00:00
|
|
|
r#"
|
2022-12-22 20:30:10 +00:00
|
|
|
null == "foo"
|
2023-07-12 17:07:20 +00:00
|
|
|
"#
|
|
|
|
));
|
2021-07-07 19:21:02 +00:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "false");
|
|
|
|
}
|
|
|
|
|
2021-08-17 13:56:35 +00:00
|
|
|
#[test]
|
2022-08-13 09:55:06 +00:00
|
|
|
fn hide_alias_shadowing() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
|
|
|
"
|
2021-08-19 07:05:36 +00:00
|
|
|
def test-shadowing [] {
|
|
|
|
alias greet = echo hello;
|
Restrict closure expression to be something like `{|| ...}` (#8290)
# Description
As title, closes: #7921 closes: #8273
# User-Facing Changes
when define a closure without pipe, nushell will raise error for now:
```
❯ let x = {ss ss}
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #2:1:1]
1 │ let x = {ss ss}
· ───┬───
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`any`, `each`, `all`, `where` command accepts closure, it forces user
input closure like `{||`, or parse error will returned.
```
❯ {major:2, minor:1, patch:4} | values | each { into string }
Error: nu::parser::closure_missing_pipe
× Missing || inside closure
╭─[entry #4:1:1]
1 │ {major:2, minor:1, patch:4} | values | each { into string }
· ───────┬───────
· ╰── Parsing as a closure, but || is missing
╰────
help: Try add || to the beginning of closure
```
`with-env`, `do`, `def`, `try` are special, they still remain the same,
although it says that it accepts a closure, but they don't need to be
written like `{||`, it's more likely a block but can capture variable
outside of scope:
```
❯ def test [input] { echo [0 1 2] | do { do { echo $input } } }; test aaa
aaa
```
Just realize that It's a big breaking change, we need to update config
and scripts...
# 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-03-17 12:36:28 +00:00
|
|
|
let xyz = {|| greet };
|
2022-08-13 09:55:06 +00:00
|
|
|
hide greet;
|
2021-08-19 07:05:36 +00:00
|
|
|
do $xyz
|
|
|
|
};
|
|
|
|
test-shadowing
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
|
|
|
));
|
2021-08-19 07:05:36 +00:00
|
|
|
assert_eq!(actual.out, "hello");
|
|
|
|
}
|
2021-08-17 13:56:35 +00:00
|
|
|
|
2022-08-13 09:55:06 +00:00
|
|
|
// FIXME: Seems like subexpression are no longer scoped. Should we remove this test?
|
2022-02-02 20:59:01 +00:00
|
|
|
#[ignore]
|
2021-08-19 07:05:36 +00:00
|
|
|
#[test]
|
2022-08-13 09:55:06 +00:00
|
|
|
fn hide_alias_does_not_escape_scope() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
|
|
|
"
|
2021-08-19 07:05:36 +00:00
|
|
|
def test-alias [] {
|
|
|
|
alias greet = echo hello;
|
2022-08-13 09:55:06 +00:00
|
|
|
(hide greet);
|
2021-08-19 07:05:36 +00:00
|
|
|
greet
|
|
|
|
};
|
|
|
|
test-alias
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
|
|
|
));
|
2021-08-19 07:05:36 +00:00
|
|
|
assert_eq!(actual.out, "hello");
|
2021-08-17 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 17:55:34 +00:00
|
|
|
#[test]
|
2022-08-13 09:55:06 +00:00
|
|
|
fn hide_alias_hides_alias() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(pipeline(
|
|
|
|
"
|
2021-08-19 07:05:36 +00:00
|
|
|
def test-alias [] {
|
|
|
|
alias ll = ls -l;
|
2022-08-13 09:55:06 +00:00
|
|
|
hide ll;
|
2021-08-19 07:05:36 +00:00
|
|
|
ll
|
|
|
|
};
|
|
|
|
test-alias
|
2023-07-12 17:07:20 +00:00
|
|
|
"
|
|
|
|
));
|
2021-08-17 17:55:34 +00:00
|
|
|
|
Rewrite run_external.rs (#12921)
This PR is a complete rewrite of `run_external.rs`. The main goal of the
rewrite is improving readability, but it also fixes some bugs related to
argument handling and the PATH variable (fixes
https://github.com/nushell/nushell/issues/6011).
I'll discuss some technical details to make reviewing easier.
## Argument handling
Quoting arguments for external commands is hard. Like, *really* hard.
We've had more than a dozen issues and PRs dedicated to quoting
arguments (see Appendix) but the current implementation is still buggy.
Here's a demonstration of the buggy behavior:
```nu
let foo = "'bar'"
^touch $foo # This creates a file named `bar`, but it should be `'bar'`
^touch ...[ "'bar'" ] # Same
```
I'll describe how this PR deals with argument handling.
First, we'll introduce the concept of **bare strings**. Bare strings are
**string literals** that are either **unquoted** or **quoted by
backticks** [^1]. Strings within a list literal are NOT considered bare
strings, even if they are unquoted or quoted by backticks.
When a bare string is used as an argument to external process, we need
to perform tilde-expansion, glob-expansion, and inner-quotes-removal, in
that order. "Inner-quotes-removal" means transforming from
`--option="value"` into `--option=value`.
## `.bat` files and CMD built-ins
On Windows, `.bat` files and `.cmd` files are considered executable, but
they need `CMD.exe` as the interpreter. The Rust standard library
supports running `.bat` files directly and will spawn `CMD.exe` under
the hood (see
[documentation](https://doc.rust-lang.org/std/process/index.html#windows-argument-splitting)).
However, other extensions are not supported [^2].
Nushell also supports a selected number of CMD built-ins. The problem
with CMD is that it uses a different set of quoting rules. Correctly
quoting for CMD requires using
[Command::raw_arg()](https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html#tymethod.raw_arg)
and manually quoting CMD special characters, on top of quoting from the
Nushell side. ~~I decided that this is too complex and chose to reject
special characters in CMD built-ins instead [^3]. Hopefully this will
not affact real-world use cases.~~ I've implemented escaping that works
reasonably well.
## `which-support` feature
The `which` crate is now a hard dependency of `nu-command`, making the
`which-support` feature essentially useless. The `which` crate is
already a hard dependency of `nu-cli`, and we should consider removing
the `which-support` feature entirely.
## Appendix
Here's a list of quoting-related issues and PRs in rough chronological
order.
* https://github.com/nushell/nushell/issues/4609
* https://github.com/nushell/nushell/issues/4631
* https://github.com/nushell/nushell/issues/4601
* https://github.com/nushell/nushell/pull/5846
* https://github.com/nushell/nushell/issues/5978
* https://github.com/nushell/nushell/pull/6014
* https://github.com/nushell/nushell/issues/6154
* https://github.com/nushell/nushell/pull/6161
* https://github.com/nushell/nushell/issues/6399
* https://github.com/nushell/nushell/pull/6420
* https://github.com/nushell/nushell/pull/6426
* https://github.com/nushell/nushell/issues/6465
* https://github.com/nushell/nushell/issues/6559
* https://github.com/nushell/nushell/pull/6560
[^1]: The idea that backtick-quoted strings act like bare strings was
introduced by Kubouch and briefly mentioned in [the language
reference](https://www.nushell.sh/lang-guide/chapters/strings_and_text.html#backtick-quotes).
[^2]: The documentation also said "running .bat scripts in this way may
be removed in the future and so should not be relied upon", which is
another reason to move away from this. But again, quoting for CMD is
hard.
[^3]: If anyone wants to try, the best resource I found on the topic is
[this](https://daviddeley.com/autohotkey/parameters/parameters.htm).
2024-05-23 02:05:27 +00:00
|
|
|
assert!(
|
|
|
|
actual.err.contains("Command `ll` not found") && actual.err.contains("Did you mean `all`?")
|
|
|
|
);
|
2021-08-17 17:55:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 07:34:43 +00:00
|
|
|
mod parse {
|
2020-05-07 11:03:43 +00:00
|
|
|
use nu_test_support::nu;
|
2020-02-13 07:34:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
The debug command's signature is:
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
> debug {flags}
|
|
|
|
|
|
|
|
flags:
|
2022-10-26 16:36:42 +00:00
|
|
|
-h, --help: Display the help message for this command
|
2020-02-13 07:34:43 +00:00
|
|
|
-r, --raw: Prints the raw value representation.
|
|
|
|
*/
|
|
|
|
|
2020-02-18 06:58:30 +00:00
|
|
|
#[test]
|
|
|
|
fn errors_if_flag_passed_is_not_exact() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("debug -ra");
|
2020-02-18 06:58:30 +00:00
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(actual.err.contains("unknown flag"),);
|
|
|
|
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("debug --rawx");
|
2022-02-02 20:59:01 +00:00
|
|
|
|
|
|
|
assert!(actual.err.contains("unknown flag"),);
|
2020-02-18 06:58:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 07:34:43 +00:00
|
|
|
#[test]
|
|
|
|
fn errors_if_flag_is_not_supported() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("debug --ferris");
|
2020-02-13 07:34:43 +00:00
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(actual.err.contains("unknown flag"),);
|
2020-02-13 07:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn errors_if_passed_an_unexpected_argument() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("debug ferris");
|
2020-02-13 07:34:43 +00:00
|
|
|
|
2022-02-02 20:59:01 +00:00
|
|
|
assert!(actual.err.contains("extra positional argument"),);
|
2020-02-13 07:34:43 +00:00
|
|
|
}
|
2023-02-16 02:30:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ensure_backticks_are_bareword_command() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("`8abc123`");
|
2023-02-16 02:30:56 +00:00
|
|
|
|
Rewrite run_external.rs (#12921)
This PR is a complete rewrite of `run_external.rs`. The main goal of the
rewrite is improving readability, but it also fixes some bugs related to
argument handling and the PATH variable (fixes
https://github.com/nushell/nushell/issues/6011).
I'll discuss some technical details to make reviewing easier.
## Argument handling
Quoting arguments for external commands is hard. Like, *really* hard.
We've had more than a dozen issues and PRs dedicated to quoting
arguments (see Appendix) but the current implementation is still buggy.
Here's a demonstration of the buggy behavior:
```nu
let foo = "'bar'"
^touch $foo # This creates a file named `bar`, but it should be `'bar'`
^touch ...[ "'bar'" ] # Same
```
I'll describe how this PR deals with argument handling.
First, we'll introduce the concept of **bare strings**. Bare strings are
**string literals** that are either **unquoted** or **quoted by
backticks** [^1]. Strings within a list literal are NOT considered bare
strings, even if they are unquoted or quoted by backticks.
When a bare string is used as an argument to external process, we need
to perform tilde-expansion, glob-expansion, and inner-quotes-removal, in
that order. "Inner-quotes-removal" means transforming from
`--option="value"` into `--option=value`.
## `.bat` files and CMD built-ins
On Windows, `.bat` files and `.cmd` files are considered executable, but
they need `CMD.exe` as the interpreter. The Rust standard library
supports running `.bat` files directly and will spawn `CMD.exe` under
the hood (see
[documentation](https://doc.rust-lang.org/std/process/index.html#windows-argument-splitting)).
However, other extensions are not supported [^2].
Nushell also supports a selected number of CMD built-ins. The problem
with CMD is that it uses a different set of quoting rules. Correctly
quoting for CMD requires using
[Command::raw_arg()](https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html#tymethod.raw_arg)
and manually quoting CMD special characters, on top of quoting from the
Nushell side. ~~I decided that this is too complex and chose to reject
special characters in CMD built-ins instead [^3]. Hopefully this will
not affact real-world use cases.~~ I've implemented escaping that works
reasonably well.
## `which-support` feature
The `which` crate is now a hard dependency of `nu-command`, making the
`which-support` feature essentially useless. The `which` crate is
already a hard dependency of `nu-cli`, and we should consider removing
the `which-support` feature entirely.
## Appendix
Here's a list of quoting-related issues and PRs in rough chronological
order.
* https://github.com/nushell/nushell/issues/4609
* https://github.com/nushell/nushell/issues/4631
* https://github.com/nushell/nushell/issues/4601
* https://github.com/nushell/nushell/pull/5846
* https://github.com/nushell/nushell/issues/5978
* https://github.com/nushell/nushell/pull/6014
* https://github.com/nushell/nushell/issues/6154
* https://github.com/nushell/nushell/pull/6161
* https://github.com/nushell/nushell/issues/6399
* https://github.com/nushell/nushell/pull/6420
* https://github.com/nushell/nushell/pull/6426
* https://github.com/nushell/nushell/issues/6465
* https://github.com/nushell/nushell/issues/6559
* https://github.com/nushell/nushell/pull/6560
[^1]: The idea that backtick-quoted strings act like bare strings was
introduced by Kubouch and briefly mentioned in [the language
reference](https://www.nushell.sh/lang-guide/chapters/strings_and_text.html#backtick-quotes).
[^2]: The documentation also said "running .bat scripts in this way may
be removed in the future and so should not be relied upon", which is
another reason to move away from this. But again, quoting for CMD is
hard.
[^3]: If anyone wants to try, the best resource I found on the topic is
[this](https://daviddeley.com/autohotkey/parameters/parameters.htm).
2024-05-23 02:05:27 +00:00
|
|
|
assert!(actual.err.contains("Command `8abc123` not found"),);
|
2023-02-16 02:30:56 +00:00
|
|
|
}
|
2020-02-13 07:34:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 09:05:53 +00:00
|
|
|
mod tilde_expansion {
|
2020-05-16 07:25:18 +00:00
|
|
|
use nu_test_support::nu;
|
2020-01-16 09:05:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn as_home_directory_when_passed_as_argument_and_begins_with_tilde() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!("
|
2020-01-16 09:05:53 +00:00
|
|
|
echo ~
|
2023-07-12 17:07:20 +00:00
|
|
|
");
|
2020-01-16 09:05:53 +00:00
|
|
|
|
2021-03-26 08:26:57 +00:00
|
|
|
assert!(!actual.out.contains('~'),);
|
2020-01-16 09:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_expand_when_passed_as_argument_and_does_not_start_with_tilde() {
|
2023-07-12 17:07:20 +00:00
|
|
|
let actual = nu!(r#"
|
2020-01-16 09:05:53 +00:00
|
|
|
echo "1~1"
|
2023-07-12 17:07:20 +00:00
|
|
|
"#);
|
2020-01-16 09:05:53 +00:00
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "1~1");
|
2020-01-16 09:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 18:21:11 +00:00
|
|
|
|
|
|
|
mod variable_scoping {
|
|
|
|
use nu_test_support::nu;
|
|
|
|
|
2024-05-03 08:35:37 +00:00
|
|
|
fn test_variable_scope(code: &str, expected: &str) {
|
|
|
|
let actual = nu!(code);
|
|
|
|
assert_eq!(actual.out, expected);
|
2021-01-19 18:21:11 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 08:35:37 +00:00
|
|
|
fn test_variable_scope_list(code: &str, expected: &[&str]) {
|
|
|
|
let actual = nu!(code);
|
|
|
|
let result: Vec<&str> = actual.out.matches("ZZZ").collect();
|
|
|
|
assert_eq!(result, expected);
|
2021-01-19 18:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn access_variables_in_scopes() {
|
2024-05-03 08:35:37 +00:00
|
|
|
test_variable_scope(
|
2023-07-12 17:07:20 +00:00
|
|
|
" def test [input] { echo [0 1 2] | do { do { echo $input } } }
|
2024-05-03 08:35:37 +00:00
|
|
|
test ZZZ ",
|
|
|
|
"ZZZ",
|
2021-01-19 18:21:11 +00:00
|
|
|
);
|
2024-05-03 08:35:37 +00:00
|
|
|
test_variable_scope(
|
2022-02-02 20:59:01 +00:00
|
|
|
r#" def test [input] { echo [0 1 2] | do { do { if $input == "ZZZ" { echo $input } else { echo $input } } } }
|
2024-05-03 08:35:37 +00:00
|
|
|
test ZZZ "#,
|
|
|
|
"ZZZ",
|
2021-01-19 18:21:11 +00:00
|
|
|
);
|
2024-05-03 08:35:37 +00:00
|
|
|
test_variable_scope(
|
2022-02-02 20:59:01 +00:00
|
|
|
r#" def test [input] { echo [0 1 2] | do { do { if $input == "ZZZ" { echo $input } else { echo $input } } } }
|
2024-05-03 08:35:37 +00:00
|
|
|
test ZZZ "#,
|
|
|
|
"ZZZ",
|
2021-01-19 18:21:11 +00:00
|
|
|
);
|
2024-05-03 08:35:37 +00:00
|
|
|
test_variable_scope(
|
2023-07-12 17:07:20 +00:00
|
|
|
" def test [input] { echo [0 1 2] | do { echo $input } }
|
2024-05-03 08:35:37 +00:00
|
|
|
test ZZZ ",
|
|
|
|
"ZZZ",
|
2021-01-19 18:21:11 +00:00
|
|
|
);
|
2024-05-03 08:35:37 +00:00
|
|
|
test_variable_scope(
|
2023-07-12 17:07:20 +00:00
|
|
|
" def test [input] { echo [0 1 2] | do { if $input == $input { echo $input } else { echo $input } } }
|
2024-05-03 08:35:37 +00:00
|
|
|
test ZZZ ",
|
|
|
|
"ZZZ"
|
2021-01-19 18:21:11 +00:00
|
|
|
);
|
2024-05-03 08:35:37 +00:00
|
|
|
test_variable_scope_list(
|
2023-07-12 17:07:20 +00:00
|
|
|
" def test [input] { echo [0 1 2] | each { |_| echo $input } }
|
2024-05-03 08:35:37 +00:00
|
|
|
test ZZZ ",
|
|
|
|
&["ZZZ", "ZZZ", "ZZZ"],
|
2021-01-19 18:21:11 +00:00
|
|
|
);
|
2024-05-03 08:35:37 +00:00
|
|
|
test_variable_scope_list(
|
2023-07-12 17:07:20 +00:00
|
|
|
" def test [input] { echo [0 1 2] | each { |it| if $it > 0 {echo $input} else {echo $input}} }
|
2024-05-03 08:35:37 +00:00
|
|
|
test ZZZ ",
|
|
|
|
&["ZZZ", "ZZZ", "ZZZ"],
|
2021-01-19 18:21:11 +00:00
|
|
|
);
|
2024-05-03 08:35:37 +00:00
|
|
|
test_variable_scope_list(
|
2023-07-12 17:07:20 +00:00
|
|
|
" def test [input] { echo [0 1 2] | each { |_| if $input == $input {echo $input} else {echo $input}} }
|
2024-05-03 08:35:37 +00:00
|
|
|
test ZZZ ",
|
|
|
|
&["ZZZ", "ZZZ", "ZZZ"],
|
2021-01-19 18:21:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-07-27 19:40:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pipe_input_to_print() {
|
|
|
|
let actual = nu!(r#""foo" | print"#);
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
}
|
2023-10-02 18:13:31 +00:00
|
|
|
|
2024-02-08 17:30:46 +00:00
|
|
|
#[test]
|
|
|
|
fn err_pipe_input_to_print() {
|
|
|
|
let actual = nu!(r#""foo" e>| print"#);
|
Replace `ExternalStream` with new `ByteStream` type (#12774)
# Description
This PR introduces a `ByteStream` type which is a `Read`-able stream of
bytes. Internally, it has an enum over three different byte stream
sources:
```rust
pub enum ByteStreamSource {
Read(Box<dyn Read + Send + 'static>),
File(File),
Child(ChildProcess),
}
```
This is in comparison to the current `RawStream` type, which is an
`Iterator<Item = Vec<u8>>` and has to allocate for each read chunk.
Currently, `PipelineData::ExternalStream` serves a weird dual role where
it is either external command output or a wrapper around `RawStream`.
`ByteStream` makes this distinction more clear (via `ByteStreamSource`)
and replaces `PipelineData::ExternalStream` in this PR:
```rust
pub enum PipelineData {
Empty,
Value(Value, Option<PipelineMetadata>),
ListStream(ListStream, Option<PipelineMetadata>),
ByteStream(ByteStream, Option<PipelineMetadata>),
}
```
The PR is relatively large, but a decent amount of it is just repetitive
changes.
This PR fixes #7017, fixes #10763, and fixes #12369.
This PR also improves performance when piping external commands. Nushell
should, in most cases, have competitive pipeline throughput compared to,
e.g., bash.
| Command | Before (MB/s) | After (MB/s) | Bash (MB/s) |
| -------------------------------------------------- | -------------:|
------------:| -----------:|
| `throughput \| rg 'x'` | 3059 | 3744 | 3739 |
| `throughput \| nu --testbin relay o> /dev/null` | 3508 | 8087 | 8136 |
# User-Facing Changes
- This is a breaking change for the plugin communication protocol,
because the `ExternalStreamInfo` was replaced with `ByteStreamInfo`.
Plugins now only have to deal with a single input stream, as opposed to
the previous three streams: stdout, stderr, and exit code.
- The output of `describe` has been changed for external/byte streams.
- Temporary breaking change: `bytes starts-with` no longer works with
byte streams. This is to keep the PR smaller, and `bytes ends-with`
already does not work on byte streams.
- If a process core dumped, then instead of having a `Value::Error` in
the `exit_code` column of the output returned from `complete`, it now is
a `Value::Int` with the negation of the signal number.
# After Submitting
- Update docs and book as necessary
- Release notes (e.g., plugin protocol changes)
- Adapt/convert commands to work with byte streams (high priority is
`str length`, `bytes starts-with`, and maybe `bytes ends-with`).
- Refactor the `tee` code, Devyn has already done some work on this.
---------
Co-authored-by: Devyn Cairns <devyn.cairns@gmail.com>
2024-05-16 14:11:18 +00:00
|
|
|
assert!(actual.err.contains("only works on external commands"));
|
2024-02-08 17:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn outerr_pipe_input_to_print() {
|
|
|
|
let actual = nu!(r#""foo" o+e>| print"#);
|
Replace `ExternalStream` with new `ByteStream` type (#12774)
# Description
This PR introduces a `ByteStream` type which is a `Read`-able stream of
bytes. Internally, it has an enum over three different byte stream
sources:
```rust
pub enum ByteStreamSource {
Read(Box<dyn Read + Send + 'static>),
File(File),
Child(ChildProcess),
}
```
This is in comparison to the current `RawStream` type, which is an
`Iterator<Item = Vec<u8>>` and has to allocate for each read chunk.
Currently, `PipelineData::ExternalStream` serves a weird dual role where
it is either external command output or a wrapper around `RawStream`.
`ByteStream` makes this distinction more clear (via `ByteStreamSource`)
and replaces `PipelineData::ExternalStream` in this PR:
```rust
pub enum PipelineData {
Empty,
Value(Value, Option<PipelineMetadata>),
ListStream(ListStream, Option<PipelineMetadata>),
ByteStream(ByteStream, Option<PipelineMetadata>),
}
```
The PR is relatively large, but a decent amount of it is just repetitive
changes.
This PR fixes #7017, fixes #10763, and fixes #12369.
This PR also improves performance when piping external commands. Nushell
should, in most cases, have competitive pipeline throughput compared to,
e.g., bash.
| Command | Before (MB/s) | After (MB/s) | Bash (MB/s) |
| -------------------------------------------------- | -------------:|
------------:| -----------:|
| `throughput \| rg 'x'` | 3059 | 3744 | 3739 |
| `throughput \| nu --testbin relay o> /dev/null` | 3508 | 8087 | 8136 |
# User-Facing Changes
- This is a breaking change for the plugin communication protocol,
because the `ExternalStreamInfo` was replaced with `ByteStreamInfo`.
Plugins now only have to deal with a single input stream, as opposed to
the previous three streams: stdout, stderr, and exit code.
- The output of `describe` has been changed for external/byte streams.
- Temporary breaking change: `bytes starts-with` no longer works with
byte streams. This is to keep the PR smaller, and `bytes ends-with`
already does not work on byte streams.
- If a process core dumped, then instead of having a `Value::Error` in
the `exit_code` column of the output returned from `complete`, it now is
a `Value::Int` with the negation of the signal number.
# After Submitting
- Update docs and book as necessary
- Release notes (e.g., plugin protocol changes)
- Adapt/convert commands to work with byte streams (high priority is
`str length`, `bytes starts-with`, and maybe `bytes ends-with`).
- Refactor the `tee` code, Devyn has already done some work on this.
---------
Co-authored-by: Devyn Cairns <devyn.cairns@gmail.com>
2024-05-16 14:11:18 +00:00
|
|
|
assert!(actual.err.contains("only works on external commands"));
|
2024-02-08 17:30:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 18:13:31 +00:00
|
|
|
#[test]
|
|
|
|
fn command_not_found_error_shows_not_found_2() {
|
|
|
|
let actual = nu!(r#"
|
|
|
|
export def --wrapped my-foo [...rest] { foo };
|
|
|
|
my-foo
|
|
|
|
"#);
|
Rewrite run_external.rs (#12921)
This PR is a complete rewrite of `run_external.rs`. The main goal of the
rewrite is improving readability, but it also fixes some bugs related to
argument handling and the PATH variable (fixes
https://github.com/nushell/nushell/issues/6011).
I'll discuss some technical details to make reviewing easier.
## Argument handling
Quoting arguments for external commands is hard. Like, *really* hard.
We've had more than a dozen issues and PRs dedicated to quoting
arguments (see Appendix) but the current implementation is still buggy.
Here's a demonstration of the buggy behavior:
```nu
let foo = "'bar'"
^touch $foo # This creates a file named `bar`, but it should be `'bar'`
^touch ...[ "'bar'" ] # Same
```
I'll describe how this PR deals with argument handling.
First, we'll introduce the concept of **bare strings**. Bare strings are
**string literals** that are either **unquoted** or **quoted by
backticks** [^1]. Strings within a list literal are NOT considered bare
strings, even if they are unquoted or quoted by backticks.
When a bare string is used as an argument to external process, we need
to perform tilde-expansion, glob-expansion, and inner-quotes-removal, in
that order. "Inner-quotes-removal" means transforming from
`--option="value"` into `--option=value`.
## `.bat` files and CMD built-ins
On Windows, `.bat` files and `.cmd` files are considered executable, but
they need `CMD.exe` as the interpreter. The Rust standard library
supports running `.bat` files directly and will spawn `CMD.exe` under
the hood (see
[documentation](https://doc.rust-lang.org/std/process/index.html#windows-argument-splitting)).
However, other extensions are not supported [^2].
Nushell also supports a selected number of CMD built-ins. The problem
with CMD is that it uses a different set of quoting rules. Correctly
quoting for CMD requires using
[Command::raw_arg()](https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html#tymethod.raw_arg)
and manually quoting CMD special characters, on top of quoting from the
Nushell side. ~~I decided that this is too complex and chose to reject
special characters in CMD built-ins instead [^3]. Hopefully this will
not affact real-world use cases.~~ I've implemented escaping that works
reasonably well.
## `which-support` feature
The `which` crate is now a hard dependency of `nu-command`, making the
`which-support` feature essentially useless. The `which` crate is
already a hard dependency of `nu-cli`, and we should consider removing
the `which-support` feature entirely.
## Appendix
Here's a list of quoting-related issues and PRs in rough chronological
order.
* https://github.com/nushell/nushell/issues/4609
* https://github.com/nushell/nushell/issues/4631
* https://github.com/nushell/nushell/issues/4601
* https://github.com/nushell/nushell/pull/5846
* https://github.com/nushell/nushell/issues/5978
* https://github.com/nushell/nushell/pull/6014
* https://github.com/nushell/nushell/issues/6154
* https://github.com/nushell/nushell/pull/6161
* https://github.com/nushell/nushell/issues/6399
* https://github.com/nushell/nushell/pull/6420
* https://github.com/nushell/nushell/pull/6426
* https://github.com/nushell/nushell/issues/6465
* https://github.com/nushell/nushell/issues/6559
* https://github.com/nushell/nushell/pull/6560
[^1]: The idea that backtick-quoted strings act like bare strings was
introduced by Kubouch and briefly mentioned in [the language
reference](https://www.nushell.sh/lang-guide/chapters/strings_and_text.html#backtick-quotes).
[^2]: The documentation also said "running .bat scripts in this way may
be removed in the future and so should not be relied upon", which is
another reason to move away from this. But again, quoting for CMD is
hard.
[^3]: If anyone wants to try, the best resource I found on the topic is
[this](https://daviddeley.com/autohotkey/parameters/parameters.htm).
2024-05-23 02:05:27 +00:00
|
|
|
assert!(
|
|
|
|
actual.err.contains("Command `foo` not found")
|
|
|
|
&& actual.err.contains("Did you mean `for`?")
|
|
|
|
);
|
2023-10-02 18:13:31 +00:00
|
|
|
}
|
2024-07-09 12:11:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_on_out_greater_pipe() {
|
|
|
|
let actual = nu!(r#""foo" o>| print"#);
|
|
|
|
assert!(actual
|
|
|
|
.err
|
|
|
|
.contains("Redirecting stdout to a pipe is the same as normal piping"))
|
|
|
|
}
|