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

@ -129,7 +129,7 @@ fn passes_with_env_env_var_to_external_process() {
#[test]
fn has_file_pwd() {
Playground::setup("has_file_pwd", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent("spam.nu", "$env.FILE_PWD")]);
sandbox.with_files(&[FileWithContent("spam.nu", "$env.FILE_PWD")]);
let actual = nu!(cwd: dirs.test(), "nu spam.nu");
@ -140,7 +140,7 @@ fn has_file_pwd() {
#[test]
fn has_file_loc() {
Playground::setup("has_file_pwd", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent("spam.nu", "$env.CURRENT_FILE")]);
sandbox.with_files(&[FileWithContent("spam.nu", "$env.CURRENT_FILE")]);
let actual = nu!(cwd: dirs.test(), "nu spam.nu");
@ -154,7 +154,7 @@ fn has_file_loc() {
#[serial]
fn passes_env_from_local_cfg_to_external_process() {
Playground::setup("autoenv_dir", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
sandbox.with_files(&[FileWithContent(
".nu-env",
r#"[env]
FOO = "foo"

View File

@ -65,7 +65,7 @@ fn nu_lib_dirs_repl() {
Playground::setup("nu_lib_dirs_repl", |dirs, sandbox| {
sandbox
.mkdir("scripts")
.with_files(vec![FileWithContentToBeTrimmed(
.with_files(&[FileWithContentToBeTrimmed(
"scripts/foo.nu",
r#"
$env.FOO = "foo"
@ -90,13 +90,13 @@ fn nu_lib_dirs_script() {
Playground::setup("nu_lib_dirs_script", |dirs, sandbox| {
sandbox
.mkdir("scripts")
.with_files(vec![FileWithContentToBeTrimmed(
.with_files(&[FileWithContentToBeTrimmed(
"scripts/foo.nu",
r#"
$env.FOO = "foo"
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
.with_files(&[FileWithContentToBeTrimmed(
"main.nu",
"
source-env foo.nu
@ -121,7 +121,7 @@ fn nu_lib_dirs_relative_repl() {
Playground::setup("nu_lib_dirs_relative_repl", |dirs, sandbox| {
sandbox
.mkdir("scripts")
.with_files(vec![FileWithContentToBeTrimmed(
.with_files(&[FileWithContentToBeTrimmed(
"scripts/foo.nu",
r#"
$env.FOO = "foo"
@ -147,13 +147,13 @@ fn const_nu_lib_dirs_relative() {
Playground::setup("const_nu_lib_dirs_relative", |dirs, sandbox| {
sandbox
.mkdir("scripts")
.with_files(vec![FileWithContentToBeTrimmed(
.with_files(&[FileWithContentToBeTrimmed(
"scripts/foo.nu",
r#"
$env.FOO = "foo"
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
.with_files(&[FileWithContentToBeTrimmed(
"main.nu",
"
const NU_LIB_DIRS = [ 'scripts' ]
@ -174,13 +174,13 @@ fn nu_lib_dirs_relative_script() {
Playground::setup("nu_lib_dirs_relative_script", |dirs, sandbox| {
sandbox
.mkdir("scripts")
.with_files(vec![FileWithContentToBeTrimmed(
.with_files(&[FileWithContentToBeTrimmed(
"scripts/main.nu",
"
source-env ../foo.nu
",
)])
.with_files(vec![FileWithContentToBeTrimmed(
.with_files(&[FileWithContentToBeTrimmed(
"foo.nu",
r#"
$env.FOO = "foo"
@ -288,7 +288,7 @@ fn run_with_no_newline() {
fn main_script_can_have_subcommands1() {
Playground::setup("main_subcommands", |dirs, sandbox| {
sandbox.mkdir("main_subcommands");
sandbox.with_files(vec![FileWithContent(
sandbox.with_files(&[FileWithContent(
"script.nu",
r#"def "main foo" [x: int] {
print ($x + 100)
@ -309,7 +309,7 @@ fn main_script_can_have_subcommands1() {
fn main_script_can_have_subcommands2() {
Playground::setup("main_subcommands", |dirs, sandbox| {
sandbox.mkdir("main_subcommands");
sandbox.with_files(vec![FileWithContent(
sandbox.with_files(&[FileWithContent(
"script.nu",
r#"def "main foo" [x: int] {
print ($x + 100)
@ -330,7 +330,7 @@ fn main_script_can_have_subcommands2() {
fn source_empty_file() {
Playground::setup("source_empty_file", |dirs, sandbox| {
sandbox.mkdir("source_empty_file");
sandbox.with_files(vec![FileWithContent("empty.nu", "")]);
sandbox.with_files(&[FileWithContent("empty.nu", "")]);
let actual = nu!(cwd: dirs.test(), pipeline("nu empty.nu"));
assert!(actual.out.is_empty());

View File

@ -168,7 +168,7 @@ fn err_pipe_with_failed_external_works() {
#[test]
fn dont_run_glob_if_pass_variable_to_external() {
Playground::setup("dont_run_glob", |dirs, sandbox| {
sandbox.with_files(vec![
sandbox.with_files(&[
EmptyFile("jt_likes_cake.txt"),
EmptyFile("andres_likes_arepas.txt"),
]);
@ -182,7 +182,7 @@ fn dont_run_glob_if_pass_variable_to_external() {
#[test]
fn run_glob_if_pass_variable_to_external() {
Playground::setup("run_glob_on_external", |dirs, sandbox| {
sandbox.with_files(vec![
sandbox.with_files(&[
EmptyFile("jt_likes_cake.txt"),
EmptyFile("andres_likes_arepas.txt"),
]);
@ -202,7 +202,7 @@ mod it_evaluation {
#[test]
fn takes_rows_of_nu_value_strings() {
Playground::setup("it_argument_test_1", |dirs, sandbox| {
sandbox.with_files(vec![
sandbox.with_files(&[
EmptyFile("jt_likes_cake.txt"),
EmptyFile("andres_likes_arepas.txt"),
]);
@ -225,7 +225,7 @@ mod it_evaluation {
#[test]
fn takes_rows_of_nu_value_lines() {
Playground::setup("it_argument_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
sandbox.with_files(&[FileWithContentToBeTrimmed(
"nu_candies.txt",
"
AndrásWithKitKatzz
@ -258,7 +258,7 @@ mod it_evaluation {
#[test]
fn supports_fetching_given_a_column_path_to_it() {
Playground::setup("it_argument_test_3", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
sandbox.with_files(&[FileWithContent(
"sample.toml",
r#"
nu_party_venue = "zion"
@ -349,7 +349,7 @@ mod external_words {
#[case("$ sign.toml", r#""$ sign.toml""#)]
fn external_arg_with_special_characters(#[case] path: &str, #[case] nu_path_argument: &str) {
Playground::setup("external_arg_with_quotes", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
sandbox.with_files(&[FileWithContent(
path,
r#"
nu_party_venue = "zion"
@ -479,7 +479,7 @@ mod external_command_arguments {
Playground::setup(
"expands_table_of_primitives_to_positional_arguments",
|dirs, sandbox| {
sandbox.with_files(vec![
sandbox.with_files(&[
EmptyFile("jt_likes_cake.txt"),
EmptyFile("andres_likes_arepas.txt"),
EmptyFile("ferris_not_here.txt"),
@ -505,7 +505,7 @@ mod external_command_arguments {
Playground::setup(
"expands_table_of_primitives_to_positional_arguments",
|dirs, sandbox| {
sandbox.with_files(vec![
sandbox.with_files(&[
EmptyFile("jt_likes_cake.txt"),
EmptyFile("andres_likes_arepas.txt"),
EmptyFile("ferris_not_here.txt"),
@ -531,7 +531,7 @@ mod external_command_arguments {
|dirs, sandbox| {
sandbox.mkdir("cd");
sandbox.with_files(vec![EmptyFile("cd/jt_likes_cake.txt")]);
sandbox.with_files(&[EmptyFile("cd/jt_likes_cake.txt")]);
let actual = nu!(
cwd: dirs.test(), pipeline(

View File

@ -29,7 +29,7 @@ fn takes_rows_of_nu_value_strings_and_pipes_it_to_stdin_of_external() {
#[test]
fn treats_dot_dot_as_path_not_range() {
Playground::setup("dot_dot_dir", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
sandbox.with_files(&[FileWithContentToBeTrimmed(
"nu_times.csv",
"
name,rusty_luck,origin
@ -83,7 +83,7 @@ fn for_loop() {
#[test]
fn subexpression_handles_dot() {
Playground::setup("subexpression_handles_dot", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
sandbox.with_files(&[FileWithContentToBeTrimmed(
"nu_times.csv",
"
name,rusty_luck,origin