nushell/crates/nu-command/tests/commands/source_env.rs

296 lines
8.5 KiB
Rust
Raw Normal View History

use nu_test_support::fs::AbsolutePath;
use nu_test_support::fs::Stub::{FileWithContent, FileWithContentToBeTrimmed};
use nu_test_support::nu;
use nu_test_support::pipeline;
use nu_test_support::playground::Playground;
Allow custom lib dir path for sourcing nu script libraries. (#3940) Given we can write nu scripts. As the codebase grows, splitting into many smaller nu scripts is necessary. In general, when we work with paths and files we seem to face quite a few difficulties. Here we just tackle one of them and it involves sourcing files that also source other nu files and so forth. The current working directory becomes important here and being on a different directory when sourcing scripts will not work. Mostly because we expand the path on the current working directory and parse the files when a source command call is done. For the moment, we introduce a `lib_dirs` configuration value and, unfortunately, introduce a new dependency in `nu-parser` (`nu-data`) to get a handle of the configuration file to retrieve it. This should give clues and ideas as the new parser engine continues (introduce a way to also know paths) With this PR we can do the following: Let's assume we want to write a nu library called `my_library`. We will have the code in a directory called `project`: The file structure will looks like this: ``` project/my_library.nu project/my_library/hello.nu project/my_library/name.nu ``` This "pattern" works well, that is, when creating a library have a directory named `my_library` and next to it a `my_library.nu` file. Filling them like this: ``` source my_library/hello.nu source my_library/name.nu ``` ``` def hello [] { "hello world" } ``` ``` def name [] { "Nu" end ``` Assuming this `project` directory is stored at `/path/to/lib/project`, we can do: ``` config set lib_dirs ['path/to/lib/project'] ``` Given we have this `lib_dirs` configuration value, we can be anywhere while using Nu and do the following: ``` source my_library.nu echo (hello) (name) ```
2021-08-26 09:04:04 +02:00
#[should_panic]
#[test]
fn sources_also_files_under_custom_lib_dirs_path() {
Playground::setup("source_test_1", |dirs, nu| {
let file = AbsolutePath::new(dirs.test().join("config.toml"));
let library_path = AbsolutePath::new(dirs.test().join("lib"));
nu.with_config(&file);
nu.with_files(vec![FileWithContent(
"config.toml",
&format!(
r#"
lib_dirs = ["{}"]
skip_welcome_message = true
"#,
Add decimals to int when using `into string --decimals` (#6085) * Add decimals to int when using `into string --decimals` * Add tests for `into string` when converting int with `--decimals` * Apply formatting * Merge `into_str` test files * Comment out unused code and add TODOs * Use decimal separator depending on system locale * Add test helper to run closure in different locale * Add tests for int-to-string conversion using different locales * Add utils function to get system locale * Add panic message when locking mutex fails * Catch and resume panic later to prevent Mutex poisoning when test fails * Move test to `nu-test-support` to keep `nu-utils` free of `nu-*` dependencies See https://github.com/nushell/nushell/pull/6085#issuecomment-1193131694 * Rename test support fn `with_fake_locale` to `with_locale_override` * Move `get_system_locale()` to `locale` module * Allow overriding locale with special env variable (when not in release) * Use special env var to override locale during testing * Allow callback to return a value in `with_locale_override()` * Allow multiple options in `nu!` macro * Allow to set locale as `nu!` macro option * Use new `locale` option of `nu!` macro instead of `with_locale_override` Using the `locale` options does not lock the `LOCALE_OVERRIDE_MUTEX` mutex in `nu-test-support::locale_override` but instead calls the `nu` command directly with the `NU_LOCALE_OVERRIDE` environment variable. This allows for parallel test excecution. * Fix: Add option identifier for `cwd` in usage of `nu!` macro * Rely on `Display` trait for formatting `nu!` macro command - Removed the `DisplayPath` trait - Implement `Display` for `AbsolutePath`, `RelativePath` and `AbsoluteFile` * Default to locale `en_US.UTF-8` for tests when using `nu!` macro * Add doc comment to `nu!` macro * Format code using `cargo fmt --all` * Pass function directly instead of wrapping the call in a closure https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure * Pass function to `or_else()` instead of calling it inside `or()` https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call * Fix: Add option identifier for `cwd` in usage of `nu!` macro
2022-08-13 04:13:50 +02:00
library_path
Allow custom lib dir path for sourcing nu script libraries. (#3940) Given we can write nu scripts. As the codebase grows, splitting into many smaller nu scripts is necessary. In general, when we work with paths and files we seem to face quite a few difficulties. Here we just tackle one of them and it involves sourcing files that also source other nu files and so forth. The current working directory becomes important here and being on a different directory when sourcing scripts will not work. Mostly because we expand the path on the current working directory and parse the files when a source command call is done. For the moment, we introduce a `lib_dirs` configuration value and, unfortunately, introduce a new dependency in `nu-parser` (`nu-data`) to get a handle of the configuration file to retrieve it. This should give clues and ideas as the new parser engine continues (introduce a way to also know paths) With this PR we can do the following: Let's assume we want to write a nu library called `my_library`. We will have the code in a directory called `project`: The file structure will looks like this: ``` project/my_library.nu project/my_library/hello.nu project/my_library/name.nu ``` This "pattern" works well, that is, when creating a library have a directory named `my_library` and next to it a `my_library.nu` file. Filling them like this: ``` source my_library/hello.nu source my_library/name.nu ``` ``` def hello [] { "hello world" } ``` ``` def name [] { "Nu" end ``` Assuming this `project` directory is stored at `/path/to/lib/project`, we can do: ``` config set lib_dirs ['path/to/lib/project'] ``` Given we have this `lib_dirs` configuration value, we can be anywhere while using Nu and do the following: ``` source my_library.nu echo (hello) (name) ```
2021-08-26 09:04:04 +02:00
),
)]);
nu.within("lib").with_files(vec![FileWithContent(
"my_library.nu",
r#"
source-env my_library/main.nu
Allow custom lib dir path for sourcing nu script libraries. (#3940) Given we can write nu scripts. As the codebase grows, splitting into many smaller nu scripts is necessary. In general, when we work with paths and files we seem to face quite a few difficulties. Here we just tackle one of them and it involves sourcing files that also source other nu files and so forth. The current working directory becomes important here and being on a different directory when sourcing scripts will not work. Mostly because we expand the path on the current working directory and parse the files when a source command call is done. For the moment, we introduce a `lib_dirs` configuration value and, unfortunately, introduce a new dependency in `nu-parser` (`nu-data`) to get a handle of the configuration file to retrieve it. This should give clues and ideas as the new parser engine continues (introduce a way to also know paths) With this PR we can do the following: Let's assume we want to write a nu library called `my_library`. We will have the code in a directory called `project`: The file structure will looks like this: ``` project/my_library.nu project/my_library/hello.nu project/my_library/name.nu ``` This "pattern" works well, that is, when creating a library have a directory named `my_library` and next to it a `my_library.nu` file. Filling them like this: ``` source my_library/hello.nu source my_library/name.nu ``` ``` def hello [] { "hello world" } ``` ``` def name [] { "Nu" end ``` Assuming this `project` directory is stored at `/path/to/lib/project`, we can do: ``` config set lib_dirs ['path/to/lib/project'] ``` Given we have this `lib_dirs` configuration value, we can be anywhere while using Nu and do the following: ``` source my_library.nu echo (hello) (name) ```
2021-08-26 09:04:04 +02:00
"#,
)]);
nu.within("lib/my_library").with_files(vec![FileWithContent(
"main.nu",
r#"
let-env hello = "hello nu"
Allow custom lib dir path for sourcing nu script libraries. (#3940) Given we can write nu scripts. As the codebase grows, splitting into many smaller nu scripts is necessary. In general, when we work with paths and files we seem to face quite a few difficulties. Here we just tackle one of them and it involves sourcing files that also source other nu files and so forth. The current working directory becomes important here and being on a different directory when sourcing scripts will not work. Mostly because we expand the path on the current working directory and parse the files when a source command call is done. For the moment, we introduce a `lib_dirs` configuration value and, unfortunately, introduce a new dependency in `nu-parser` (`nu-data`) to get a handle of the configuration file to retrieve it. This should give clues and ideas as the new parser engine continues (introduce a way to also know paths) With this PR we can do the following: Let's assume we want to write a nu library called `my_library`. We will have the code in a directory called `project`: The file structure will looks like this: ``` project/my_library.nu project/my_library/hello.nu project/my_library/name.nu ``` This "pattern" works well, that is, when creating a library have a directory named `my_library` and next to it a `my_library.nu` file. Filling them like this: ``` source my_library/hello.nu source my_library/name.nu ``` ``` def hello [] { "hello world" } ``` ``` def name [] { "Nu" end ``` Assuming this `project` directory is stored at `/path/to/lib/project`, we can do: ``` config set lib_dirs ['path/to/lib/project'] ``` Given we have this `lib_dirs` configuration value, we can be anywhere while using Nu and do the following: ``` source my_library.nu echo (hello) (name) ```
2021-08-26 09:04:04 +02:00
"#,
)]);
let actual = nu!(
cwd: ".", pipeline(
r#"
source-env my_library.nu ;
hello
"#
));
assert_eq!(actual.out, "hello nu");
Allow custom lib dir path for sourcing nu script libraries. (#3940) Given we can write nu scripts. As the codebase grows, splitting into many smaller nu scripts is necessary. In general, when we work with paths and files we seem to face quite a few difficulties. Here we just tackle one of them and it involves sourcing files that also source other nu files and so forth. The current working directory becomes important here and being on a different directory when sourcing scripts will not work. Mostly because we expand the path on the current working directory and parse the files when a source command call is done. For the moment, we introduce a `lib_dirs` configuration value and, unfortunately, introduce a new dependency in `nu-parser` (`nu-data`) to get a handle of the configuration file to retrieve it. This should give clues and ideas as the new parser engine continues (introduce a way to also know paths) With this PR we can do the following: Let's assume we want to write a nu library called `my_library`. We will have the code in a directory called `project`: The file structure will looks like this: ``` project/my_library.nu project/my_library/hello.nu project/my_library/name.nu ``` This "pattern" works well, that is, when creating a library have a directory named `my_library` and next to it a `my_library.nu` file. Filling them like this: ``` source my_library/hello.nu source my_library/name.nu ``` ``` def hello [] { "hello world" } ``` ``` def name [] { "Nu" end ``` Assuming this `project` directory is stored at `/path/to/lib/project`, we can do: ``` config set lib_dirs ['path/to/lib/project'] ``` Given we have this `lib_dirs` configuration value, we can be anywhere while using Nu and do the following: ``` source my_library.nu echo (hello) (name) ```
2021-08-26 09:04:04 +02:00
})
}
fn try_source_foo_with_double_quotes_in(testdir: &str, playdir: &str) {
Playground::setup(playdir, |dirs, sandbox| {
let testdir = String::from(testdir);
let mut foo_file = testdir.clone();
foo_file.push_str("/foo.nu");
sandbox.mkdir(&testdir);
sandbox.with_files(vec![FileWithContent(&foo_file, "echo foo")]);
let cmd = String::from("source-env ") + r#"""# + foo_file.as_str() + r#"""#;
let actual = nu!(cwd: dirs.test(), &cmd);
assert_eq!(actual.out, "foo");
});
}
fn try_source_foo_with_single_quotes_in(testdir: &str, playdir: &str) {
Playground::setup(playdir, |dirs, sandbox| {
let testdir = String::from(testdir);
let mut foo_file = testdir.clone();
foo_file.push_str("/foo.nu");
sandbox.mkdir(&testdir);
sandbox.with_files(vec![FileWithContent(&foo_file, "echo foo")]);
let cmd = String::from("source-env ") + r#"'"# + foo_file.as_str() + r#"'"#;
let actual = nu!(cwd: dirs.test(), &cmd);
assert_eq!(actual.out, "foo");
});
}
fn try_source_foo_without_quotes_in(testdir: &str, playdir: &str) {
Playground::setup(playdir, |dirs, sandbox| {
let testdir = String::from(testdir);
let mut foo_file = testdir.clone();
foo_file.push_str("/foo.nu");
sandbox.mkdir(&testdir);
sandbox.with_files(vec![FileWithContent(&foo_file, "echo foo")]);
let cmd = String::from("source-env ") + foo_file.as_str();
let actual = nu!(cwd: dirs.test(), &cmd);
assert_eq!(actual.out, "foo");
});
}
#[test]
fn sources_unicode_file_in_normal_dir() {
try_source_foo_with_single_quotes_in("foo", "source_test_1");
try_source_foo_with_double_quotes_in("foo", "source_test_2");
try_source_foo_without_quotes_in("foo", "source_test_3");
}
#[test]
fn sources_unicode_file_in_unicode_dir_without_spaces_1() {
try_source_foo_with_single_quotes_in("🚒", "source_test_4");
try_source_foo_with_double_quotes_in("🚒", "source_test_5");
try_source_foo_without_quotes_in("🚒", "source_test_6");
}
#[cfg(not(windows))] // ':' is not allowed in Windows paths
#[test]
fn sources_unicode_file_in_unicode_dir_without_spaces_2() {
try_source_foo_with_single_quotes_in(":fire_engine:", "source_test_7");
try_source_foo_with_double_quotes_in(":fire_engine:", "source_test_8");
try_source_foo_without_quotes_in(":fire_engine:", "source_test_9");
}
#[test]
fn sources_unicode_file_in_unicode_dir_with_spaces_1() {
2022-03-18 18:35:28 +01:00
// this one fails
try_source_foo_with_single_quotes_in("e-$ èрт🚒♞中片-j", "source_test_8");
2022-03-18 18:35:28 +01:00
// this one passes
try_source_foo_with_double_quotes_in("e-$ èрт🚒♞中片-j", "source_test_9");
}
#[cfg(not(windows))] // ':' is not allowed in Windows paths
#[test]
fn sources_unicode_file_in_unicode_dir_with_spaces_2() {
try_source_foo_with_single_quotes_in("e-$ èрт:fire_engine:♞中片-j", "source_test_10");
try_source_foo_with_double_quotes_in("e-$ èрт:fire_engine:♞中片-j", "source_test_11");
}
#[ignore]
#[test]
fn sources_unicode_file_in_non_utf8_dir() {
// How do I create non-UTF-8 path???
}
#[ignore]
#[test]
fn can_source_dynamic_path() {
Playground::setup("can_source_dynamic_path", |dirs, sandbox| {
let foo_file = "foo.nu";
sandbox.with_files(vec![FileWithContent(foo_file, "echo foo")]);
let cmd = format!("let file = `{}`; source-env $file", foo_file);
let actual = nu!(cwd: dirs.test(), &cmd);
assert_eq!(actual.out, "foo");
});
}
#[test]
fn source_env_eval_export_env() {
Playground::setup("source_env_eval_export_env", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export-env { let-env FOO = 'foo' }
"#,
)]);
let inp = &[r#"source-env spam.nu"#, r#"$env.FOO"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn source_env_eval_export_env_hide() {
Playground::setup("source_env_eval_export_env", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export-env { hide-env FOO }
"#,
)]);
let inp = &[
r#"let-env FOO = 'foo'"#,
r#"source-env spam.nu"#,
r#"$env.FOO"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert!(actual.err.contains("cannot find column"));
})
}
#[test]
fn source_env_do_cd() {
Playground::setup("source_env_do_cd", |dirs, sandbox| {
sandbox
.mkdir("test1/test2")
.with_files(vec![FileWithContentToBeTrimmed(
"test1/test2/spam.nu",
r#"
cd test1/test2
"#,
)]);
let inp = &[
r#"source-env test1/test2/spam.nu"#,
r#"$env.PWD | path basename"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "test2");
})
}
#[test]
fn source_env_do_cd_file_relative() {
Playground::setup("source_env_do_cd_file_relative", |dirs, sandbox| {
sandbox
.mkdir("test1/test2")
.with_files(vec![FileWithContentToBeTrimmed(
"test1/test2/spam.nu",
r#"
cd ($env.FILE_PWD | path join '..')
"#,
)]);
let inp = &[
r#"source-env test1/test2/spam.nu"#,
r#"$env.PWD | path basename"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "test1");
})
}
#[test]
fn source_env_dont_cd_overlay() {
Playground::setup("source_env_dont_cd_overlay", |dirs, sandbox| {
sandbox
.mkdir("test1/test2")
.with_files(vec![FileWithContentToBeTrimmed(
"test1/test2/spam.nu",
r#"
overlay new spam
cd test1/test2
overlay hide spam
"#,
)]);
let inp = &[
r#"source-env test1/test2/spam.nu"#,
r#"$env.PWD | path basename"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "source_env_dont_cd_overlay");
})
}
#[test]
fn source_env_is_scoped() {
Playground::setup("source_env_is_scoped", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
def no-name-similar-to-this [] { 'no-name-similar-to-this' }
alias nor-similar-to-this = 'nor-similar-to-this'
"#,
)]);
let inp = &[r#"source-env spam.nu"#, r#"no-name-similar-to-this"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert!(actual.err.contains("executable was not found"));
let inp = &[r#"source-env spam.nu"#, r#"nor-similar-to-this"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert!(actual.err.contains("executable was not found"));
})
}