make tests pass

This commit is contained in:
Devyn Cairns 2024-07-11 18:22:07 -07:00
parent 59b217443a
commit d4bb4feafa
4 changed files with 39 additions and 18 deletions

View File

@ -4,7 +4,7 @@ use nu_protocol::{
ast::Block, ast::Block,
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{StateDelta, StateWorkingSet}, engine::{StateDelta, StateWorkingSet},
Range, report_error_new, Range,
}; };
use std::{ use std::{
sync::Arc, sync::Arc,
@ -124,7 +124,10 @@ pub fn eval_block(
nu_engine::eval_block::<WithoutDebug>(engine_state, &mut stack, &block, input) nu_engine::eval_block::<WithoutDebug>(engine_state, &mut stack, &block, input)
.and_then(|data| data.into_value(Span::test_data())) .and_then(|data| data.into_value(Span::test_data()))
.unwrap_or_else(|err| panic!("test eval error in `{}`: {:?}", "TODO", err)) .unwrap_or_else(|err| {
report_error_new(engine_state, &err);
panic!("test eval error in `{}`: {:?}", "TODO", err)
})
} }
pub fn check_example_evaluates_to_expected_output( pub fn check_example_evaluates_to_expected_output(

View File

@ -31,7 +31,7 @@ mod test_examples {
check_example_evaluates_to_expected_output, check_example_evaluates_to_expected_output,
check_example_input_and_output_types_match_command_signature, check_example_input_and_output_types_match_command_signature,
}; };
use nu_cmd_lang::{Break, Echo, If, Let, Mut}; use nu_cmd_lang::{Break, Describe, Echo, If, Let, Mut};
use nu_protocol::{ use nu_protocol::{
engine::{Command, EngineState, StateWorkingSet}, engine::{Command, EngineState, StateWorkingSet},
Type, Type,
@ -81,6 +81,7 @@ mod test_examples {
working_set.add_decl(Box::new(Break)); working_set.add_decl(Box::new(Break));
working_set.add_decl(Box::new(Date)); working_set.add_decl(Box::new(Date));
working_set.add_decl(Box::new(Default)); 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(Each));
working_set.add_decl(Box::new(Echo)); working_set.add_decl(Box::new(Echo));
working_set.add_decl(Box::new(Enumerate)); working_set.add_decl(Box::new(Enumerate));

View File

@ -172,23 +172,22 @@ pub(crate) fn compile_expression(
} }
} }
Expr::Collect(var_id, expr) => { Expr::Collect(var_id, expr) => {
let in_reg = match in_reg { let store_reg = if let Some(in_reg) = in_reg {
Some(in_reg) => in_reg, // Collect, clone, store
None => { builder.push(Instruction::Collect { src_dst: in_reg }.into_spanned(expr.span))?;
let reg_id = builder.next_register()?; builder.clone_reg(in_reg, expr.span)?
builder.load_empty(reg_id)?; } else {
reg_id // Just store nothing in the variable
} builder.literal(Literal::Nothing.into_spanned(Span::unknown()))?
}; };
// Implicit collect
builder.push( builder.push(
Instruction::StoreVariable { Instruction::StoreVariable {
var_id: *var_id, var_id: *var_id,
src: in_reg, src: store_reg,
} }
.into_spanned(expr.span), .into_spanned(expr.span),
)?; )?;
compile_expression(working_set, builder, expr, redirect_modes, None, out_reg)?; compile_expression(working_set, builder, expr, redirect_modes, in_reg, out_reg)?;
// Clean it up afterward // Clean it up afterward
builder.push(Instruction::DropVariable { var_id: *var_id }.into_spanned(expr.span))?; builder.push(Instruction::DropVariable { var_id: *var_id }.into_spanned(expr.span))?;
Ok(()) Ok(())

View File

@ -10,8 +10,8 @@ use nu_protocol::{
debugger::DebugContext, debugger::DebugContext,
engine::{Closure, EngineState, Redirection, Stack, StateWorkingSet}, engine::{Closure, EngineState, Redirection, Stack, StateWorkingSet},
eval_base::Eval, eval_base::Eval,
ByteStreamSource, Config, FromValue, IntoPipelineData, OutDest, PipelineData, ShellError, Span, ByteStreamSource, Config, DataSource, FromValue, IntoPipelineData, OutDest, PipelineData,
Spanned, Type, Value, VarId, ENV_VARIABLE_ID, PipelineMetadata, ShellError, Span, Spanned, Type, Value, VarId, ENV_VARIABLE_ID,
}; };
use nu_utils::IgnoreCaseExt; use nu_utils::IgnoreCaseExt;
use std::{fs::OpenOptions, path::PathBuf, sync::Arc}; use std::{fs::OpenOptions, path::PathBuf, sync::Arc};
@ -619,9 +619,27 @@ pub fn eval_collect<D: DebugContext>(
// Evaluate the expression with the variable set to the collected input // Evaluate the expression with the variable set to the collected input
let span = input.span().unwrap_or(Span::unknown()); let span = input.span().unwrap_or(Span::unknown());
stack.add_var(var_id, input.into_value(span)?); let metadata = match input.metadata() {
// Remove the `FilePath` metadata, because after `collect` it's no longer necessary to
// check where some input came from.
Some(PipelineMetadata {
data_source: DataSource::FilePath(_),
content_type: None,
}) => None,
other => other,
};
let result = eval_expression_with_input::<D>(engine_state, stack, expr, PipelineData::empty()) let input = input.into_value(span)?;
stack.add_var(var_id, input.clone());
let result = eval_expression_with_input::<D>(
engine_state,
stack,
expr,
// We still have to pass it as input
input.into_pipeline_data_with_metadata(metadata),
)
.map(|(result, _failed)| result); .map(|(result, _failed)| result);
stack.remove_var(var_id); stack.remove_var(var_id);