2022-08-23 07:18:14 +00:00
|
|
|
use nu_test_support::nu;
|
2023-04-08 18:52:37 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2023-03-31 16:17:37 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
|
Canonicalize default-config-dir and plugin-path (#11999)
<!--
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 makes sure `$nu.default-config-dir` and `$nu.plugin-path` are
canonicalized.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
`$nu.default-config-dir` (and `$nu.plugin-path`) will now give canonical
paths, with symlinks and whatnot resolved.
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `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
> ```
-->
I've added a couple of tests to check that even if the config folder
and/or any of the config files within are symlinks, the `$nu.*`
variables are properly canonicalized. These tests unfortunately only run
on Linux and MacOS, because I couldn't figure out how to change the
config directory on Windows. Also, given that they involve creating
files, I'm not sure if they're excessive, so I could remove one or two
of them.
# 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.
-->
2024-03-02 17:15:31 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2023-03-31 16:17:37 +00:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
|
|
fn adjust_canonicalization<P: AsRef<Path>>(p: P) -> String {
|
|
|
|
p.as_ref().display().to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
fn adjust_canonicalization<P: AsRef<Path>>(p: P) -> String {
|
2023-11-16 21:14:45 +00:00
|
|
|
const VERBATIM_PREFIX: &str = r"\\?\";
|
2023-03-31 16:17:37 +00:00
|
|
|
let p = p.as_ref().display().to_string();
|
2023-09-16 19:49:10 +00:00
|
|
|
if let Some(stripped) = p.strip_prefix(VERBATIM_PREFIX) {
|
|
|
|
stripped.to_string()
|
2023-03-31 16:17:37 +00:00
|
|
|
} else {
|
|
|
|
p
|
|
|
|
}
|
|
|
|
}
|
2022-08-23 07:18:14 +00:00
|
|
|
|
Canonicalize default-config-dir and plugin-path (#11999)
<!--
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 makes sure `$nu.default-config-dir` and `$nu.plugin-path` are
canonicalized.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
`$nu.default-config-dir` (and `$nu.plugin-path`) will now give canonical
paths, with symlinks and whatnot resolved.
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `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
> ```
-->
I've added a couple of tests to check that even if the config folder
and/or any of the config files within are symlinks, the `$nu.*`
variables are properly canonicalized. These tests unfortunately only run
on Linux and MacOS, because I couldn't figure out how to change the
config directory on Windows. Also, given that they involve creating
files, I'm not sure if they're excessive, so I could remove one or two
of them.
# 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.
-->
2024-03-02 17:15:31 +00:00
|
|
|
/// Make the config directory a symlink that points to a temporary folder.
|
|
|
|
/// Returns the path to the `nushell` config folder inside, via the symlink.
|
|
|
|
///
|
|
|
|
/// Need to figure out how to change config directory on Windows.
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
fn setup_fake_config(playground: &mut Playground) -> PathBuf {
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
{
|
|
|
|
let config_dir = "config";
|
|
|
|
let config_link = "config_link";
|
|
|
|
playground.mkdir(&format!("{config_dir}/nushell"));
|
|
|
|
playground.symlink(config_dir, config_link);
|
|
|
|
playground.with_env(
|
|
|
|
"XDG_CONFIG_HOME",
|
|
|
|
&playground.cwd().join(config_link).display().to_string(),
|
|
|
|
);
|
|
|
|
Path::new(config_link).join("nushell")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
{
|
|
|
|
let fake_home = "fake_home";
|
|
|
|
let home_link = "home_link";
|
|
|
|
let dir_end = "fake-home/Library/Application\\ Support/nushell";
|
|
|
|
playground.mkdir(&format!("{fake_home}/{dir_end}"));
|
|
|
|
playground.symlink(fake_home, home_link);
|
|
|
|
playground.with_env(
|
|
|
|
"HOME",
|
|
|
|
&playground.cwd().join(home_link).display().to_string(),
|
|
|
|
);
|
|
|
|
PathBuf::from(home_link).join(dir_end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_config_path_helper() {
|
2022-08-23 07:18:14 +00:00
|
|
|
let config_dir = nu_path::config_dir().expect("Could not get config directory");
|
2023-03-31 16:17:37 +00:00
|
|
|
let config_dir_nushell = config_dir.join("nushell");
|
|
|
|
// Create the config dir folder structure if it does not already exist
|
|
|
|
if !config_dir_nushell.exists() {
|
|
|
|
let _ = fs::create_dir_all(&config_dir_nushell);
|
|
|
|
}
|
2022-08-23 07:18:14 +00:00
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory");
|
2023-03-31 16:17:37 +00:00
|
|
|
|
Add ConfigDirNotFound error (#11849)
<!--
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.
-->
Currently, there's multiple places that look for a config directory, and
each of them has different error messages when it can't be found. This
PR makes a `ConfigDirNotFound` error to standardize the error message
for all of these cases.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Previously, the errors in `create_nu_constant()` would say which config
file Nushell was trying to get when it couldn't find the config
directory. Now it doesn't. However, I think that's fine, given that it
doesn't matter whether it couldn't find the config directory while
looking for `login.nu` or `env.nu`, it only matters that it couldn't
find it.
This is what the error looks like:
![image](https://github.com/nushell/nushell/assets/45539777/52298ed4-f9e9-4900-bb94-1154d389efa7)
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `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.
-->
---------
Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
2024-02-26 07:42:20 +00:00
|
|
|
let config_dir_nushell =
|
|
|
|
std::fs::canonicalize(&config_dir_nushell).expect("canonicalize config dir failed");
|
|
|
|
let actual = nu!(cwd: &cwd, "$nu.default-config-dir");
|
|
|
|
assert_eq!(actual.out, adjust_canonicalization(&config_dir_nushell));
|
2023-03-31 16:17:37 +00:00
|
|
|
|
Add ConfigDirNotFound error (#11849)
<!--
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.
-->
Currently, there's multiple places that look for a config directory, and
each of them has different error messages when it can't be found. This
PR makes a `ConfigDirNotFound` error to standardize the error message
for all of these cases.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Previously, the errors in `create_nu_constant()` would say which config
file Nushell was trying to get when it couldn't find the config
directory. Now it doesn't. However, I think that's fine, given that it
doesn't matter whether it couldn't find the config directory while
looking for `login.nu` or `env.nu`, it only matters that it couldn't
find it.
This is what the error looks like:
![image](https://github.com/nushell/nushell/assets/45539777/52298ed4-f9e9-4900-bb94-1154d389efa7)
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `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.
-->
---------
Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
2024-02-26 07:42:20 +00:00
|
|
|
let config_path = config_dir_nushell.join("config.nu");
|
2023-03-31 16:17:37 +00:00
|
|
|
// We use canonicalize here in case the config or env is symlinked since $nu.config-path is returning the canonicalized path in #8653
|
Add ConfigDirNotFound error (#11849)
<!--
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.
-->
Currently, there's multiple places that look for a config directory, and
each of them has different error messages when it can't be found. This
PR makes a `ConfigDirNotFound` error to standardize the error message
for all of these cases.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Previously, the errors in `create_nu_constant()` would say which config
file Nushell was trying to get when it couldn't find the config
directory. Now it doesn't. However, I think that's fine, given that it
doesn't matter whether it couldn't find the config directory while
looking for `login.nu` or `env.nu`, it only matters that it couldn't
find it.
This is what the error looks like:
![image](https://github.com/nushell/nushell/assets/45539777/52298ed4-f9e9-4900-bb94-1154d389efa7)
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `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.
-->
---------
Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
2024-02-26 07:42:20 +00:00
|
|
|
let canon_config_path =
|
|
|
|
adjust_canonicalization(std::fs::canonicalize(&config_path).unwrap_or(config_path));
|
2023-01-09 05:53:52 +00:00
|
|
|
let actual = nu!(cwd: &cwd, "$nu.config-path");
|
2023-03-31 16:17:37 +00:00
|
|
|
assert_eq!(actual.out, canon_config_path);
|
2022-08-23 07:18:14 +00:00
|
|
|
|
Add ConfigDirNotFound error (#11849)
<!--
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.
-->
Currently, there's multiple places that look for a config directory, and
each of them has different error messages when it can't be found. This
PR makes a `ConfigDirNotFound` error to standardize the error message
for all of these cases.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Previously, the errors in `create_nu_constant()` would say which config
file Nushell was trying to get when it couldn't find the config
directory. Now it doesn't. However, I think that's fine, given that it
doesn't matter whether it couldn't find the config directory while
looking for `login.nu` or `env.nu`, it only matters that it couldn't
find it.
This is what the error looks like:
![image](https://github.com/nushell/nushell/assets/45539777/52298ed4-f9e9-4900-bb94-1154d389efa7)
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `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.
-->
---------
Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
2024-02-26 07:42:20 +00:00
|
|
|
let env_path = config_dir_nushell.join("env.nu");
|
|
|
|
let canon_env_path =
|
|
|
|
adjust_canonicalization(std::fs::canonicalize(&env_path).unwrap_or(env_path));
|
2022-08-23 07:18:14 +00:00
|
|
|
let actual = nu!(cwd: &cwd, "$nu.env-path");
|
2023-03-31 16:17:37 +00:00
|
|
|
assert_eq!(actual.out, canon_env_path);
|
Add ConfigDirNotFound error (#11849)
<!--
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.
-->
Currently, there's multiple places that look for a config directory, and
each of them has different error messages when it can't be found. This
PR makes a `ConfigDirNotFound` error to standardize the error message
for all of these cases.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Previously, the errors in `create_nu_constant()` would say which config
file Nushell was trying to get when it couldn't find the config
directory. Now it doesn't. However, I think that's fine, given that it
doesn't matter whether it couldn't find the config directory while
looking for `login.nu` or `env.nu`, it only matters that it couldn't
find it.
This is what the error looks like:
![image](https://github.com/nushell/nushell/assets/45539777/52298ed4-f9e9-4900-bb94-1154d389efa7)
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `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.
-->
---------
Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
2024-02-26 07:42:20 +00:00
|
|
|
|
|
|
|
let history_path = config_dir_nushell.join("history.txt");
|
|
|
|
let canon_history_path =
|
|
|
|
adjust_canonicalization(std::fs::canonicalize(&history_path).unwrap_or(history_path));
|
|
|
|
let actual = nu!(cwd: &cwd, "$nu.history-path");
|
|
|
|
assert_eq!(actual.out, canon_history_path);
|
|
|
|
|
|
|
|
let login_path = config_dir_nushell.join("login.nu");
|
|
|
|
let canon_login_path =
|
|
|
|
adjust_canonicalization(std::fs::canonicalize(&login_path).unwrap_or(login_path));
|
|
|
|
let actual = nu!(cwd: &cwd, "$nu.loginshell-path");
|
|
|
|
assert_eq!(actual.out, canon_login_path);
|
|
|
|
|
|
|
|
#[cfg(feature = "plugin")]
|
|
|
|
{
|
|
|
|
let plugin_path = config_dir_nushell.join("plugin.nu");
|
|
|
|
let canon_plugin_path =
|
|
|
|
adjust_canonicalization(std::fs::canonicalize(&plugin_path).unwrap_or(plugin_path));
|
|
|
|
let actual = nu!(cwd: &cwd, "$nu.plugin-path");
|
|
|
|
assert_eq!(actual.out, canon_plugin_path);
|
|
|
|
}
|
2022-08-23 07:18:14 +00:00
|
|
|
}
|
|
|
|
|
Canonicalize default-config-dir and plugin-path (#11999)
<!--
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 makes sure `$nu.default-config-dir` and `$nu.plugin-path` are
canonicalized.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
`$nu.default-config-dir` (and `$nu.plugin-path`) will now give canonical
paths, with symlinks and whatnot resolved.
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `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
> ```
-->
I've added a couple of tests to check that even if the config folder
and/or any of the config files within are symlinks, the `$nu.*`
variables are properly canonicalized. These tests unfortunately only run
on Linux and MacOS, because I couldn't figure out how to change the
config directory on Windows. Also, given that they involve creating
files, I'm not sure if they're excessive, so I could remove one or two
of them.
# 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.
-->
2024-03-02 17:15:31 +00:00
|
|
|
#[test]
|
|
|
|
fn test_default_config_path() {
|
|
|
|
test_config_path_helper();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Make the config folder a symlink to a temporary folder without any config files
|
|
|
|
/// and see if the config files' paths are properly canonicalized
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
#[test]
|
|
|
|
fn test_default_symlinked_config_path_empty() {
|
|
|
|
Playground::setup("symlinked_empty_config_dir", |_, playground| {
|
|
|
|
let _ = setup_fake_config(playground);
|
|
|
|
|
|
|
|
test_config_path_helper();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like [[test_default_symlinked_config_path_empty]], but fill the temporary folder
|
|
|
|
/// with broken symlinks and see if they're properly canonicalized
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
#[test]
|
|
|
|
fn test_default_symlink_config_path_broken_symlink_config_files() {
|
|
|
|
Playground::setup(
|
|
|
|
"symlinked_cfg_dir_with_symlinked_cfg_files",
|
|
|
|
|_, playground| {
|
|
|
|
let fake_config_dir_nushell = setup_fake_config(playground);
|
|
|
|
|
|
|
|
for config_file in [
|
|
|
|
"config.nu",
|
|
|
|
"env.nu",
|
|
|
|
"history.txt",
|
|
|
|
"history.sqlite3",
|
|
|
|
"login.nu",
|
|
|
|
"plugin.nu",
|
|
|
|
] {
|
|
|
|
playground.symlink(
|
|
|
|
format!("fake/{config_file}"),
|
|
|
|
fake_config_dir_nushell.join(config_file),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
test_config_path_helper();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like [[test_default_symlinked_config_path_empty]], but fill the temporary folder
|
|
|
|
/// with working symlinks to empty files and see if they're properly canonicalized
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
#[test]
|
|
|
|
fn test_default_config_path_symlinked_config_files() {
|
|
|
|
use std::fs::File;
|
|
|
|
|
|
|
|
Playground::setup(
|
|
|
|
"symlinked_cfg_dir_with_symlinked_cfg_files",
|
|
|
|
|_, playground| {
|
|
|
|
let fake_config_dir_nushell = setup_fake_config(playground);
|
|
|
|
|
|
|
|
for config_file in [
|
|
|
|
"config.nu",
|
|
|
|
"env.nu",
|
|
|
|
"history.txt",
|
|
|
|
"history.sqlite3",
|
|
|
|
"login.nu",
|
|
|
|
"plugin.nu",
|
|
|
|
] {
|
|
|
|
let empty_file = playground.cwd().join(format!("empty-{config_file}"));
|
|
|
|
File::create(&empty_file).unwrap();
|
|
|
|
playground.symlink(empty_file, fake_config_dir_nushell.join(config_file));
|
|
|
|
}
|
|
|
|
|
|
|
|
test_config_path_helper();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-23 07:18:14 +00:00
|
|
|
#[test]
|
|
|
|
fn test_alternate_config_path() {
|
|
|
|
let config_file = "crates/nu-utils/src/sample_config/default_config.nu";
|
|
|
|
let env_file = "crates/nu-utils/src/sample_config/default_env.nu";
|
|
|
|
|
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory");
|
|
|
|
|
|
|
|
let config_path =
|
|
|
|
nu_path::canonicalize_with(config_file, &cwd).expect("Could not get config path");
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: &cwd,
|
2023-01-30 01:37:54 +00:00
|
|
|
format!("nu --config {config_path:?} -c '$nu.config-path'")
|
2022-08-23 07:18:14 +00:00
|
|
|
);
|
2023-01-09 05:53:52 +00:00
|
|
|
assert_eq!(actual.out, config_path.to_string_lossy().to_string());
|
2022-08-23 07:18:14 +00:00
|
|
|
|
|
|
|
let env_path = nu_path::canonicalize_with(env_file, &cwd).expect("Could not get env path");
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: &cwd,
|
2023-01-30 01:37:54 +00:00
|
|
|
format!("nu --env-config {env_path:?} -c '$nu.env-path'")
|
2022-08-23 07:18:14 +00:00
|
|
|
);
|
2023-01-09 05:53:52 +00:00
|
|
|
assert_eq!(actual.out, env_path.to_string_lossy().to_string());
|
2022-08-23 07:18:14 +00:00
|
|
|
}
|