2020-01-16 10:05:53 +01:00
|
|
|
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
|
|
|
use nu_test_support::nu;
|
2020-05-16 09:25:18 +02:00
|
|
|
use nu_test_support::pipeline;
|
|
|
|
use nu_test_support::playground::Playground;
|
2020-01-16 10:05:53 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn takes_rows_of_nu_value_strings_and_pipes_it_to_stdin_of_external() {
|
|
|
|
Playground::setup("internal_to_external_pipe_test_1", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"nu_times.csv",
|
|
|
|
r#"
|
|
|
|
name,rusty_luck,origin
|
|
|
|
Jason,1,Canada
|
|
|
|
Jonathan,1,New Zealand
|
|
|
|
Andrés,1,Ecuador
|
|
|
|
AndKitKatz,1,Estados Unidos
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
open nu_times.csv
|
2020-05-26 00:50:54 +02:00
|
|
|
| get origin
|
2022-02-17 12:40:24 +01:00
|
|
|
| each { |it| ^echo $it | nu --testbin chop }
|
2022-02-02 21:59:01 +01:00
|
|
|
| get 2
|
2020-01-16 10:05:53 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-26 00:50:54 +02:00
|
|
|
// chop will remove the last escaped double quote from \"Estados Unidos\"
|
|
|
|
assert_eq!(actual.out, "Ecuado");
|
2020-01-16 10:05:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-19 22:18:29 +02:00
|
|
|
#[test]
|
|
|
|
fn treats_dot_dot_as_path_not_range() {
|
|
|
|
Playground::setup("dot_dot_dir", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"nu_times.csv",
|
|
|
|
r#"
|
|
|
|
name,rusty_luck,origin
|
|
|
|
Jason,1,Canada
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
mkdir temp;
|
|
|
|
cd temp;
|
2022-02-02 21:59:01 +01:00
|
|
|
echo (open ../nu_times.csv).name.0 | table;
|
2021-04-19 22:18:29 +02:00
|
|
|
cd ..;
|
|
|
|
rmdir temp
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
// chop will remove the last escaped double quote from \"Estados Unidos\"
|
|
|
|
assert_eq!(actual.out, "Jason");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-02 09:49:14 +02:00
|
|
|
#[test]
|
2021-06-07 10:08:35 +02:00
|
|
|
fn subexpression_properly_redirects() {
|
2020-06-27 23:04:57 +02:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-09-11 10:48:27 +02:00
|
|
|
echo (nu --testbin cococo "hello") | str join
|
2020-06-27 23:04:57 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello");
|
|
|
|
}
|
|
|
|
|
2020-05-16 09:25:18 +02:00
|
|
|
#[test]
|
2021-06-07 10:08:35 +02:00
|
|
|
fn argument_subexpression() {
|
2020-05-16 09:25:18 +02:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-17 12:40:24 +01:00
|
|
|
echo "foo" | each { |it| echo (echo $it) }
|
2020-06-27 23:04:57 +02:00
|
|
|
"#
|
2020-05-16 09:25:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
}
|
|
|
|
|
2022-11-10 21:05:34 +01:00
|
|
|
#[test]
|
|
|
|
fn for_loop() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
for i in 1..3 { print $i }
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "123");
|
|
|
|
}
|
|
|
|
|
2020-05-16 09:25:18 +02:00
|
|
|
#[test]
|
2021-06-07 10:08:35 +02:00
|
|
|
fn subexpression_handles_dot() {
|
|
|
|
Playground::setup("subexpression_handles_dot", |dirs, sandbox| {
|
2020-05-16 09:25:18 +02:00
|
|
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"nu_times.csv",
|
|
|
|
r#"
|
|
|
|
name,rusty_luck,origin
|
|
|
|
Jason,1,Canada
|
|
|
|
Jonathan,1,New Zealand
|
|
|
|
Andrés,1,Ecuador
|
|
|
|
AndKitKatz,1,Estados Unidos
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
2021-05-12 03:01:48 +02:00
|
|
|
echo (open nu_times.csv)
|
2020-05-16 09:25:18 +02:00
|
|
|
| get name
|
2022-02-17 12:40:24 +01:00
|
|
|
| each { |it| nu --testbin chop $it }
|
2022-02-02 21:59:01 +01:00
|
|
|
| get 3
|
2020-05-16 09:25:18 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "AndKitKat");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-19 21:27:26 +02:00
|
|
|
#[test]
|
|
|
|
fn string_interpolation_with_it() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-17 12:40:24 +01:00
|
|
|
echo "foo" | each { |it| echo $"($it)" }
|
2020-05-19 21:27:26 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_interpolation_with_it_column_path() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-17 12:40:24 +01:00
|
|
|
echo [[name]; [sammie]] | each { |it| echo $"($it.name)" } | get 0
|
2020-05-19 21:27:26 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "sammie");
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:20:29 +02:00
|
|
|
#[test]
|
|
|
|
fn string_interpolation_shorthand_overlap() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2021-05-21 00:55:38 +02:00
|
|
|
$"3 + 4 = (3 + 4)"
|
2021-05-12 06:20:29 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "3 + 4 = 7");
|
|
|
|
}
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
// FIXME: jt - we don't currently have a way to escape the single ticks easily
|
|
|
|
#[ignore]
|
2021-05-21 00:55:38 +02:00
|
|
|
#[test]
|
|
|
|
fn string_interpolation_and_paren() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
$"a paren is ('(')"
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "a paren is (");
|
|
|
|
}
|
|
|
|
|
2021-07-30 02:07:34 +02:00
|
|
|
#[test]
|
|
|
|
fn string_interpolation_with_unicode() {
|
|
|
|
//カ = U+30AB : KATAKANA LETTER KA
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
$"カ"
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "カ");
|
|
|
|
}
|
|
|
|
|
2021-01-09 00:53:59 +01:00
|
|
|
#[test]
|
2020-12-18 08:53:49 +01:00
|
|
|
fn run_custom_command() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2021-05-12 03:01:48 +02:00
|
|
|
def add-me [x y] { $x + $y}; add-me 10 5
|
2020-12-18 08:53:49 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "15");
|
|
|
|
}
|
|
|
|
|
2020-12-21 08:36:59 +01:00
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_flag() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-09-05 16:41:06 +02:00
|
|
|
def foo [--bar:number] { if ($bar | is-empty) { echo "empty" } else { echo $bar } }; foo --bar 10
|
2020-12-21 08:36:59 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "10");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_flag_missing() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-09-05 16:41:06 +02:00
|
|
|
def foo [--bar:number] { if ($bar | is-empty) { echo "empty" } else { echo $bar } }; foo
|
2020-12-21 08:36:59 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "empty");
|
|
|
|
}
|
|
|
|
|
2020-12-23 08:43:56 +01:00
|
|
|
#[test]
|
|
|
|
fn run_custom_subcommand() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-09-11 10:48:27 +02:00
|
|
|
def "str double" [x] { echo $x $x | str join }; str double bob
|
2020-12-23 08:43:56 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "bobbob");
|
|
|
|
}
|
|
|
|
|
2021-01-01 07:23:54 +01:00
|
|
|
#[test]
|
|
|
|
fn run_inner_custom_command() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
def outer [x] { def inner [y] { echo $y }; inner $x }; outer 10
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "10");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_broken_inner_custom_command() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
def outer [x] { def inner [y] { echo $y }; inner $x }; inner 10
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
assert!(!actual.err.is_empty());
|
2021-01-01 07:23:54 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 22:28:32 +01:00
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_rest() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
def rest-me [...rest: string] { echo $rest.1 $rest.0}; rest-me "hello" "world" | to json --raw
|
2021-01-22 22:28:32 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, r#"["world","hello"]"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_rest_and_arg() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01: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
|
2021-01-22 22:28:32 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, r#"["yay","world","hello"]"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_rest_and_flag() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01: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
|
2021-01-22 22:28:32 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, r#"["world","hello","yay"]"#);
|
|
|
|
}
|
|
|
|
|
2021-04-12 04:37:36 +02:00
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_empty_rest() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2023-02-22 17:18:33 +01:00
|
|
|
def rest-me-with-empty-rest [...rest: string] { $rest }; rest-me-with-empty-rest | is-empty
|
2021-04-12 04:37:36 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2023-02-22 17:18:33 +01:00
|
|
|
assert_eq!(actual.out, r#"true"#);
|
2021-04-12 04:37:36 +02:00
|
|
|
assert_eq!(actual.err, r#""#);
|
|
|
|
}
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
//FIXME: jt: blocked on https://github.com/nushell/engine-q/issues/912
|
|
|
|
#[ignore]
|
2021-08-26 19:58:53 +02:00
|
|
|
#[test]
|
|
|
|
fn run_custom_command_with_rest_other_name() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
def say-hello [
|
|
|
|
greeting:string,
|
|
|
|
...names:string # All of the names
|
|
|
|
] {
|
2022-09-11 10:48:27 +02:00
|
|
|
echo $"($greeting), ($names | sort-by | str join)"
|
2021-08-26 19:58:53 +02:00
|
|
|
}
|
|
|
|
say-hello Salutations E D C A B
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
assert_eq!(actual.out, r#"Salutations, ABCDE"#);
|
2021-08-26 19:58:53 +02:00
|
|
|
assert_eq!(actual.err, r#""#);
|
|
|
|
}
|
|
|
|
|
2021-05-26 07:58:32 +02:00
|
|
|
#[test]
|
|
|
|
fn alias_a_load_env() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
def activate-helper [] { {BOB: SAM} }; alias activate = load-env (activate-helper); activate; $env.BOB
|
2021-05-26 07:58:32 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, r#"SAM"#);
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
#[test]
|
2021-05-20 06:26:54 +02:00
|
|
|
fn let_variable() {
|
2020-12-18 08:53:49 +01:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2021-01-05 00:30:55 +01:00
|
|
|
let x = 5
|
|
|
|
let y = 12
|
2021-05-12 03:01:48 +02:00
|
|
|
$x + $y
|
2020-12-18 08:53:49 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "17");
|
|
|
|
}
|
|
|
|
|
2021-01-03 07:44:21 +01:00
|
|
|
#[test]
|
2021-05-20 06:26:54 +02:00
|
|
|
fn let_doesnt_leak() {
|
2021-01-03 07:44:21 +01:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2021-01-05 00:30:55 +01:00
|
|
|
do { let x = 5 }; echo $x
|
2021-01-03 07:44:21 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
assert!(actual.err.contains("variable not found"));
|
2021-01-03 07:44:21 +01:00
|
|
|
}
|
|
|
|
|
2020-12-19 07:25:03 +01:00
|
|
|
#[test]
|
2021-05-20 06:26:54 +02:00
|
|
|
fn let_env_variable() {
|
2020-12-19 07:25:03 +01:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2021-01-05 00:30:55 +01:00
|
|
|
let-env TESTENVVAR = "hello world"
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.TESTENVVAR
|
2020-12-19 07:25:03 +01: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 15:15:58 +02:00
|
|
|
#[test]
|
|
|
|
fn let_env_hides_variable() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
let-env TESTENVVAR = "hello world"
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.TESTENVVAR
|
2022-08-13 11:55:06 +02:00
|
|
|
hide-env TESTENVVAR
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.TESTENVVAR
|
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 15:15:58 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello world");
|
2022-12-10 18:23:34 +01: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 15:15:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_env_hides_variable_in_parent_scope() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
let-env TESTENVVAR = "hello world"
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.TESTENVVAR
|
|
|
|
do {
|
2022-08-13 11:55:06 +02:00
|
|
|
hide-env TESTENVVAR
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.TESTENVVAR
|
|
|
|
}
|
|
|
|
echo $env.TESTENVVAR
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello world");
|
2022-12-10 18:23:34 +01: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 15:15:58 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 05:00:07 +02:00
|
|
|
#[test]
|
|
|
|
fn unlet_env_variable() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
let-env TEST_VAR = "hello world"
|
2022-08-13 11:55:06 +02:00
|
|
|
hide-env TEST_VAR
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.TEST_VAR
|
2021-06-19 05:00:07 +02:00
|
|
|
"#
|
|
|
|
);
|
2022-12-10 18:23:34 +01:00
|
|
|
assert!(actual.err.contains("not_found"));
|
2021-06-19 05:00:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-03-17 23:35:50 +01:00
|
|
|
#[ignore]
|
2021-06-19 05:00:07 +02:00
|
|
|
fn unlet_nonexistent_variable() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-08-13 11:55:06 +02:00
|
|
|
hide-env NONEXISTENT_VARIABLE
|
2022-02-02 21:59:01 +01: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 15:15:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unlet_variable_in_parent_scope() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
let-env DEBUG = "1"
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.DEBUG
|
|
|
|
do {
|
|
|
|
let-env DEBUG = "2"
|
|
|
|
echo $env.DEBUG
|
2022-08-13 11:55:06 +02:00
|
|
|
hide-env DEBUG
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.DEBUG
|
|
|
|
}
|
|
|
|
echo $env.DEBUG
|
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 15:15:58 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1211");
|
2021-06-19 05:00:07 +02:00
|
|
|
}
|
|
|
|
|
2021-01-03 07:44:21 +01:00
|
|
|
#[test]
|
2021-05-20 06:26:54 +02:00
|
|
|
fn let_env_doesnt_leak() {
|
2021-01-03 07:44:21 +01:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
do { let-env xyz = "my message" }; echo $env.xyz
|
2021-01-03 07:44:21 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2022-12-10 18:23:34 +01:00
|
|
|
assert!(actual.err.contains("not_found"));
|
2021-01-03 07:44:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-03 08:48:02 +01:00
|
|
|
#[test]
|
2021-05-20 06:26:54 +02:00
|
|
|
fn proper_shadow_let_env_aliases() {
|
2021-01-03 08:48:02 +01:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-09 19:41:41 +01:00
|
|
|
let-env DEBUG = "true"; echo $env.DEBUG | table; do { let-env DEBUG = "false"; echo $env.DEBUG } | table; echo $env.DEBUG
|
2021-01-03 08:48:02 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "truefalsetrue");
|
|
|
|
}
|
|
|
|
|
2021-05-25 20:18:20 +02:00
|
|
|
#[test]
|
|
|
|
fn load_env_variable() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
echo {TESTENVVAR: "hello world"} | load-env
|
|
|
|
echo $env.TESTENVVAR
|
2021-05-25 20:18:20 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello world");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load_env_variable_arg() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
load-env {TESTENVVAR: "hello world"}
|
|
|
|
echo $env.TESTENVVAR
|
2021-05-25 20:18:20 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello world");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load_env_doesnt_leak() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
do { echo { name: xyz, value: "my message" } | load-env }; echo $env.xyz
|
2021-05-25 20:18:20 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2022-12-10 18:23:34 +01:00
|
|
|
assert!(actual.err.contains("not_found"));
|
2021-05-25 20:18:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proper_shadow_load_env_aliases() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-09 19:41:41 +01:00
|
|
|
let-env DEBUG = "true"; echo $env.DEBUG | table; do { echo {DEBUG: "false"} | load-env; echo $env.DEBUG } | table; echo $env.DEBUG
|
2021-05-25 20:18:20 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "truefalsetrue");
|
|
|
|
}
|
|
|
|
|
2022-12-22 21:30:10 +01:00
|
|
|
//FIXME: jt: load-env can not currently hide variables because null no longer hides
|
2022-02-02 21:59:01 +01: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 15:15:58 +02:00
|
|
|
#[test]
|
|
|
|
fn load_env_can_hide_var_envs() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
let-env DEBUG = "1"
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.DEBUG
|
2022-12-22 21:30:10 +01:00
|
|
|
load-env [[name, value]; [DEBUG null]]
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.DEBUG
|
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 15:15:58 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "1");
|
|
|
|
assert!(actual.err.contains("error"));
|
|
|
|
assert!(actual.err.contains("Unknown column"));
|
|
|
|
}
|
|
|
|
|
2022-12-22 21:30:10 +01:00
|
|
|
//FIXME: jt: load-env can not currently hide variables because null no longer hides
|
2022-02-02 21:59:01 +01: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 15:15:58 +02:00
|
|
|
#[test]
|
|
|
|
fn load_env_can_hide_var_envs_in_parent_scope() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
let-env DEBUG = "1"
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.DEBUG
|
|
|
|
do {
|
2022-12-22 21:30:10 +01:00
|
|
|
load-env [[name, value]; [DEBUG null]]
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.DEBUG
|
|
|
|
}
|
|
|
|
echo $env.DEBUG
|
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 15:15:58 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "11");
|
|
|
|
assert!(actual.err.contains("error"));
|
|
|
|
assert!(actual.err.contains("Unknown column"));
|
|
|
|
}
|
|
|
|
|
2021-01-03 08:48:02 +01:00
|
|
|
#[test]
|
2021-05-20 06:26:54 +02:00
|
|
|
fn proper_shadow_let_aliases() {
|
2021-01-03 08:48:02 +01:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-03-03 01:55:03 +01:00
|
|
|
let DEBUG = false; echo $DEBUG | table; do { let DEBUG = true; echo $DEBUG } | table; echo $DEBUG
|
2021-01-03 08:48:02 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "falsetruefalse");
|
|
|
|
}
|
|
|
|
|
2021-05-20 06:26:54 +02:00
|
|
|
#[test]
|
2021-05-21 09:04:27 +02:00
|
|
|
fn block_params_override() {
|
2021-05-20 06:26:54 +02:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2021-05-21 09:04:27 +02:00
|
|
|
[1, 2, 3] | each { |a| echo $it }
|
2021-05-20 06:26:54 +02:00
|
|
|
"#
|
|
|
|
);
|
2022-02-02 21:59:01 +01:00
|
|
|
assert!(actual.err.contains("variable not found"));
|
2021-05-20 06:26:54 +02:00
|
|
|
}
|
|
|
|
|
2022-03-18 20:03:57 +01:00
|
|
|
#[test]
|
|
|
|
fn alias_reuse() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"alias foo = echo bob; foo; foo"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.out.contains("bob"));
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
}
|
|
|
|
|
2021-05-20 06:26:54 +02:00
|
|
|
#[test]
|
2021-05-21 09:04:27 +02:00
|
|
|
fn block_params_override_correct() {
|
2021-05-20 06:26:54 +02:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
[1, 2, 3] | each { |a| echo $a } | to json --raw
|
2021-05-20 06:26:54 +02:00
|
|
|
"#
|
|
|
|
);
|
2021-05-21 09:04:27 +02:00
|
|
|
assert_eq!(actual.out, "[1,2,3]");
|
2021-05-20 06:26:54 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:14:51 +02:00
|
|
|
#[test]
|
|
|
|
fn hex_number() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
0x10
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "16");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn binary_number() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
0b10
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "2");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn octal_number() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
0o10
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "8");
|
|
|
|
}
|
|
|
|
|
2021-04-21 04:31:54 +02:00
|
|
|
#[test]
|
|
|
|
fn run_dynamic_blocks() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2021-05-12 03:01:48 +02:00
|
|
|
let block = { echo "holaaaa" }; do $block
|
2021-04-21 04:31:54 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "holaaaa");
|
|
|
|
}
|
|
|
|
|
2022-03-29 13:10:43 +02:00
|
|
|
#[cfg(feature = "which-support")]
|
2020-05-19 14:57:25 +02:00
|
|
|
#[test]
|
2021-06-07 10:08:35 +02:00
|
|
|
fn argument_subexpression_reports_errors() {
|
2020-05-19 14:57:25 +02:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
2021-05-12 03:01:48 +02:00
|
|
|
"echo (ferris_is_not_here.exe)"
|
2020-05-19 14:57:25 +02:00
|
|
|
);
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
assert!(!actual.err.is_empty());
|
2020-05-19 14:57:25 +02:00
|
|
|
}
|
|
|
|
|
2020-01-16 10:05:53 +01:00
|
|
|
#[test]
|
|
|
|
fn can_process_one_row_from_internal_and_pipes_it_to_stdin_of_external() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
2022-10-26 18:36:42 +02:00
|
|
|
r#""nushelll" | nu --testbin chop"#
|
2020-01-16 10:05:53 +01:00
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "nushell");
|
2020-01-16 10:05:53 +01:00
|
|
|
}
|
|
|
|
|
2021-05-24 07:27:10 +02:00
|
|
|
#[test]
|
|
|
|
fn bad_operator() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
2 $ 2
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.err.contains("operator"));
|
|
|
|
}
|
|
|
|
|
2021-01-23 00:14:13 +01:00
|
|
|
#[test]
|
|
|
|
fn index_out_of_bounds() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
let foo = [1, 2, 3]; echo $foo.5
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
assert!(actual.err.contains("too large"));
|
|
|
|
}
|
|
|
|
|
2021-06-09 19:17:45 +02:00
|
|
|
#[test]
|
|
|
|
fn negative_decimal_start() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
-1.3 + 4
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "2.7");
|
|
|
|
}
|
2021-08-06 23:49:37 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_inside_of() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
"bob" in "bobby"
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_not_inside_of() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
"bob" not-in "bobby"
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "false");
|
|
|
|
}
|
|
|
|
|
2021-01-23 00:14:13 +01:00
|
|
|
#[test]
|
|
|
|
fn index_row() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
let foo = [[name]; [joe] [bob]]; echo $foo.1 | to json --raw
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, r#"{"name": "bob"}"#);
|
2021-01-23 00:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn index_cell() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
let foo = [[name]; [joe] [bob]]; echo $foo.name.1
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "bob");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn index_cell_alt() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
let foo = [[name]; [joe] [bob]]; echo $foo.1.name
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "bob");
|
|
|
|
}
|
|
|
|
|
2021-04-15 21:07:06 +02:00
|
|
|
#[test]
|
|
|
|
fn not_echoing_ranges_without_numbers() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo ..
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "..");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_echoing_exclusive_ranges_without_numbers() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo ..<
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "..<");
|
|
|
|
}
|
|
|
|
|
2020-05-27 20:07:53 +02:00
|
|
|
#[test]
|
|
|
|
fn echoing_ranges() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2020-06-19 04:02:01 +02:00
|
|
|
echo 1..3 | math sum
|
2020-05-27 20:07:53 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "6");
|
|
|
|
}
|
|
|
|
|
2020-09-13 23:53:08 +02:00
|
|
|
#[test]
|
|
|
|
fn echoing_exclusive_ranges() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo 1..<4 | math sum
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "6");
|
|
|
|
}
|
|
|
|
|
2020-08-30 06:55:33 +02:00
|
|
|
#[test]
|
|
|
|
fn table_literals1() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-09 11:58:54 +01:00
|
|
|
echo [[name age]; [foo 13]] | get age.0
|
2020-08-30 06:55:33 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "13");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn table_literals2() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo [[name age] ; [bob 13] [sally 20]] | get age | math sum
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "33");
|
|
|
|
}
|
|
|
|
|
2020-08-30 08:19:54 +02:00
|
|
|
#[test]
|
|
|
|
fn list_with_commas() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo [1, 2, 3] | math sum
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "6");
|
|
|
|
}
|
|
|
|
|
2020-09-07 04:43:58 +02:00
|
|
|
#[test]
|
|
|
|
fn range_with_left_var() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
({ size: 3}.size)..10 | math sum
|
2020-09-07 04:43:58 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "52");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_with_right_var() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
4..({ size: 30}.size) | math sum
|
2020-09-07 04:43:58 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "459");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_with_open_left() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo ..30 | math sum
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "465");
|
|
|
|
}
|
|
|
|
|
2020-09-13 23:53:08 +02:00
|
|
|
#[test]
|
|
|
|
fn exclusive_range_with_open_left() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo ..<31 | math sum
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "465");
|
|
|
|
}
|
|
|
|
|
2020-09-07 04:43:58 +02:00
|
|
|
#[test]
|
|
|
|
fn range_with_open_right() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo 5.. | first 10 | math sum
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "95");
|
|
|
|
}
|
|
|
|
|
2020-09-13 23:53:08 +02:00
|
|
|
#[test]
|
|
|
|
fn exclusive_range_with_open_right() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo 5..< | first 10 | math sum
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "95");
|
|
|
|
}
|
|
|
|
|
2020-09-07 19:30:11 +02:00
|
|
|
#[test]
|
|
|
|
fn range_with_mixed_types() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo 1..10.5 | math sum
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "55");
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:35:18 +01:00
|
|
|
#[test]
|
|
|
|
fn filesize_math() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
100 * 10kib
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1000.0 KiB");
|
2021-02-10 03:31:12 +01:00
|
|
|
// why 1000.0 KB instead of 1.0 MB?
|
|
|
|
// looks like `byte.get_appropriate_unit(false)` behaves this way
|
2021-01-29 23:35:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesize_math2() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2021-05-12 03:01:48 +02:00
|
|
|
100 / 10kb
|
2021-01-29 23:35:18 +01:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
assert!(actual.err.contains("doesn't support"));
|
2021-01-29 23:35:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesize_math3() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
100kib / 10
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "10.0 KiB");
|
2021-01-29 23:35:18 +01:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn filesize_math4() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
100kib * 5
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "500.0 KiB");
|
2021-02-10 03:31:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesize_math5() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
1000 * 1kib
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1000.0 KiB");
|
2021-02-10 03:31:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesize_math6() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
1000 * 1mib
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1000.0 MiB");
|
2021-02-10 03:31:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesize_math7() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
2022-02-02 21:59:01 +01:00
|
|
|
1000 * 1gib
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1000.0 GiB");
|
2021-01-29 23:35:18 +01:00
|
|
|
}
|
|
|
|
|
2020-09-13 23:53:08 +02:00
|
|
|
#[test]
|
|
|
|
fn exclusive_range_with_mixed_types() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo 1..<10.5 | math sum
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "55");
|
|
|
|
}
|
|
|
|
|
2020-08-30 08:19:54 +02:00
|
|
|
#[test]
|
|
|
|
fn table_with_commas() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo [[name, age, height]; [JT, 42, 185] [Unknown, 99, 99]] | get age | math sum
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "141");
|
|
|
|
}
|
|
|
|
|
2021-06-04 18:54:18 +02:00
|
|
|
#[test]
|
|
|
|
fn duration_overflow() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
2022-02-17 12:40:24 +01:00
|
|
|
ls | get modified | each { |it| $it + 10000000000000000day }
|
2021-06-04 18:54:18 +02:00
|
|
|
"#)
|
|
|
|
);
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
assert!(actual.err.contains("duration too large"));
|
2021-06-04 18:54:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn date_and_duration_overflow() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
2022-02-17 12:40:24 +01:00
|
|
|
ls | get modified | each { |it| $it + 1000000000day }
|
2021-06-04 18:54:18 +02:00
|
|
|
"#)
|
|
|
|
);
|
|
|
|
|
|
|
|
// assert_eq!(actual.err, "overflow");
|
2022-02-02 21:59:01 +01:00
|
|
|
assert!(actual.err.contains("duration too large"));
|
2021-06-04 18:54:18 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 02:31:01 +02:00
|
|
|
#[test]
|
|
|
|
fn pipeline_params_simple() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
echo 1 2 3 | $in.1 * $in.2
|
|
|
|
"#)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "6");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pipeline_params_inner() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
echo 1 2 3 | (echo $in.2 6 7 | $in.0 * $in.1 * $in.2)
|
|
|
|
"#)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "126");
|
|
|
|
}
|
|
|
|
|
2021-06-25 07:50:24 +02:00
|
|
|
#[test]
|
|
|
|
fn better_table_lex() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
let table = [
|
|
|
|
[name, size];
|
|
|
|
[small, 7]
|
|
|
|
[medium, 10]
|
|
|
|
[large, 12]
|
|
|
|
];
|
|
|
|
$table.1.size
|
|
|
|
"#)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "10");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn better_subexpr_lex() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
(echo boo
|
|
|
|
sam | str length | math sum)
|
|
|
|
"#)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "6");
|
|
|
|
}
|
|
|
|
|
2021-06-25 23:09:06 +02:00
|
|
|
#[test]
|
|
|
|
fn subsubcommand() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
def "aws s3 rb" [url] { $url + " loaded" }; aws s3 rb localhost
|
|
|
|
"#)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "localhost loaded");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn manysubcommand() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
def "aws s3 rb ax vf qqqq rrrr" [url] { $url + " loaded" }; aws s3 rb ax vf qqqq rrrr localhost
|
|
|
|
"#)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "localhost loaded");
|
|
|
|
}
|
|
|
|
|
2021-07-07 21:21:02 +02:00
|
|
|
#[test]
|
|
|
|
fn nothing_string_1() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
2022-12-22 21:30:10 +01:00
|
|
|
null == "foo"
|
2021-07-07 21:21:02 +02:00
|
|
|
"#)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "false");
|
|
|
|
}
|
|
|
|
|
2021-08-17 15:56:35 +02:00
|
|
|
#[test]
|
2022-08-13 11:55:06 +02:00
|
|
|
fn hide_alias_shadowing() {
|
2021-08-17 15:56:35 +02:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
2021-08-19 09:05:36 +02:00
|
|
|
def test-shadowing [] {
|
|
|
|
alias greet = echo hello;
|
|
|
|
let xyz = { greet };
|
2022-08-13 11:55:06 +02:00
|
|
|
hide greet;
|
2021-08-19 09:05:36 +02:00
|
|
|
do $xyz
|
|
|
|
};
|
|
|
|
test-shadowing
|
2021-08-17 15:56:35 +02:00
|
|
|
"#)
|
|
|
|
);
|
2021-08-19 09:05:36 +02:00
|
|
|
assert_eq!(actual.out, "hello");
|
|
|
|
}
|
2021-08-17 15:56:35 +02:00
|
|
|
|
2022-08-13 11:55:06 +02:00
|
|
|
// FIXME: Seems like subexpression are no longer scoped. Should we remove this test?
|
2022-02-02 21:59:01 +01:00
|
|
|
#[ignore]
|
2021-08-19 09:05:36 +02:00
|
|
|
#[test]
|
2022-08-13 11:55:06 +02:00
|
|
|
fn hide_alias_does_not_escape_scope() {
|
2021-08-19 09:05:36 +02:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
def test-alias [] {
|
|
|
|
alias greet = echo hello;
|
2022-08-13 11:55:06 +02:00
|
|
|
(hide greet);
|
2021-08-19 09:05:36 +02:00
|
|
|
greet
|
|
|
|
};
|
|
|
|
test-alias
|
|
|
|
"#)
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "hello");
|
2021-08-17 15:56:35 +02:00
|
|
|
}
|
|
|
|
|
2021-08-17 19:55:34 +02:00
|
|
|
#[test]
|
2022-08-13 11:55:06 +02:00
|
|
|
fn hide_alias_hides_alias() {
|
2021-08-17 19:55:34 +02:00
|
|
|
let actual = nu!(cwd: ".", pipeline(
|
2021-08-19 09:05:36 +02:00
|
|
|
r#"
|
|
|
|
def test-alias [] {
|
|
|
|
alias ll = ls -l;
|
2022-08-13 11:55:06 +02:00
|
|
|
hide ll;
|
2021-08-19 09:05:36 +02:00
|
|
|
ll
|
|
|
|
};
|
|
|
|
test-alias
|
2021-08-17 19:55:34 +02:00
|
|
|
"#)
|
|
|
|
);
|
|
|
|
|
2022-09-21 02:46:01 +02:00
|
|
|
assert!(actual.err.contains("did you mean 'all'?"));
|
2021-08-17 19:55:34 +02:00
|
|
|
}
|
|
|
|
|
2020-02-13 08:34:43 +01:00
|
|
|
mod parse {
|
2020-05-07 13:03:43 +02:00
|
|
|
use nu_test_support::nu;
|
2020-02-13 08:34:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
The debug command's signature is:
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
> debug {flags}
|
|
|
|
|
|
|
|
flags:
|
2022-10-26 18:36:42 +02:00
|
|
|
-h, --help: Display the help message for this command
|
2020-02-13 08:34:43 +01:00
|
|
|
-r, --raw: Prints the raw value representation.
|
|
|
|
*/
|
|
|
|
|
2020-02-18 07:58:30 +01:00
|
|
|
#[test]
|
|
|
|
fn errors_if_flag_passed_is_not_exact() {
|
2020-05-07 13:03:43 +02:00
|
|
|
let actual = nu!(cwd: ".", "debug -ra");
|
2020-02-18 07:58:30 +01:00
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
assert!(actual.err.contains("unknown flag"),);
|
|
|
|
|
|
|
|
let actual = nu!(cwd: ".", "debug --rawx");
|
|
|
|
|
|
|
|
assert!(actual.err.contains("unknown flag"),);
|
2020-02-18 07:58:30 +01:00
|
|
|
}
|
|
|
|
|
2020-02-13 08:34:43 +01:00
|
|
|
#[test]
|
|
|
|
fn errors_if_flag_is_not_supported() {
|
2020-05-07 13:03:43 +02:00
|
|
|
let actual = nu!(cwd: ".", "debug --ferris");
|
2020-02-13 08:34:43 +01:00
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
assert!(actual.err.contains("unknown flag"),);
|
2020-02-13 08:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn errors_if_passed_an_unexpected_argument() {
|
2020-05-07 13:03:43 +02:00
|
|
|
let actual = nu!(cwd: ".", "debug ferris");
|
2020-02-13 08:34:43 +01:00
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
assert!(actual.err.contains("extra positional argument"),);
|
2020-02-13 08:34:43 +01:00
|
|
|
}
|
2023-02-16 03:30:56 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ensure_backticks_are_bareword_command() {
|
|
|
|
let actual = nu!(cwd: ".", "`8abc123`");
|
|
|
|
|
|
|
|
assert!(actual.err.contains("was not found"),);
|
|
|
|
}
|
2020-02-13 08:34:43 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 10:05:53 +01:00
|
|
|
mod tilde_expansion {
|
2020-05-16 09:25:18 +02:00
|
|
|
use nu_test_support::nu;
|
2020-01-16 10:05:53 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn as_home_directory_when_passed_as_argument_and_begins_with_tilde() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo ~
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2021-03-26 09:26:57 +01:00
|
|
|
assert!(!actual.out.contains('~'),);
|
2020-01-16 10:05:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_expand_when_passed_as_argument_and_does_not_start_with_tilde() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
r#"
|
|
|
|
echo "1~1"
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "1~1");
|
2020-01-16 10:05:53 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 19:21:11 +01:00
|
|
|
|
|
|
|
mod variable_scoping {
|
|
|
|
use nu_test_support::nu;
|
|
|
|
|
|
|
|
macro_rules! test_variable_scope {
|
|
|
|
($func:literal == $res:literal $(,)*) => {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
$func
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, $res);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
macro_rules! test_variable_scope_list {
|
|
|
|
($func:literal == $res:expr $(,)*) => {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".",
|
|
|
|
$func
|
|
|
|
);
|
|
|
|
|
|
|
|
let result: Vec<&str> = actual.out.matches("ZZZ").collect();
|
|
|
|
assert_eq!(result, $res);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn access_variables_in_scopes() {
|
|
|
|
test_variable_scope!(
|
|
|
|
r#" def test [input] { echo [0 1 2] | do { do { echo $input } } }
|
|
|
|
test ZZZ "#
|
|
|
|
== "ZZZ"
|
|
|
|
);
|
|
|
|
test_variable_scope!(
|
2022-02-02 21:59:01 +01:00
|
|
|
r#" def test [input] { echo [0 1 2] | do { do { if $input == "ZZZ" { echo $input } else { echo $input } } } }
|
2021-01-19 19:21:11 +01:00
|
|
|
test ZZZ "#
|
|
|
|
== "ZZZ"
|
|
|
|
);
|
|
|
|
test_variable_scope!(
|
2022-02-02 21:59:01 +01:00
|
|
|
r#" def test [input] { echo [0 1 2] | do { do { if $input == "ZZZ" { echo $input } else { echo $input } } } }
|
2021-01-19 19:21:11 +01:00
|
|
|
test ZZZ "#
|
|
|
|
== "ZZZ"
|
|
|
|
);
|
|
|
|
test_variable_scope!(
|
|
|
|
r#" def test [input] { echo [0 1 2] | do { echo $input } }
|
|
|
|
test ZZZ "#
|
|
|
|
== "ZZZ"
|
|
|
|
);
|
|
|
|
test_variable_scope!(
|
2022-02-02 21:59:01 +01:00
|
|
|
r#" def test [input] { echo [0 1 2] | do { if $input == $input { echo $input } else { echo $input } } }
|
2021-01-19 19:21:11 +01:00
|
|
|
test ZZZ "#
|
|
|
|
== "ZZZ"
|
|
|
|
);
|
|
|
|
test_variable_scope_list!(
|
2022-02-17 12:40:24 +01:00
|
|
|
r#" def test [input] { echo [0 1 2] | each { |_| echo $input } }
|
2021-01-19 19:21:11 +01:00
|
|
|
test ZZZ "#
|
|
|
|
== ["ZZZ", "ZZZ", "ZZZ"]
|
|
|
|
);
|
|
|
|
test_variable_scope_list!(
|
2022-02-17 12:40:24 +01:00
|
|
|
r#" def test [input] { echo [0 1 2] | each { |it| if $it > 0 {echo $input} else {echo $input}} }
|
2021-01-19 19:21:11 +01:00
|
|
|
test ZZZ "#
|
|
|
|
== ["ZZZ", "ZZZ", "ZZZ"]
|
|
|
|
);
|
|
|
|
test_variable_scope_list!(
|
2022-02-17 12:40:24 +01:00
|
|
|
r#" def test [input] { echo [0 1 2] | each { |_| if $input == $input {echo $input} else {echo $input}} }
|
2021-01-19 19:21:11 +01:00
|
|
|
test ZZZ "#
|
|
|
|
== ["ZZZ", "ZZZ", "ZZZ"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|