Skip to content

Commit

Permalink
test(auto_complete): Add codebase of auto-complete
Browse files Browse the repository at this point in the history
  • Loading branch information
shannmu committed Sep 4, 2024
1 parent 40b6638 commit 19dcced
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 0 deletions.
95 changes: 95 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ unicode-width.workspace = true
url.workspace = true
walkdir.workspace = true
supports-unicode = "3.0.0"
completest = "0.4.2"
completest-pty = "0.5.3"

[target.'cfg(target_has_atomic = "64")'.dependencies]
tracing-chrome.workspace = true
Expand Down
68 changes: 68 additions & 0 deletions tests/testsuite/auto_complete/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
pub(crate) fn complete(input: &str, shell: impl Into<Shell>) -> String {
let shell = shell.into();

// Load the runtime
let (mut runtime, _scratch) = match shell {
Shell::Bash => load_runtime::<completest_pty::BashRuntimeBuilder>(),
Shell::Zsh => load_runtime::<completest_pty::ZshRuntimeBuilder>(),
};

// Exec the completion
let term = completest::Term::new();
let actual = runtime.complete(input, &term).unwrap();

actual
}

// Return the scratch directory to keep it not being dropped
pub(crate) fn load_runtime<R: completest::RuntimeBuilder>(
) -> (Box<dyn completest::Runtime>, snapbox::dir::DirRoot)
where
<R as completest::RuntimeBuilder>::Runtime: 'static,
{
let shell_name = R::name();

let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/testsuite/auto_complete/snapshots")
.join(shell_name);

let scratch = snapbox::dir::DirRoot::mutable_temp()
.unwrap()
.with_template(&home)
.unwrap();

let home = scratch.path().unwrap().to_owned();

let bin_path = cargo_test_support::cargo_exe();
let bin_root = bin_path.parent().unwrap().to_owned();

let runtime = Box::new(R::with_home(bin_root, home).unwrap());

(runtime, scratch)
}

#[non_exhaustive]
pub(crate) enum Shell {
Bash,
Zsh,
}

impl From<String> for Shell {
fn from(value: String) -> Self {
match value.as_str() {
"bash" => Shell::Bash,
"zsh" => Shell::Zsh,
_ => panic!("Unsupported shell"),
}
}
}

impl From<&str> for Shell {
fn from(value: &str) -> Self {
match value {
"bash" => Shell::Bash,
"zsh" => Shell::Zsh,
_ => panic!("Unsupported shell"),
}
}
}
1 change: 1 addition & 0 deletions tests/testsuite/auto_complete/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod common;
4 changes: 4 additions & 0 deletions tests/testsuite/auto_complete/snapshots/bash/.bashrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PS1='% '
. /etc/bash_completion

source <(CARGO_COMPLETE=bash cargo)
5 changes: 5 additions & 0 deletions tests/testsuite/auto_complete/snapshots/zsh/.zshenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fpath=($fpath $ZDOTDIR/zsh)
autoload -U +X compinit && compinit -u # bypass compaudit security checking
precmd_functions="" # avoid the prompt being overwritten
PS1='%% '
PROMPT='%% '
2 changes: 2 additions & 0 deletions tests/testsuite/auto_complete/snapshots/zsh/zsh/_cargo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#compdef cargo
source <(CARGO_COMPLETE=zsh cargo)
1 change: 1 addition & 0 deletions tests/testsuite/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod advanced_env;
mod alt_registry;
mod artifact_dep;
mod artifact_dir;
mod auto_complete;
mod bad_config;
mod bad_manifest_path;
mod bench;
Expand Down

0 comments on commit 19dcced

Please sign in to comment.