From f4c0d9d45b14c3eb0c02339fae1f04aaf2305953 Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Sat, 3 Aug 2024 08:09:13 +0000 Subject: [PATCH] Path migration part 4: various tests (#13373) # Description Part 4 of replacing std::path types with nu_path types added in https://github.com/nushell/nushell/pull/13115. This PR migrates various tests throughout the code base. --- crates/nu-cli/src/repl.rs | 75 ++++++++++++------- crates/nu-command/tests/commands/cd.rs | 29 ++++--- .../tests/commands/database/into_sqlite.rs | 8 +- crates/nu-command/tests/commands/mktemp.rs | 9 +-- crates/nu-command/tests/commands/move_/umv.rs | 36 ++++----- .../nu-command/tests/commands/path/expand.rs | 17 ++--- crates/nu-command/tests/commands/rm.rs | 31 +++----- crates/nu-command/tests/commands/touch.rs | 5 +- crates/nu-command/tests/commands/ucp.rs | 72 +++++++----------- crates/nu-command/tests/commands/umkdir.rs | 15 +--- crates/nu-protocol/src/engine/engine_state.rs | 55 ++++++++------ crates/nu-test-support/src/fs.rs | 2 +- tests/repl/test_config_path.rs | 71 +++++++++++------- 13 files changed, 213 insertions(+), 212 deletions(-) diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index 817950a445..28a3e2cec7 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -1337,20 +1337,26 @@ fn are_session_ids_in_sync() { #[cfg(test)] mod test_auto_cd { use super::{do_auto_cd, parse_operation, ReplOperation}; + use nu_path::AbsolutePath; use nu_protocol::engine::{EngineState, Stack}; - use std::path::Path; use tempfile::tempdir; /// Create a symlink. Works on both Unix and Windows. #[cfg(any(unix, windows))] - fn symlink(original: impl AsRef, link: impl AsRef) -> std::io::Result<()> { + fn symlink( + original: impl AsRef, + link: impl AsRef, + ) -> std::io::Result<()> { + let original = original.as_ref(); + let link = link.as_ref(); + #[cfg(unix)] { std::os::unix::fs::symlink(original, link) } #[cfg(windows)] { - if original.as_ref().is_dir() { + if original.is_dir() { std::os::windows::fs::symlink_dir(original, link) } else { std::os::windows::fs::symlink_file(original, link) @@ -1362,11 +1368,11 @@ mod test_auto_cd { /// `before`, and after `input` is parsed and evaluated, PWD should be /// changed to `after`. #[track_caller] - fn check(before: impl AsRef, input: &str, after: impl AsRef) { + fn check(before: impl AsRef, input: &str, after: impl AsRef) { // Setup EngineState and Stack. let mut engine_state = EngineState::new(); let mut stack = Stack::new(); - stack.set_cwd(before).unwrap(); + stack.set_cwd(before.as_ref()).unwrap(); // Parse the input. It must be an auto-cd operation. let op = parse_operation(input.to_string(), &engine_state, &stack).unwrap(); @@ -1382,54 +1388,66 @@ mod test_auto_cd { // don't have to be byte-wise equal (on Windows, the 8.3 filename // conversion messes things up), let updated_cwd = std::fs::canonicalize(updated_cwd).unwrap(); - let after = std::fs::canonicalize(after).unwrap(); + let after = std::fs::canonicalize(after.as_ref()).unwrap(); assert_eq!(updated_cwd, after); } #[test] fn auto_cd_root() { let tempdir = tempdir().unwrap(); - let root = if cfg!(windows) { r"C:\" } else { "/" }; - check(&tempdir, root, root); + let tempdir = AbsolutePath::try_new(tempdir.path()).unwrap(); + + let input = if cfg!(windows) { r"C:\" } else { "/" }; + let root = AbsolutePath::try_new(input).unwrap(); + check(tempdir, input, root); } #[test] fn auto_cd_tilde() { let tempdir = tempdir().unwrap(); + let tempdir = AbsolutePath::try_new(tempdir.path()).unwrap(); + let home = nu_path::home_dir().unwrap(); - check(&tempdir, "~", home); + check(tempdir, "~", home); } #[test] fn auto_cd_dot() { let tempdir = tempdir().unwrap(); - check(&tempdir, ".", &tempdir); + let tempdir = AbsolutePath::try_new(tempdir.path()).unwrap(); + + check(tempdir, ".", tempdir); } #[test] fn auto_cd_double_dot() { let tempdir = tempdir().unwrap(); - let dir = tempdir.path().join("foo"); + let tempdir = AbsolutePath::try_new(tempdir.path()).unwrap(); + + let dir = tempdir.join("foo"); std::fs::create_dir_all(&dir).unwrap(); - check(dir, "..", &tempdir); + check(dir, "..", tempdir); } #[test] fn auto_cd_triple_dot() { let tempdir = tempdir().unwrap(); - let dir = tempdir.path().join("foo").join("bar"); + let tempdir = AbsolutePath::try_new(tempdir.path()).unwrap(); + + let dir = tempdir.join("foo").join("bar"); std::fs::create_dir_all(&dir).unwrap(); - check(dir, "...", &tempdir); + check(dir, "...", tempdir); } #[test] fn auto_cd_relative() { let tempdir = tempdir().unwrap(); - let foo = tempdir.path().join("foo"); - let bar = tempdir.path().join("bar"); + let tempdir = AbsolutePath::try_new(tempdir.path()).unwrap(); + + let foo = tempdir.join("foo"); + let bar = tempdir.join("bar"); std::fs::create_dir_all(&foo).unwrap(); std::fs::create_dir_all(&bar).unwrap(); - let input = if cfg!(windows) { r"..\bar" } else { "../bar" }; check(foo, input, bar); } @@ -1437,32 +1455,35 @@ mod test_auto_cd { #[test] fn auto_cd_trailing_slash() { let tempdir = tempdir().unwrap(); - let dir = tempdir.path().join("foo"); - std::fs::create_dir_all(&dir).unwrap(); + let tempdir = AbsolutePath::try_new(tempdir.path()).unwrap(); + let dir = tempdir.join("foo"); + std::fs::create_dir_all(&dir).unwrap(); let input = if cfg!(windows) { r"foo\" } else { "foo/" }; - check(&tempdir, input, dir); + check(tempdir, input, dir); } #[test] fn auto_cd_symlink() { let tempdir = tempdir().unwrap(); - let dir = tempdir.path().join("foo"); - std::fs::create_dir_all(&dir).unwrap(); - let link = tempdir.path().join("link"); - symlink(&dir, &link).unwrap(); + let tempdir = AbsolutePath::try_new(tempdir.path()).unwrap(); + let dir = tempdir.join("foo"); + std::fs::create_dir_all(&dir).unwrap(); + let link = tempdir.join("link"); + symlink(&dir, &link).unwrap(); let input = if cfg!(windows) { r".\link" } else { "./link" }; - check(&tempdir, input, link); + check(tempdir, input, link); } #[test] #[should_panic(expected = "was not parsed into an auto-cd operation")] fn auto_cd_nonexistent_directory() { let tempdir = tempdir().unwrap(); - let dir = tempdir.path().join("foo"); + let tempdir = AbsolutePath::try_new(tempdir.path()).unwrap(); + let dir = tempdir.join("foo"); let input = if cfg!(windows) { r"foo\" } else { "foo/" }; - check(&tempdir, input, dir); + check(tempdir, input, dir); } } diff --git a/crates/nu-command/tests/commands/cd.rs b/crates/nu-command/tests/commands/cd.rs index f58638cc35..1b8e2dad5a 100644 --- a/crates/nu-command/tests/commands/cd.rs +++ b/crates/nu-command/tests/commands/cd.rs @@ -1,7 +1,7 @@ +use nu_path::Path; use nu_test_support::fs::Stub::EmptyFile; use nu_test_support::nu; use nu_test_support::playground::Playground; -use std::path::PathBuf; #[test] fn cd_works_with_in_var() { @@ -22,7 +22,7 @@ fn filesystem_change_from_current_directory_using_relative_path() { Playground::setup("cd_test_1", |dirs, _| { let actual = nu!( cwd: dirs.root(), "cd cd_test_1; $env.PWD"); - assert_eq!(PathBuf::from(actual.out), *dirs.test()); + assert_eq!(Path::new(&actual.out), dirs.test()); }) } @@ -32,7 +32,7 @@ fn filesystem_change_from_current_directory_using_relative_path_with_trailing_sl // Intentionally not using correct path sep because this should work on Windows let actual = nu!( cwd: dirs.root(), "cd cd_test_1_slash/; $env.PWD"); - assert_eq!(PathBuf::from(actual.out), *dirs.test()); + assert_eq!(Path::new(&actual.out), *dirs.test()); }) } @@ -48,7 +48,7 @@ fn filesystem_change_from_current_directory_using_absolute_path() { dirs.formats().display() ); - assert_eq!(PathBuf::from(actual.out), dirs.formats()); + assert_eq!(Path::new(&actual.out), dirs.formats()); }) } @@ -65,7 +65,7 @@ fn filesystem_change_from_current_directory_using_absolute_path_with_trailing_sl std::path::MAIN_SEPARATOR_STR, ); - assert_eq!(PathBuf::from(actual.out), dirs.formats()); + assert_eq!(Path::new(&actual.out), dirs.formats()); }) } @@ -84,7 +84,7 @@ fn filesystem_switch_back_to_previous_working_directory() { dirs.test().display() ); - assert_eq!(PathBuf::from(actual.out), dirs.test().join("odin")); + assert_eq!(Path::new(&actual.out), dirs.test().join("odin")); }) } @@ -101,10 +101,7 @@ fn filesystem_change_from_current_directory_using_relative_path_and_dash() { " ); - assert_eq!( - PathBuf::from(actual.out), - dirs.test().join("odin").join("-") - ); + assert_eq!(Path::new(&actual.out), dirs.test().join("odin").join("-")); }) } @@ -119,7 +116,7 @@ fn filesystem_change_current_directory_to_parent_directory() { " ); - assert_eq!(PathBuf::from(actual.out), *dirs.root()); + assert_eq!(Path::new(&actual.out), *dirs.root()); }) } @@ -136,7 +133,7 @@ fn filesystem_change_current_directory_to_two_parents_up_using_multiple_dots() { " ); - assert_eq!(PathBuf::from(actual.out), *dirs.test()); + assert_eq!(Path::new(&actual.out), *dirs.test()); }) } @@ -151,7 +148,7 @@ fn filesystem_change_to_home_directory() { " ); - assert_eq!(Some(PathBuf::from(actual.out)), dirs::home_dir()); + assert_eq!(Path::new(&actual.out), dirs::home_dir().unwrap()); }) } @@ -169,7 +166,7 @@ fn filesystem_change_to_a_directory_containing_spaces() { ); assert_eq!( - PathBuf::from(actual.out), + Path::new(&actual.out), dirs.test().join("robalino turner katz") ); }) @@ -234,7 +231,7 @@ fn filesystem_change_directory_to_symlink_relative() { $env.PWD " ); - assert_eq!(PathBuf::from(actual.out), dirs.test().join("foo_link")); + assert_eq!(Path::new(&actual.out), dirs.test().join("foo_link")); let actual = nu!( cwd: dirs.test().join("boo"), @@ -243,7 +240,7 @@ fn filesystem_change_directory_to_symlink_relative() { $env.PWD " ); - assert_eq!(PathBuf::from(actual.out), dirs.test().join("foo")); + assert_eq!(Path::new(&actual.out), dirs.test().join("foo")); }) } diff --git a/crates/nu-command/tests/commands/database/into_sqlite.rs b/crates/nu-command/tests/commands/database/into_sqlite.rs index faa45040b3..3de7973588 100644 --- a/crates/nu-command/tests/commands/database/into_sqlite.rs +++ b/crates/nu-command/tests/commands/database/into_sqlite.rs @@ -1,6 +1,5 @@ -use std::{io::Write, path::PathBuf}; - use chrono::{DateTime, FixedOffset}; +use nu_path::AbsolutePathBuf; use nu_protocol::{ast::PathMember, record, Span, Value}; use nu_test_support::{ fs::{line_ending, Stub}, @@ -13,6 +12,7 @@ use rand::{ rngs::StdRng, Rng, SeedableRng, }; +use std::io::Write; #[test] fn into_sqlite_schema() { @@ -453,7 +453,7 @@ impl Distribution for Standard { } } -fn make_sqlite_db(dirs: &Dirs, nu_table: &str) -> PathBuf { +fn make_sqlite_db(dirs: &Dirs, nu_table: &str) -> AbsolutePathBuf { let testdir = dirs.test(); let testdb_path = testdir.join(testdir.file_name().unwrap().to_str().unwrap().to_owned() + ".db"); @@ -465,7 +465,7 @@ fn make_sqlite_db(dirs: &Dirs, nu_table: &str) -> PathBuf { ); assert!(nucmd.status.success()); - testdb_path.into() + testdb_path } fn insert_test_rows(dirs: &Dirs, nu_table: &str, sql_query: Option<&str>, expected: Vec) { diff --git a/crates/nu-command/tests/commands/mktemp.rs b/crates/nu-command/tests/commands/mktemp.rs index cda3f59978..30de3beb33 100644 --- a/crates/nu-command/tests/commands/mktemp.rs +++ b/crates/nu-command/tests/commands/mktemp.rs @@ -1,6 +1,6 @@ +use nu_path::AbsolutePath; use nu_test_support::nu; use nu_test_support::playground::Playground; -use std::path::PathBuf; #[test] fn creates_temp_file() { @@ -9,7 +9,7 @@ fn creates_temp_file() { cwd: dirs.test(), "mktemp" ); - let loc = PathBuf::from(output.out.clone()); + let loc = AbsolutePath::try_new(&output.out).unwrap(); println!("{:?}", loc); assert!(loc.exists()); }) @@ -22,7 +22,7 @@ fn creates_temp_file_with_suffix() { cwd: dirs.test(), "mktemp --suffix .txt tempfileXXX" ); - let loc = PathBuf::from(output.out.clone()); + let loc = AbsolutePath::try_new(&output.out).unwrap(); assert!(loc.exists()); assert!(loc.is_file()); assert!(output.out.ends_with(".txt")); @@ -37,8 +37,7 @@ fn creates_temp_directory() { cwd: dirs.test(), "mktemp -d" ); - - let loc = PathBuf::from(output.out); + let loc = AbsolutePath::try_new(&output.out).unwrap(); assert!(loc.exists()); assert!(loc.is_dir()); }) diff --git a/crates/nu-command/tests/commands/move_/umv.rs b/crates/nu-command/tests/commands/move_/umv.rs index 0c96c88a36..b193d5b7cb 100644 --- a/crates/nu-command/tests/commands/move_/umv.rs +++ b/crates/nu-command/tests/commands/move_/umv.rs @@ -2,7 +2,6 @@ use nu_test_support::fs::{files_exist_at, Stub::EmptyFile, Stub::FileWithContent use nu_test_support::nu; use nu_test_support::playground::Playground; use rstest::rstest; -use std::path::Path; #[test] fn moves_a_file() { @@ -96,7 +95,7 @@ fn moves_the_directory_inside_directory_if_path_to_move_is_existing_directory() assert!(!original_dir.exists()); assert!(expected.exists()); - assert!(files_exist_at(vec!["jttxt"], expected)) + assert!(files_exist_at(&["jttxt"], expected)) }) } @@ -125,7 +124,7 @@ fn moves_using_path_with_wildcard() { nu!(cwd: work_dir, "mv ../originals/*.ini ../expected"); assert!(files_exist_at( - vec!["yehuda.ini", "jt.ini", "sample.ini", "andres.ini",], + &["yehuda.ini", "jt.ini", "sample.ini", "andres.ini",], expected )); }) @@ -152,7 +151,7 @@ fn moves_using_a_glob() { assert!(meal_dir.exists()); assert!(files_exist_at( - vec!["arepa.txt", "empanada.txt", "taquiza.txt",], + &["arepa.txt", "empanada.txt", "taquiza.txt",], expected )); }) @@ -184,7 +183,7 @@ fn moves_a_directory_with_files() { assert!(!original_dir.exists()); assert!(expected_dir.exists()); assert!(files_exist_at( - vec![ + &[ "car/car1.txt", "car/car2.txt", "bicycle/bicycle1.txt", @@ -322,7 +321,7 @@ fn move_files_using_glob_two_parents_up_using_multiple_dots() { "# ); - let files = vec![ + let files = &[ "yehuda.yaml", "jtjson", "andres.xml", @@ -333,7 +332,7 @@ fn move_files_using_glob_two_parents_up_using_multiple_dots() { let original_dir = dirs.test().join("foo/bar"); let destination_dir = dirs.test(); - assert!(files_exist_at(files.clone(), destination_dir)); + assert!(files_exist_at(files, destination_dir)); assert!(!files_exist_at(files, original_dir)) }) } @@ -440,10 +439,7 @@ fn mv_change_case_of_directory() { ); #[cfg(any(target_os = "linux", target_os = "freebsd"))] - assert!(files_exist_at( - vec!["somefile.txt",], - dirs.test().join(new_dir) - )); + assert!(files_exist_at(&["somefile.txt"], dirs.test().join(new_dir))); #[cfg(not(any(target_os = "linux", target_os = "freebsd")))] _actual.err.contains("to a subdirectory of itself"); @@ -647,10 +643,10 @@ fn test_cp_inside_glob_metachars_dir() { assert!(actual.err.is_empty()); assert!(!files_exist_at( - vec!["test_file.txt"], + &["test_file.txt"], dirs.test().join(sub_dir) )); - assert!(files_exist_at(vec!["test_file.txt"], dirs.test())); + assert!(files_exist_at(&["test_file.txt"], dirs.test())); }); } @@ -667,19 +663,13 @@ fn mv_with_tilde() { // mv file let actual = nu!(cwd: dirs.test(), "mv '~tilde/f1.txt' ./"); assert!(actual.err.is_empty()); - assert!(!files_exist_at( - vec![Path::new("f1.txt")], - dirs.test().join("~tilde") - )); - assert!(files_exist_at(vec![Path::new("f1.txt")], dirs.test())); + assert!(!files_exist_at(&["f1.txt"], dirs.test().join("~tilde"))); + assert!(files_exist_at(&["f1.txt"], dirs.test())); // pass variable let actual = nu!(cwd: dirs.test(), "let f = '~tilde/f2.txt'; mv $f ./"); assert!(actual.err.is_empty()); - assert!(!files_exist_at( - vec![Path::new("f2.txt")], - dirs.test().join("~tilde") - )); - assert!(files_exist_at(vec![Path::new("f1.txt")], dirs.test())); + assert!(!files_exist_at(&["f2.txt"], dirs.test().join("~tilde"))); + assert!(files_exist_at(&["f1.txt"], dirs.test())); }) } diff --git a/crates/nu-command/tests/commands/path/expand.rs b/crates/nu-command/tests/commands/path/expand.rs index 71f2e03284..4c18c5fd4e 100644 --- a/crates/nu-command/tests/commands/path/expand.rs +++ b/crates/nu-command/tests/commands/path/expand.rs @@ -1,9 +1,8 @@ +use nu_path::Path; 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| { @@ -18,7 +17,7 @@ fn expands_path_with_dot() { )); let expected = dirs.test.join("menu").join("spam.txt"); - assert_eq!(PathBuf::from(actual.out), expected); + assert_eq!(Path::new(&actual.out), expected); }) } @@ -38,7 +37,7 @@ fn expands_path_without_follow_symlink() { )); let expected = dirs.test.join("menu").join("spam_link.ln"); - assert_eq!(PathBuf::from(actual.out), expected); + assert_eq!(Path::new(&actual.out), expected); }) } @@ -56,7 +55,7 @@ fn expands_path_with_double_dot() { )); let expected = dirs.test.join("menu").join("spam.txt"); - assert_eq!(PathBuf::from(actual.out), expected); + assert_eq!(Path::new(&actual.out), expected); }) } @@ -74,7 +73,7 @@ fn const_path_expand() { )); let expected = dirs.test.join("menu").join("spam.txt"); - assert_eq!(PathBuf::from(actual.out), expected); + assert_eq!(Path::new(&actual.out), expected); }) } @@ -92,7 +91,7 @@ mod windows { "# )); - assert!(!PathBuf::from(actual.out).starts_with("~")); + assert!(!Path::new(&actual.out).starts_with("~")); }) } @@ -106,7 +105,7 @@ mod windows { "# )); - assert!(!PathBuf::from(actual.out).starts_with("~")); + assert!(!Path::new(&actual.out).starts_with("~")); }) } @@ -131,7 +130,7 @@ mod windows { )); let expected = dirs.test.join("menu").join("spam_link.ln"); - assert_eq!(PathBuf::from(actual.out), expected); + assert_eq!(Path::new(&actual.out), expected); }) } } diff --git a/crates/nu-command/tests/commands/rm.rs b/crates/nu-command/tests/commands/rm.rs index fbea7edaf1..fdd7258bd4 100644 --- a/crates/nu-command/tests/commands/rm.rs +++ b/crates/nu-command/tests/commands/rm.rs @@ -6,7 +6,6 @@ use nu_test_support::playground::Playground; use rstest::rstest; #[cfg(not(windows))] use std::fs; -use std::path::Path; #[test] fn removes_a_file() { @@ -50,7 +49,7 @@ fn removes_files_with_wildcard() { ); assert!(!files_exist_at( - vec![ + &[ "src/parser/parse/token_tree.rs", "src/parser/hir/baseline_parse.rs", "src/parser/hir/baseline_parse_tokens.rs" @@ -91,7 +90,7 @@ fn removes_deeply_nested_directories_with_wildcard_and_recursive_flag() { ); assert!(!files_exist_at( - vec!["src/parser/parse", "src/parser/hir"], + &["src/parser/parse", "src/parser/hir"], dirs.test() )); }) @@ -277,7 +276,7 @@ fn remove_files_from_two_parents_up_using_multiple_dots_and_glob() { ); assert!(!files_exist_at( - vec!["yehuda.txt", "jttxt", "kevin.txt"], + &["yehuda.txt", "jttxt", "kevin.txt"], dirs.test() )); }) @@ -305,8 +304,8 @@ fn rm_wildcard_keeps_dotfiles() { r#"rm *"# ); - assert!(!files_exist_at(vec!["foo"], dirs.test())); - assert!(files_exist_at(vec![".bar"], dirs.test())); + assert!(!files_exist_at(&["foo"], dirs.test())); + assert!(files_exist_at(&[".bar"], dirs.test())); }) } @@ -320,8 +319,8 @@ fn rm_wildcard_leading_dot_deletes_dotfiles() { "rm .*" ); - assert!(files_exist_at(vec!["foo"], dirs.test())); - assert!(!files_exist_at(vec![".bar"], dirs.test())); + assert!(files_exist_at(&["foo"], dirs.test())); + assert!(!files_exist_at(&[".bar"], dirs.test())); }) } @@ -453,7 +452,7 @@ fn rm_prints_filenames_on_error() { // This rm is expected to fail, and stderr output indicating so is also expected. let actual = nu!(cwd: test_dir, "rm test*.txt"); - assert!(files_exist_at(file_names.clone(), test_dir)); + assert!(files_exist_at(&file_names, test_dir)); for file_name in file_names { let path = test_dir.join(file_name); let substr = format!("Could not delete {}", path.to_string_lossy()); @@ -482,7 +481,7 @@ fn rm_files_inside_glob_metachars_dir() { assert!(actual.err.is_empty()); assert!(!files_exist_at( - vec!["test_file.txt"], + &["test_file.txt"], dirs.test().join(sub_dir) )); }); @@ -556,22 +555,16 @@ fn rm_with_tilde() { let actual = nu!(cwd: dirs.test(), "rm '~tilde/f1.txt'"); assert!(actual.err.is_empty()); - assert!(!files_exist_at( - vec![Path::new("f1.txt")], - dirs.test().join("~tilde") - )); + assert!(!files_exist_at(&["f1.txt"], dirs.test().join("~tilde"))); // pass variable let actual = nu!(cwd: dirs.test(), "let f = '~tilde/f2.txt'; rm $f"); assert!(actual.err.is_empty()); - assert!(!files_exist_at( - vec![Path::new("f2.txt")], - dirs.test().join("~tilde") - )); + assert!(!files_exist_at(&["f2.txt"], dirs.test().join("~tilde"))); // remove directory let actual = nu!(cwd: dirs.test(), "let f = '~tilde'; rm -r $f"); assert!(actual.err.is_empty()); - assert!(!files_exist_at(vec![Path::new("~tilde")], dirs.test())); + assert!(!files_exist_at(&["~tilde"], dirs.test())); }) } diff --git a/crates/nu-command/tests/commands/touch.rs b/crates/nu-command/tests/commands/touch.rs index 5f454cdec4..10ae299a0f 100644 --- a/crates/nu-command/tests/commands/touch.rs +++ b/crates/nu-command/tests/commands/touch.rs @@ -2,7 +2,6 @@ use chrono::{DateTime, Local}; use nu_test_support::fs::{files_exist_at, Stub}; use nu_test_support::nu; use nu_test_support::playground::Playground; -use std::path::Path; // Use 1 instead of 0 because 0 has a special meaning in Windows const TIME_ONE: filetime::FileTime = filetime::FileTime::from_unix_time(1, 0); @@ -494,12 +493,12 @@ fn create_a_file_with_tilde() { Playground::setup("touch with tilde", |dirs, _| { let actual = nu!(cwd: dirs.test(), "touch '~tilde'"); assert!(actual.err.is_empty()); - assert!(files_exist_at(vec![Path::new("~tilde")], dirs.test())); + assert!(files_exist_at(&["~tilde"], dirs.test())); // pass variable let actual = nu!(cwd: dirs.test(), "let f = '~tilde2'; touch $f"); assert!(actual.err.is_empty()); - assert!(files_exist_at(vec![Path::new("~tilde2")], dirs.test())); + assert!(files_exist_at(&["~tilde2"], dirs.test())); }) } diff --git a/crates/nu-command/tests/commands/ucp.rs b/crates/nu-command/tests/commands/ucp.rs index 2c2b30f252..eb2363e68c 100644 --- a/crates/nu-command/tests/commands/ucp.rs +++ b/crates/nu-command/tests/commands/ucp.rs @@ -7,7 +7,6 @@ use nu_test_support::nu; use nu_test_support::playground::Playground; use rstest::rstest; -use std::path::Path; #[cfg(not(target_os = "windows"))] const PATH_SEPARATOR: &str = "/"; @@ -131,11 +130,7 @@ fn copies_the_directory_inside_directory_if_path_to_copy_is_directory_and_with_r assert!(expected_dir.exists()); assert!(files_exist_at( - vec![ - Path::new("yehuda.txt"), - Path::new("jttxt"), - Path::new("andres.txt") - ], + &["yehuda.txt", "jttxt", "andres.txt"], &expected_dir )); }) @@ -181,15 +176,15 @@ fn deep_copies_with_recursive_flag_impl(progress: bool) { assert!(expected_dir.exists()); assert!(files_exist_at( - vec![Path::new("errors.txt"), Path::new("multishells.txt")], + &["errors.txt", "multishells.txt"], jts_expected_copied_dir )); assert!(files_exist_at( - vec![Path::new("coverage.txt"), Path::new("commands.txt")], + &["coverage.txt", "commands.txt"], andres_expected_copied_dir )); assert!(files_exist_at( - vec![Path::new("defer-evaluation.txt")], + &["defer-evaluation.txt"], yehudas_expected_copied_dir )); }) @@ -220,13 +215,13 @@ fn copies_using_path_with_wildcard_impl(progress: bool) { ); assert!(files_exist_at( - vec![ - Path::new("caco3_plastics.csv"), - Path::new("cargo_sample.toml"), - Path::new("jt.xml"), - Path::new("sample.ini"), - Path::new("sgml_description.json"), - Path::new("utf16.ini"), + &[ + "caco3_plastics.csv", + "cargo_sample.toml", + "jt.xml", + "sample.ini", + "sgml_description.json", + "utf16.ini", ], dirs.test() )); @@ -265,13 +260,13 @@ fn copies_using_a_glob_impl(progress: bool) { ); assert!(files_exist_at( - vec![ - Path::new("caco3_plastics.csv"), - Path::new("cargo_sample.toml"), - Path::new("jt.xml"), - Path::new("sample.ini"), - Path::new("sgml_description.json"), - Path::new("utf16.ini"), + &[ + "caco3_plastics.csv", + "cargo_sample.toml", + "jt.xml", + "sample.ini", + "sgml_description.json", + "utf16.ini", ], dirs.test() )); @@ -341,7 +336,7 @@ fn copy_files_using_glob_two_parents_up_using_multiple_dots_imp(progress: bool) ); assert!(files_exist_at( - vec![ + &[ "yehuda.yaml", "jtjson", "andres.xml", @@ -377,7 +372,7 @@ fn copy_file_and_dir_from_two_parents_up_using_multiple_dots_to_current_dir_recu let expected = dirs.test().join("foo/bar"); - assert!(files_exist_at(vec!["hello_there", "hello_again"], expected)); + assert!(files_exist_at(&["hello_there", "hello_again"], expected)); }) } @@ -428,7 +423,7 @@ fn copy_dir_contains_symlink_ignored_impl(progress: bool) { // check hello_there exists inside `tmp_dir_2`, and `dangle_symlink` don't exists inside `tmp_dir_2`. let expected = sandbox.cwd().join("tmp_dir_2"); - assert!(files_exist_at(vec!["hello_there"], expected)); + assert!(files_exist_at(&["hello_there"], expected)); // GNU cp will copy the broken symlink, so following their behavior // thus commenting out below // let path = expected.join("dangle_symlink"); @@ -461,7 +456,7 @@ fn copy_dir_contains_symlink_impl(progress: bool) { // check hello_there exists inside `tmp_dir_2`, and `dangle_symlink` also exists inside `tmp_dir_2`. let expected = sandbox.cwd().join("tmp_dir_2"); - assert!(files_exist_at(vec!["hello_there"], expected.clone())); + assert!(files_exist_at(&["hello_there"], expected.clone())); let path = expected.join("dangle_symlink"); assert!(path.is_symlink()); }); @@ -1151,10 +1146,10 @@ fn test_cp_inside_glob_metachars_dir() { assert!(actual.err.is_empty()); assert!(files_exist_at( - vec!["test_file.txt"], + &["test_file.txt"], dirs.test().join(sub_dir) )); - assert!(files_exist_at(vec!["test_file.txt"], dirs.test())); + assert!(files_exist_at(&["test_file.txt"], dirs.test())); }); } @@ -1167,10 +1162,7 @@ fn test_cp_to_customized_home_directory() { let actual = nu!(cwd: dirs.test(), "mkdir test; cp test_file.txt ~/test/"); assert!(actual.err.is_empty()); - assert!(files_exist_at( - vec!["test_file.txt"], - dirs.test().join("test") - )); + assert!(files_exist_at(&["test_file.txt"], dirs.test().join("test"))); }) } @@ -1193,20 +1185,14 @@ fn cp_with_tilde() { // cp file let actual = nu!(cwd: dirs.test(), "cp '~tilde/f1.txt' ./"); assert!(actual.err.is_empty()); - assert!(files_exist_at( - vec![Path::new("f1.txt")], - dirs.test().join("~tilde") - )); - assert!(files_exist_at(vec![Path::new("f1.txt")], dirs.test())); + assert!(files_exist_at(&["f1.txt"], dirs.test().join("~tilde"))); + assert!(files_exist_at(&["f1.txt"], dirs.test())); // pass variable let actual = nu!(cwd: dirs.test(), "let f = '~tilde/f2.txt'; cp $f ./"); assert!(actual.err.is_empty()); - assert!(files_exist_at( - vec![Path::new("f2.txt")], - dirs.test().join("~tilde") - )); - assert!(files_exist_at(vec![Path::new("f1.txt")], dirs.test())); + assert!(files_exist_at(&["f2.txt"], dirs.test().join("~tilde"))); + assert!(files_exist_at(&["f1.txt"], dirs.test())); }) } diff --git a/crates/nu-command/tests/commands/umkdir.rs b/crates/nu-command/tests/commands/umkdir.rs index 3ca1b3bdfb..fdaaf4f721 100644 --- a/crates/nu-command/tests/commands/umkdir.rs +++ b/crates/nu-command/tests/commands/umkdir.rs @@ -1,7 +1,6 @@ use nu_test_support::fs::files_exist_at; use nu_test_support::playground::Playground; use nu_test_support::{nu, pipeline}; -use std::path::Path; #[test] fn creates_directory() { @@ -25,10 +24,7 @@ fn accepts_and_creates_directories() { "mkdir dir_1 dir_2 dir_3" ); - assert!(files_exist_at( - vec![Path::new("dir_1"), Path::new("dir_2"), Path::new("dir_3")], - dirs.test() - )); + assert!(files_exist_at(&["dir_1", "dir_2", "dir_3"], dirs.test())); }) } @@ -70,10 +66,7 @@ fn print_created_paths() { pipeline("mkdir -v dir_1 dir_2 dir_3") ); - assert!(files_exist_at( - vec![Path::new("dir_1"), Path::new("dir_2"), Path::new("dir_3")], - dirs.test() - )); + assert!(files_exist_at(&["dir_1", "dir_2", "dir_3"], dirs.test())); assert!(actual.out.contains("dir_1")); assert!(actual.out.contains("dir_2")); @@ -165,11 +158,11 @@ fn mkdir_with_tilde() { Playground::setup("mkdir with tilde", |dirs, _| { let actual = nu!(cwd: dirs.test(), "mkdir '~tilde'"); assert!(actual.err.is_empty()); - assert!(files_exist_at(vec![Path::new("~tilde")], dirs.test())); + assert!(files_exist_at(&["~tilde"], dirs.test())); // pass variable let actual = nu!(cwd: dirs.test(), "let f = '~tilde2'; mkdir $f"); assert!(actual.err.is_empty()); - assert!(files_exist_at(vec![Path::new("~tilde2")], dirs.test())); + assert!(files_exist_at(&["~tilde2"], dirs.test())); }) } diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index 7c7cfb9bb5..bd049bbb2d 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -1169,20 +1169,25 @@ mod test_cwd { engine::{EngineState, Stack}, Span, Value, }; - use nu_path::assert_path_eq; - use std::path::Path; + use nu_path::{assert_path_eq, AbsolutePath, Path}; use tempfile::{NamedTempFile, TempDir}; /// Creates a symlink. Works on both Unix and Windows. #[cfg(any(unix, windows))] - fn symlink(original: impl AsRef, link: impl AsRef) -> std::io::Result<()> { + fn symlink( + original: impl AsRef, + link: impl AsRef, + ) -> std::io::Result<()> { + let original = original.as_ref(); + let link = link.as_ref(); + #[cfg(unix)] { std::os::unix::fs::symlink(original, link) } #[cfg(windows)] { - if original.as_ref().is_dir() { + if original.is_dir() { std::os::windows::fs::symlink_dir(original, link) } else { std::os::windows::fs::symlink_file(original, link) @@ -1195,10 +1200,7 @@ mod test_cwd { let mut engine_state = EngineState::new(); engine_state.add_env_var( "PWD".into(), - Value::String { - val: path.as_ref().to_string_lossy().to_string(), - internal_span: Span::unknown(), - }, + Value::test_string(path.as_ref().to_str().unwrap()), ); engine_state } @@ -1208,10 +1210,7 @@ mod test_cwd { let mut stack = Stack::new(); stack.add_env_var( "PWD".into(), - Value::String { - val: path.as_ref().to_string_lossy().to_string(), - internal_span: Span::unknown(), - }, + Value::test_string(path.as_ref().to_str().unwrap()), ); stack } @@ -1289,9 +1288,12 @@ mod test_cwd { #[test] fn pwd_points_to_symlink_to_file() { let file = NamedTempFile::new().unwrap(); + let temp_file = AbsolutePath::try_new(file.path()).unwrap(); let dir = TempDir::new().unwrap(); - let link = dir.path().join("link"); - symlink(file.path(), &link).unwrap(); + let temp = AbsolutePath::try_new(dir.path()).unwrap(); + + let link = temp.join("link"); + symlink(temp_file, &link).unwrap(); let engine_state = engine_state_with_pwd(&link); engine_state.cwd(None).unwrap_err(); @@ -1300,8 +1302,10 @@ mod test_cwd { #[test] fn pwd_points_to_symlink_to_directory() { let dir = TempDir::new().unwrap(); - let link = dir.path().join("link"); - symlink(dir.path(), &link).unwrap(); + let temp = AbsolutePath::try_new(dir.path()).unwrap(); + + let link = temp.join("link"); + symlink(temp, &link).unwrap(); let engine_state = engine_state_with_pwd(&link); let cwd = engine_state.cwd(None).unwrap(); @@ -1311,10 +1315,15 @@ mod test_cwd { #[test] fn pwd_points_to_broken_symlink() { let dir = TempDir::new().unwrap(); - let link = dir.path().join("link"); - symlink(TempDir::new().unwrap().path(), &link).unwrap(); + let temp = AbsolutePath::try_new(dir.path()).unwrap(); + let other_dir = TempDir::new().unwrap(); + let other_temp = AbsolutePath::try_new(other_dir.path()).unwrap(); + + let link = temp.join("link"); + symlink(other_temp, &link).unwrap(); let engine_state = engine_state_with_pwd(&link); + drop(other_dir); engine_state.cwd(None).unwrap_err(); } @@ -1357,12 +1366,14 @@ mod test_cwd { #[test] fn stack_pwd_points_to_normal_directory_with_symlink_components() { - // `/tmp/dir/link` points to `/tmp/dir`, then we set PWD to `/tmp/dir/link/foo` let dir = TempDir::new().unwrap(); - let link = dir.path().join("link"); - symlink(dir.path(), &link).unwrap(); + let temp = AbsolutePath::try_new(dir.path()).unwrap(); + + // `/tmp/dir/link` points to `/tmp/dir`, then we set PWD to `/tmp/dir/link/foo` + let link = temp.join("link"); + symlink(temp, &link).unwrap(); let foo = link.join("foo"); - std::fs::create_dir(dir.path().join("foo")).unwrap(); + std::fs::create_dir(temp.join("foo")).unwrap(); let engine_state = EngineState::new(); let stack = stack_with_pwd(&foo); diff --git a/crates/nu-test-support/src/fs.rs b/crates/nu-test-support/src/fs.rs index 230e5eafe3..d591a7de18 100644 --- a/crates/nu-test-support/src/fs.rs +++ b/crates/nu-test-support/src/fs.rs @@ -35,7 +35,7 @@ pub fn line_ending() -> String { } } -pub fn files_exist_at(files: Vec>, path: impl AsRef) -> bool { +pub fn files_exist_at(files: &[impl AsRef], path: impl AsRef) -> bool { let path = path.as_ref(); files.iter().all(|f| path.join(f.as_ref()).exists()) } diff --git a/tests/repl/test_config_path.rs b/tests/repl/test_config_path.rs index b66633c879..6bee043765 100644 --- a/tests/repl/test_config_path.rs +++ b/tests/repl/test_config_path.rs @@ -1,9 +1,8 @@ -use nu_path::canonicalize_with; +use nu_path::{AbsolutePath, AbsolutePathBuf, Path}; use nu_test_support::nu; use nu_test_support::playground::{Executable, Playground}; use pretty_assertions::assert_eq; use std::fs::{self, File}; -use std::path::{Path, PathBuf}; #[cfg(not(target_os = "windows"))] fn adjust_canonicalization>(p: P) -> String { @@ -24,21 +23,26 @@ fn adjust_canonicalization>(p: P) -> String { /// Make the config directory a symlink that points to a temporary folder, and also makes /// the nushell directory inside a symlink. /// Returns the path to the `nushell` config folder inside, via the symlink. -fn setup_fake_config(playground: &mut Playground) -> PathBuf { - let config_dir = "config_real"; +fn setup_fake_config(playground: &mut Playground) -> AbsolutePathBuf { + let config_real = "config_real"; let config_link = "config_link"; let nushell_real = "nushell_real"; - let nushell_config_dir = Path::new(config_dir).join("nushell").display().to_string(); + let nushell_link = Path::new(config_real) + .join("nushell") + .into_os_string() + .into_string() + .unwrap(); + + let config_home = playground.cwd().join(config_link); + playground.mkdir(nushell_real); - playground.mkdir(config_dir); - playground.symlink(nushell_real, &nushell_config_dir); - playground.symlink(config_dir, config_link); - playground.with_env( - "XDG_CONFIG_HOME", - &playground.cwd().join(config_link).display().to_string(), - ); - let path = Path::new(config_link).join("nushell"); - canonicalize_with(&path, playground.cwd()).unwrap_or(path) + playground.mkdir(config_real); + playground.symlink(nushell_real, &nushell_link); + playground.symlink(config_real, config_link); + playground.with_env("XDG_CONFIG_HOME", config_home.to_str().unwrap()); + + let path = config_home.join("nushell"); + path.canonicalize().map(Into::into).unwrap_or(path) } fn run(playground: &mut Playground, command: &str) -> String { @@ -79,47 +83,55 @@ fn run_interactive_stderr(xdg_config_home: impl AsRef) -> String { .to_string(); } -fn test_config_path_helper(playground: &mut Playground, config_dir_nushell: PathBuf) { +fn test_config_path_helper( + playground: &mut Playground, + config_dir_nushell: impl AsRef, +) { + let config_dir_nushell = config_dir_nushell.as_ref(); + // 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); + let _ = fs::create_dir_all(config_dir_nushell); } - let config_dir_nushell = - std::fs::canonicalize(&config_dir_nushell).expect("canonicalize config dir failed"); + let config_dir_nushell = config_dir_nushell + .canonicalize() + .expect("canonicalize config dir failed"); let actual = run(playground, "$nu.default-config-dir"); assert_eq!(actual, adjust_canonicalization(&config_dir_nushell)); let config_path = config_dir_nushell.join("config.nu"); // We use canonicalize here in case the config or env is symlinked since $nu.config-path is returning the canonicalized path in #8653 let canon_config_path = - adjust_canonicalization(std::fs::canonicalize(&config_path).unwrap_or(config_path)); + adjust_canonicalization(std::fs::canonicalize(&config_path).unwrap_or(config_path.into())); let actual = run(playground, "$nu.config-path"); assert_eq!(actual, canon_config_path); let env_path = config_dir_nushell.join("env.nu"); let canon_env_path = - adjust_canonicalization(std::fs::canonicalize(&env_path).unwrap_or(env_path)); + adjust_canonicalization(std::fs::canonicalize(&env_path).unwrap_or(env_path.into())); let actual = run(playground, "$nu.env-path"); assert_eq!(actual, canon_env_path); 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 canon_history_path = adjust_canonicalization( + std::fs::canonicalize(&history_path).unwrap_or(history_path.into()), + ); let actual = run(playground, "$nu.history-path"); assert_eq!(actual, 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)); + adjust_canonicalization(std::fs::canonicalize(&login_path).unwrap_or(login_path.into())); let actual = run(playground, "$nu.loginshell-path"); assert_eq!(actual, canon_login_path); #[cfg(feature = "plugin")] { let plugin_path = config_dir_nushell.join("plugin.msgpackz"); - let canon_plugin_path = - adjust_canonicalization(std::fs::canonicalize(&plugin_path).unwrap_or(plugin_path)); + let canon_plugin_path = adjust_canonicalization( + std::fs::canonicalize(&plugin_path).unwrap_or(plugin_path.into()), + ); let actual = run(playground, "$nu.plugin-path"); assert_eq!(actual, canon_plugin_path); } @@ -129,7 +141,7 @@ fn test_config_path_helper(playground: &mut Playground, config_dir_nushell: Path fn test_default_config_path() { Playground::setup("default_config_path", |_, playground| { let config_dir = nu_path::config_dir().expect("Could not get config directory"); - test_config_path_helper(playground, config_dir.join("nushell").into()); + test_config_path_helper(playground, config_dir.join("nushell")); }); } @@ -152,8 +164,9 @@ fn test_default_symlink_config_path_broken_symlink_config_files() { |_, playground| { let fake_config_dir_nushell = setup_fake_config(playground); - let fake_dir = PathBuf::from("fake"); - playground.mkdir(&fake_dir.display().to_string()); + let fake_dir = "fake"; + playground.mkdir(fake_dir); + let fake_dir = Path::new(fake_dir); for config_file in [ "config.nu", @@ -172,7 +185,7 @@ fn test_default_symlink_config_path_broken_symlink_config_files() { // Windows doesn't allow creating a symlink without the file existing, // so we first create original files for the symlinks, then delete them // to break the symlinks - std::fs::remove_dir_all(playground.cwd().join(&fake_dir)).unwrap(); + std::fs::remove_dir_all(playground.cwd().join(fake_dir)).unwrap(); test_config_path_helper(playground, fake_config_dir_nushell); },