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
This commit is contained in:
Stefan Holderbach
2024-05-04 02:53:15 +02:00
committed by GitHub
parent e6f473695c
commit 406df7f208
69 changed files with 527 additions and 553 deletions

View File

@ -8,7 +8,7 @@ use std::path::Path;
#[test]
fn canonicalize_path() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let mut spam = dirs.test().to_owned();
spam.push("spam.txt");
@ -23,7 +23,7 @@ fn canonicalize_path() {
#[test]
fn canonicalize_unicode_path() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("🚒.txt")]);
sandbox.with_files(&[EmptyFile("🚒.txt")]);
let mut spam = dirs.test().to_owned();
spam.push("🚒.txt");
@ -45,7 +45,7 @@ fn canonicalize_non_utf8_path() {
#[test]
fn canonicalize_path_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let actual = canonicalize_with("spam.txt", dirs.test()).expect("Failed to canonicalize");
let mut expected = dirs.test().to_owned();
@ -59,7 +59,7 @@ fn canonicalize_path_relative_to() {
fn canonicalize_unicode_path_relative_to_unicode_path_with_spaces() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("e-$ èрт🚒♞中片-j");
sandbox.with_files(vec![EmptyFile("e-$ èрт🚒♞中片-j/🚒.txt")]);
sandbox.with_files(&[EmptyFile("e-$ èрт🚒♞中片-j/🚒.txt")]);
let mut relative_to = dirs.test().to_owned();
relative_to.push("e-$ èрт🚒♞中片-j");
@ -81,7 +81,7 @@ fn canonicalize_non_utf8_path_relative_to_non_utf8_path_with_spaces() {
#[test]
fn canonicalize_absolute_path_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let mut absolute_path = dirs.test().to_owned();
absolute_path.push("spam.txt");
@ -116,7 +116,7 @@ fn canonicalize_many_dots() {
#[test]
fn canonicalize_path_with_dot_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let actual = canonicalize_with("./spam.txt", dirs.test()).expect("Failed to canonicalize");
let mut expected = dirs.test().to_owned();
@ -129,7 +129,7 @@ fn canonicalize_path_with_dot_relative_to() {
#[test]
fn canonicalize_path_with_many_dots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let actual = canonicalize_with("././/.//////./././//.////spam.txt", dirs.test())
.expect("Failed to canonicalize");
@ -155,7 +155,7 @@ fn canonicalize_double_dot() {
fn canonicalize_path_with_double_dot_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo");
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let actual =
canonicalize_with("foo/../spam.txt", dirs.test()).expect("Failed to canonicalize");
@ -170,7 +170,7 @@ fn canonicalize_path_with_double_dot_relative_to() {
fn canonicalize_path_with_many_double_dots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz");
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let actual = canonicalize_with("foo/bar/baz/../../../spam.txt", dirs.test())
.expect("Failed to canonicalize");
@ -205,7 +205,7 @@ fn canonicalize_ndots2() {
fn canonicalize_path_with_3_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar");
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let actual =
canonicalize_with("foo/bar/.../spam.txt", dirs.test()).expect("Failed to canonicalize");
@ -220,7 +220,7 @@ fn canonicalize_path_with_3_ndots_relative_to() {
fn canonicalize_path_with_many_3_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz/eggs/sausage/bacon");
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let actual = canonicalize_with(
"foo/bar/baz/eggs/sausage/bacon/.../.../.../spam.txt",
@ -238,7 +238,7 @@ fn canonicalize_path_with_many_3_ndots_relative_to() {
fn canonicalize_path_with_4_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz");
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let actual = canonicalize_with("foo/bar/baz/..../spam.txt", dirs.test())
.expect("Failed to canonicalize");
@ -253,7 +253,7 @@ fn canonicalize_path_with_4_ndots_relative_to() {
fn canonicalize_path_with_many_4_ndots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz/eggs/sausage/bacon");
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let actual = canonicalize_with(
"foo/bar/baz/eggs/sausage/bacon/..../..../spam.txt",
@ -271,7 +271,7 @@ fn canonicalize_path_with_many_4_ndots_relative_to() {
fn canonicalize_path_with_way_too_many_dots_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz/eggs/sausage/bacon/vikings");
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
let mut relative_to = dirs.test().to_owned();
relative_to.push("foo/bar/baz/eggs/sausage/bacon/vikings");
@ -289,7 +289,7 @@ fn canonicalize_path_with_way_too_many_dots_relative_to() {
fn canonicalize_unicode_path_with_way_too_many_dots_relative_to_unicode_path_with_spaces() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/áčěéí +šř=é/baz/eggs/e-$ èрт🚒♞中片-j/bacon/öäöä öäöä");
sandbox.with_files(vec![EmptyFile("🚒.txt")]);
sandbox.with_files(&[EmptyFile("🚒.txt")]);
let mut relative_to = dirs.test().to_owned();
relative_to.push("foo/áčěéí +šř=é/baz/eggs/e-$ èрт🚒♞中片-j/bacon/öäöä öäöä");
@ -329,7 +329,7 @@ fn canonicalize_tilde_relative_to() {
#[test]
fn canonicalize_symlink() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
sandbox.symlink("spam.txt", "link_to_spam.txt");
let mut symlink_path = dirs.test().to_owned();
@ -348,7 +348,7 @@ fn canonicalize_symlink() {
#[test]
fn canonicalize_symlink_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
sandbox.symlink("spam.txt", "link_to_spam.txt");
let actual =
@ -379,7 +379,7 @@ fn canonicalize_symlink_loop_relative_to_should_fail() {
#[test]
fn canonicalize_nested_symlink_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("spam.txt")]);
sandbox.with_files(&[EmptyFile("spam.txt")]);
sandbox.symlink("spam.txt", "link_to_spam.txt");
sandbox.symlink("link_to_spam.txt", "link_to_link_to_spam.txt");
@ -397,7 +397,7 @@ fn canonicalize_nested_symlink_relative_to() {
fn canonicalize_nested_symlink_within_symlink_dir_relative_to() {
Playground::setup("nu_path_test_1", |dirs, sandbox| {
sandbox.mkdir("foo/bar/baz");
sandbox.with_files(vec![EmptyFile("foo/bar/baz/spam.txt")]);
sandbox.with_files(&[EmptyFile("foo/bar/baz/spam.txt")]);
sandbox.symlink("foo/bar/baz/spam.txt", "foo/bar/link_to_spam.txt");
sandbox.symlink("foo/bar/link_to_spam.txt", "foo/link_to_link_to_spam.txt");
sandbox.symlink("foo", "link_to_foo");