mirror of
https://github.com/nushell/nushell.git
synced 2025-08-18 14:52:12 +02:00
Allow modules to use
other modules (#6162)
* Allow private imports inside modules Can call `use ...` inside modules now. * Add more tests * Add a leak test * Refactor exportables; Prepare for 'export use' * Fix description * Implement 'export use' command This allows re-exporting module's commands and aliases from another module. * Add more tests; Fix import pattern list strings The import pattern strings didn't trim the surrounding quotes. * Add ignored test
This commit is contained in:
347
tests/modules/mod.rs
Normal file
347
tests/modules/mod.rs
Normal file
@@ -0,0 +1,347 @@
|
||||
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
||||
use nu_test_support::playground::Playground;
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn module_private_import_decl() {
|
||||
Playground::setup("module_private_import_decl", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"main.nu",
|
||||
r#"
|
||||
use spam.nu foo-helper
|
||||
|
||||
export def foo [] { foo-helper }
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam.nu",
|
||||
r#"
|
||||
def get-foo [] { "foo" }
|
||||
export def foo-helper [] { get-foo }
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let inp = &[r#"use main.nu foo"#, r#"foo"#];
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
||||
|
||||
assert_eq!(actual.out, "foo");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_private_import_alias() {
|
||||
Playground::setup("module_private_import_alias", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"main.nu",
|
||||
r#"
|
||||
use spam.nu foo-helper
|
||||
|
||||
export def foo [] { foo-helper }
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam.nu",
|
||||
r#"
|
||||
export alias foo-helper = "foo"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let inp = &[r#"use main.nu foo"#, r#"foo"#];
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
||||
|
||||
assert_eq!(actual.out, "foo");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_private_import_decl_not_public() {
|
||||
Playground::setup("module_private_import_decl_not_public", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"main.nu",
|
||||
r#"
|
||||
use spam.nu foo-helper
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam.nu",
|
||||
r#"
|
||||
def get-foo [] { "foo" }
|
||||
export def foo-helper [] { get-foo }
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let inp = &[r#"use main.nu foo"#, r#"foo-helper"#];
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
||||
|
||||
assert!(!actual.err.is_empty());
|
||||
})
|
||||
}
|
||||
|
||||
// TODO -- doesn't work because modules are never evaluated
|
||||
#[ignore]
|
||||
#[test]
|
||||
fn module_private_import_env() {
|
||||
Playground::setup("module_private_import_env", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"main.nu",
|
||||
r#"
|
||||
use spam.nu FOO_HELPER
|
||||
|
||||
export def foo [] { $env.FOO_HELPER }
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam.nu",
|
||||
r#"
|
||||
export env FOO_HELPER { "foo" }
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let inp = &[r#"use main.nu foo"#, r#"foo"#];
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
||||
|
||||
assert_eq!(actual.out, "foo");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_public_import_decl() {
|
||||
Playground::setup("module_public_import_decl", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"main.nu",
|
||||
r#"
|
||||
export use spam.nu foo
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam.nu",
|
||||
r#"
|
||||
def foo-helper [] { "foo" }
|
||||
export def foo [] { foo-helper }
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let inp = &[r#"use main.nu foo"#, r#"foo"#];
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
||||
|
||||
assert_eq!(actual.out, "foo");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_public_import_alias() {
|
||||
Playground::setup("module_public_import_alias", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"main.nu",
|
||||
r#"
|
||||
export use spam.nu foo
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam.nu",
|
||||
r#"
|
||||
export alias foo = "foo"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let inp = &[r#"use main.nu foo"#, r#"foo"#];
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
||||
|
||||
assert_eq!(actual.out, "foo");
|
||||
})
|
||||
}
|
||||
|
||||
// TODO -- doesn't work because modules are never evaluated
|
||||
#[ignore]
|
||||
#[test]
|
||||
fn module_public_import_env() {
|
||||
Playground::setup("module_public_import_decl", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"main.nu",
|
||||
r#"
|
||||
export use spam.nu FOO
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam.nu",
|
||||
r#"
|
||||
export env FOO { "foo" }
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let inp = &[r#"use main.nu FOO"#, r#"$env.FOO"#];
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
||||
|
||||
assert_eq!(actual.out, "foo");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_nested_imports() {
|
||||
Playground::setup("module_nested_imports", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"main.nu",
|
||||
r#"
|
||||
export use spam.nu [ foo bar ]
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam.nu",
|
||||
r#"
|
||||
export use spam2.nu [ foo bar ]
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam2.nu",
|
||||
r#"
|
||||
export use spam3.nu [ foo bar ]
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam3.nu",
|
||||
r#"
|
||||
export def foo [] { "foo" }
|
||||
export alias bar = "bar"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let inp1 = &[r#"use main.nu foo"#, r#"foo"#];
|
||||
let inp2 = &[r#"use main.nu bar"#, r#"bar"#];
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp1.join("; ")));
|
||||
assert_eq!(actual.out, "foo");
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp2.join("; ")));
|
||||
assert_eq!(actual.out, "bar");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_nested_imports_in_dirs() {
|
||||
Playground::setup("module_nested_imports_in_dirs", |dirs, sandbox| {
|
||||
sandbox
|
||||
.mkdir("spam")
|
||||
.mkdir("spam/spam2")
|
||||
.mkdir("spam/spam3")
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"main.nu",
|
||||
r#"
|
||||
export use spam/spam.nu [ foo bar ]
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam/spam.nu",
|
||||
r#"
|
||||
export use spam2/spam2.nu [ foo bar ]
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam/spam2/spam2.nu",
|
||||
r#"
|
||||
export use ../spam3/spam3.nu [ foo bar ]
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam/spam3/spam3.nu",
|
||||
r#"
|
||||
export def foo [] { "foo" }
|
||||
export alias bar = "bar"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let inp1 = &[r#"use main.nu foo"#, r#"foo"#];
|
||||
let inp2 = &[r#"use main.nu bar"#, r#"bar"#];
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp1.join("; ")));
|
||||
assert_eq!(actual.out, "foo");
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp2.join("; ")));
|
||||
assert_eq!(actual.out, "bar");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_public_import_decl_prefixed() {
|
||||
Playground::setup("module_public_import_decl", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"main.nu",
|
||||
r#"
|
||||
export use spam.nu
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam.nu",
|
||||
r#"
|
||||
def foo-helper [] { "foo" }
|
||||
export def foo [] { foo-helper }
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let inp = &[r#"use main.nu"#, r#"main spam foo"#];
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
||||
|
||||
assert_eq!(actual.out, "foo");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_nested_imports_in_dirs_prefixed() {
|
||||
Playground::setup("module_nested_imports_in_dirs", |dirs, sandbox| {
|
||||
sandbox
|
||||
.mkdir("spam")
|
||||
.mkdir("spam/spam2")
|
||||
.mkdir("spam/spam3")
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"main.nu",
|
||||
r#"
|
||||
export use spam/spam.nu [ "spam2 foo" "spam2 spam3 bar" ]
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam/spam.nu",
|
||||
r#"
|
||||
export use spam2/spam2.nu
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam/spam2/spam2.nu",
|
||||
r#"
|
||||
export use ../spam3/spam3.nu
|
||||
export use ../spam3/spam3.nu foo
|
||||
"#,
|
||||
)])
|
||||
.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"spam/spam3/spam3.nu",
|
||||
r#"
|
||||
export def foo [] { "foo" }
|
||||
export alias bar = "bar"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let inp1 = &[r#"use main.nu"#, r#"main spam2 foo"#];
|
||||
let inp2 = &[r#"use main.nu "spam2 spam3 bar""#, r#"spam2 spam3 bar"#];
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp1.join("; ")));
|
||||
assert_eq!(actual.out, "foo");
|
||||
|
||||
let actual = nu!(cwd: dirs.test(), pipeline(&inp2.join("; ")));
|
||||
assert_eq!(actual.out, "bar");
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user