mirror of
https://github.com/nushell/nushell.git
synced 2024-11-30 04:14:17 +01:00
5e937ca1af
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> This PR refactors `nu-check` and makes it possible to check module directories. Also removes the requirement for files to end with .nu: It was too limiting for module directories and there are executable scripts [around](https://github.com/nushell/nu_scripts/tree/main/make_release/release-note) that do not end with .nu, it's a common practice for scripts to omit it. Other changes are: * Removed the `--all` flag and heuristic parse because these are irrelevant now when module syntax is a subset of script syntax (i.e., every module can be parsed as script). * Reduced code duplication and in general tidied up the code * Replaced unspanned errors with spanned ones. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> * `nu-check` doesn't require files to end with .nu * can check module directories * Removed `--all` flag # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` 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 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. -->
747 lines
20 KiB
Rust
747 lines
20 KiB
Rust
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(
|
|
"
|
|
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(
|
|
"
|
|
nu-check --debug --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(
|
|
"
|
|
nu-check --debug 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(
|
|
"
|
|
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(
|
|
"
|
|
nu-check --debug 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(
|
|
"
|
|
nu-check --debug --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(
|
|
"
|
|
nu-check --as-module foo.nu
|
|
"
|
|
));
|
|
|
|
assert!(actual.err.contains("file not found"));
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn parse_module_success_2() {
|
|
Playground::setup("nu_check_test_10", |dirs, sandbox| {
|
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
|
"foo.nu",
|
|
r#"
|
|
# foo.nu
|
|
|
|
export-env { $env.MYNAME = "Arthur, King of the Britons" }
|
|
"#,
|
|
)]);
|
|
|
|
let actual = nu!(
|
|
cwd: dirs.test(), pipeline(
|
|
"
|
|
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(
|
|
"
|
|
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(
|
|
"
|
|
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 --debug --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(
|
|
"
|
|
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(
|
|
"
|
|
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(
|
|
"
|
|
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(
|
|
"
|
|
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(
|
|
"
|
|
open grep.nu | nu-check --debug --as-module
|
|
"
|
|
));
|
|
|
|
assert!(actual.err.is_empty());
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn parse_with_flag_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(
|
|
"
|
|
open grep.nu | nu-check --debug
|
|
"
|
|
));
|
|
|
|
assert!(actual.err.is_empty());
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn parse_with_flag_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(
|
|
"
|
|
open grep.nu | nu-check --debug
|
|
"
|
|
));
|
|
|
|
assert!(actual.err.contains("Failed to parse content"));
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn parse_with_flag_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(
|
|
"
|
|
open grep.nu | lines | nu-check --debug
|
|
"
|
|
));
|
|
|
|
assert!(actual.err.contains("Failed to parse content"));
|
|
})
|
|
}
|
|
|
|
#[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#"
|
|
$env.FOO = 'foo'
|
|
"#,
|
|
)]);
|
|
|
|
let actual = nu!(
|
|
cwd: dirs.test(), pipeline(
|
|
"
|
|
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#"
|
|
$env.RETURN = (nu-check ../foo.nu)
|
|
"#,
|
|
)])
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
"foo.nu",
|
|
r#"
|
|
echo 'foo'
|
|
"#,
|
|
)]);
|
|
|
|
let actual = nu!(
|
|
cwd: dirs.test(), pipeline(
|
|
"
|
|
source-env lol/lol.nu;
|
|
$env.RETURN
|
|
"
|
|
));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
})
|
|
}
|
|
#[test]
|
|
fn nu_check_module_dir() {
|
|
Playground::setup("nu_check_test_26", |dirs, sandbox| {
|
|
sandbox
|
|
.mkdir("lol")
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
"lol/mod.nu",
|
|
r#"
|
|
export module foo.nu
|
|
export def main [] { 'lol' }
|
|
"#,
|
|
)])
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
"lol/foo.nu",
|
|
r#"
|
|
export def main [] { 'lol foo' }
|
|
"#,
|
|
)]);
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline( "nu-check lol"));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
})
|
|
}
|