Files
Piepmatz b9d514df7b Replace old Playground usages with the new one (#18915)
## Description

This PR replaces all the `Playground::setup` calls with the new injected
`Playground`.

Most of the changes are done via [this
script](https://tangled.org/cptpiepmatz.de/nushell_playground_asr) which
uses the rust-analyzer internals to replace code via the AST.

All the test should behave exactly the same as before but they don't
need to manually set up a playground anymore. Also this removes quite a
lot of lines, which is nice.

## User-facing changes (Release notes)

n/a

---

<sub>Stack created with <a
href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a
href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>
2026-11-19 14:29:12 +01:00

338 lines
10 KiB
Rust

use nu_path::absolute_with;
use nu_test_support::prelude::*;
use pretty_assertions::assert_eq;
#[test]
fn absolute_path(playground: Playground) {
let mut spam = playground.path().to_owned();
spam.push("spam.txt");
let cwd = std::env::current_dir().expect("Could not get current directory");
let actual = absolute_with(spam, cwd).expect("Failed to make absolute");
assert!(actual.ends_with("spam.txt"));
}
#[test]
fn absolute_unicode_path(playground: Playground) {
let mut spam = playground.path().to_owned();
spam.push("🚒.txt");
let cwd = std::env::current_dir().expect("Could not get current directory");
let actual = absolute_with(spam, cwd).expect("Failed to make absolute");
assert!(actual.ends_with("🚒.txt"));
}
#[ignore]
#[test]
fn absolute_non_utf8_path() {
// TODO
}
#[test]
fn absolute_path_relative_to(playground: Playground) {
let actual = absolute_with("spam.txt", playground.path()).expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
expected.push("spam.txt");
assert_eq!(actual, expected);
}
#[test]
fn absolute_unicode_path_relative_to_unicode_path_with_spaces(playground: Playground) {
let mut relative_to = playground.path().to_owned();
relative_to.push("e-$ èрт🚒♞中片-j");
let actual = absolute_with("🚒.txt", relative_to).expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
expected.push("e-$ èрт🚒♞中片-j/🚒.txt");
assert_eq!(actual, expected);
}
#[ignore]
#[test]
fn absolute_non_utf8_path_relative_to_non_utf8_path_with_spaces() {
// TODO
}
#[test]
fn absolute_absolute_path_relative_to(playground: Playground) {
let mut absolute_path = playground.path().to_owned();
absolute_path.push("spam.txt");
let actual =
absolute_with(&absolute_path, "non/existent/directory").expect("Failed to make absolute");
let expected = absolute_path;
assert_eq!(actual, expected);
}
#[test]
fn absolute_dot() {
let expected = std::env::current_dir().expect("Could not get current directory");
let actual = absolute_with(".", expected.as_path()).expect("Failed to make absolute");
assert_eq!(actual, expected);
}
#[test]
fn absolute_many_dots() {
let expected = std::env::current_dir().expect("Could not get current directory");
let actual = absolute_with("././/.//////./././//.///", expected.as_path())
.expect("Failed to make absolute");
assert_eq!(actual, expected);
}
#[test]
fn absolute_path_with_dot_relative_to(playground: Playground) {
let actual = absolute_with("./spam.txt", playground.path()).expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
expected.push("spam.txt");
assert_eq!(actual, expected);
}
#[test]
fn absolute_path_with_many_dots_relative_to(playground: Playground) {
let actual = absolute_with("././/.//////./././//.////spam.txt", playground.path())
.expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
expected.push("spam.txt");
assert_eq!(actual, expected);
}
#[test]
fn absolute_double_dot() {
let cwd = std::env::current_dir().expect("Could not get current directory");
let actual = absolute_with("..", &cwd).expect("Failed to make absolute");
// On Windows .. components are resolved. On Unix they are not.
#[cfg(windows)]
let expected = cwd
.parent()
.expect("Could not get parent of current directory");
#[cfg(not(windows))]
let expected = cwd.join("..");
assert_eq!(actual, expected);
}
#[test]
fn absolute_path_with_double_dot_relative_to(playground: Playground) {
let actual =
absolute_with("foo/../spam.txt", playground.path()).expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
// On Windows .. components are resolved. On Unix they are not.
#[cfg(windows)]
expected.push("spam.txt");
#[cfg(not(windows))]
expected.push("foo/../spam.txt");
assert_eq!(actual, expected);
}
#[test]
fn absolute_path_with_many_double_dots_relative_to(playground: Playground) {
let actual = absolute_with("foo/bar/baz/../../../spam.txt", playground.path())
.expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
// On Windows .. components are resolved. On Unix they are not.
#[cfg(windows)]
expected.push("spam.txt");
#[cfg(not(windows))]
expected.push("foo/bar/baz/../../../spam.txt");
assert_eq!(actual, expected);
}
#[test]
fn absolute_ndots2(playground: Playground) -> Result {
// This test will fail if you have the nushell repo on the root partition
// So, let's start in a nested folder before trying to absolute_with "..."
let cwd = playground.dir("aaa/bbb/ccc")?;
let actual = absolute_with("...", &cwd).expect("Failed to make absolute");
// On Windows .. components are resolved. On Unix they are not.
#[cfg(windows)]
let expected = cwd
.parent()
.expect("Could not get parent of current directory")
.parent()
.expect("Could not get parent of a parent of current directory");
#[cfg(not(windows))]
let expected = cwd.join("../..");
assert_eq!(actual, expected);
Ok(())
}
#[test]
fn absolute_path_with_3_ndots_relative_to(playground: Playground) {
let actual =
absolute_with("foo/bar/.../spam.txt", playground.path()).expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
// On Windows .. components are resolved. On Unix they are not.
#[cfg(windows)]
expected.push("spam.txt");
#[cfg(not(windows))]
expected.push("foo/bar/../../spam.txt");
assert_eq!(actual, expected);
}
#[test]
fn absolute_path_with_many_3_ndots_relative_to(playground: Playground) {
let actual = absolute_with(
"foo/bar/baz/eggs/sausage/bacon/.../.../.../spam.txt",
playground.path(),
)
.expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
// On Windows .. components are resolved. On Unix they are not.
#[cfg(windows)]
expected.push("spam.txt");
#[cfg(not(windows))]
expected.push("foo/bar/baz/eggs/sausage/bacon/../../../../../../spam.txt");
assert_eq!(actual, expected);
}
#[test]
fn absolute_path_with_4_ndots_relative_to(playground: Playground) {
let actual = absolute_with("foo/bar/baz/..../spam.txt", playground.path())
.expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
// On Windows .. components are resolved. On Unix they are not.
#[cfg(windows)]
expected.push("spam.txt");
#[cfg(not(windows))]
expected.push("foo/bar/baz/../../../spam.txt");
assert_eq!(actual, expected);
}
#[test]
fn absolute_path_with_many_4_ndots_relative_to(playground: Playground) {
let actual = absolute_with(
"foo/bar/baz/eggs/sausage/bacon/..../..../spam.txt",
playground.path(),
)
.expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
// On Windows .. components are resolved. On Unix they are not.
#[cfg(windows)]
expected.push("spam.txt");
#[cfg(not(windows))]
expected.push("foo/bar/baz/eggs/sausage/bacon/../../../../../../spam.txt");
assert_eq!(actual, expected);
}
#[test]
fn absolute_path_with_way_too_many_dots_relative_to(playground: Playground) {
let mut relative_to = playground.path().to_owned();
relative_to.push("foo/bar/baz/eggs/sausage/bacon/vikings");
let actual = absolute_with("././..////././...///././.....///spam.txt", relative_to)
.expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
// On Windows .. components are resolved. On Unix they are not.
#[cfg(windows)]
expected.push("spam.txt");
#[cfg(not(windows))]
expected.push("foo/bar/baz/eggs/sausage/bacon/vikings/../../../../../../../spam.txt");
assert_eq!(actual, expected);
}
#[test]
fn absolute_unicode_path_with_way_too_many_dots_relative_to_unicode_path_with_spaces(
playground: Playground,
) {
let mut relative_to = playground.path().to_owned();
relative_to.push("foo/áčěéí +šř=é/baz/eggs/e-$ èрт🚒♞中片-j/bacon/öäöä öäöä");
let actual = absolute_with("././..////././...///././.....///🚒.txt", relative_to)
.expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
// On Windows .. components are resolved. On Unix they are not.
#[cfg(windows)]
expected.push("🚒.txt");
#[cfg(not(windows))]
expected.push(
"foo/áčěéí +šř=é/baz/eggs/e-$ èрт🚒♞中片-j/bacon/öäöä öäöä/../../../../../../../🚒.txt",
);
assert_eq!(actual, expected);
}
#[test]
fn absolute_tilde() {
let tilde_path = "~";
let cwd = std::env::current_dir().expect("Could not get current directory");
let actual = absolute_with(tilde_path, cwd).expect("Failed to make absolute");
assert!(actual.is_absolute());
assert!(!actual.starts_with("~"));
}
#[test]
fn absolute_tilde_relative_to() {
let tilde_path = "~";
let actual = absolute_with(tilde_path, "non/existent/path").expect("Failed to make absolute");
assert!(actual.is_absolute());
assert!(!actual.starts_with("~"));
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn absolute_symlink(playground: Playground) -> Result {
playground.empty_file("spam.txt")?;
playground.symlink("spam.txt", "link_to_spam.txt")?;
let mut symlink_path = playground.path().to_owned();
symlink_path.push("link_to_spam.txt");
let cwd = std::env::current_dir().expect("Could not get current directory");
let actual = absolute_with(symlink_path, cwd).expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
expected.push("link_to_spam.txt");
assert_eq!(actual, expected);
Ok(())
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn absolute_symlink_relative_to(playground: Playground) -> Result {
playground.empty_file("spam.txt")?;
playground.symlink("spam.txt", "link_to_spam.txt")?;
let actual =
absolute_with("link_to_spam.txt", playground.path()).expect("Failed to make absolute");
let mut expected = playground.path().to_owned();
expected.push("link_to_spam.txt");
assert_eq!(actual, expected);
Ok(())
}