2022-05-10 22:17:07 +02:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use nu_engine::eval_block;
|
|
|
|
use nu_parser::parse;
|
|
|
|
use nu_protocol::{
|
2022-07-14 16:09:27 +02:00
|
|
|
engine::{EngineState, Stack, StateWorkingSet},
|
2023-09-01 08:18:55 +02:00
|
|
|
eval_const::create_nu_constant,
|
|
|
|
PipelineData, ShellError, Span, Value, NU_VARIABLE_ID,
|
2022-05-10 22:17:07 +02:00
|
|
|
};
|
|
|
|
use nu_test_support::fs;
|
|
|
|
use reedline::Suggestion;
|
|
|
|
const SEP: char = std::path::MAIN_SEPARATOR;
|
|
|
|
|
2023-06-14 23:12:55 +02:00
|
|
|
fn create_default_context() -> EngineState {
|
|
|
|
nu_command::add_shell_command_context(nu_cmd_lang::create_default_context())
|
|
|
|
}
|
|
|
|
|
2022-05-10 22:17:07 +02:00
|
|
|
// creates a new engine with the current path into the completions fixtures folder
|
|
|
|
pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
|
|
|
|
// Target folder inside assets
|
|
|
|
let dir = fs::fixtures().join("completions");
|
|
|
|
let mut dir_str = dir
|
|
|
|
.clone()
|
|
|
|
.into_os_string()
|
|
|
|
.into_string()
|
|
|
|
.unwrap_or_default();
|
|
|
|
dir_str.push(SEP);
|
|
|
|
|
|
|
|
// Create a new engine with default context
|
2022-07-14 16:09:27 +02:00
|
|
|
let mut engine_state = create_default_context();
|
2022-05-10 22:17:07 +02:00
|
|
|
|
2023-09-01 08:18:55 +02:00
|
|
|
// Add $nu
|
|
|
|
let nu_const =
|
|
|
|
create_nu_constant(&engine_state, Span::test_data()).expect("Failed creating $nu");
|
|
|
|
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
|
|
|
|
|
2022-05-10 22:17:07 +02:00
|
|
|
// New stack
|
|
|
|
let mut stack = Stack::new();
|
|
|
|
|
2022-12-08 21:37:10 +01:00
|
|
|
// Add pwd as env var
|
|
|
|
stack.add_env_var(
|
|
|
|
"PWD".to_string(),
|
2023-09-03 16:27:29 +02:00
|
|
|
Value::string(dir_str.clone(), nu_protocol::Span::new(0, dir_str.len())),
|
2022-12-08 21:37:10 +01:00
|
|
|
);
|
|
|
|
stack.add_env_var(
|
|
|
|
"TEST".to_string(),
|
2023-09-03 16:27:29 +02:00
|
|
|
Value::string(
|
|
|
|
"NUSHELL".to_string(),
|
|
|
|
nu_protocol::Span::new(0, dir_str.len()),
|
|
|
|
),
|
2023-02-09 03:53:46 +01:00
|
|
|
);
|
|
|
|
#[cfg(windows)]
|
|
|
|
stack.add_env_var(
|
|
|
|
"Path".to_string(),
|
2023-09-03 16:27:29 +02:00
|
|
|
Value::string(
|
|
|
|
"c:\\some\\path;c:\\some\\other\\path".to_string(),
|
|
|
|
nu_protocol::Span::new(0, dir_str.len()),
|
|
|
|
),
|
2023-02-09 03:53:46 +01:00
|
|
|
);
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
stack.add_env_var(
|
|
|
|
"PATH".to_string(),
|
2023-09-03 16:27:29 +02:00
|
|
|
Value::string(
|
|
|
|
"/some/path:/some/other/path".to_string(),
|
|
|
|
nu_protocol::Span::new(0, dir_str.len()),
|
|
|
|
),
|
2022-12-08 21:37:10 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Merge environment into the permanent state
|
2023-06-04 21:04:28 +02:00
|
|
|
let merge_result = engine_state.merge_env(&mut stack, &dir);
|
|
|
|
assert!(merge_result.is_ok());
|
2022-12-08 21:37:10 +01:00
|
|
|
|
|
|
|
(dir, dir_str, engine_state, stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_quote_engine() -> (PathBuf, String, EngineState, Stack) {
|
|
|
|
// Target folder inside assets
|
|
|
|
let dir = fs::fixtures().join("quoted_completions");
|
|
|
|
let mut dir_str = dir
|
|
|
|
.clone()
|
|
|
|
.into_os_string()
|
|
|
|
.into_string()
|
|
|
|
.unwrap_or_default();
|
|
|
|
dir_str.push(SEP);
|
|
|
|
|
|
|
|
// Create a new engine with default context
|
|
|
|
let mut engine_state = create_default_context();
|
|
|
|
|
|
|
|
// New stack
|
|
|
|
let mut stack = Stack::new();
|
|
|
|
|
2022-05-10 22:17:07 +02:00
|
|
|
// Add pwd as env var
|
|
|
|
stack.add_env_var(
|
|
|
|
"PWD".to_string(),
|
2023-09-03 16:27:29 +02:00
|
|
|
Value::string(dir_str.clone(), nu_protocol::Span::new(0, dir_str.len())),
|
2022-05-10 22:17:07 +02:00
|
|
|
);
|
2022-05-26 23:38:03 +02:00
|
|
|
stack.add_env_var(
|
2023-10-02 19:44:51 +02:00
|
|
|
"TEST".to_string(),
|
|
|
|
Value::string(
|
|
|
|
"NUSHELL".to_string(),
|
|
|
|
nu_protocol::Span::new(0, dir_str.len()),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Merge environment into the permanent state
|
|
|
|
let merge_result = engine_state.merge_env(&mut stack, &dir);
|
|
|
|
assert!(merge_result.is_ok());
|
|
|
|
|
|
|
|
(dir, dir_str, engine_state, stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_partial_engine() -> (PathBuf, String, EngineState, Stack) {
|
|
|
|
// Target folder inside assets
|
|
|
|
let dir = fs::fixtures().join("partial_completions");
|
|
|
|
let mut dir_str = dir
|
|
|
|
.clone()
|
|
|
|
.into_os_string()
|
|
|
|
.into_string()
|
|
|
|
.unwrap_or_default();
|
|
|
|
dir_str.push(SEP);
|
|
|
|
|
|
|
|
// Create a new engine with default context
|
|
|
|
let mut engine_state = create_default_context();
|
|
|
|
|
|
|
|
// New stack
|
|
|
|
let mut stack = Stack::new();
|
|
|
|
|
|
|
|
// Add pwd as env var
|
|
|
|
stack.add_env_var(
|
|
|
|
"PWD".to_string(),
|
|
|
|
Value::string(dir_str.clone(), nu_protocol::Span::new(0, dir_str.len())),
|
|
|
|
);
|
|
|
|
stack.add_env_var(
|
2022-05-26 23:38:03 +02:00
|
|
|
"TEST".to_string(),
|
2023-09-03 16:27:29 +02:00
|
|
|
Value::string(
|
|
|
|
"NUSHELL".to_string(),
|
|
|
|
nu_protocol::Span::new(0, dir_str.len()),
|
|
|
|
),
|
2022-05-26 23:38:03 +02:00
|
|
|
);
|
2022-05-10 22:17:07 +02:00
|
|
|
|
2022-07-14 16:09:27 +02:00
|
|
|
// Merge environment into the permanent state
|
2023-06-04 21:04:28 +02:00
|
|
|
let merge_result = engine_state.merge_env(&mut stack, &dir);
|
|
|
|
assert!(merge_result.is_ok());
|
2022-05-10 22:17:07 +02:00
|
|
|
|
2022-05-14 22:09:41 +02:00
|
|
|
(dir, dir_str, engine_state, stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
// match a list of suggestions with the expected values
|
|
|
|
pub fn match_suggestions(expected: Vec<String>, suggestions: Vec<Suggestion>) {
|
2022-07-17 14:46:40 +02:00
|
|
|
let expected_len = expected.len();
|
|
|
|
let suggestions_len = suggestions.len();
|
|
|
|
if expected_len != suggestions_len {
|
|
|
|
panic!(
|
|
|
|
"\nexpected {expected_len} suggestions but got {suggestions_len}: \n\
|
|
|
|
Suggestions: {suggestions:#?} \n\
|
|
|
|
Expected: {expected:#?}\n"
|
|
|
|
)
|
|
|
|
}
|
2022-05-14 22:09:41 +02:00
|
|
|
expected.iter().zip(suggestions).for_each(|it| {
|
|
|
|
assert_eq!(it.0, &it.1.value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// append the separator to the converted path
|
|
|
|
pub fn folder(path: PathBuf) -> String {
|
|
|
|
let mut converted_path = file(path);
|
|
|
|
converted_path.push(SEP);
|
|
|
|
|
|
|
|
converted_path
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert a given path to string
|
|
|
|
pub fn file(path: PathBuf) -> String {
|
|
|
|
path.into_os_string().into_string().unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge_input executes the given input into the engine
|
|
|
|
// and merges the state
|
|
|
|
pub fn merge_input(
|
|
|
|
input: &[u8],
|
|
|
|
engine_state: &mut EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
dir: PathBuf,
|
|
|
|
) -> Result<(), ShellError> {
|
2022-05-10 22:17:07 +02:00
|
|
|
let (block, delta) = {
|
2022-06-04 08:47:36 +02:00
|
|
|
let mut working_set = StateWorkingSet::new(engine_state);
|
2022-05-10 22:17:07 +02:00
|
|
|
|
2023-04-07 20:09:38 +02:00
|
|
|
let block = parse(&mut working_set, None, input, false);
|
2022-05-10 22:17:07 +02:00
|
|
|
|
2023-04-07 02:35:45 +02:00
|
|
|
assert!(working_set.parse_errors.is_empty());
|
2022-05-10 22:17:07 +02:00
|
|
|
|
|
|
|
(block, working_set.render())
|
|
|
|
};
|
2022-07-14 16:09:27 +02:00
|
|
|
|
2022-09-26 19:29:25 +02:00
|
|
|
engine_state.merge_delta(delta)?;
|
2022-07-14 16:09:27 +02:00
|
|
|
|
2022-05-10 22:17:07 +02:00
|
|
|
assert!(eval_block(
|
2022-06-04 08:47:36 +02:00
|
|
|
engine_state,
|
2022-05-14 22:09:41 +02:00
|
|
|
stack,
|
2022-05-10 22:17:07 +02:00
|
|
|
&block,
|
2023-09-03 16:27:29 +02:00
|
|
|
PipelineData::Value(Value::nothing(Span::unknown(),), None),
|
2022-05-10 22:17:07 +02:00
|
|
|
false,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.is_ok());
|
|
|
|
|
2022-07-14 16:09:27 +02:00
|
|
|
// Merge environment into the permanent state
|
2023-06-04 21:04:28 +02:00
|
|
|
engine_state.merge_env(stack, &dir)
|
2022-05-10 22:17:07 +02:00
|
|
|
}
|