test: Add test codebase for shell completions

This commit is contained in:
shannmu 2024-09-09 23:36:10 +08:00
parent d0cb869167
commit f25806c472
4 changed files with 469 additions and 225 deletions

633
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -36,7 +36,9 @@ cargo-util = { version = "0.2.14", path = "crates/cargo-util" }
cargo-util-schemas = { version = "0.6.0", path = "crates/cargo-util-schemas" }
cargo_metadata = "0.18.1"
clap = "4.5.11"
clap_complete = { version = "4.5.24", features = ["unstable-dynamic"] }
color-print = "0.3.6"
completest-pty = "0.5.3"
core-foundation = { version = "0.10.0", features = ["mac_os_10_7_support"] }
crates-io = { version = "0.40.4", path = "crates/crates-io" }
criterion = { version = "0.5.1", features = ["html_reports"] }
@ -153,6 +155,7 @@ cargo-platform.workspace = true
cargo-util-schemas.workspace = true
cargo-util.workspace = true
clap = { workspace = true, features = ["wrap_help"] }
clap_complete.workspace = true
color-print.workspace = true
crates-io.workspace = true
curl = { workspace = true, features = ["http2"] }
@ -239,6 +242,7 @@ features = [
[dev-dependencies]
annotate-snippets = { workspace = true, features = ["testing-colors"] }
cargo-test-support.workspace = true
completest-pty.workspace = true
gix = { workspace = true, features = ["revision"] }
same-file.workspace = true
snapbox.workspace = true

View File

@ -166,6 +166,7 @@ mod rustflags;
mod rustup;
mod script;
mod search;
mod shell_completions;
mod shell_quoting;
mod source_replacement;
mod ssh;

View File

@ -0,0 +1,56 @@
use completest_pty::Runtime;
fn complete(input: &str, shell: &str) -> String {
let shell = shell.into();
// Load the runtime
let mut runtime = match shell {
"bash" => load_runtime::<completest_pty::BashRuntimeBuilder>("bash"),
"elvish" => load_runtime::<completest_pty::ElvishRuntimeBuilder>("elvish"),
"fish" => load_runtime::<completest_pty::FishRuntimeBuilder>("fish"),
"zsh" => load_runtime::<completest_pty::ZshRuntimeBuilder>("zsh"),
_ => panic!("Unsupported shell: {}", shell),
};
// Exec the completion
let term = completest_pty::Term::new();
let actual = runtime.complete(input, &term).unwrap();
actual
}
// Return the scratch directory to keep it not being dropped
fn load_runtime<R: completest_pty::RuntimeBuilder>(shell: &str) -> Box<dyn completest_pty::Runtime>
where
<R as completest_pty::RuntimeBuilder>::Runtime: 'static,
{
let home = cargo_test_support::paths::home();
let bin_path = cargo_test_support::cargo_exe();
let bin_root = bin_path.parent().unwrap().to_owned();
let mut runtime = Box::new(R::new(bin_root, home).unwrap());
match shell {
"bash" => runtime
.register("", "source <(CARGO_COMPLETE=bash cargo)")
.unwrap(),
"elvish" => runtime
.register("", "eval (E:CARGO_COMPLETE=elvish cargo | slurp)")
.unwrap(),
"fish" => runtime
.register("cargo", "source (CARGO_COMPLETE=fish cargo | psub)")
.unwrap(),
"zsh" => runtime
.register(
"cargo",
"#compdef cargo
source <(CARGO_COMPLETE=zsh cargo)",
)
.unwrap(),
_ => panic!("Unsupported shell: {}", shell),
}
runtime
}