mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
chore: Update completest
This commit is contained in:
parent
6e862f5617
commit
419f231dad
12 changed files with 142 additions and 41 deletions
24
Cargo.lock
generated
24
Cargo.lock
generated
|
@ -505,6 +505,7 @@ dependencies = [
|
|||
"clap 4.4.17",
|
||||
"clap_lex 0.6.0",
|
||||
"completest",
|
||||
"completest-pty",
|
||||
"is_executable",
|
||||
"pathdiff",
|
||||
"shlex",
|
||||
|
@ -529,6 +530,7 @@ dependencies = [
|
|||
"clap 4.4.17",
|
||||
"clap_complete",
|
||||
"completest",
|
||||
"completest-nu",
|
||||
"snapbox",
|
||||
]
|
||||
|
||||
|
@ -599,10 +601,17 @@ checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
|
|||
|
||||
[[package]]
|
||||
name = "completest"
|
||||
version = "0.2.0"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ab8199863e4b9c23d9668cddbcd0c977cad43dc70d921118e31aa3f1f312ef4b"
|
||||
checksum = "8229e041ca8f8130ad7f0ce1afb9cfdb3033de7fd548e6422dbb2f4f12184f41"
|
||||
|
||||
[[package]]
|
||||
name = "completest-nu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9432569c7de3f7aed70f775c455b3d49328449edc0d3655920189cc269f7f93b"
|
||||
dependencies = [
|
||||
"completest",
|
||||
"dunce",
|
||||
"nu-cli",
|
||||
"nu-cmd-lang",
|
||||
|
@ -610,8 +619,17 @@ dependencies = [
|
|||
"nu-parser",
|
||||
"nu-protocol",
|
||||
"nu-test-support",
|
||||
"ptyprocess",
|
||||
"reedline",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "completest-pty"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7c3a1a4d91c06d649d8b3db8d1ff8712623c7a5642d7aad74bb122522ac9e055"
|
||||
dependencies = [
|
||||
"completest",
|
||||
"ptyprocess",
|
||||
"vt100",
|
||||
]
|
||||
|
||||
|
|
|
@ -45,7 +45,8 @@ unicode-xid = { version = "0.2.2", optional = true }
|
|||
snapbox = { version = "0.4.15", features = ["diff", "path", "examples"] }
|
||||
# Cutting out `filesystem` feature
|
||||
trycmd = { version = "0.14.19", default-features = false, features = ["color-auto", "diff", "examples"] }
|
||||
completest = "0.2.0"
|
||||
completest = "0.4.0"
|
||||
completest-pty = "0.4.0"
|
||||
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "derive", "help"] }
|
||||
|
||||
[[example]]
|
||||
|
|
|
@ -141,7 +141,7 @@ fn subcommand_last() {
|
|||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn register_completion() {
|
||||
common::register_example("static", "exhaustive", completest::Shell::Bash);
|
||||
common::register_example::<completest_pty::BashRuntimeBuilder>("static", "exhaustive");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -152,7 +152,8 @@ fn complete() {
|
|||
}
|
||||
|
||||
let term = completest::Term::new();
|
||||
let mut runtime = common::load_runtime("static", "exhaustive", completest::Shell::Bash);
|
||||
let mut runtime =
|
||||
common::load_runtime::<completest_pty::BashRuntimeBuilder>("static", "exhaustive");
|
||||
|
||||
let input = "exhaustive \t\t";
|
||||
let expected = r#"%
|
||||
|
@ -165,5 +166,5 @@ fn complete() {
|
|||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn register_dynamic_completion() {
|
||||
common::register_example("dynamic", "exhaustive", completest::Shell::Bash);
|
||||
common::register_example::<completest_pty::BashRuntimeBuilder>("dynamic", "exhaustive");
|
||||
}
|
||||
|
|
|
@ -296,11 +296,13 @@ pub fn assert_matches_path(
|
|||
.matches_path(expected_path, buf);
|
||||
}
|
||||
|
||||
pub fn register_example(context: &str, name: &str, shell: completest::Shell) {
|
||||
pub fn register_example<R: completest::RuntimeBuilder>(context: &str, name: &str) {
|
||||
use completest::Runtime as _;
|
||||
|
||||
let scratch = snapbox::path::PathFixture::mutable_temp().unwrap();
|
||||
let scratch_path = scratch.path().unwrap();
|
||||
|
||||
let shell_name = shell.name();
|
||||
let shell_name = R::name();
|
||||
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/snapshots/home")
|
||||
.join(context)
|
||||
|
@ -341,7 +343,7 @@ pub fn register_example(context: &str, name: &str, shell: completest::Shell) {
|
|||
let registration = std::str::from_utf8(®istration.stdout).unwrap();
|
||||
assert!(!registration.is_empty());
|
||||
|
||||
let mut runtime = shell.init(bin_root, scratch_path.to_owned()).unwrap();
|
||||
let mut runtime = R::new(bin_root, scratch_path.to_owned()).unwrap();
|
||||
|
||||
runtime.register(name, registration).unwrap();
|
||||
|
||||
|
@ -350,12 +352,14 @@ pub fn register_example(context: &str, name: &str, shell: completest::Shell) {
|
|||
scratch.close().unwrap();
|
||||
}
|
||||
|
||||
pub fn load_runtime(
|
||||
pub fn load_runtime<R: completest::RuntimeBuilder>(
|
||||
context: &str,
|
||||
name: &str,
|
||||
shell: completest::Shell,
|
||||
) -> Box<dyn completest::Runtime> {
|
||||
let shell_name = shell.name();
|
||||
) -> Box<dyn completest::Runtime>
|
||||
where
|
||||
<R as completest::RuntimeBuilder>::Runtime: 'static,
|
||||
{
|
||||
let shell_name = R::name();
|
||||
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/snapshots/home")
|
||||
.join(context)
|
||||
|
@ -382,11 +386,11 @@ pub fn load_runtime(
|
|||
println!("Compiled");
|
||||
let bin_root = bin_path.parent().unwrap().to_owned();
|
||||
|
||||
let runtime = shell.with_home(bin_root, home).unwrap();
|
||||
let runtime = R::with_home(bin_root, home).unwrap();
|
||||
|
||||
Box::new(ScratchRuntime {
|
||||
_scratch: scratch,
|
||||
runtime,
|
||||
runtime: Box::new(runtime),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -123,7 +123,7 @@ fn subcommand_last() {
|
|||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn register_completion() {
|
||||
common::register_example("static", "exhaustive", completest::Shell::Elvish);
|
||||
common::register_example::<completest_pty::ElvishRuntimeBuilder>("static", "exhaustive");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -134,7 +134,8 @@ fn complete() {
|
|||
}
|
||||
|
||||
let term = completest::Term::new();
|
||||
let mut runtime = common::load_runtime("static", "exhaustive", completest::Shell::Elvish);
|
||||
let mut runtime =
|
||||
common::load_runtime::<completest_pty::ElvishRuntimeBuilder>("static", "exhaustive");
|
||||
|
||||
let input = "exhaustive \t";
|
||||
let expected = r#"% exhaustive --generate
|
||||
|
|
|
@ -123,7 +123,7 @@ fn subcommand_last() {
|
|||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn register_completion() {
|
||||
common::register_example("static", "exhaustive", completest::Shell::Fish);
|
||||
common::register_example::<completest_pty::FishRuntimeBuilder>("static", "exhaustive");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -134,7 +134,8 @@ fn complete() {
|
|||
}
|
||||
|
||||
let term = completest::Term::new();
|
||||
let mut runtime = common::load_runtime("static", "exhaustive", completest::Shell::Fish);
|
||||
let mut runtime =
|
||||
common::load_runtime::<completest_pty::FishRuntimeBuilder>("static", "exhaustive");
|
||||
|
||||
let input = "exhaustive \t";
|
||||
let expected = r#"% exhaustive
|
||||
|
@ -153,7 +154,7 @@ bash (bash (shell)) fish (fish shell) zsh (zsh shell)"#;
|
|||
#[cfg(all(unix, feature = "unstable-dynamic"))]
|
||||
#[test]
|
||||
fn register_dynamic() {
|
||||
common::register_example("dynamic", "exhaustive", completest::Shell::Fish);
|
||||
common::register_example::<completest_pty::FishRuntimeBuilder>("dynamic", "exhaustive");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -164,7 +165,8 @@ fn complete_dynamic() {
|
|||
}
|
||||
|
||||
let term = completest::Term::new();
|
||||
let mut runtime = common::load_runtime("dynamic", "exhaustive", completest::Shell::Fish);
|
||||
let mut runtime =
|
||||
common::load_runtime::<completest_pty::FishRuntimeBuilder>("dynamic", "exhaustive");
|
||||
|
||||
let input = "exhaustive \t";
|
||||
let expected = r#"% exhaustive
|
||||
|
|
|
@ -123,7 +123,7 @@ fn subcommand_last() {
|
|||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn register_completion() {
|
||||
common::register_example("static", "exhaustive", completest::Shell::Zsh);
|
||||
common::register_example::<completest_pty::ZshRuntimeBuilder>("static", "exhaustive");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -134,7 +134,8 @@ fn complete() {
|
|||
}
|
||||
|
||||
let term = completest::Term::new();
|
||||
let mut runtime = common::load_runtime("static", "exhaustive", completest::Shell::Zsh);
|
||||
let mut runtime =
|
||||
common::load_runtime::<completest_pty::ZshRuntimeBuilder>("static", "exhaustive");
|
||||
|
||||
let input = "exhaustive \t";
|
||||
let expected = r#"% exhaustive
|
||||
|
|
|
@ -37,4 +37,5 @@ clap_complete = { path = "../clap_complete", version = "4.0.0" }
|
|||
[dev-dependencies]
|
||||
snapbox = { version = "0.4.15", features = ["diff", "examples", "path"] }
|
||||
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "help"] }
|
||||
completest = { version = "0.2.0", features = ["nu"] }
|
||||
completest = "0.4.0"
|
||||
completest-nu = "0.4.0"
|
||||
|
|
|
@ -258,13 +258,16 @@ pub fn assert_matches_path(
|
|||
.matches_path(expected_path, buf);
|
||||
}
|
||||
|
||||
pub fn register_example(name: &str, shell: completest::Shell) {
|
||||
pub fn register_example<R: completest::RuntimeBuilder>(context: &str, name: &str) {
|
||||
use completest::Runtime as _;
|
||||
|
||||
let scratch = snapbox::path::PathFixture::mutable_temp().unwrap();
|
||||
let scratch_path = scratch.path().unwrap();
|
||||
|
||||
let shell_name = shell.name();
|
||||
let shell_name = R::name();
|
||||
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/snapshots/home")
|
||||
.join(context)
|
||||
.join(name)
|
||||
.join(shell_name);
|
||||
println!("Compiling");
|
||||
|
@ -275,10 +278,17 @@ pub fn register_example(name: &str, shell: completest::Shell) {
|
|||
println!("Compiled");
|
||||
let bin_root = bin_path.parent().unwrap().to_owned();
|
||||
|
||||
let registration = std::process::Command::new(&bin_path)
|
||||
.arg(format!("--generate={shell_name}"))
|
||||
.output()
|
||||
.unwrap();
|
||||
let mut registration = std::process::Command::new(&bin_path);
|
||||
match context {
|
||||
"static" => registration.args([format!("--generate={shell_name}")]),
|
||||
"dynamic" => registration.args([
|
||||
"complete".to_owned(),
|
||||
"--register=-".to_owned(),
|
||||
format!("--shell={shell_name}"),
|
||||
]),
|
||||
_ => unreachable!("unsupported context {}", context),
|
||||
};
|
||||
let registration = registration.output().unwrap();
|
||||
assert!(
|
||||
registration.status.success(),
|
||||
"{}",
|
||||
|
@ -287,7 +297,7 @@ pub fn register_example(name: &str, shell: completest::Shell) {
|
|||
let registration = std::str::from_utf8(®istration.stdout).unwrap();
|
||||
assert!(!registration.is_empty());
|
||||
|
||||
let mut runtime = shell.init(bin_root, scratch_path.to_owned()).unwrap();
|
||||
let mut runtime = R::new(bin_root, scratch_path.to_owned()).unwrap();
|
||||
|
||||
runtime.register(name, registration).unwrap();
|
||||
|
||||
|
@ -296,14 +306,23 @@ pub fn register_example(name: &str, shell: completest::Shell) {
|
|||
scratch.close().unwrap();
|
||||
}
|
||||
|
||||
pub fn load_runtime(name: &str, shell: completest::Shell) -> Box<dyn completest::Runtime> {
|
||||
let shell_name = shell.name();
|
||||
pub fn load_runtime<R: completest::RuntimeBuilder>(
|
||||
context: &str,
|
||||
name: &str,
|
||||
) -> Box<dyn completest::Runtime>
|
||||
where
|
||||
<R as completest::RuntimeBuilder>::Runtime: 'static,
|
||||
{
|
||||
let shell_name = R::name();
|
||||
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/snapshots/home")
|
||||
.join(context)
|
||||
.join(name)
|
||||
.join(shell_name);
|
||||
std::fs::create_dir_all(&home).unwrap();
|
||||
let scratch = snapbox::path::PathFixture::immutable(&home);
|
||||
let scratch = snapbox::path::PathFixture::mutable_temp()
|
||||
.unwrap()
|
||||
.with_template(&home)
|
||||
.unwrap();
|
||||
let home = scratch.path().unwrap().to_owned();
|
||||
println!("Compiling");
|
||||
let manifest_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
|
||||
|
@ -313,11 +332,11 @@ pub fn load_runtime(name: &str, shell: completest::Shell) -> Box<dyn completest:
|
|||
println!("Compiled");
|
||||
let bin_root = bin_path.parent().unwrap().to_owned();
|
||||
|
||||
let runtime = shell.with_home(bin_root, home).unwrap();
|
||||
let runtime = R::with_home(bin_root, home).unwrap();
|
||||
|
||||
Box::new(ScratchRuntime {
|
||||
_scratch: scratch,
|
||||
runtime,
|
||||
runtime: Box::new(runtime),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -345,3 +364,56 @@ impl completest::Runtime for ScratchRuntime {
|
|||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_command(command: &str) -> bool {
|
||||
let output = match std::process::Command::new(command)
|
||||
.arg("--version")
|
||||
.output()
|
||||
{
|
||||
Ok(output) => output,
|
||||
Err(e) => {
|
||||
// CI is expected to support all of the commands
|
||||
if is_ci() && cfg!(linux) {
|
||||
panic!(
|
||||
"expected command `{}` to be somewhere in PATH: {}",
|
||||
command, e
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
if !output.status.success() {
|
||||
panic!(
|
||||
"expected command `{}` to be runnable, got error {}:\n\
|
||||
stderr:{}\n\
|
||||
stdout:{}\n",
|
||||
command,
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stderr),
|
||||
String::from_utf8_lossy(&output.stdout)
|
||||
);
|
||||
}
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
println!(
|
||||
"$ {command} --version
|
||||
{}",
|
||||
stdout
|
||||
);
|
||||
if cfg!(target_os = "macos") && stdout.starts_with("GNU bash, version 3") {
|
||||
return false;
|
||||
}
|
||||
if cfg!(target_os = "macos") && command == "zsh" {
|
||||
// HACK: At least on CI, the prompt override is not working
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
/// Whether or not this running in a Continuous Integration environment.
|
||||
fn is_ci() -> bool {
|
||||
// Consider using `tracked_env` instead of option_env! when it is stabilized.
|
||||
// `tracked_env` will handle changes, but not require rebuilding the macro
|
||||
// itself like option_env does.
|
||||
option_env!("CI").is_some() || option_env!("TF_BUILD").is_some()
|
||||
}
|
||||
|
|
|
@ -2,13 +2,13 @@ mod common;
|
|||
|
||||
#[test]
|
||||
fn register_completion() {
|
||||
common::register_example("test", completest::Shell::Nu);
|
||||
common::register_example::<completest_nu::NuRuntimeBuilder>("static", "test");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion() {
|
||||
let term = completest::Term::new();
|
||||
let mut runtime = common::load_runtime("test", completest::Shell::Nu);
|
||||
let mut runtime = common::load_runtime::<completest_nu::NuRuntimeBuilder>("static", "test");
|
||||
|
||||
let input = "test -\t";
|
||||
let expected = r#"% test -
|
||||
|
@ -41,7 +41,7 @@ fn completion() {
|
|||
#[test]
|
||||
fn completion_value_hint() {
|
||||
let term = completest::Term::new();
|
||||
let mut runtime = common::load_runtime("test", completest::Shell::Nu);
|
||||
let mut runtime = common::load_runtime::<completest_nu::NuRuntimeBuilder>("static", "test");
|
||||
|
||||
let input = "test hint -\t";
|
||||
let expected = r#"% test hint -
|
||||
|
|
Loading…
Reference in a new issue