From 3a69e0ed00cfde332be5c1b060528b2fe2b1a442 Mon Sep 17 00:00:00 2001 From: Zhenping Zhao Date: Fri, 20 Dec 2024 17:48:09 -0800 Subject: [PATCH] test case for ls --- Cargo.lock | 10 +++---- crates/nu-cli/src/lib.rs | 2 +- crates/nu-cli/src/repl.rs | 4 +-- crates/nu-command/Cargo.toml | 1 + crates/nu-command/src/system/run_external.rs | 22 ++++++++++++-- crates/nu-command/tests/commands/ls.rs | 31 ++++++++++++++++++++ 6 files changed, 60 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4ad46b7392..c18e67e6c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2458,9 +2458,9 @@ checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45" [[package]] name = "is_debug" -version = "1.0.2" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8ea828c9d6638a5bd3d8b14e37502b4d56cae910ccf8a5b7f51c7a0eb1d0508" +checksum = "06d198e9919d9822d5f7083ba8530e04de87841eaf21ead9af8f2304efd57c89" [[package]] name = "is_executable" @@ -3355,6 +3355,7 @@ dependencies = [ "nix 0.29.0", "notify-debouncer-full", "nu-ansi-term", + "nu-cli", "nu-cmd-base", "nu-cmd-lang", "nu-color-config", @@ -3737,7 +3738,6 @@ dependencies = [ "nix 0.29.0", "num-format", "serde", - "serde_json", "strip-ansi-escapes", "sys-locale", "unicase", @@ -6294,9 +6294,9 @@ dependencies = [ [[package]] name = "shadow-rs" -version = "0.37.0" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "974eb8222c62a8588bc0f02794dd1ba5b60b3ec88b58e050729d0907ed6af610" +checksum = "58cfcd0643497a9f780502063aecbcc4a3212cbe4948fd25ee8fd179c2cf9a18" dependencies = [ "const_format", "is_debug", diff --git a/crates/nu-cli/src/lib.rs b/crates/nu-cli/src/lib.rs index 3fe293a030..ee3f22ab34 100644 --- a/crates/nu-cli/src/lib.rs +++ b/crates/nu-cli/src/lib.rs @@ -24,7 +24,7 @@ pub use menus::NuHelpCompleter; pub use nu_highlight::NuHighlight; pub use print::Print; pub use prompt::NushellPrompt; -pub use repl::evaluate_repl; +pub use repl::{do_auto_cd, evaluate_repl}; pub use syntax_highlight::NuHighlighter; pub use util::{eval_source, gather_parent_env_vars}; pub use validation::NuValidator; diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index 9e43bb009d..77fe6596c2 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -843,11 +843,11 @@ fn parse_operation( /// /// Execute an "auto-cd" operation, changing the current working directory. /// -fn do_auto_cd( +pub fn do_auto_cd( path: PathBuf, cwd: String, stack: &mut Stack, - engine_state: &mut EngineState, + engine_state: &EngineState, span: Span, ) { let path = { diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index ef88aa92e4..24abd7b9b2 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -16,6 +16,7 @@ bench = false workspace = true [dependencies] +nu-cli = { path = "../nu-cli", version = "0.100.1"} nu-cmd-base = { path = "../nu-cmd-base", version = "0.100.1" } nu-color-config = { path = "../nu-color-config", version = "0.100.1" } nu-engine = { path = "../nu-engine", version = "0.100.1", default-features = false } diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index e3d74c055b..b86ed523d4 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -1,7 +1,11 @@ +use nu_cli::do_auto_cd; use nu_cmd_base::hook::eval_hook; use nu_engine::{command_prelude::*, env_to_strings}; use nu_path::{dots::expand_ndots, expand_tilde, AbsolutePath}; -use nu_protocol::{did_you_mean, process::ChildProcess, ByteStream, NuGlob, OutDest, Signals}; +use nu_protocol::{ + did_you_mean, engine::expand_path_with, process::ChildProcess, ByteStream, NuGlob, OutDest, + Signals, +}; use nu_system::ForegroundChild; use nu_utils::IgnoreCaseExt; use pathdiff::diff_paths; @@ -61,7 +65,7 @@ impl Command for External { _ => Cow::Owned(name.clone().coerce_into_string()?), }; - let expanded_name = match &name { + let mut expanded_name = match &name { // Expand tilde and ndots on the name if it's a bare string / glob (#13000) Value::Glob { no_expand, .. } if !*no_expand => { expand_ndots_safe(expand_tilde(&*name_str)) @@ -69,6 +73,20 @@ impl Command for External { _ => Path::new(&*name_str).to_owned(), }; + if call.req::(engine_state, stack, 1).is_err() && expanded_name.is_dir() { + expanded_name = + expand_path_with(stack, engine_state, expanded_name, cwd.clone(), false); + // do_auto_cd() report ShellError via report_shell_error() and does not return error. + do_auto_cd( + expanded_name, + cwd.to_string_lossy().to_string(), + stack, + engine_state, + Span::unknown(), + ); + return Ok(PipelineData::Empty); + } + // On Windows, the user could have run the cmd.exe built-in "assoc" command // Example: "assoc .nu=nuscript" and then run the cmd.exe built-in "ftype" command // Example: "ftype nuscript=C:\path\to\nu.exe '%1' %*" and then added the nushell diff --git a/crates/nu-command/tests/commands/ls.rs b/crates/nu-command/tests/commands/ls.rs index 416a6aaf7f..b4e8b57c1e 100644 --- a/crates/nu-command/tests/commands/ls.rs +++ b/crates/nu-command/tests/commands/ls.rs @@ -1,6 +1,8 @@ +use nu_path::assert_path_eq; use nu_test_support::fs::Stub::EmptyFile; use nu_test_support::playground::Playground; use nu_test_support::{nu, pipeline}; +use std::path::Path; #[test] fn lists_regular_files() { @@ -862,3 +864,32 @@ fn consistent_list_order() { assert_eq!(no_arg.out, with_arg.out); }) } + +#[cfg(windows)] +#[test] +fn support_pwd_per_drive() { + Playground::setup("ls_test_pwd_per_drive", |dirs, sandbox| { + sandbox.mkdir("test_folder"); + let _actual = nu!( + cwd: dirs.test(), + r#" + subst X: /D | touch out + subst X: test_folder + x: + mkdir test_folder_on_x + cd - + x:test_folder_on_x\ + touch test_file_on_x.txt + cd - + ls x: | get name | save test_folder\result.out.txt + subst X: /D | touch out + open test_folder\result.out.txt + "# + ); + eprintln!("std out: {}", _actual.out); + assert_path_eq!(_actual.out, r"X:\test_folder_on_x\test_file_on_x.txt"); + //assert!(_actual.err.is_empty()); + //assert!(dirs.test.join("test_folder").exists()); + //assert!(!dirs.test.join("test_folder").join("test_file.txt").exists()); + }) +}