mirror of
https://github.com/nushell/nushell
synced 2024-11-10 15:14:14 +00:00
Removes export env
command (#6468)
* remove export_env command * remove several export env usage in test code * adjust hiding relative test case * fix clippy * adjust tests * update tests * unignore these tests to expose ut failed * using `use` instead of `overlay use` in some tests * Revert "using `use` instead of `overlay use` in some tests" This reverts commit2ae24b24c3
. * Revert "adjust hiding relative test case" This reverts commit4369af6d05
. * Bring back module example * Revert "update tests" This reverts commit6ae94ef513
. * Fix tests * "Fix" a test * Remove remaining deprecated env functionality * Re-enable environment hiding for `hide` To not break virtualenv since the overlay update is not merged yet * Fix hiding env in `hide` and ignore some tests Co-authored-by: kubouch <kubouch@gmail.com>
This commit is contained in:
parent
7f21b7fd7e
commit
b47bd22b37
18 changed files with 71 additions and 539 deletions
|
@ -1,62 +0,0 @@
|
|||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, Example, PipelineData, Signature, Span, SyntaxShape, Value};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ExportEnvModule;
|
||||
|
||||
impl Command for ExportEnvModule {
|
||||
fn name(&self) -> &str {
|
||||
"export env"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Export a block from a module that will be evaluated as an environment variable when imported."
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("export env")
|
||||
.required(
|
||||
"name",
|
||||
SyntaxShape::String,
|
||||
"name of the environment variable",
|
||||
)
|
||||
.required(
|
||||
"block",
|
||||
SyntaxShape::Block(Some(vec![])),
|
||||
"body of the environment variable definition",
|
||||
)
|
||||
.category(Category::Core)
|
||||
}
|
||||
|
||||
fn extra_usage(&self) -> &str {
|
||||
r#"This command is a parser keyword. For details, check:
|
||||
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||
}
|
||||
|
||||
fn is_parser_keyword(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
//TODO: Add the env to stack
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Import and evaluate environment variable from a module",
|
||||
example: r#"module foo { export env FOO_ENV { "BAZ" } }; use foo FOO_ENV; $env.FOO_ENV"#,
|
||||
result: Some(Value::String {
|
||||
val: "BAZ".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
}]
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
use nu_protocol::ast::{Call, Expr, Expression, ImportPatternMember};
|
||||
use nu_protocol::ast::{Call, Expr, Expression};
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
|
@ -40,12 +40,15 @@ This command is a parser keyword. For details, check:
|
|||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let import_pattern = if let Some(Expression {
|
||||
let env_var_name = if let Some(Expression {
|
||||
expr: Expr::ImportPattern(pat),
|
||||
..
|
||||
}) = call.positional_nth(0)
|
||||
{
|
||||
pat
|
||||
Spanned {
|
||||
item: String::from_utf8_lossy(&pat.head.name).to_string(),
|
||||
span: pat.head.span,
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::GenericError(
|
||||
"Unexpected import".into(),
|
||||
|
@ -56,78 +59,7 @@ This command is a parser keyword. For details, check:
|
|||
));
|
||||
};
|
||||
|
||||
let head_name_str = if let Ok(s) = String::from_utf8(import_pattern.head.name.clone()) {
|
||||
s
|
||||
} else {
|
||||
return Err(ShellError::NonUtf8(import_pattern.head.span));
|
||||
};
|
||||
|
||||
if let Some(module_id) = engine_state.find_module(&import_pattern.head.name, &[]) {
|
||||
// The first word is a module
|
||||
let module = engine_state.get_module(module_id);
|
||||
|
||||
let env_vars_to_hide = if import_pattern.members.is_empty() {
|
||||
module.env_vars_with_head(&import_pattern.head.name)
|
||||
} else {
|
||||
match &import_pattern.members[0] {
|
||||
ImportPatternMember::Glob { .. } => module.env_vars(),
|
||||
ImportPatternMember::Name { name, span } => {
|
||||
let mut output = vec![];
|
||||
|
||||
if let Some((name, id)) =
|
||||
module.env_var_with_head(name, &import_pattern.head.name)
|
||||
{
|
||||
output.push((name, id));
|
||||
} else if !(module.has_alias(name) || module.has_decl(name)) {
|
||||
return Err(ShellError::EnvVarNotFoundAtRuntime(
|
||||
String::from_utf8_lossy(name).into(),
|
||||
*span,
|
||||
));
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
ImportPatternMember::List { names } => {
|
||||
let mut output = vec![];
|
||||
|
||||
for (name, span) in names {
|
||||
if let Some((name, id)) =
|
||||
module.env_var_with_head(name, &import_pattern.head.name)
|
||||
{
|
||||
output.push((name, id));
|
||||
} else if !(module.has_alias(name) || module.has_decl(name)) {
|
||||
return Err(ShellError::EnvVarNotFoundAtRuntime(
|
||||
String::from_utf8_lossy(name).into(),
|
||||
*span,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (name, _) in env_vars_to_hide {
|
||||
let name = if let Ok(s) = String::from_utf8(name.clone()) {
|
||||
s
|
||||
} else {
|
||||
return Err(ShellError::NonUtf8(import_pattern.span()));
|
||||
};
|
||||
|
||||
if stack.remove_env_var(engine_state, &name).is_none() {
|
||||
return Err(ShellError::NotFound(
|
||||
call.positional_nth(0)
|
||||
.expect("already checked for present positional")
|
||||
.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
} else if !import_pattern.hidden.contains(&import_pattern.head.name)
|
||||
&& stack.remove_env_var(engine_state, &head_name_str).is_none()
|
||||
{
|
||||
// TODO: we may want to error in the future
|
||||
}
|
||||
stack.remove_env_var(engine_state, &env_var_name.item);
|
||||
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
|
|
|
@ -12,7 +12,6 @@ mod export;
|
|||
mod export_alias;
|
||||
mod export_def;
|
||||
mod export_def_env;
|
||||
mod export_env;
|
||||
mod export_extern;
|
||||
mod export_use;
|
||||
mod extern_;
|
||||
|
@ -43,7 +42,6 @@ pub use export::ExportCommand;
|
|||
pub use export_alias::ExportAlias;
|
||||
pub use export_def::ExportDef;
|
||||
pub use export_def_env::ExportDefEnv;
|
||||
pub use export_env::ExportEnvModule;
|
||||
pub use export_extern::ExportExtern;
|
||||
pub use export_use::ExportUse;
|
||||
pub use extern_::Extern;
|
||||
|
|
|
@ -55,8 +55,8 @@ impl Command for Module {
|
|||
}),
|
||||
},
|
||||
Example {
|
||||
description: "Define an environment variable in a module and evaluate it",
|
||||
example: r#"module foo { export env FOO_ENV { "BAZ" } }; use foo FOO_ENV; $env.FOO_ENV"#,
|
||||
description: "Define an environment variable in a module",
|
||||
example: r#"module foo { export-env { let-env FOO = "BAZ" } }; use foo; $env.FOO"#,
|
||||
result: Some(Value::String {
|
||||
val: "BAZ".to_string(),
|
||||
span: Span::test_data(),
|
||||
|
|
|
@ -20,13 +20,13 @@ impl Command for OverlayHide {
|
|||
.optional("name", SyntaxShape::String, "Overlay to hide")
|
||||
.switch(
|
||||
"keep-custom",
|
||||
"Keep all newly added symbols within the next activated overlay",
|
||||
"Keep all newly added commands and aliases in the next activated overlay",
|
||||
Some('k'),
|
||||
)
|
||||
.named(
|
||||
"keep-env",
|
||||
SyntaxShape::List(Box::new(SyntaxShape::String)),
|
||||
"List of environment variables to keep from the hidden overlay",
|
||||
"List of environment variables to keep in the next activated overlay",
|
||||
Some('e'),
|
||||
)
|
||||
.category(Category::Core)
|
||||
|
@ -67,23 +67,7 @@ impl Command for OverlayHide {
|
|||
let keep_env: Option<Vec<Spanned<String>>> =
|
||||
call.get_flag(engine_state, stack, "keep-env")?;
|
||||
|
||||
let env_vars_to_keep = if call.has_flag("keep-custom") {
|
||||
if let Some(overlay_id) = engine_state.find_overlay(overlay_name.item.as_bytes()) {
|
||||
let overlay_frame = engine_state.get_overlay(overlay_id);
|
||||
let origin_module = engine_state.get_module(overlay_frame.origin);
|
||||
|
||||
stack
|
||||
.get_overlay_env_vars(engine_state, &overlay_name.item)
|
||||
.into_iter()
|
||||
.filter(|(name, _)| !origin_module.has_env_var(name.as_bytes()))
|
||||
.collect()
|
||||
} else {
|
||||
return Err(ShellError::OverlayNotFoundAtRuntime(
|
||||
overlay_name.item,
|
||||
overlay_name.span,
|
||||
));
|
||||
}
|
||||
} else if let Some(env_var_names_to_keep) = keep_env {
|
||||
let env_vars_to_keep = if let Some(env_var_names_to_keep) = keep_env {
|
||||
let mut env_vars_to_keep = vec![];
|
||||
|
||||
for name in env_var_names_to_keep.into_iter() {
|
||||
|
@ -110,10 +94,13 @@ impl Command for OverlayHide {
|
|||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Hide an overlay created from a module",
|
||||
description: "Keep a custom command after hiding the overlay",
|
||||
example: r#"module spam { export def foo [] { "foo" } }
|
||||
overlay use spam
|
||||
overlay hide spam"#,
|
||||
def bar [] { "bar" }
|
||||
overlay hide spam --keep-custom
|
||||
bar
|
||||
"#,
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
|
@ -125,7 +112,7 @@ impl Command for OverlayHide {
|
|||
},
|
||||
Example {
|
||||
description: "Hide the last activated overlay",
|
||||
example: r#"module spam { export env FOO { "foo" } }
|
||||
example: r#"module spam { export-env { let-env FOO = "foo" } }
|
||||
overlay use spam
|
||||
overlay hide"#,
|
||||
result: None,
|
||||
|
|
|
@ -123,28 +123,6 @@ impl Command for OverlayUse {
|
|||
|
||||
let module = engine_state.get_module(module_id);
|
||||
|
||||
for (name, block_id) in module.env_vars() {
|
||||
let name = if let Ok(s) = String::from_utf8(name.clone()) {
|
||||
s
|
||||
} else {
|
||||
return Err(ShellError::NonUtf8(call.head));
|
||||
};
|
||||
|
||||
let block = engine_state.get_block(block_id);
|
||||
|
||||
let val = eval_block(
|
||||
engine_state,
|
||||
caller_stack,
|
||||
block,
|
||||
PipelineData::new(call.head),
|
||||
false,
|
||||
true,
|
||||
)?
|
||||
.into_value(call.head);
|
||||
|
||||
caller_stack.add_env_var(name, val);
|
||||
}
|
||||
|
||||
// Evaluate the export-env block (if any) and keep its environment
|
||||
if let Some(block_id) = module.env_block {
|
||||
let maybe_path = find_in_dirs_env(&name_arg.item, engine_state, caller_stack)?;
|
||||
|
@ -212,7 +190,7 @@ impl Command for OverlayUse {
|
|||
},
|
||||
Example {
|
||||
description: "Create an overlay from a file",
|
||||
example: r#"echo 'export env FOO { "foo" }' | save spam.nu
|
||||
example: r#"echo 'export-env { let-env FOO = "foo" }' | save spam.nu
|
||||
overlay use spam.nu
|
||||
$env.FOO"#,
|
||||
result: None,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use nu_engine::{eval_block, find_in_dirs_env, redirect_env};
|
||||
use nu_protocol::ast::{Call, Expr, Expression, ImportPatternMember};
|
||||
use nu_protocol::ast::{Call, Expr, Expression};
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
|
||||
|
@ -58,66 +58,6 @@ impl Command for Use {
|
|||
if let Some(module_id) = import_pattern.head.id {
|
||||
let module = engine_state.get_module(module_id);
|
||||
|
||||
let env_vars_to_use = if import_pattern.members.is_empty() {
|
||||
module.env_vars_with_head(&import_pattern.head.name)
|
||||
} else {
|
||||
match &import_pattern.members[0] {
|
||||
ImportPatternMember::Glob { .. } => module.env_vars(),
|
||||
ImportPatternMember::Name { name, span } => {
|
||||
let mut output = vec![];
|
||||
|
||||
if let Some(id) = module.get_env_var_id(name) {
|
||||
output.push((name.clone(), id));
|
||||
} else if !module.has_decl(name) && !module.has_alias(name) {
|
||||
return Err(ShellError::EnvVarNotFoundAtRuntime(
|
||||
String::from_utf8_lossy(name).into(),
|
||||
*span,
|
||||
));
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
ImportPatternMember::List { names } => {
|
||||
let mut output = vec![];
|
||||
|
||||
for (name, span) in names {
|
||||
if let Some(id) = module.get_env_var_id(name) {
|
||||
output.push((name.clone(), id));
|
||||
} else if !module.has_decl(name) && !module.has_alias(name) {
|
||||
return Err(ShellError::EnvVarNotFoundAtRuntime(
|
||||
String::from_utf8_lossy(name).into(),
|
||||
*span,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (name, block_id) in env_vars_to_use {
|
||||
let name = if let Ok(s) = String::from_utf8(name.clone()) {
|
||||
s
|
||||
} else {
|
||||
return Err(ShellError::NonUtf8(import_pattern.head.span));
|
||||
};
|
||||
|
||||
let block = engine_state.get_block(block_id);
|
||||
|
||||
let val = eval_block(
|
||||
engine_state,
|
||||
caller_stack,
|
||||
block,
|
||||
PipelineData::new(call.head),
|
||||
false,
|
||||
true,
|
||||
)?
|
||||
.into_value(call.head);
|
||||
|
||||
caller_stack.add_env_var(name, val);
|
||||
}
|
||||
|
||||
// Evaluate the export-env block if there is one
|
||||
if let Some(block_id) = module.env_block {
|
||||
let block = engine_state.get_block(block_id);
|
||||
|
@ -184,14 +124,6 @@ impl Command for Use {
|
|||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
Example {
|
||||
description: "Define an environment variable in a module and evaluate it",
|
||||
example: r#"module foo { export env FOO_ENV { "BAZ" } }; use foo FOO_ENV; $env.FOO_ENV"#,
|
||||
result: Some(Value::String {
|
||||
val: "BAZ".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
Example {
|
||||
description: "Define a custom command that participates in the environment in a module and call it",
|
||||
example: r#"module foo { export def-env bar [] { let-env FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#,
|
||||
|
|
|
@ -42,7 +42,6 @@ pub fn create_default_context() -> EngineState {
|
|||
ExportCommand,
|
||||
ExportDef,
|
||||
ExportDefEnv,
|
||||
ExportEnvModule,
|
||||
ExportExtern,
|
||||
ExportUse,
|
||||
Extern,
|
||||
|
|
|
@ -189,9 +189,9 @@ impl Command for ViewSource {
|
|||
},
|
||||
Example {
|
||||
description: "View the source of a module",
|
||||
example: r#"module mod-foo { export env FOO_ENV { 'BAZ' } }; view-source mod-foo"#,
|
||||
example: r#"module mod-foo { export-env { let-env FOO_ENV = 'BAZ' } }; view-source mod-foo"#,
|
||||
result: Some(Value::String {
|
||||
val: " export env FOO_ENV { 'BAZ' }".to_string(),
|
||||
val: " export-env { let-env FOO_ENV = 'BAZ' }".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
|
|
|
@ -230,7 +230,7 @@ fn parse_module_success_2() {
|
|||
r#"
|
||||
# foo.nu
|
||||
|
||||
export env MYNAME { "Arthur, King of the Britons" }
|
||||
export-env { let-env MYNAME = "Arthur, King of the Britons" }
|
||||
"#,
|
||||
)]);
|
||||
|
||||
|
|
|
@ -19,9 +19,9 @@ use crate::{
|
|||
lex, lite_parse,
|
||||
lite_parse::LiteCommand,
|
||||
parser::{
|
||||
check_call, check_name, garbage, garbage_pipeline, parse, parse_block_expression,
|
||||
parse_internal_call, parse_multispan_value, parse_signature, parse_string,
|
||||
parse_var_with_opt_type, trim_quotes, ParsedInternalCall,
|
||||
check_call, check_name, garbage, garbage_pipeline, parse, parse_internal_call,
|
||||
parse_multispan_value, parse_signature, parse_string, parse_var_with_opt_type, trim_quotes,
|
||||
ParsedInternalCall,
|
||||
},
|
||||
unescape_unquote_string, ParseError,
|
||||
};
|
||||
|
@ -1093,94 +1093,6 @@ pub fn parse_export_in_module(
|
|||
|
||||
exportables
|
||||
}
|
||||
b"env" => {
|
||||
if let Some(id) = working_set.find_decl(b"export env", &Type::Any) {
|
||||
call.decl_id = id;
|
||||
} else {
|
||||
return (
|
||||
garbage_pipeline(spans),
|
||||
vec![],
|
||||
Some(ParseError::InternalError(
|
||||
"missing 'export env' command".into(),
|
||||
export_span,
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
let sig = working_set.get_decl(call.decl_id);
|
||||
let call_signature = sig.signature().call_signature();
|
||||
|
||||
call.head = span(&spans[0..=1]);
|
||||
|
||||
let mut result = vec![];
|
||||
|
||||
if let Some(name_span) = spans.get(2) {
|
||||
let (name_expr, err) =
|
||||
parse_string(working_set, *name_span, expand_aliases_denylist);
|
||||
error = error.or(err);
|
||||
call.add_positional(name_expr);
|
||||
|
||||
let env_var_name = working_set.get_span_contents(*name_span).to_vec();
|
||||
|
||||
if let Some(block_span) = spans.get(3) {
|
||||
let (block_expr, err) = parse_block_expression(
|
||||
working_set,
|
||||
&SyntaxShape::Block(None),
|
||||
*block_span,
|
||||
expand_aliases_denylist,
|
||||
);
|
||||
error = error.or(err);
|
||||
|
||||
if let Expression {
|
||||
expr: Expr::Block(block_id),
|
||||
..
|
||||
} = block_expr
|
||||
{
|
||||
result.push(Exportable::EnvVar {
|
||||
name: env_var_name,
|
||||
id: block_id,
|
||||
});
|
||||
} else {
|
||||
error = error.or_else(|| {
|
||||
Some(ParseError::InternalError(
|
||||
"block was not parsed as a block".into(),
|
||||
*block_span,
|
||||
))
|
||||
});
|
||||
}
|
||||
|
||||
call.add_positional(block_expr);
|
||||
} else {
|
||||
let err_span = Span {
|
||||
start: name_span.end,
|
||||
end: name_span.end,
|
||||
};
|
||||
|
||||
error = error.or_else(|| {
|
||||
Some(ParseError::MissingPositional(
|
||||
"block".into(),
|
||||
err_span,
|
||||
call_signature,
|
||||
))
|
||||
});
|
||||
}
|
||||
} else {
|
||||
let err_span = Span {
|
||||
start: kw_span.end,
|
||||
end: kw_span.end,
|
||||
};
|
||||
|
||||
error = error.or_else(|| {
|
||||
Some(ParseError::MissingPositional(
|
||||
"environment variable name".into(),
|
||||
err_span,
|
||||
call_signature,
|
||||
))
|
||||
});
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
_ => {
|
||||
error = error.or_else(|| {
|
||||
Some(ParseError::Expected(
|
||||
|
@ -1345,7 +1257,6 @@ pub fn parse_module_block(
|
|||
error = error.or(err);
|
||||
|
||||
for pipeline in &output.block {
|
||||
// TODO: Should we add export env predecls as well?
|
||||
if pipeline.commands.len() == 1 {
|
||||
parse_def_predecl(
|
||||
working_set,
|
||||
|
@ -1414,9 +1325,6 @@ pub fn parse_module_block(
|
|||
Exportable::Alias { name, id } => {
|
||||
module.add_alias(name, id);
|
||||
}
|
||||
Exportable::EnvVar { name, id } => {
|
||||
module.add_env_var(name, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1820,7 +1728,7 @@ pub fn parse_use(
|
|||
decl_output.push((name.clone(), id));
|
||||
} else if let Some(id) = module.get_alias_id(name) {
|
||||
alias_output.push((name.clone(), id));
|
||||
} else if !module.has_env_var(name) {
|
||||
} else {
|
||||
error = error.or(Some(ParseError::ExportNotFound(*span)))
|
||||
}
|
||||
|
||||
|
@ -1835,7 +1743,7 @@ pub fn parse_use(
|
|||
decl_output.push((name.clone(), id));
|
||||
} else if let Some(id) = module.get_alias_id(name) {
|
||||
alias_output.push((name.clone(), id));
|
||||
} else if !module.has_env_var(name) {
|
||||
} else {
|
||||
error = error.or(Some(ParseError::ExportNotFound(*span)));
|
||||
break;
|
||||
}
|
||||
|
@ -2036,7 +1944,7 @@ pub fn parse_hide(
|
|||
module.decl_name_with_head(name, &import_pattern.head.name)
|
||||
{
|
||||
decls.push(item);
|
||||
} else if !module.has_env_var(name) {
|
||||
} else {
|
||||
error = error.or(Some(ParseError::ExportNotFound(*span)));
|
||||
}
|
||||
|
||||
|
@ -2055,7 +1963,7 @@ pub fn parse_hide(
|
|||
module.decl_name_with_head(name, &import_pattern.head.name)
|
||||
{
|
||||
decls.push(item);
|
||||
} else if !module.has_env_var(name) {
|
||||
} else {
|
||||
error = error.or(Some(ParseError::ExportNotFound(*span)));
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -178,39 +178,6 @@ impl Stack {
|
|||
result
|
||||
}
|
||||
|
||||
/// Flatten the env var scope frames into one frame, only from one overlay
|
||||
pub fn get_overlay_env_vars(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
overlay_name: &str,
|
||||
) -> HashMap<String, Value> {
|
||||
let mut result = HashMap::new();
|
||||
|
||||
// for active_overlay in self.active_overlays.iter() {
|
||||
if let Some(active_overlay) = self.active_overlays.iter().find(|n| n == &overlay_name) {
|
||||
if let Some(env_vars) = engine_state.env_vars.get(active_overlay) {
|
||||
result.extend(
|
||||
env_vars
|
||||
.iter()
|
||||
.filter(|(k, _)| {
|
||||
if let Some(env_hidden) = self.env_hidden.get(active_overlay) {
|
||||
!env_hidden.contains(*k)
|
||||
} else {
|
||||
// nothing has been hidden in this overlay
|
||||
true
|
||||
}
|
||||
})
|
||||
.map(|(k, v)| (k.clone(), v.clone()))
|
||||
.collect::<HashMap<String, Value>>(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
result.extend(self.get_stack_overlay_env_vars(overlay_name));
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Get flattened environment variables only from the stack
|
||||
pub fn get_stack_env_vars(&self) -> HashMap<String, Value> {
|
||||
let mut result = HashMap::new();
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use crate::{AliasId, BlockId, DeclId};
|
||||
use crate::{AliasId, DeclId};
|
||||
|
||||
pub enum Exportable {
|
||||
Decl { name: Vec<u8>, id: DeclId },
|
||||
Alias { name: Vec<u8>, id: AliasId },
|
||||
EnvVar { name: Vec<u8>, id: BlockId },
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ use indexmap::IndexMap;
|
|||
pub struct Module {
|
||||
pub decls: IndexMap<Vec<u8>, DeclId>,
|
||||
pub aliases: IndexMap<Vec<u8>, AliasId>,
|
||||
pub env_vars: IndexMap<Vec<u8>, BlockId>,
|
||||
pub env_block: Option<BlockId>,
|
||||
pub span: Option<Span>,
|
||||
}
|
||||
|
@ -20,7 +19,6 @@ impl Module {
|
|||
Module {
|
||||
decls: IndexMap::new(),
|
||||
aliases: IndexMap::new(),
|
||||
env_vars: IndexMap::new(),
|
||||
env_block: None,
|
||||
span: None,
|
||||
}
|
||||
|
@ -30,7 +28,6 @@ impl Module {
|
|||
Module {
|
||||
decls: IndexMap::new(),
|
||||
aliases: IndexMap::new(),
|
||||
env_vars: IndexMap::new(),
|
||||
env_block: None,
|
||||
span: Some(span),
|
||||
}
|
||||
|
@ -44,21 +41,17 @@ impl Module {
|
|||
self.aliases.insert(name, alias_id)
|
||||
}
|
||||
|
||||
pub fn add_env_var(&mut self, name: Vec<u8>, block_id: BlockId) -> Option<BlockId> {
|
||||
self.env_vars.insert(name, block_id)
|
||||
}
|
||||
|
||||
pub fn add_env_block(&mut self, block_id: BlockId) {
|
||||
self.env_block = Some(block_id);
|
||||
}
|
||||
|
||||
pub fn extend(&mut self, other: &Module) {
|
||||
self.decls.extend(other.decls.clone());
|
||||
self.env_vars.extend(other.env_vars.clone());
|
||||
self.aliases.extend(other.aliases.clone());
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.decls.is_empty() && self.env_vars.is_empty()
|
||||
self.decls.is_empty() && self.aliases.is_empty()
|
||||
}
|
||||
|
||||
pub fn get_decl_id(&self, name: &[u8]) -> Option<DeclId> {
|
||||
|
@ -168,44 +161,6 @@ impl Module {
|
|||
.map(|(name, id)| (name.clone(), *id))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn get_env_var_id(&self, name: &[u8]) -> Option<BlockId> {
|
||||
self.env_vars.get(name).copied()
|
||||
}
|
||||
|
||||
pub fn has_env_var(&self, name: &[u8]) -> bool {
|
||||
self.env_vars.contains_key(name)
|
||||
}
|
||||
|
||||
pub fn env_var_with_head(&self, name: &[u8], head: &[u8]) -> Option<(Vec<u8>, BlockId)> {
|
||||
if let Some(id) = self.get_env_var_id(name) {
|
||||
let mut new_name = head.to_vec();
|
||||
new_name.push(b' ');
|
||||
new_name.extend(name);
|
||||
Some((new_name, id))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn env_vars_with_head(&self, head: &[u8]) -> Vec<(Vec<u8>, BlockId)> {
|
||||
self.env_vars
|
||||
.iter()
|
||||
.map(|(name, id)| {
|
||||
let mut new_name = head.to_vec();
|
||||
new_name.push(b' ');
|
||||
new_name.extend(name);
|
||||
(new_name, *id)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn env_vars(&self) -> Vec<(Vec<u8>, BlockId)> {
|
||||
self.env_vars
|
||||
.iter()
|
||||
.map(|(name, id)| (name.clone(), *id))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Module {
|
||||
|
|
|
@ -114,7 +114,10 @@ fn hides_alias_in_scope_4() -> TestResult {
|
|||
|
||||
#[test]
|
||||
fn hides_env_in_scope_1() -> TestResult {
|
||||
fail_test(r#"let-env foo = "foo"; do { hide-env foo; $env.foo }"#, "")
|
||||
fail_test(
|
||||
r#"let-env foo = "foo"; do { hide-env foo; $env.foo }"#,
|
||||
"cannot find column",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -173,6 +176,7 @@ fn hide_env_twice_allowed() -> TestResult {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "Re-enable after virtualenv update"]
|
||||
fn hides_def_runs_env_1() -> TestResult {
|
||||
run_test(
|
||||
r#"let-env foo = "bar"; def foo [] { "foo" }; hide foo; $env.foo"#,
|
||||
|
@ -181,6 +185,7 @@ fn hides_def_runs_env_1() -> TestResult {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "Re-enable after virtualenv update"]
|
||||
fn hides_def_runs_env_2() -> TestResult {
|
||||
run_test(
|
||||
r#"def foo [] { "foo" }; let-env foo = "bar"; hide foo; $env.foo"#,
|
||||
|
@ -327,58 +332,16 @@ fn hides_alias_import_then_reimports() -> TestResult {
|
|||
#[test]
|
||||
fn hides_env_import_1() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export env foo { "foo" } }; use spam; hide-env 'spam foo'; $env.'spam foo'"#,
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn hides_env_import_2() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export env foo { "foo" } }; use spam; hide-env spam; $env.'spam foo'"#,
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn hides_env_import_3() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export env foo { "foo" } }; use spam; hide-env spam [foo]; $env.'spam foo'"#,
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hides_env_import_4() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export env foo { "foo" } }; use spam foo; hide-env foo; $env.foo"#,
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hides_env_import_5() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export env foo { "foo" } }; use spam *; hide-env foo; $env.foo"#,
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn hides_env_import_6() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export env foo { "foo" } }; use spam *; hide-env spam *; $env.foo"#,
|
||||
r#"module spam { export-env { let-env foo = "foo" } }; use spam; hide-env foo; $env.foo"#,
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "Re-enable after virtualenv update"]
|
||||
fn hides_def_runs_env_import() -> TestResult {
|
||||
run_test(
|
||||
r#"module spam { export env foo { "foo" }; export def foo [] { "bar" } }; use spam foo; hide foo; $env.foo"#,
|
||||
r#"module spam { export-env { let-env foo = "foo" }; export def foo [] { "bar" } }; use spam foo; hide foo; $env.foo"#,
|
||||
"foo",
|
||||
)
|
||||
}
|
||||
|
@ -386,19 +349,11 @@ fn hides_def_runs_env_import() -> TestResult {
|
|||
#[test]
|
||||
fn hides_def_and_env_import_1() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export env foo { "foo" }; export def foo [] { "bar" } }; use spam foo; hide foo; hide-env foo; $env.foo"#,
|
||||
r#"module spam { export-env { let-env foo = "foo" }; export def foo [] { "bar" } }; use spam foo; hide foo; hide-env foo; $env.foo"#,
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hides_def_and_env_import_2() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export env foo { "foo" }; export def foo [] { "bar" } }; use spam foo; hide foo; hide-env foo; foo"#,
|
||||
"", // we just care if it errors
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn use_def_import_after_hide() -> TestResult {
|
||||
run_test(
|
||||
|
@ -410,7 +365,7 @@ fn use_def_import_after_hide() -> TestResult {
|
|||
#[test]
|
||||
fn use_env_import_after_hide() -> TestResult {
|
||||
run_test(
|
||||
r#"module spam { export env foo { "foo" } }; use spam foo; hide-env foo; use spam foo; $env.foo"#,
|
||||
r#"module spam { export-env { let-env foo = "foo" } }; use spam; hide-env foo; use spam; $env.foo"#,
|
||||
"foo",
|
||||
)
|
||||
}
|
||||
|
@ -426,7 +381,7 @@ fn hide_shadowed_decl() -> TestResult {
|
|||
#[test]
|
||||
fn hide_shadowed_env() -> TestResult {
|
||||
run_test(
|
||||
r#"module spam { export env foo { "bar" } }; let-env foo = "foo"; do { use spam foo; hide-env foo; $env.foo }"#,
|
||||
r#"module spam { export-env { let-env foo = "bar" } }; let-env foo = "foo"; do { use spam; hide-env foo; $env.foo }"#,
|
||||
"foo",
|
||||
)
|
||||
}
|
||||
|
@ -442,7 +397,7 @@ fn hides_all_decls_within_scope() -> TestResult {
|
|||
#[test]
|
||||
fn hides_all_envs_within_scope() -> TestResult {
|
||||
fail_test(
|
||||
r#"module spam { export env foo { "bar" } }; let-env foo = "foo"; use spam foo; hide-env foo; $env.foo"#,
|
||||
r#"module spam { export-env { let-env foo = "bar" } }; let-env foo = "foo"; use spam; hide-env foo; $env.foo"#,
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ fn module_def_imports_5() -> TestResult {
|
|||
#[test]
|
||||
fn module_env_imports_1() -> TestResult {
|
||||
run_test(
|
||||
r#"module foo { export env a { '1' } }; use foo; $env.'foo a'"#,
|
||||
r#"module foo { export-env { let-env a = '1' } }; use foo; $env.a"#,
|
||||
"1",
|
||||
)
|
||||
}
|
||||
|
@ -51,31 +51,15 @@ fn module_env_imports_1() -> TestResult {
|
|||
#[test]
|
||||
fn module_env_imports_2() -> TestResult {
|
||||
run_test(
|
||||
r#"module foo { export env a { '1' } }; use foo a; $env.a"#,
|
||||
"1",
|
||||
r#"module foo { export-env { let-env a = '1'; let-env b = '2' } }; use foo; $env.b"#,
|
||||
"2",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_env_imports_3() -> TestResult {
|
||||
run_test(
|
||||
r#"module foo { export env a { '1' }; export env b { '2' } }; use foo *; $env.b"#,
|
||||
"2",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_env_imports_4() -> TestResult {
|
||||
fail_test(
|
||||
r#"module foo { export env a { '1' }; export env b { '2' } }; use foo c"#,
|
||||
"not find import",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_env_imports_5() -> TestResult {
|
||||
run_test(
|
||||
r#"module foo { export env a { '1' }; export env b { '2' }; export env c { '3' } }; use foo [a, c]; $env.c"#,
|
||||
r#"module foo { export-env { let-env a = '1' }; export-env { let-env b = '2' }; export-env {let-env c = '3'} }; use foo; $env.c"#,
|
||||
"3",
|
||||
)
|
||||
}
|
||||
|
@ -83,7 +67,7 @@ fn module_env_imports_5() -> TestResult {
|
|||
#[test]
|
||||
fn module_def_and_env_imports_1() -> TestResult {
|
||||
run_test(
|
||||
r#"module spam { export env foo { "foo" }; export def foo [] { "bar" } }; use spam foo; $env.foo"#,
|
||||
r#"module spam { export-env { let-env foo = "foo" }; export def foo [] { "bar" } }; use spam; $env.foo"#,
|
||||
"foo",
|
||||
)
|
||||
}
|
||||
|
@ -91,7 +75,7 @@ fn module_def_and_env_imports_1() -> TestResult {
|
|||
#[test]
|
||||
fn module_def_and_env_imports_2() -> TestResult {
|
||||
run_test(
|
||||
r#"module spam { export env foo { "foo" }; export def foo [] { "bar" } }; use spam foo; foo"#,
|
||||
r#"module spam { export-env { let-env foo = "foo" }; export def foo [] { "bar" } }; use spam foo; foo"#,
|
||||
"bar",
|
||||
)
|
||||
}
|
||||
|
@ -107,7 +91,7 @@ fn module_def_import_uses_internal_command() -> TestResult {
|
|||
#[test]
|
||||
fn module_env_import_uses_internal_command() -> TestResult {
|
||||
run_test(
|
||||
r#"module foo { def b [] { "2" }; export env a { b } }; use foo; $env.'foo a'"#,
|
||||
r#"module foo { def b [] { "2" }; export-env { let-env a = b } }; use foo; $env.a"#,
|
||||
"2",
|
||||
)
|
||||
}
|
||||
|
|
|
@ -131,7 +131,7 @@ fn prefixed_overlay_keeps_custom_decl() {
|
|||
#[test]
|
||||
fn add_overlay_env() {
|
||||
let inp = &[
|
||||
r#"module spam { export env FOO { "foo" } }"#,
|
||||
r#"module spam { export-env { let-env FOO = "foo" } }"#,
|
||||
r#"overlay use spam"#,
|
||||
r#"$env.FOO"#,
|
||||
];
|
||||
|
@ -146,7 +146,7 @@ fn add_overlay_env() {
|
|||
#[test]
|
||||
fn add_prefixed_overlay_env_no_prefix() {
|
||||
let inp = &[
|
||||
r#"module spam { export env FOO { "foo" } }"#,
|
||||
r#"module spam { export-env { let-env FOO = "foo" } }"#,
|
||||
r#"overlay use --prefix spam"#,
|
||||
r#"$env.FOO"#,
|
||||
];
|
||||
|
@ -239,9 +239,9 @@ fn update_overlay_from_module() {
|
|||
#[test]
|
||||
fn update_overlay_from_module_env() {
|
||||
let inp = &[
|
||||
r#"module spam { export env FOO { "foo" } }"#,
|
||||
r#"module spam { export-env { let-env FOO = "foo" } }"#,
|
||||
r#"overlay use spam"#,
|
||||
r#"module spam { export env FOO { "bar" } }"#,
|
||||
r#"module spam { export-env { let-env FOO = "bar" } }"#,
|
||||
r#"overlay use spam"#,
|
||||
r#"$env.FOO"#,
|
||||
];
|
||||
|
@ -256,7 +256,7 @@ fn update_overlay_from_module_env() {
|
|||
#[test]
|
||||
fn overlay_use_do_not_eval_twice() {
|
||||
let inp = &[
|
||||
r#"module spam { export env FOO { "foo" } }"#,
|
||||
r#"module spam { export-env { let-env FOO = "foo" } }"#,
|
||||
r#"overlay use spam"#,
|
||||
r#"let-env FOO = "bar""#,
|
||||
r#"overlay hide spam"#,
|
||||
|
@ -328,7 +328,7 @@ fn remove_overlay_scoped() {
|
|||
#[test]
|
||||
fn remove_overlay_env() {
|
||||
let inp = &[
|
||||
r#"module spam { export env FOO { "foo" } }"#,
|
||||
r#"module spam { export-env { let-env FOO = "foo" } }"#,
|
||||
r#"overlay use spam"#,
|
||||
r#"overlay hide spam"#,
|
||||
r#"$env.FOO"#,
|
||||
|
@ -344,7 +344,7 @@ fn remove_overlay_env() {
|
|||
#[test]
|
||||
fn remove_overlay_scoped_env() {
|
||||
let inp = &[
|
||||
r#"module spam { export env FOO { "foo" } }"#,
|
||||
r#"module spam { export-env { let-env FOO = "foo" } }"#,
|
||||
r#"overlay use spam"#,
|
||||
r#"do { overlay hide spam }"#,
|
||||
r#"$env.FOO"#,
|
||||
|
@ -485,7 +485,7 @@ fn remove_overlay_keep_alias() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn remove_overlay_keep_env() {
|
||||
fn remove_overlay_dont_keep_env() {
|
||||
let inp = &[
|
||||
r#"overlay use samples/spam.nu"#,
|
||||
r#"let-env BAGR = `bagr`"#,
|
||||
|
@ -496,12 +496,12 @@ fn remove_overlay_keep_env() {
|
|||
let actual = nu!(cwd: "tests/overlays", pipeline(&inp.join("; ")));
|
||||
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
||||
|
||||
assert!(actual.out.contains("bagr"));
|
||||
assert!(actual_repl.out.contains("bagr"));
|
||||
assert!(actual.err.contains("cannot find column"));
|
||||
assert!(actual_repl.err.contains("cannot find column"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_overlay_keep_discard_overwritten_decl() {
|
||||
fn remove_overlay_dont_keep_overwritten_decl() {
|
||||
let inp = &[
|
||||
r#"overlay use samples/spam.nu"#,
|
||||
r#"def foo [] { 'bar' }"#,
|
||||
|
@ -520,7 +520,7 @@ fn remove_overlay_keep_discard_overwritten_decl() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn remove_overlay_keep_discard_overwritten_alias() {
|
||||
fn remove_overlay_dont_keep_overwritten_alias() {
|
||||
let inp = &[
|
||||
r#"overlay use samples/spam.nu"#,
|
||||
r#"alias bar = `baz`"#,
|
||||
|
@ -539,7 +539,7 @@ fn remove_overlay_keep_discard_overwritten_alias() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn remove_overlay_keep_discard_overwritten_env() {
|
||||
fn remove_overlay_dont_keep_overwritten_env() {
|
||||
let inp = &[
|
||||
r#"overlay use samples/spam.nu"#,
|
||||
r#"let-env BAZ = `bagr`"#,
|
||||
|
@ -591,7 +591,7 @@ fn remove_overlay_keep_alias_in_latest_overlay() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn remove_overlay_keep_env_in_latest_overlay() {
|
||||
fn remove_overlay_dont_keep_env_in_latest_overlay() {
|
||||
let inp = &[
|
||||
r#"overlay use samples/spam.nu"#,
|
||||
r#"let-env BAGR = `bagr`"#,
|
||||
|
@ -604,8 +604,8 @@ fn remove_overlay_keep_env_in_latest_overlay() {
|
|||
let actual = nu!(cwd: "tests/overlays", pipeline(&inp.join("; ")));
|
||||
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
|
||||
|
||||
assert!(actual.out.contains("bagr"));
|
||||
assert!(actual_repl.out.contains("bagr"));
|
||||
assert!(actual.err.contains("cannot find column"));
|
||||
assert!(actual_repl.err.contains("cannot find column"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -2,4 +2,4 @@ export def foo [] { "foo" }
|
|||
|
||||
export alias bar = "bar"
|
||||
|
||||
export env BAZ { "baz" }
|
||||
export-env { let-env BAZ = "baz" } }
|
||||
|
|
Loading…
Reference in a new issue