2022-07-10 12:45:46 +02:00
|
|
|
use nu_cli::{eval_env_change_hook, eval_hook};
|
2022-05-08 15:09:39 +02:00
|
|
|
use nu_command::create_default_context;
|
|
|
|
use nu_engine::eval_block;
|
|
|
|
use nu_parser::parse;
|
2022-07-10 12:45:46 +02:00
|
|
|
use nu_protocol::engine::{EngineState, Stack, StateDelta, StateWorkingSet};
|
|
|
|
use nu_protocol::{CliError, PipelineData, Span, Value};
|
2022-05-08 15:09:39 +02:00
|
|
|
use nu_test_support::fs::in_directory;
|
|
|
|
use nu_test_support::Outcome;
|
|
|
|
|
2022-07-10 12:45:46 +02:00
|
|
|
fn outcome_err(
|
|
|
|
engine_state: &EngineState,
|
|
|
|
error: &(dyn miette::Diagnostic + Send + Sync + 'static),
|
|
|
|
) -> Outcome {
|
2022-07-12 03:25:31 +02:00
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
2022-07-10 12:45:46 +02:00
|
|
|
|
2022-07-12 03:25:31 +02:00
|
|
|
eprintln!("Error: {:?}", CliError(error, &working_set));
|
2022-07-10 12:45:46 +02:00
|
|
|
|
2022-05-08 15:09:39 +02:00
|
|
|
Outcome {
|
|
|
|
out: String::new(),
|
2022-07-10 12:45:46 +02:00
|
|
|
err: format!("{:?}", error),
|
2022-05-08 15:09:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn outcome_ok(msg: String) -> Outcome {
|
|
|
|
Outcome {
|
|
|
|
out: msg,
|
|
|
|
err: String::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nu_repl(cwd: &str, source_lines: &[&str]) -> Outcome {
|
|
|
|
let cwd = in_directory(cwd);
|
|
|
|
|
|
|
|
let mut engine_state = create_default_context(&cwd);
|
|
|
|
let mut stack = Stack::new();
|
|
|
|
|
|
|
|
stack.add_env_var(
|
|
|
|
"PWD".to_string(),
|
|
|
|
Value::String {
|
|
|
|
val: cwd.to_string(),
|
|
|
|
span: Span::test_data(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let delta = StateDelta::new(&engine_state);
|
|
|
|
if let Err(err) = engine_state.merge_delta(delta, Some(&mut stack), cwd) {
|
2022-07-10 12:45:46 +02:00
|
|
|
return outcome_err(&engine_state, &err);
|
2022-05-08 15:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut last_output = String::new();
|
|
|
|
|
|
|
|
for (i, line) in source_lines.iter().enumerate() {
|
2022-07-10 12:45:46 +02:00
|
|
|
// Check for pre_prompt hook
|
|
|
|
let config = engine_state.get_config();
|
|
|
|
if let Some(hook) = config.hooks.pre_prompt.clone() {
|
|
|
|
if let Err(err) = eval_hook(&mut engine_state, &mut stack, vec![], &hook) {
|
|
|
|
return outcome_err(&engine_state, &err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for env change hook
|
|
|
|
let config = engine_state.get_config();
|
|
|
|
if let Err(err) = eval_env_change_hook(
|
|
|
|
config.hooks.env_change.clone(),
|
|
|
|
&mut engine_state,
|
|
|
|
&mut stack,
|
|
|
|
) {
|
|
|
|
return outcome_err(&engine_state, &err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for pre_execution hook
|
|
|
|
let config = engine_state.get_config();
|
|
|
|
if let Some(hook) = config.hooks.pre_execution.clone() {
|
|
|
|
if let Err(err) = eval_hook(&mut engine_state, &mut stack, vec![], &hook) {
|
|
|
|
return outcome_err(&engine_state, &err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eval the REPL line
|
2022-05-08 15:09:39 +02:00
|
|
|
let (block, delta) = {
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
let (block, err) = parse(
|
|
|
|
&mut working_set,
|
|
|
|
Some(&format!("line{}", i)),
|
|
|
|
line.as_bytes(),
|
|
|
|
false,
|
|
|
|
&[],
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(err) = err {
|
2022-07-10 12:45:46 +02:00
|
|
|
return outcome_err(&engine_state, &err);
|
2022-05-08 15:09:39 +02:00
|
|
|
}
|
|
|
|
(block, working_set.render())
|
|
|
|
};
|
|
|
|
|
|
|
|
let cwd = match nu_engine::env::current_dir(&engine_state, &stack) {
|
|
|
|
Ok(p) => p,
|
|
|
|
Err(e) => {
|
2022-07-10 12:45:46 +02:00
|
|
|
return outcome_err(&engine_state, &e);
|
2022-05-08 15:09:39 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = engine_state.merge_delta(delta, Some(&mut stack), &cwd) {
|
2022-07-10 12:45:46 +02:00
|
|
|
return outcome_err(&engine_state, &err);
|
2022-05-08 15:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let input = PipelineData::new(Span::test_data());
|
|
|
|
let config = engine_state.get_config();
|
|
|
|
|
|
|
|
match eval_block(&engine_state, &mut stack, &block, input, false, false) {
|
|
|
|
Ok(pipeline_data) => match pipeline_data.collect_string("", config) {
|
|
|
|
Ok(s) => last_output = s,
|
2022-07-10 12:45:46 +02:00
|
|
|
Err(err) => return outcome_err(&engine_state, &err),
|
2022-05-08 15:09:39 +02:00
|
|
|
},
|
2022-07-10 12:45:46 +02:00
|
|
|
Err(err) => return outcome_err(&engine_state, &err),
|
2022-05-08 15:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(cwd) = stack.get_env_var(&engine_state, "PWD") {
|
|
|
|
let path = match cwd.as_string() {
|
|
|
|
Ok(p) => p,
|
2022-07-10 12:45:46 +02:00
|
|
|
Err(err) => return outcome_err(&engine_state, &err),
|
2022-05-08 15:09:39 +02:00
|
|
|
};
|
|
|
|
let _ = std::env::set_current_dir(path);
|
|
|
|
engine_state.add_env_var("PWD".into(), cwd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outcome_ok(last_output)
|
|
|
|
}
|