Use overlay ID for module import lookup (#4514)

* Add id to import pattern

* Finish testing importing in a block
This commit is contained in:
Jakub Žádník
2022-02-18 03:58:24 +02:00
committed by GitHub
parent c7c427723b
commit bccce0ab46
6 changed files with 53 additions and 12 deletions

View File

@ -42,7 +42,7 @@ impl Command for Use {
));
};
if let Some(overlay_id) = engine_state.find_overlay(&import_pattern.head.name) {
if let Some(overlay_id) = import_pattern.head.id {
let overlay = engine_state.get_overlay(overlay_id);
let env_vars_to_use = if import_pattern.members.is_empty() {
@ -102,14 +102,13 @@ impl Command for Use {
} else {
// TODO: This is a workaround since call.positional[0].span points at 0 for some reason
// when this error is triggered
let bytes = engine_state.get_span_contents(&call.positional[0].span);
return Err(ShellError::SpannedLabeledError(
format!(
"Could not use '{}' import pattern",
String::from_utf8_lossy(bytes)
"Could not import from '{}'",
String::from_utf8_lossy(&import_pattern.head.name)
),
"called here".to_string(),
call.head,
"module does not exist".to_string(),
import_pattern.head.span,
));
}

View File

@ -60,6 +60,7 @@ mod str_;
mod touch;
mod uniq;
mod update;
mod use_;
mod where_;
#[cfg(feature = "which")]
mod which;

View File

@ -0,0 +1,34 @@
use nu_test_support::fs::{AbsolutePath, DisplayPath, Stub::FileWithContent};
use nu_test_support::nu;
use nu_test_support::pipeline;
use nu_test_support::playground::Playground;
#[test]
fn use_module_file_within_block() {
Playground::setup("use_test_1", |dirs, nu| {
let file = AbsolutePath::new(dirs.test().join("spam.nu"));
nu.with_files(vec![FileWithContent(
&file.display_path(),
r#"
export def foo [] {
echo "hello world"
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
def bar [] {
use spam.nu foo;
foo
};
bar
"#
)
);
assert_eq!(actual.out, "hello world");
})
}