test case for ls

This commit is contained in:
Zhenping Zhao 2024-12-20 17:48:09 -08:00
parent 1e6df57ba6
commit 3a69e0ed00
6 changed files with 60 additions and 10 deletions

10
Cargo.lock generated
View file

@ -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",

View file

@ -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;

View file

@ -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 = {

View file

@ -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 }

View file

@ -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::<Value>(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

View file

@ -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());
})
}