nu_cli refactor in preparation for a crate called nu_command (#2907)

* move basic_shell_manager to nu-engine

* move basic_evaluation_context to nu-engine

* fix failing test in feature which commands/classified/external.rs
This commit is contained in:
Michael Angerman
2021-01-10 20:58:15 -08:00
committed by GitHub
parent 231a445809
commit 481c6d4511
13 changed files with 60 additions and 40 deletions

View File

@ -1,13 +1,7 @@
use crate::commands::default_context::create_default_context;
use crate::env::basic_host::BasicHost;
use crate::line_editor::configure_ctrl_c;
use nu_engine::run_block;
use nu_engine::EvaluationContext;
use nu_engine::{FilesystemShell, Scope, ShellManager};
use parking_lot::Mutex;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
#[allow(unused_imports)]
pub(crate) use crate::script::{process_script, LineResult};
@ -85,25 +79,6 @@ pub fn maybe_print_errors(context: &EvaluationContext, source: Text) -> bool {
}
}
pub fn basic_shell_manager() -> Result<ShellManager, Box<dyn Error>> {
Ok(ShellManager {
current_shell: Arc::new(AtomicUsize::new(0)),
shells: Arc::new(Mutex::new(vec![Box::new(FilesystemShell::basic()?)])),
})
}
pub fn basic_evaluation_context() -> Result<EvaluationContext, Box<dyn Error>> {
Ok(EvaluationContext {
scope: Scope::new(),
host: Arc::new(parking_lot::Mutex::new(Box::new(BasicHost))),
current_errors: Arc::new(Mutex::new(vec![])),
ctrl_c: Arc::new(AtomicBool::new(false)),
user_recently_used_autoenv_untrust: Arc::new(AtomicBool::new(false)),
shell_manager: crate::cli::basic_shell_manager()?,
windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())),
})
}
pub async fn run_script_file(
file_contents: String,
redirect_stdin: bool,
@ -445,13 +420,14 @@ pub async fn parse_and_eval(line: &str, ctx: &EvaluationContext) -> Result<Strin
#[cfg(test)]
mod tests {
use nu_engine::basic_evaluation_context;
#[quickcheck]
fn quickcheck_parse(data: String) -> bool {
let (tokens, err) = nu_parser::lex(&data, 0);
let (lite_block, err2) = nu_parser::block(tokens);
if err.is_none() && err2.is_none() {
let context = crate::cli::basic_evaluation_context().unwrap();
let context = basic_evaluation_context().unwrap();
let _ = nu_parser::classify_block(&lite_block, &context.scope);
}
true

View File

@ -538,10 +538,11 @@ mod tests {
#[cfg(feature = "which")]
use futures::executor::block_on;
#[cfg(feature = "which")]
use nu_engine::basic_evaluation_context;
#[cfg(feature = "which")]
use nu_errors::ShellError;
#[cfg(feature = "which")]
use nu_test_support::commands::ExternalBuilder;
// async fn read(mut stream: OutputStream) -> Option<Value> {
// match stream.try_next().await {
// Ok(val) => {
@ -561,8 +562,8 @@ mod tests {
let cmd = ExternalBuilder::for_name("i_dont_exist.exe").build();
let input = InputStream::empty();
let mut ctx = crate::cli::basic_evaluation_context()
.expect("There was a problem creating a basic context.");
let mut ctx =
basic_evaluation_context().expect("There was a problem creating a basic context.");
assert!(
run_external_command(cmd, &mut ctx, input, ExternalRedirection::Stdout)

View File

@ -1,9 +1,10 @@
use crate::prelude::*;
use nu_engine::basic_evaluation_context;
use nu_engine::whole_stream_command;
use std::error::Error;
pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Box<dyn Error>> {
let context = crate::cli::basic_evaluation_context()?;
let context = basic_evaluation_context()?;
{
use crate::commands::*;

View File

@ -1,4 +1,3 @@
pub(crate) mod basic_host;
pub(crate) mod directory_specific_environment;
pub(crate) mod environment;
pub(crate) mod environment_syncer;

View File

@ -1,77 +0,0 @@
use nu_engine::Host;
use nu_protocol::{errln, outln};
use std::ffi::OsString;
#[derive(Debug)]
pub struct BasicHost;
impl Host for BasicHost {
fn stdout(&mut self, out: &str) {
match out {
"\n" => outln!(""),
other => outln!("{}", other),
}
}
fn stderr(&mut self, out: &str) {
match out {
"\n" => errln!(""),
other => errln!("{}", other),
}
}
#[allow(unused_variables)]
fn vars(&mut self) -> Vec<(String, String)> {
#[cfg(not(target_arch = "wasm32"))]
{
std::env::vars().collect::<Vec<_>>()
}
#[cfg(target_arch = "wasm32")]
{
vec![]
}
}
#[allow(unused_variables)]
fn env_get(&mut self, key: OsString) -> Option<OsString> {
#[cfg(not(target_arch = "wasm32"))]
{
std::env::var_os(key)
}
#[cfg(target_arch = "wasm32")]
{
None
}
}
#[allow(unused_variables)]
fn env_set(&mut self, key: OsString, value: OsString) {
#[cfg(not(target_arch = "wasm32"))]
{
std::env::set_var(key, value);
}
}
#[allow(unused_variables)]
fn env_rm(&mut self, key: OsString) {
#[cfg(not(target_arch = "wasm32"))]
{
std::env::remove_var(key);
}
}
fn out_termcolor(&self) -> termcolor::StandardStream {
termcolor::StandardStream::stdout(termcolor::ColorChoice::Auto)
}
fn err_termcolor(&self) -> termcolor::StandardStream {
termcolor::StandardStream::stderr(termcolor::ColorChoice::Auto)
}
fn width(&self) -> usize {
let (mut term_width, _) = term_size::dimensions().unwrap_or((80, 20));
term_width -= 1;
term_width
}
}

View File

@ -155,6 +155,7 @@ mod tests {
use super::EnvironmentSyncer;
use indexmap::IndexMap;
use nu_data::config::tests::FakeConfig;
use nu_engine::basic_evaluation_context;
use nu_engine::Env;
use nu_errors::ShellError;
use nu_test_support::fs::Stub::FileWithContent;
@ -170,7 +171,7 @@ mod tests {
#[test]
fn syncs_env_if_new_env_entry_is_added_to_an_existing_configuration() -> Result<(), ShellError>
{
let mut ctx = crate::cli::basic_evaluation_context()?;
let mut ctx = basic_evaluation_context()?;
ctx.host = Arc::new(Mutex::new(Box::new(nu_engine::FakeHost::new())));
let mut expected = IndexMap::new();
@ -273,7 +274,7 @@ mod tests {
#[test]
fn syncs_env_if_new_env_entry_in_session_is_not_in_configuration_file() -> Result<(), ShellError>
{
let mut ctx = crate::cli::basic_evaluation_context()?;
let mut ctx = basic_evaluation_context()?;
ctx.host = Arc::new(Mutex::new(Box::new(nu_engine::FakeHost::new())));
let mut expected = IndexMap::new();
@ -372,7 +373,7 @@ mod tests {
#[test]
fn nu_envs_have_higher_priority_and_does_not_get_overwritten() -> Result<(), ShellError> {
let mut ctx = crate::cli::basic_evaluation_context()?;
let mut ctx = basic_evaluation_context()?;
ctx.host = Arc::new(Mutex::new(Box::new(nu_engine::FakeHost::new())));
let mut expected = IndexMap::new();
@ -448,7 +449,7 @@ mod tests {
#[test]
fn syncs_path_if_new_path_entry_in_session_is_not_in_configuration_file(
) -> Result<(), ShellError> {
let mut ctx = crate::cli::basic_evaluation_context()?;
let mut ctx = basic_evaluation_context()?;
ctx.host = Arc::new(Mutex::new(Box::new(nu_engine::FakeHost::new())));
let expected = std::env::join_paths(vec![
@ -535,7 +536,7 @@ mod tests {
#[test]
fn nu_paths_have_higher_priority_and_new_paths_get_appended_to_the_end(
) -> Result<(), ShellError> {
let mut ctx = crate::cli::basic_evaluation_context()?;
let mut ctx = basic_evaluation_context()?;
ctx.host = Arc::new(Mutex::new(Box::new(nu_engine::FakeHost::new())));
let expected = std::env::join_paths(vec![

View File

@ -1,3 +1,4 @@
use nu_engine::basic_evaluation_context;
use nu_errors::ShellError;
use nu_parser::ParserScope;
use nu_protocol::hir::ClassifiedBlock;
@ -25,7 +26,7 @@ use serde::Deserialize;
pub fn test_examples(cmd: Command) -> Result<(), ShellError> {
let examples = cmd.examples();
let base_context = crate::cli::basic_evaluation_context()?;
let base_context = basic_evaluation_context()?;
base_context.add_commands(vec![
// Mocks
@ -89,7 +90,7 @@ pub fn test_examples(cmd: Command) -> Result<(), ShellError> {
pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> {
let examples = cmd.examples();
let base_context = crate::cli::basic_evaluation_context()?;
let base_context = basic_evaluation_context()?;
base_context.add_commands(vec![
whole_stream_command(Echo {}),
@ -145,7 +146,7 @@ pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> {
pub fn test_anchors(cmd: Command) -> Result<(), ShellError> {
let examples = cmd.examples();
let base_context = crate::cli::basic_evaluation_context()?;
let base_context = basic_evaluation_context()?;
base_context.add_commands(vec![
// Minimal restricted commands to aid in testing