From eded64a13bfcaa9991a3773229bb564fb5d89844 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 21 Aug 2024 15:04:57 -0500 Subject: [PATCH 1/2] test(complete): Verify PathCompleter::dir --- clap_complete/tests/testsuite/engine.rs | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/clap_complete/tests/testsuite/engine.rs b/clap_complete/tests/testsuite/engine.rs index 56410a22aff..6f4d4fd5d0d 100644 --- a/clap_complete/tests/testsuite/engine.rs +++ b/clap_complete/tests/testsuite/engine.rs @@ -616,6 +616,40 @@ d_dir/ ); } +#[test] +fn suggest_value_path_dir() { + let testdir = snapbox::dir::DirRoot::mutable_temp().unwrap(); + let testdir_path = testdir.path().unwrap(); + fs::write(testdir_path.join("a_file"), "").unwrap(); + fs::write(testdir_path.join("b_file"), "").unwrap(); + fs::create_dir_all(testdir_path.join("c_dir")).unwrap(); + fs::create_dir_all(testdir_path.join("d_dir")).unwrap(); + + let mut cmd = Command::new("dynamic") + .arg( + clap::Arg::new("input") + .long("input") + .short('i') + .add(ArgValueCompleter::new( + PathCompleter::dir().current_dir(testdir_path.to_owned()), + )), + ) + .args_conflicts_with_subcommands(true); + + assert_data_eq!( + complete!(cmd, "--input [TAB]", current_dir = Some(testdir_path)), + snapbox::str![[r#" +c_dir/ +d_dir/ +"#]], + ); + + assert_data_eq!( + complete!(cmd, "--input c[TAB]", current_dir = Some(testdir_path)), + snapbox::str!["c_dir/"], + ); +} + #[test] fn suggest_custom_arg_value() { fn custom_completer() -> Vec { From 84252b71237d8b8df969b265ec0c0b0f43ba491f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 21 Aug 2024 15:06:46 -0500 Subject: [PATCH 2/2] fix(complete): Allow completing '.' --- clap_complete/src/engine/custom.rs | 4 ++++ clap_complete/tests/testsuite/engine.rs | 1 + 2 files changed, 5 insertions(+) diff --git a/clap_complete/src/engine/custom.rs b/clap_complete/src/engine/custom.rs index 10c15f3806c..51da696fefe 100644 --- a/clap_complete/src/engine/custom.rs +++ b/clap_complete/src/engine/custom.rs @@ -259,6 +259,10 @@ pub(crate) fn complete_path( }; debug!("complete_path: search_root={search_root:?}, prefix={prefix:?}"); + if value_os.is_empty() && is_wanted(&search_root) { + completions.push(".".into()); + } + for entry in std::fs::read_dir(&search_root) .ok() .into_iter() diff --git a/clap_complete/tests/testsuite/engine.rs b/clap_complete/tests/testsuite/engine.rs index 6f4d4fd5d0d..a4211183902 100644 --- a/clap_complete/tests/testsuite/engine.rs +++ b/clap_complete/tests/testsuite/engine.rs @@ -639,6 +639,7 @@ fn suggest_value_path_dir() { assert_data_eq!( complete!(cmd, "--input [TAB]", current_dir = Some(testdir_path)), snapbox::str![[r#" +. c_dir/ d_dir/ "#]],