mirror of
https://github.com/nushell/nushell.git
synced 2025-01-08 23:40:17 +01:00
aa7d7d0cc3
# Description This grew quite a bit beyond its original scope, but I've tried to make `$in` a bit more consistent and easier to work with. Instead of the parser generating calls to `collect` and creating closures, this adds `Expr::Collect` which just evaluates in the same scope and doesn't require any closure. When `$in` is detected in an expression, it is replaced with a new variable (also called `$in`) and wrapped in `Expr::Collect`. During eval, this expression is evaluated directly, with the input and with that new variable set to the collected value. Other than being faster and less prone to gotchas, it also makes it possible to typecheck the output of an expression containing `$in`, which is nice. This is a breaking change though, because of the lack of the closure and because now typechecking will actually happen. Also, I haven't attempted to typecheck the input yet. The IR generated now just looks like this: ```gas collect %in clone %tmp, %in store-variable $in, %tmp # %out <- ...expression... <- %in drop-variable $in ``` (where `$in` is the local variable created for this collection, and not `IN_VARIABLE_ID`) which is a lot better than having to create a closure and call `collect --keep-env`, dealing with all of the capture gathering and allocation that entails. Ideally we can also detect whether that input is actually needed, so maybe we don't have to clone, but I haven't tried to do that yet. Theoretically now that the variable is a unique one every time, it should be possible to give it a type - I just don't know how to determine that yet. On top of that, I've also reworked how `$in` works in pipeline-initial position. Previously, it was a little bit inconsistent. For example, this worked: ```nushell > 3 | do { let x = $in; let y = $in; print $x $y } 3 3 ``` However, this causes a runtime variable not found error on the second `$in`: ```nushell > def foo [] { let x = $in; let y = $in; print $x $y }; 3 | foo Error: nu:🐚:variable_not_found × Variable not found ╭─[entry #115:1:35] 1 │ def foo [] { let x = $in; let y = $in; print $x $y }; 3 | foo · ─┬─ · ╰── variable not found ╰──── ``` I've fixed this by making the first element `$in` detection *always* happen at the block level, so if you use `$in` in pipeline-initial position anywhere in a block, it will collect with an implicit subexpression around the whole thing, and you can then use that `$in` more than once. In doing this I also rewrote `parse_pipeline()` and hopefully it's a bit more straightforward and possibly more efficient too now. Finally, I've tried to make `let` and `mut` a lot more straightforward with how they handle the rest of the pipeline, and using a redirection with `let`/`mut` now does what you'd expect if you assume that they consume the whole pipeline - the redirection is just processed as normal. These both work now: ```nushell let x = ^foo err> err.txt let y = ^foo out+err>| str length ``` It was previously possible to accomplish this with a subexpression, but it just seemed like a weird gotcha that you couldn't do it. Intuitively, `let` and `mut` just seem to take the whole line. - closes #13137 # User-Facing Changes - `$in` will behave more consistently with blocks and closures, since the entire block is now just wrapped to handle it if it appears in the first pipeline element - `$in` no longer creates a closure, so what can be done within an expression containing `$in` is less restrictive - `$in` containing expressions are now type checked, rather than just resulting in `any`. However, `$in` itself is still `any`, so this isn't quite perfect yet - Redirections are now allowed in `let` and `mut` and behave pretty much how you'd expect # Tests + Formatting Added tests to cover the new behaviour. # After Submitting - [ ] release notes (definitely breaking change)
140 lines
5.6 KiB
Rust
140 lines
5.6 KiB
Rust
#[cfg(test)]
|
|
use nu_protocol::engine::Command;
|
|
|
|
#[cfg(test)]
|
|
/// Runs the test examples in the passed in command and check their signatures and return values.
|
|
///
|
|
/// # Panics
|
|
/// If you get a ExternalNotSupported panic, you may be using a command
|
|
/// that's not in the default working set of the test harness.
|
|
/// You may want to use test_examples_with_commands and include any other dependencies.
|
|
pub fn test_examples(cmd: impl Command + 'static) {
|
|
test_examples::test_examples(cmd, &[]);
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub fn test_examples_with_commands(cmd: impl Command + 'static, commands: &[&dyn Command]) {
|
|
test_examples::test_examples(cmd, commands);
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test_examples {
|
|
use super::super::{
|
|
Ansi, Date, Enumerate, Filter, First, Flatten, From, Get, Into, IntoDatetime, IntoString,
|
|
Lines, Math, MathRound, MathSum, ParEach, Path, PathParse, Random, Seq, Sort, SortBy,
|
|
Split, SplitColumn, SplitRow, Str, StrJoin, StrLength, StrReplace, Update, Url, Values,
|
|
Wrap,
|
|
};
|
|
use crate::{Default, Each, To};
|
|
use nu_cmd_lang::example_support::{
|
|
check_all_signature_input_output_types_entries_have_examples,
|
|
check_example_evaluates_to_expected_output,
|
|
check_example_input_and_output_types_match_command_signature,
|
|
};
|
|
use nu_cmd_lang::{Break, Describe, Echo, If, Let, Mut};
|
|
use nu_protocol::{
|
|
engine::{Command, EngineState, StateWorkingSet},
|
|
Type,
|
|
};
|
|
use std::collections::HashSet;
|
|
|
|
pub fn test_examples(cmd: impl Command + 'static, commands: &[&dyn Command]) {
|
|
let examples = cmd.examples();
|
|
let signature = cmd.signature();
|
|
let mut engine_state = make_engine_state(cmd.clone_box(), commands);
|
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory.");
|
|
|
|
let mut witnessed_type_transformations = HashSet::<(Type, Type)>::new();
|
|
|
|
for example in examples {
|
|
if example.result.is_none() {
|
|
continue;
|
|
}
|
|
|
|
witnessed_type_transformations.extend(
|
|
check_example_input_and_output_types_match_command_signature(
|
|
&example,
|
|
&cwd,
|
|
&mut make_engine_state(cmd.clone_box(), commands),
|
|
&signature.input_output_types,
|
|
signature.operates_on_cell_paths(),
|
|
),
|
|
);
|
|
check_example_evaluates_to_expected_output(&example, cwd.as_path(), &mut engine_state);
|
|
}
|
|
|
|
check_all_signature_input_output_types_entries_have_examples(
|
|
signature,
|
|
witnessed_type_transformations,
|
|
);
|
|
}
|
|
|
|
fn make_engine_state(cmd: Box<dyn Command>, commands: &[&dyn Command]) -> Box<EngineState> {
|
|
let mut engine_state = Box::new(EngineState::new());
|
|
|
|
let delta = {
|
|
// Base functions that are needed for testing
|
|
// Try to keep this working set small to keep tests running as fast as possible
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
working_set.add_decl(Box::new(Ansi));
|
|
working_set.add_decl(Box::new(Break));
|
|
working_set.add_decl(Box::new(Date));
|
|
working_set.add_decl(Box::new(Default));
|
|
working_set.add_decl(Box::new(Describe));
|
|
working_set.add_decl(Box::new(Each));
|
|
working_set.add_decl(Box::new(Echo));
|
|
working_set.add_decl(Box::new(Enumerate));
|
|
working_set.add_decl(Box::new(Filter));
|
|
working_set.add_decl(Box::new(First));
|
|
working_set.add_decl(Box::new(Flatten));
|
|
working_set.add_decl(Box::new(From));
|
|
working_set.add_decl(Box::new(Get));
|
|
working_set.add_decl(Box::new(If));
|
|
working_set.add_decl(Box::new(Into));
|
|
working_set.add_decl(Box::new(IntoString));
|
|
working_set.add_decl(Box::new(IntoDatetime));
|
|
working_set.add_decl(Box::new(Let));
|
|
working_set.add_decl(Box::new(Lines));
|
|
working_set.add_decl(Box::new(Math));
|
|
working_set.add_decl(Box::new(MathRound));
|
|
working_set.add_decl(Box::new(MathSum));
|
|
working_set.add_decl(Box::new(Mut));
|
|
working_set.add_decl(Box::new(Path));
|
|
working_set.add_decl(Box::new(PathParse));
|
|
working_set.add_decl(Box::new(ParEach));
|
|
working_set.add_decl(Box::new(Random));
|
|
working_set.add_decl(Box::new(Seq));
|
|
working_set.add_decl(Box::new(Sort));
|
|
working_set.add_decl(Box::new(SortBy));
|
|
working_set.add_decl(Box::new(Split));
|
|
working_set.add_decl(Box::new(SplitColumn));
|
|
working_set.add_decl(Box::new(SplitRow));
|
|
working_set.add_decl(Box::new(Str));
|
|
working_set.add_decl(Box::new(StrJoin));
|
|
working_set.add_decl(Box::new(StrLength));
|
|
working_set.add_decl(Box::new(StrReplace));
|
|
working_set.add_decl(Box::new(To));
|
|
working_set.add_decl(Box::new(Url));
|
|
working_set.add_decl(Box::new(Update));
|
|
working_set.add_decl(Box::new(Values));
|
|
working_set.add_decl(Box::new(Wrap));
|
|
|
|
// Add any extra commands that the test harness needs
|
|
for command in commands {
|
|
working_set.add_decl(command.clone_box());
|
|
}
|
|
|
|
// Adding the command that is being tested to the working set
|
|
working_set.add_decl(cmd);
|
|
|
|
working_set.render()
|
|
};
|
|
|
|
engine_state
|
|
.merge_delta(delta)
|
|
.expect("Error merging delta");
|
|
engine_state
|
|
}
|
|
}
|