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

333 lines
9.1 KiB
Rust
Raw Normal View History

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 = dirs.test().join("config.toml");
let library_path = dirs.test().join("lib");
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.with_config(file);
nu.with_files(&[FileWithContent(
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
"config.toml",
&format!(
r#"
lib_dirs = ["{}"]
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
skip_welcome_message = true
"#,
library_path.as_os_str().to_str().unwrap(),
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(&[FileWithContent(
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
"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(&[FileWithContent(
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
"main.nu",
r#"
remove let-env, focus on mutating $env (#9574) # Description For years, Nushell has used `let-env` to set a single environment variable. As our work on scoping continued, we refined what it meant for a variable to be in scope using `let` but never updated how `let-env` would work. Instead, `let-env` confusingly created mutations to the command's copy of `$env`. So, to help fix the mental model and point people to the right way of thinking about what changing the environment means, this PR removes `let-env` to encourage people to think of it as updating the command's environment variable via mutation. Before: ``` let-env FOO = "BAR" ``` Now: ``` $env.FOO = "BAR" ``` It's also a good reminder that the environment owned by the command is in the `$env` variable rather than global like it is in other shells. # User-Facing Changes BREAKING CHANGE BREAKING CHANGE This completely removes `let-env FOO = "BAR"` so that we can focus on `$env.FOO = "BAR"`. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After / Before Submitting integration scripts to update: - :heavy_check_mark: [starship](https://github.com/starship/starship/blob/master/src/init/starship.nu) - :heavy_check_mark: [virtualenv](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu) - :heavy_check_mark: [atuin](https://github.com/ellie/atuin/blob/main/atuin/src/shell/atuin.nu) (PR: https://github.com/ellie/atuin/pull/1080) - :x: [zoxide](https://github.com/ajeetdsouza/zoxide/blob/main/templates/nushell.txt) (PR: https://github.com/ajeetdsouza/zoxide/pull/587) - :heavy_check_mark: [oh-my-posh](https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/src/shell/scripts/omp.nu) (pr: https://github.com/JanDeDobbeleer/oh-my-posh/pull/4011)
2023-06-30 21:57:51 +02:00
$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(
Fix: remove unnecessary `r#"..."#` (#8670) (#9764) <!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> This PR is related to **Tests: clean up unnecessary use of cwd, pipeline(), etc. [#8670](https://github.com/nushell/nushell/issues/8670)** - Removed the `r#"..."#` raw string literal syntax, which is unnecessary when there are no special characters that need quoting from the tests that use the `nu!` macro. - `cwd:` and `pipeline()` has not changed # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-07-21 17:32:37 +02:00
"
source-env my_library.nu ;
hello
Fix: remove unnecessary `r#"..."#` (#8670) (#9764) <!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> This PR is related to **Tests: clean up unnecessary use of cwd, pipeline(), etc. [#8670](https://github.com/nushell/nushell/issues/8670)** - Removed the `r#"..."#` raw string literal syntax, which is unnecessary when there are no special characters that need quoting from the tests that use the `nu!` macro. - `cwd:` and `pipeline()` has not changed # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-07-21 17:32:37 +02:00
"
));
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(&[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(&[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(&[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(&[FileWithContent(foo_file, "echo foo")]);
let cmd = format!("let file = `{foo_file}`; source-env $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(&[FileWithContentToBeTrimmed(
"spam.nu",
r#"
remove let-env, focus on mutating $env (#9574) # Description For years, Nushell has used `let-env` to set a single environment variable. As our work on scoping continued, we refined what it meant for a variable to be in scope using `let` but never updated how `let-env` would work. Instead, `let-env` confusingly created mutations to the command's copy of `$env`. So, to help fix the mental model and point people to the right way of thinking about what changing the environment means, this PR removes `let-env` to encourage people to think of it as updating the command's environment variable via mutation. Before: ``` let-env FOO = "BAR" ``` Now: ``` $env.FOO = "BAR" ``` It's also a good reminder that the environment owned by the command is in the `$env` variable rather than global like it is in other shells. # User-Facing Changes BREAKING CHANGE BREAKING CHANGE This completely removes `let-env FOO = "BAR"` so that we can focus on `$env.FOO = "BAR"`. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After / Before Submitting integration scripts to update: - :heavy_check_mark: [starship](https://github.com/starship/starship/blob/master/src/init/starship.nu) - :heavy_check_mark: [virtualenv](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu) - :heavy_check_mark: [atuin](https://github.com/ellie/atuin/blob/main/atuin/src/shell/atuin.nu) (PR: https://github.com/ellie/atuin/pull/1080) - :x: [zoxide](https://github.com/ajeetdsouza/zoxide/blob/main/templates/nushell.txt) (PR: https://github.com/ajeetdsouza/zoxide/pull/587) - :heavy_check_mark: [oh-my-posh](https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/src/shell/scripts/omp.nu) (pr: https://github.com/JanDeDobbeleer/oh-my-posh/pull/4011)
2023-06-30 21:57:51 +02:00
export-env { $env.FOO = 'foo' }
"#,
)]);
let inp = &[r#"source-env spam.nu"#, r#"$env.FOO"#];
let actual = nu!(cwd: dirs.test(), &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(&[FileWithContentToBeTrimmed(
"spam.nu",
r#"
export-env { hide-env FOO }
"#,
)]);
let inp = &[
remove let-env, focus on mutating $env (#9574) # Description For years, Nushell has used `let-env` to set a single environment variable. As our work on scoping continued, we refined what it meant for a variable to be in scope using `let` but never updated how `let-env` would work. Instead, `let-env` confusingly created mutations to the command's copy of `$env`. So, to help fix the mental model and point people to the right way of thinking about what changing the environment means, this PR removes `let-env` to encourage people to think of it as updating the command's environment variable via mutation. Before: ``` let-env FOO = "BAR" ``` Now: ``` $env.FOO = "BAR" ``` It's also a good reminder that the environment owned by the command is in the `$env` variable rather than global like it is in other shells. # User-Facing Changes BREAKING CHANGE BREAKING CHANGE This completely removes `let-env FOO = "BAR"` so that we can focus on `$env.FOO = "BAR"`. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After / Before Submitting integration scripts to update: - :heavy_check_mark: [starship](https://github.com/starship/starship/blob/master/src/init/starship.nu) - :heavy_check_mark: [virtualenv](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu) - :heavy_check_mark: [atuin](https://github.com/ellie/atuin/blob/main/atuin/src/shell/atuin.nu) (PR: https://github.com/ellie/atuin/pull/1080) - :x: [zoxide](https://github.com/ajeetdsouza/zoxide/blob/main/templates/nushell.txt) (PR: https://github.com/ajeetdsouza/zoxide/pull/587) - :heavy_check_mark: [oh-my-posh](https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/src/shell/scripts/omp.nu) (pr: https://github.com/JanDeDobbeleer/oh-my-posh/pull/4011)
2023-06-30 21:57:51 +02:00
r#"$env.FOO = 'foo'"#,
r#"source-env spam.nu"#,
r#"$env.FOO"#,
];
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
assert!(actual.err.contains("not_found"));
})
}
#[test]
fn source_env_do_cd() {
Playground::setup("source_env_do_cd", |dirs, sandbox| {
sandbox
.mkdir("test1/test2")
.with_files(&[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(), &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(&[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(), &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(&[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(), &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(&[FileWithContentToBeTrimmed(
"spam.nu",
r#"
def no-name-similar-to-this [] { 'no-name-similar-to-this' }
alias nor-similar-to-this = echo 'nor-similar-to-this'
"#,
)]);
let inp = &[r#"source-env spam.nu"#, r#"no-name-similar-to-this"#];
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
Rewrite run_external.rs (#12921) This PR is a complete rewrite of `run_external.rs`. The main goal of the rewrite is improving readability, but it also fixes some bugs related to argument handling and the PATH variable (fixes https://github.com/nushell/nushell/issues/6011). I'll discuss some technical details to make reviewing easier. ## Argument handling Quoting arguments for external commands is hard. Like, *really* hard. We've had more than a dozen issues and PRs dedicated to quoting arguments (see Appendix) but the current implementation is still buggy. Here's a demonstration of the buggy behavior: ```nu let foo = "'bar'" ^touch $foo # This creates a file named `bar`, but it should be `'bar'` ^touch ...[ "'bar'" ] # Same ``` I'll describe how this PR deals with argument handling. First, we'll introduce the concept of **bare strings**. Bare strings are **string literals** that are either **unquoted** or **quoted by backticks** [^1]. Strings within a list literal are NOT considered bare strings, even if they are unquoted or quoted by backticks. When a bare string is used as an argument to external process, we need to perform tilde-expansion, glob-expansion, and inner-quotes-removal, in that order. "Inner-quotes-removal" means transforming from `--option="value"` into `--option=value`. ## `.bat` files and CMD built-ins On Windows, `.bat` files and `.cmd` files are considered executable, but they need `CMD.exe` as the interpreter. The Rust standard library supports running `.bat` files directly and will spawn `CMD.exe` under the hood (see [documentation](https://doc.rust-lang.org/std/process/index.html#windows-argument-splitting)). However, other extensions are not supported [^2]. Nushell also supports a selected number of CMD built-ins. The problem with CMD is that it uses a different set of quoting rules. Correctly quoting for CMD requires using [Command::raw_arg()](https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html#tymethod.raw_arg) and manually quoting CMD special characters, on top of quoting from the Nushell side. ~~I decided that this is too complex and chose to reject special characters in CMD built-ins instead [^3]. Hopefully this will not affact real-world use cases.~~ I've implemented escaping that works reasonably well. ## `which-support` feature The `which` crate is now a hard dependency of `nu-command`, making the `which-support` feature essentially useless. The `which` crate is already a hard dependency of `nu-cli`, and we should consider removing the `which-support` feature entirely. ## Appendix Here's a list of quoting-related issues and PRs in rough chronological order. * https://github.com/nushell/nushell/issues/4609 * https://github.com/nushell/nushell/issues/4631 * https://github.com/nushell/nushell/issues/4601 * https://github.com/nushell/nushell/pull/5846 * https://github.com/nushell/nushell/issues/5978 * https://github.com/nushell/nushell/pull/6014 * https://github.com/nushell/nushell/issues/6154 * https://github.com/nushell/nushell/pull/6161 * https://github.com/nushell/nushell/issues/6399 * https://github.com/nushell/nushell/pull/6420 * https://github.com/nushell/nushell/pull/6426 * https://github.com/nushell/nushell/issues/6465 * https://github.com/nushell/nushell/issues/6559 * https://github.com/nushell/nushell/pull/6560 [^1]: The idea that backtick-quoted strings act like bare strings was introduced by Kubouch and briefly mentioned in [the language reference](https://www.nushell.sh/lang-guide/chapters/strings_and_text.html#backtick-quotes). [^2]: The documentation also said "running .bat scripts in this way may be removed in the future and so should not be relied upon", which is another reason to move away from this. But again, quoting for CMD is hard. [^3]: If anyone wants to try, the best resource I found on the topic is [this](https://daviddeley.com/autohotkey/parameters/parameters.htm).
2024-05-23 04:05:27 +02:00
assert!(actual
.err
.contains("Command `no-name-similar-to-this` not found"));
let inp = &[r#"source-env spam.nu"#, r#"nor-similar-to-this"#];
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
Rewrite run_external.rs (#12921) This PR is a complete rewrite of `run_external.rs`. The main goal of the rewrite is improving readability, but it also fixes some bugs related to argument handling and the PATH variable (fixes https://github.com/nushell/nushell/issues/6011). I'll discuss some technical details to make reviewing easier. ## Argument handling Quoting arguments for external commands is hard. Like, *really* hard. We've had more than a dozen issues and PRs dedicated to quoting arguments (see Appendix) but the current implementation is still buggy. Here's a demonstration of the buggy behavior: ```nu let foo = "'bar'" ^touch $foo # This creates a file named `bar`, but it should be `'bar'` ^touch ...[ "'bar'" ] # Same ``` I'll describe how this PR deals with argument handling. First, we'll introduce the concept of **bare strings**. Bare strings are **string literals** that are either **unquoted** or **quoted by backticks** [^1]. Strings within a list literal are NOT considered bare strings, even if they are unquoted or quoted by backticks. When a bare string is used as an argument to external process, we need to perform tilde-expansion, glob-expansion, and inner-quotes-removal, in that order. "Inner-quotes-removal" means transforming from `--option="value"` into `--option=value`. ## `.bat` files and CMD built-ins On Windows, `.bat` files and `.cmd` files are considered executable, but they need `CMD.exe` as the interpreter. The Rust standard library supports running `.bat` files directly and will spawn `CMD.exe` under the hood (see [documentation](https://doc.rust-lang.org/std/process/index.html#windows-argument-splitting)). However, other extensions are not supported [^2]. Nushell also supports a selected number of CMD built-ins. The problem with CMD is that it uses a different set of quoting rules. Correctly quoting for CMD requires using [Command::raw_arg()](https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html#tymethod.raw_arg) and manually quoting CMD special characters, on top of quoting from the Nushell side. ~~I decided that this is too complex and chose to reject special characters in CMD built-ins instead [^3]. Hopefully this will not affact real-world use cases.~~ I've implemented escaping that works reasonably well. ## `which-support` feature The `which` crate is now a hard dependency of `nu-command`, making the `which-support` feature essentially useless. The `which` crate is already a hard dependency of `nu-cli`, and we should consider removing the `which-support` feature entirely. ## Appendix Here's a list of quoting-related issues and PRs in rough chronological order. * https://github.com/nushell/nushell/issues/4609 * https://github.com/nushell/nushell/issues/4631 * https://github.com/nushell/nushell/issues/4601 * https://github.com/nushell/nushell/pull/5846 * https://github.com/nushell/nushell/issues/5978 * https://github.com/nushell/nushell/pull/6014 * https://github.com/nushell/nushell/issues/6154 * https://github.com/nushell/nushell/pull/6161 * https://github.com/nushell/nushell/issues/6399 * https://github.com/nushell/nushell/pull/6420 * https://github.com/nushell/nushell/pull/6426 * https://github.com/nushell/nushell/issues/6465 * https://github.com/nushell/nushell/issues/6559 * https://github.com/nushell/nushell/pull/6560 [^1]: The idea that backtick-quoted strings act like bare strings was introduced by Kubouch and briefly mentioned in [the language reference](https://www.nushell.sh/lang-guide/chapters/strings_and_text.html#backtick-quotes). [^2]: The documentation also said "running .bat scripts in this way may be removed in the future and so should not be relied upon", which is another reason to move away from this. But again, quoting for CMD is hard. [^3]: If anyone wants to try, the best resource I found on the topic is [this](https://daviddeley.com/autohotkey/parameters/parameters.htm).
2024-05-23 04:05:27 +02:00
assert!(actual
.err
.contains("Command `nor-similar-to-this` not found"));
})
}
#[test]
fn source_env_const_file() {
Playground::setup("source_env_const_file", |dirs, sandbox| {
sandbox.with_files(&[FileWithContentToBeTrimmed(
"spam.nu",
r#"
remove let-env, focus on mutating $env (#9574) # Description For years, Nushell has used `let-env` to set a single environment variable. As our work on scoping continued, we refined what it meant for a variable to be in scope using `let` but never updated how `let-env` would work. Instead, `let-env` confusingly created mutations to the command's copy of `$env`. So, to help fix the mental model and point people to the right way of thinking about what changing the environment means, this PR removes `let-env` to encourage people to think of it as updating the command's environment variable via mutation. Before: ``` let-env FOO = "BAR" ``` Now: ``` $env.FOO = "BAR" ``` It's also a good reminder that the environment owned by the command is in the `$env` variable rather than global like it is in other shells. # User-Facing Changes BREAKING CHANGE BREAKING CHANGE This completely removes `let-env FOO = "BAR"` so that we can focus on `$env.FOO = "BAR"`. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After / Before Submitting integration scripts to update: - :heavy_check_mark: [starship](https://github.com/starship/starship/blob/master/src/init/starship.nu) - :heavy_check_mark: [virtualenv](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu) - :heavy_check_mark: [atuin](https://github.com/ellie/atuin/blob/main/atuin/src/shell/atuin.nu) (PR: https://github.com/ellie/atuin/pull/1080) - :x: [zoxide](https://github.com/ajeetdsouza/zoxide/blob/main/templates/nushell.txt) (PR: https://github.com/ajeetdsouza/zoxide/pull/587) - :heavy_check_mark: [oh-my-posh](https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/src/shell/scripts/omp.nu) (pr: https://github.com/JanDeDobbeleer/oh-my-posh/pull/4011)
2023-06-30 21:57:51 +02:00
$env.FOO = 'foo'
"#,
)]);
let inp = &[
r#"const file = 'spam.nu'"#,
r#"source-env $file"#,
r#"$env.FOO"#,
];
let actual = nu!(cwd: dirs.test(), &inp.join("; "));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn source_respects_early_return() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
Fix: remove unnecessary `r#"..."#` (#8670) (#9764) <!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> This PR is related to **Tests: clean up unnecessary use of cwd, pipeline(), etc. [#8670](https://github.com/nushell/nushell/issues/8670)** - Removed the `r#"..."#` raw string literal syntax, which is unnecessary when there are no special characters that need quoting from the tests that use the `nu!` macro. - `cwd:` and `pipeline()` has not changed # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-07-21 17:32:37 +02:00
"
source early_return.nu
Fix: remove unnecessary `r#"..."#` (#8670) (#9764) <!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> This PR is related to **Tests: clean up unnecessary use of cwd, pipeline(), etc. [#8670](https://github.com/nushell/nushell/issues/8670)** - Removed the `r#"..."#` raw string literal syntax, which is unnecessary when there are no special characters that need quoting from the tests that use the `nu!` macro. - `cwd:` and `pipeline()` has not changed # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2023-07-21 17:32:37 +02:00
"
));
assert!(actual.err.is_empty());
}