nushell/tests/modules/mod.rs

348 lines
10 KiB
Rust
Raw Normal View History

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");
})
}