ensure that when nu evaluates files, it allows early returns (#7415)

# Description

Fixes #7301.

# User-Facing Changes

`return` can now be used in scripts without explicit `def main`.

# Tests + Formatting

Don't forget to add tests that cover your changes. (I'm not sure how to
test this.)

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
pwygab
2023-02-02 07:02:27 +08:00
committed by GitHub
parent c130ca1bc6
commit 24d7227e27
10 changed files with 56 additions and 8 deletions

View File

@ -23,3 +23,15 @@ fn early_return_if_false() {
assert_eq!(actual.out, r#"100"#);
}
#[test]
fn return_works_in_script_without_def_main() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
nu early_return.nu
"#
));
assert!(actual.err.is_empty());
}

View File

@ -314,3 +314,15 @@ fn source_env_const_file() {
assert_eq!(actual.out, "foo");
})
}
#[test]
fn source_respects_early_return() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
source early_return.nu
"#
));
assert!(actual.err.is_empty());
}