mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 14:45:56 +02:00
Playground infraestructure (tests, etc) additions. (#3179)
* Playground infraestructure (tests, etc) additions. A few things to note: * Nu can be started with a custom configuration file (`nu --config-file /path/to/sample_config.toml`). Useful for mocking the configuration on test runs. * When given a custom configuration file Nu will save any changes to the file supplied appropiately. * The `$nu.config-path` variable either shows the default configuration file (or the custom one, if given) * We can now run end to end tests with finer grained control (currently, since this is baseline work, standard out) This will allow to check things like exit status, assert the contents with a format, etc) * Remove (for another PR)
This commit is contained in:
committed by
GitHub
parent
82b6300dcb
commit
d2213d18fa
@ -51,6 +51,7 @@ users = "0.11.0"
|
||||
|
||||
[dev-dependencies]
|
||||
nu-test-support = { version = "0.28.0", path = "../nu-test-support" }
|
||||
hamcrest2 = "0.3.0"
|
||||
|
||||
[features]
|
||||
rustyline-support = []
|
||||
|
@ -232,7 +232,7 @@ fn evaluate_literal(literal: &hir::Literal, span: Span) -> Value {
|
||||
|
||||
fn evaluate_reference(name: &str, ctx: &EvaluationContext, tag: Tag) -> Result<Value, ShellError> {
|
||||
match name {
|
||||
"$nu" => crate::evaluate::variables::nu(&ctx.scope.get_env_vars(), tag),
|
||||
"$nu" => crate::evaluate::variables::nu(&ctx.scope, tag),
|
||||
|
||||
"$true" => Ok(Value {
|
||||
value: UntaggedValue::boolean(true),
|
||||
|
@ -1,10 +1,11 @@
|
||||
use crate::evaluate::scope::Scope;
|
||||
use crate::history_path::history_path;
|
||||
use indexmap::IndexMap;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value};
|
||||
use nu_protocol::{Primitive, TaggedDictBuilder, UntaggedValue, Value};
|
||||
use nu_source::Tag;
|
||||
|
||||
pub fn nu(env: &IndexMap<String, String>, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
||||
pub fn nu(scope: &Scope, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
||||
let env = &scope.get_env_vars();
|
||||
let tag = tag.into();
|
||||
|
||||
let mut nu_dict = TaggedDictBuilder::new(&tag);
|
||||
@ -17,7 +18,15 @@ pub fn nu(env: &IndexMap<String, String>, tag: impl Into<Tag>) -> Result<Value,
|
||||
}
|
||||
nu_dict.insert_value("env", dict.into_value());
|
||||
|
||||
let config = nu_data::config::read(&tag, &None)?;
|
||||
let config_file = match scope.get_var("config-path") {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::FilePath(path)),
|
||||
..
|
||||
}) => Some(path),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let config = nu_data::config::read(&tag, &config_file)?;
|
||||
nu_dict.insert_value("config", UntaggedValue::row(config).into_value(&tag));
|
||||
|
||||
let mut table = vec![];
|
||||
@ -40,7 +49,12 @@ pub fn nu(env: &IndexMap<String, String>, tag: impl Into<Tag>) -> Result<Value,
|
||||
let temp = std::env::temp_dir();
|
||||
nu_dict.insert_value("temp-dir", UntaggedValue::filepath(temp).into_value(&tag));
|
||||
|
||||
let config = nu_data::config::default_path()?;
|
||||
let config = if let Some(path) = config_file {
|
||||
path
|
||||
} else {
|
||||
nu_data::config::default_path()?
|
||||
};
|
||||
|
||||
nu_dict.insert_value(
|
||||
"config-path",
|
||||
UntaggedValue::filepath(config).into_value(&tag),
|
||||
|
@ -1,2 +1,3 @@
|
||||
mod invocation;
|
||||
mod operator;
|
||||
mod variables;
|
||||
|
34
crates/nu-engine/tests/evaluate/variables.rs
Normal file
34
crates/nu-engine/tests/evaluate/variables.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use nu_test_support::fs::Stub::FileWithContent;
|
||||
use nu_test_support::fs::{AbsolutePath, DisplayPath};
|
||||
use nu_test_support::playground::{says, Playground};
|
||||
|
||||
use hamcrest2::assert_that;
|
||||
use hamcrest2::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn config_path_variable_present() {
|
||||
Playground::setup("nu_variable_test_1", |_, nu| {
|
||||
assert_that!(
|
||||
nu.pipeline("echo $nu.config-path"),
|
||||
says().to_stdout(nu.get_config())
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_config_path_variable_present() {
|
||||
Playground::setup("nu_variable_test_2", |dirs, nu| {
|
||||
let file = AbsolutePath::new(dirs.test().join("config.toml"));
|
||||
|
||||
nu.with_config(&file);
|
||||
nu.with_files(vec![FileWithContent(
|
||||
"config.toml",
|
||||
"skip_welcome_message = true",
|
||||
)]);
|
||||
|
||||
assert_that!(
|
||||
nu.pipeline("echo $nu.config-path"),
|
||||
says().to_stdout(&file.display_path())
|
||||
);
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user