From 19dcced438b685e1f032ea8962dd4875614ade6b Mon Sep 17 00:00:00 2001 From: shannmu Date: Sun, 25 Aug 2024 00:04:57 +0800 Subject: [PATCH] test(auto_complete): Add codebase of auto-complete --- Cargo.lock | 95 +++++++++++++++++++ Cargo.toml | 2 + tests/testsuite/auto_complete/common.rs | 68 +++++++++++++ tests/testsuite/auto_complete/mod.rs | 1 + .../auto_complete/snapshots/bash/.bashrc | 4 + .../auto_complete/snapshots/zsh/.zshenv | 5 + .../auto_complete/snapshots/zsh/zsh/_cargo | 2 + tests/testsuite/main.rs | 1 + 8 files changed, 178 insertions(+) create mode 100644 tests/testsuite/auto_complete/common.rs create mode 100644 tests/testsuite/auto_complete/mod.rs create mode 100644 tests/testsuite/auto_complete/snapshots/bash/.bashrc create mode 100644 tests/testsuite/auto_complete/snapshots/zsh/.zshenv create mode 100644 tests/testsuite/auto_complete/snapshots/zsh/zsh/_cargo diff --git a/Cargo.lock b/Cargo.lock index 8ef9370bde59..77d6da29c3bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -134,6 +134,12 @@ version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + [[package]] name = "autocfg" version = "1.3.0" @@ -284,6 +290,8 @@ dependencies = [ "cargo-util-schemas", "clap", "color-print", + "completest", + "completest-pty", "crates-io", "curl", "curl-sys", @@ -598,6 +606,23 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" +[[package]] +name = "completest" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "410de1ffe61368aa040f599747584e9e3d19235cf4045be6159edb167ef45ddb" + +[[package]] +name = "completest-pty" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "641f8ea301379c034f4d0b82f243b03dd4f6946c882aa914d47c45060482fb25" +dependencies = [ + "completest", + "ptyprocess", + "vt100", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -2305,6 +2330,15 @@ dependencies = [ "libc", ] +[[package]] +name = "memoffset" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" +dependencies = [ + "autocfg", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -2329,6 +2363,19 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "nix" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" +dependencies = [ + "bitflags 1.3.2", + "cfg-if", + "libc", + "memoffset", + "pin-utils", +] + [[package]] name = "nom" version = "7.1.3" @@ -2651,6 +2698,12 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "pkcs8" version = "0.10.2" @@ -2754,6 +2807,15 @@ dependencies = [ "unarray", ] +[[package]] +name = "ptyprocess" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e05aef7befb11a210468a2d77d978dde2c6381a0381e33beb575e91f57fe8cf" +dependencies = [ + "nix", +] + [[package]] name = "pulldown-cmark" version = "0.11.0" @@ -3766,6 +3828,39 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "vt100" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84cd863bf0db7e392ba3bd04994be3473491b31e66340672af5d11943c6274de" +dependencies = [ + "itoa 1.0.11", + "log", + "unicode-width", + "vte", +] + +[[package]] +name = "vte" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5022b5fbf9407086c180e9557be968742d839e68346af7792b8592489732197" +dependencies = [ + "arrayvec", + "utf8parse", + "vte_generate_state_changes", +] + +[[package]] +name = "vte_generate_state_changes" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e369bee1b05d510a7b4ed645f5faa90619e05437111783ea5848f28d97d3c2e" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "wait-timeout" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 563c0c031134..85dd7729f21e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/tests/testsuite/auto_complete/common.rs b/tests/testsuite/auto_complete/common.rs new file mode 100644 index 000000000000..5d6cc9f9a8df --- /dev/null +++ b/tests/testsuite/auto_complete/common.rs @@ -0,0 +1,68 @@ +pub(crate) fn complete(input: &str, shell: impl Into) -> String { + let shell = shell.into(); + + // Load the runtime + let (mut runtime, _scratch) = match shell { + Shell::Bash => load_runtime::(), + Shell::Zsh => load_runtime::(), + }; + + // 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( +) -> (Box, snapbox::dir::DirRoot) +where + ::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 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"), + } + } +} diff --git a/tests/testsuite/auto_complete/mod.rs b/tests/testsuite/auto_complete/mod.rs new file mode 100644 index 000000000000..34994bf5afcc --- /dev/null +++ b/tests/testsuite/auto_complete/mod.rs @@ -0,0 +1 @@ +pub mod common; diff --git a/tests/testsuite/auto_complete/snapshots/bash/.bashrc b/tests/testsuite/auto_complete/snapshots/bash/.bashrc new file mode 100644 index 000000000000..bf4a07b1c5ad --- /dev/null +++ b/tests/testsuite/auto_complete/snapshots/bash/.bashrc @@ -0,0 +1,4 @@ +PS1='% ' +. /etc/bash_completion + +source <(CARGO_COMPLETE=bash cargo) \ No newline at end of file diff --git a/tests/testsuite/auto_complete/snapshots/zsh/.zshenv b/tests/testsuite/auto_complete/snapshots/zsh/.zshenv new file mode 100644 index 000000000000..e94d2a08c51a --- /dev/null +++ b/tests/testsuite/auto_complete/snapshots/zsh/.zshenv @@ -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='%% ' \ No newline at end of file diff --git a/tests/testsuite/auto_complete/snapshots/zsh/zsh/_cargo b/tests/testsuite/auto_complete/snapshots/zsh/zsh/_cargo new file mode 100644 index 000000000000..216bf96a756e --- /dev/null +++ b/tests/testsuite/auto_complete/snapshots/zsh/zsh/_cargo @@ -0,0 +1,2 @@ +#compdef cargo +source <(CARGO_COMPLETE=zsh cargo) \ No newline at end of file diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index 0c0434271ddb..a56628600ddd 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -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;