nushell/crates/nu-command/tests/commands/nu_check.rs

796 lines
21 KiB
Rust
Raw Normal View History

use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn parse_script_success() {
Playground::setup("nu_check_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"script.nu",
r#"
greet "world"
def greet [name] {
echo "hello" $name
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check script.nu
"#
));
assert!(actual.err.is_empty());
})
}
#[test]
fn parse_script_with_wrong_type() {
Playground::setup("nu_check_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"script.nu",
r#"
greet "world"
def greet [name] {
echo "hello" $name
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check -d --as-module script.nu
"#
));
assert!(actual.err.contains("Failed to parse content"));
})
}
#[test]
fn parse_script_failure() {
Playground::setup("nu_check_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"script.nu",
r#"
greet "world"
def greet [name {
echo "hello" $name
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check -d script.nu
"#
));
assert!(actual.err.contains("Unexpected end of code"));
})
}
#[test]
fn parse_module_success() {
Playground::setup("nu_check_test_4", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"foo.nu",
r#"
# foo.nu
export def hello [name: string] {
$"hello ($name)!"
}
export def hi [where: string] {
$"hi ($where)!"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check --as-module foo.nu
"#
));
assert!(actual.err.is_empty());
})
}
#[test]
fn parse_module_with_wrong_type() {
Playground::setup("nu_check_test_5", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"foo.nu",
r#"
# foo.nu
export def hello [name: string {
$"hello ($name)!"
}
export def hi [where: string] {
$"hi ($where)!"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check -d foo.nu
"#
));
assert!(actual.err.contains("Failed to parse content"));
})
}
#[test]
fn parse_module_failure() {
Playground::setup("nu_check_test_6", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"foo.nu",
r#"
# foo.nu
export def hello [name: string {
$"hello ($name)!"
}
export def hi [where: string] {
$"hi ($where)!"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check -d --as-module foo.nu
"#
));
assert!(actual.err.contains("Unexpected end of code"));
})
}
#[test]
fn file_not_exist() {
Playground::setup("nu_check_test_7", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check --as-module foo.nu
"#
));
assert!(actual.err.contains("file not found"));
})
}
#[test]
fn parse_unsupported_file() {
Playground::setup("nu_check_test_8", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"foo.txt",
r#"
# foo.nu
export def hello [name: string {
$"hello ($name)!"
}
export def hi [where: string] {
$"hi ($where)!"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check --as-module foo.txt
"#
));
assert!(actual
.err
.contains("File extension must be the type of .nu"));
})
}
#[test]
fn parse_dir_failure() {
Playground::setup("nu_check_test_9", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check --as-module ~
"#
));
assert!(actual
.err
.contains("File extension must be the type of .nu"));
})
}
#[test]
fn parse_module_success_2() {
Playground::setup("nu_check_test_10", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"foo.nu",
r#"
# foo.nu
remove let-env, focus on mutating $env (#9574) # Description For years, Nushell has used `let-env` to set a single environment variable. As our work on scoping continued, we refined what it meant for a variable to be in scope using `let` but never updated how `let-env` would work. Instead, `let-env` confusingly created mutations to the command's copy of `$env`. So, to help fix the mental model and point people to the right way of thinking about what changing the environment means, this PR removes `let-env` to encourage people to think of it as updating the command's environment variable via mutation. Before: ``` let-env FOO = "BAR" ``` Now: ``` $env.FOO = "BAR" ``` It's also a good reminder that the environment owned by the command is in the `$env` variable rather than global like it is in other shells. # User-Facing Changes BREAKING CHANGE BREAKING CHANGE This completely removes `let-env FOO = "BAR"` so that we can focus on `$env.FOO = "BAR"`. # 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 -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After / Before Submitting integration scripts to update: - :heavy_check_mark: [starship](https://github.com/starship/starship/blob/master/src/init/starship.nu) - :heavy_check_mark: [virtualenv](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu) - :heavy_check_mark: [atuin](https://github.com/ellie/atuin/blob/main/atuin/src/shell/atuin.nu) (PR: https://github.com/ellie/atuin/pull/1080) - :x: [zoxide](https://github.com/ajeetdsouza/zoxide/blob/main/templates/nushell.txt) (PR: https://github.com/ajeetdsouza/zoxide/pull/587) - :heavy_check_mark: [oh-my-posh](https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/src/shell/scripts/omp.nu) (pr: https://github.com/JanDeDobbeleer/oh-my-posh/pull/4011)
2023-06-30 21:57:51 +02:00
export-env { $env.MYNAME = "Arthur, King of the Britons" }
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check --as-module foo.nu
"#
));
assert!(actual.err.is_empty());
})
}
#[test]
fn parse_script_success_with_raw_stream() {
Playground::setup("nu_check_test_11", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"script.nu",
r#"
greet "world"
def greet [name] {
echo "hello" $name
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open script.nu | nu-check
"#
));
assert!(actual.err.is_empty());
})
}
#[test]
fn parse_module_success_with_raw_stream() {
Playground::setup("nu_check_test_12", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"foo.nu",
r#"
# foo.nu
export def hello [name: string] {
$"hello ($name)!"
}
export def hi [where: string] {
$"hi ($where)!"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open foo.nu | nu-check --as-module
"#
));
assert!(actual.err.is_empty());
})
}
#[test]
fn parse_string_as_script_success() {
Playground::setup("nu_check_test_13", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
echo $'two(char nl)lines' | nu-check
"#
));
assert!(actual.err.is_empty());
})
}
#[test]
fn parse_string_as_script() {
Playground::setup("nu_check_test_14", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
echo $'two(char nl)lines' | nu-check -d --as-module
"#
));
println!("the output is {}", actual.err);
assert!(actual.err.contains("Failed to parse content"));
})
}
#[test]
fn parse_module_success_with_internal_stream() {
Playground::setup("nu_check_test_15", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"foo.nu",
r#"
# foo.nu
export def hello [name: string] {
$"hello ($name)!"
}
export def hi [where: string] {
$"hi ($where)!"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open foo.nu | lines | nu-check --as-module
"#
));
assert!(actual.err.is_empty());
})
}
#[test]
fn parse_script_success_with_complex_internal_stream() {
Playground::setup("nu_check_test_16", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"grep.nu",
r#"
#grep for nu
def grep-nu [
search #search term
entrada? #file or pipe
#
#Examples
#grep-nu search file.txt
#ls **/* | some_filter | grep-nu search
#open file.txt | grep-nu search
] {
if ($entrada | is-empty) {
if ($in | column? name) {
grep -ihHn $search ($in | get name)
} else {
($in | into string) | grep -ihHn $search
}
} else {
grep -ihHn $search $entrada
}
| lines
| parse "{file}:{line}:{match}"
| str trim
| update match {|f|
$f.match
| nu-highlight
}
| rename "source file" "line number"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open grep.nu | lines | nu-check
"#
));
assert!(actual.err.is_empty());
})
}
#[test]
fn parse_script_failure_with_complex_internal_stream() {
Playground::setup("nu_check_test_17", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"grep.nu",
r#"
#grep for nu
def grep-nu [
search #search term
entrada? #file or pipe
#
#Examples
#grep-nu search file.txt
#ls **/* | some_filter | grep-nu search
#open file.txt | grep-nu search
]
if ($entrada | is-empty) {
if ($in | column? name) {
grep -ihHn $search ($in | get name)
} else {
($in | into string) | grep -ihHn $search
}
} else {
grep -ihHn $search $entrada
}
| lines
| parse "{file}:{line}:{match}"
| str trim
| update match {|f|
$f.match
| nu-highlight
}
| rename "source file" "line number"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open grep.nu | lines | nu-check
"#
));
assert_eq!(actual.out, "false".to_string());
})
}
#[test]
fn parse_script_success_with_complex_external_stream() {
Playground::setup("nu_check_test_18", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"grep.nu",
r#"
#grep for nu
def grep-nu [
search #search term
entrada? #file or pipe
#
#Examples
#grep-nu search file.txt
#ls **/* | some_filter | grep-nu search
#open file.txt | grep-nu search
] {
if ($entrada | is-empty) {
if ($in | column? name) {
grep -ihHn $search ($in | get name)
} else {
($in | into string) | grep -ihHn $search
}
} else {
grep -ihHn $search $entrada
}
| lines
| parse "{file}:{line}:{match}"
| str trim
| update match {|f|
$f.match
| nu-highlight
}
| rename "source file" "line number"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open grep.nu | nu-check
"#
));
assert!(actual.err.is_empty());
})
}
#[test]
fn parse_module_success_with_complex_external_stream() {
Playground::setup("nu_check_test_19", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"grep.nu",
r#"
#grep for nu
def grep-nu [
search #search term
entrada? #file or pipe
#
#Examples
#grep-nu search file.txt
#ls **/* | some_filter | grep-nu search
#open file.txt | grep-nu search
] {
if ($entrada | is-empty) {
if ($in | column? name) {
grep -ihHn $search ($in | get name)
} else {
($in | into string) | grep -ihHn $search
}
} else {
grep -ihHn $search $entrada
}
| lines
| parse "{file}:{line}:{match}"
| str trim
| update match {|f|
$f.match
| nu-highlight
}
| rename "source file" "line number"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open grep.nu | nu-check -d --as-module
"#
));
assert!(actual.err.is_empty());
})
}
#[test]
fn parse_with_flag_all_success_for_complex_external_stream() {
Playground::setup("nu_check_test_20", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"grep.nu",
r#"
#grep for nu
def grep-nu [
search #search term
entrada? #file or pipe
#
#Examples
#grep-nu search file.txt
#ls **/* | some_filter | grep-nu search
#open file.txt | grep-nu search
] {
if ($entrada | is-empty) {
if ($in | column? name) {
grep -ihHn $search ($in | get name)
} else {
($in | into string) | grep -ihHn $search
}
} else {
grep -ihHn $search $entrada
}
| lines
| parse "{file}:{line}:{match}"
| str trim
| update match {|f|
$f.match
| nu-highlight
}
| rename "source file" "line number"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open grep.nu | nu-check -ad
"#
));
assert!(actual.err.is_empty());
})
}
#[test]
fn parse_with_flag_all_failure_for_complex_external_stream() {
Playground::setup("nu_check_test_21", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"grep.nu",
r#"
#grep for nu
def grep-nu
search #search term
entrada? #file or pipe
#
#Examples
#grep-nu search file.txt
#ls **/* | some_filter | grep-nu search
#open file.txt | grep-nu search
] {
if ($entrada | is-empty) {
if ($in | column? name) {
grep -ihHn $search ($in | get name)
} else {
($in | into string) | grep -ihHn $search
}
} else {
grep -ihHn $search $entrada
}
| lines
| parse "{file}:{line}:{match}"
| str trim
| update match {|f|
$f.match
| nu-highlight
}
| rename "source file" "line number"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open grep.nu | nu-check -ad
"#
));
assert!(actual.err.contains("syntax error"));
})
}
#[test]
fn parse_with_flag_all_failure_for_complex_list_stream() {
Playground::setup("nu_check_test_22", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"grep.nu",
r#"
#grep for nu
def grep-nu
search #search term
entrada? #file or pipe
#
#Examples
#grep-nu search file.txt
#ls **/* | some_filter | grep-nu search
#open file.txt | grep-nu search
] {
if ($entrada | is-empty) {
if ($in | column? name) {
grep -ihHn $search ($in | get name)
} else {
($in | into string) | grep -ihHn $search
}
} else {
grep -ihHn $search $entrada
}
| lines
| parse "{file}:{line}:{match}"
| str trim
| update match {|f|
$f.match
| nu-highlight
}
| rename "source file" "line number"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
open grep.nu | lines | nu-check -ad
"#
));
assert!(actual.err.contains("syntax error"));
})
}
#[test]
fn parse_failure_due_conflicted_flags() {
Playground::setup("nu_check_test_23", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"script.nu",
r#"
greet "world"
def greet [name] {
echo "hello" $name
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check -a --as-module script.nu
"#
));
assert!(actual
.err
.contains("You cannot have both `--all` and `--as-module` on the same command line"));
})
}
#[test]
fn parse_script_with_nested_scripts_success() {
Playground::setup("nu_check_test_24", |dirs, sandbox| {
sandbox
.mkdir("lol")
.with_files(vec![FileWithContentToBeTrimmed(
"lol/lol.nu",
r#"
source-env ../foo.nu
use lol_shell.nu
overlay use ../lol/lol_shell.nu
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"lol/lol_shell.nu",
r#"
export def ls [] { "lol" }
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"foo.nu",
r#"
remove let-env, focus on mutating $env (#9574) # Description For years, Nushell has used `let-env` to set a single environment variable. As our work on scoping continued, we refined what it meant for a variable to be in scope using `let` but never updated how `let-env` would work. Instead, `let-env` confusingly created mutations to the command's copy of `$env`. So, to help fix the mental model and point people to the right way of thinking about what changing the environment means, this PR removes `let-env` to encourage people to think of it as updating the command's environment variable via mutation. Before: ``` let-env FOO = "BAR" ``` Now: ``` $env.FOO = "BAR" ``` It's also a good reminder that the environment owned by the command is in the `$env` variable rather than global like it is in other shells. # User-Facing Changes BREAKING CHANGE BREAKING CHANGE This completely removes `let-env FOO = "BAR"` so that we can focus on `$env.FOO = "BAR"`. # 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 -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After / Before Submitting integration scripts to update: - :heavy_check_mark: [starship](https://github.com/starship/starship/blob/master/src/init/starship.nu) - :heavy_check_mark: [virtualenv](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu) - :heavy_check_mark: [atuin](https://github.com/ellie/atuin/blob/main/atuin/src/shell/atuin.nu) (PR: https://github.com/ellie/atuin/pull/1080) - :x: [zoxide](https://github.com/ajeetdsouza/zoxide/blob/main/templates/nushell.txt) (PR: https://github.com/ajeetdsouza/zoxide/pull/587) - :heavy_check_mark: [oh-my-posh](https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/src/shell/scripts/omp.nu) (pr: https://github.com/JanDeDobbeleer/oh-my-posh/pull/4011)
2023-06-30 21:57:51 +02:00
$env.FOO = 'foo'
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu-check lol/lol.nu
"#
));
assert_eq!(actual.out, "true");
})
}
#[test]
fn nu_check_respects_file_pwd() {
Playground::setup("nu_check_test_25", |dirs, sandbox| {
sandbox
.mkdir("lol")
.with_files(vec![FileWithContentToBeTrimmed(
"lol/lol.nu",
r#"
remove let-env, focus on mutating $env (#9574) # Description For years, Nushell has used `let-env` to set a single environment variable. As our work on scoping continued, we refined what it meant for a variable to be in scope using `let` but never updated how `let-env` would work. Instead, `let-env` confusingly created mutations to the command's copy of `$env`. So, to help fix the mental model and point people to the right way of thinking about what changing the environment means, this PR removes `let-env` to encourage people to think of it as updating the command's environment variable via mutation. Before: ``` let-env FOO = "BAR" ``` Now: ``` $env.FOO = "BAR" ``` It's also a good reminder that the environment owned by the command is in the `$env` variable rather than global like it is in other shells. # User-Facing Changes BREAKING CHANGE BREAKING CHANGE This completely removes `let-env FOO = "BAR"` so that we can focus on `$env.FOO = "BAR"`. # 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 -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After / Before Submitting integration scripts to update: - :heavy_check_mark: [starship](https://github.com/starship/starship/blob/master/src/init/starship.nu) - :heavy_check_mark: [virtualenv](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu) - :heavy_check_mark: [atuin](https://github.com/ellie/atuin/blob/main/atuin/src/shell/atuin.nu) (PR: https://github.com/ellie/atuin/pull/1080) - :x: [zoxide](https://github.com/ajeetdsouza/zoxide/blob/main/templates/nushell.txt) (PR: https://github.com/ajeetdsouza/zoxide/pull/587) - :heavy_check_mark: [oh-my-posh](https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/src/shell/scripts/omp.nu) (pr: https://github.com/JanDeDobbeleer/oh-my-posh/pull/4011)
2023-06-30 21:57:51 +02:00
$env.RETURN = (nu-check ../foo.nu)
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"foo.nu",
r#"
echo 'foo'
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
source-env lol/lol.nu;
$env.RETURN
"#
));
assert_eq!(actual.out, "true");
})
}