nushell/crates/nu-command/tests/commands/path/expand.rs
Stefan Holderbach 406df7f208
Avoid taking unnecessary ownership of intermediates (#12740)
# Description

Judiciously try to avoid allocations/clone by changing the signature of
functions

- **Don't pass str by value unnecessarily if only read**
- **Don't require a vec in `Sandbox::with_files`**
- **Remove unnecessary string clone**
- **Fixup unnecessary borrow**
- **Use `&str` in shape color instead**
- **Vec -> Slice**
- **Elide string clone**
- **Elide `Path` clone**
- **Take &str to elide clone in tests**

# User-Facing Changes
None

# Tests + Formatting
This touches many tests purely in changing from owned to borrowed/static
data
2024-05-04 00:53:15 +00:00

138 lines
3.7 KiB
Rust

use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
use std::path::PathBuf;
#[test]
fn expands_path_with_dot() {
Playground::setup("path_expand_1", |dirs, sandbox| {
sandbox.within("menu").with_files(&[EmptyFile("spam.txt")]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
echo "menu/./spam.txt"
| path expand
"#
));
let expected = dirs.test.join("menu").join("spam.txt");
assert_eq!(PathBuf::from(actual.out), expected);
})
}
#[cfg(unix)]
#[test]
fn expands_path_without_follow_symlink() {
Playground::setup("path_expand_3", |dirs, sandbox| {
sandbox.within("menu").with_files(&[EmptyFile("spam.txt")]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ln -s spam.txt menu/spam_link.ln;
echo "menu/./spam_link.ln"
| path expand -n
"#
));
let expected = dirs.test.join("menu").join("spam_link.ln");
assert_eq!(PathBuf::from(actual.out), expected);
})
}
#[test]
fn expands_path_with_double_dot() {
Playground::setup("path_expand_2", |dirs, sandbox| {
sandbox.within("menu").with_files(&[EmptyFile("spam.txt")]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
echo "menu/../menu/spam.txt"
| path expand
"#
));
let expected = dirs.test.join("menu").join("spam.txt");
assert_eq!(PathBuf::from(actual.out), expected);
})
}
#[test]
fn const_path_expand() {
Playground::setup("const_path_expand", |dirs, sandbox| {
sandbox.within("menu").with_files(&[EmptyFile("spam.txt")]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
const result = ("menu/./spam.txt" | path expand);
$result
"#
));
let expected = dirs.test.join("menu").join("spam.txt");
assert_eq!(PathBuf::from(actual.out), expected);
})
}
#[cfg(windows)]
mod windows {
use super::*;
#[test]
fn expands_path_with_tilde_backward_slash() {
Playground::setup("path_expand_2", |dirs, _| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
echo "~\tmp.txt" | path expand
"#
));
assert!(!PathBuf::from(actual.out).starts_with("~"));
})
}
#[test]
fn win_expands_path_with_tilde_forward_slash() {
Playground::setup("path_expand_2", |dirs, _| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
echo "~/tmp.txt" | path expand
"#
));
assert!(!PathBuf::from(actual.out).starts_with("~"));
})
}
#[test]
fn expands_path_without_follow_symlink() {
Playground::setup("path_expand_3", |dirs, sandbox| {
sandbox.within("menu").with_files(&[EmptyFile("spam.txt")]);
let cwd = dirs.test();
std::os::windows::fs::symlink_file(
cwd.join("menu").join("spam.txt"),
cwd.join("menu").join("spam_link.ln"),
)
.unwrap();
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
echo "menu/./spam_link.ln"
| path expand -n
"#
));
let expected = dirs.test.join("menu").join("spam_link.ln");
assert_eq!(PathBuf::from(actual.out), expected);
})
}
}