|
| 1 | +#![cfg(unix)] |
| 2 | + |
| 3 | +use cargo_test_support::cargo_test; |
| 4 | +use completest_pty::Runtime; |
| 5 | +use snapbox::assert_data_eq; |
| 6 | + |
| 7 | +#[cargo_test] |
| 8 | +fn bash() { |
| 9 | + // HACK: At least on CI, bash is not working on macOS |
| 10 | + if cfg!(target_os = "macos") { |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + let input = "cargo \t\t"; |
| 15 | + let expected = snapbox::str![ |
| 16 | + "% |
| 17 | +--version --help check install read-manifest update |
| 18 | +--list -V clean locate-project remove vendor |
| 19 | +--explain -v config login report verify-project |
| 20 | +--verbose -q doc logout run version |
| 21 | +--quiet -C fetch metadata rustc yank |
| 22 | +--color -Z fix new rustdoc |
| 23 | +--locked -h generate-lockfile owner search |
| 24 | +--offline add help package test |
| 25 | +--frozen bench info pkgid tree |
| 26 | +--config build init publish uninstall " |
| 27 | + ]; |
| 28 | + let actual = complete(input, "bash"); |
| 29 | + assert_data_eq!(actual, expected); |
| 30 | +} |
| 31 | + |
| 32 | +#[cargo_test] |
| 33 | +fn elvish() { |
| 34 | + // HACK: At least on CI, elvish is not working on macOS |
| 35 | + if cfg!(target_os = "macos") { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + let input = "cargo \t\t"; |
| 40 | + let expected = snapbox::str![ |
| 41 | + "% cargo --config |
| 42 | + COMPLETING argument |
| 43 | +--color --version check install read-manifest update |
| 44 | +--config -C clean locate-project remove vendor |
| 45 | +--explain -V config login report verify-project |
| 46 | +--frozen -Z doc logout run version |
| 47 | +--help -h fetch metadata rustc yank |
| 48 | +--list -q fix new rustdoc |
| 49 | +--locked -v generate-lockfile owner search |
| 50 | +--offline add help package test |
| 51 | +--quiet bench info pkgid tree |
| 52 | +--verbose build init publish uninstall " |
| 53 | + ]; |
| 54 | + let actual = complete(input, "elvish"); |
| 55 | + assert_data_eq!(actual, expected); |
| 56 | +} |
| 57 | + |
| 58 | +#[cargo_test] |
| 59 | +fn fish() { |
| 60 | + // HACK: At least on CI, fish is not working on macOS |
| 61 | + if cfg!(target_os = "macos") { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + let input = "cargo \t\t"; |
| 66 | + let expected = snapbox::str![ |
| 67 | + "% cargo |
| 68 | +--version (Print version info and exit) |
| 69 | +--list (List installed commands) |
| 70 | +--explain (Provide a detailed explanation of a rustc error message) |
| 71 | +--verbose (Use verbose output (-vv very verbose/build.rs output)) |
| 72 | +--quiet (Do not print cargo log messages) |
| 73 | +--color (Coloring: auto, always, never) |
| 74 | +--locked (Assert that `Cargo.lock` will remain unchanged) |
| 75 | +--offline (Run without accessing the network) |
| 76 | +--frozen (Equivalent to specifying both --locked and --offline) |
| 77 | +--config (Override a configuration value) |
| 78 | +--help (Print help) |
| 79 | +-V (Print version info and exit) |
| 80 | +-v (Use verbose output (-vv very verbose/build.rs output)) |
| 81 | +-q (Do not print cargo log messages) |
| 82 | +-C (Change to DIRECTORY before doing anything (nightly-only)) |
| 83 | +-Z (Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details) |
| 84 | +-h (Print help) |
| 85 | +add (Add dependencies to a Cargo.toml manifest file) |
| 86 | +bench (Execute all benchmarks of a local package) |
| 87 | +build (Compile a local package and all of its dependencies) |
| 88 | +check (Check a local package and all of its dependencies for errors) |
| 89 | +clean (Remove artifacts that cargo has generated in the past) |
| 90 | +config (Inspect configuration values) |
| 91 | +doc (Build a package's documentation) |
| 92 | +fetch (Fetch dependencies of a package from the network) |
| 93 | +fix (Automatically fix lint warnings reported by rustc) |
| 94 | +generate-lockfile (Generate the lockfile for a package) |
| 95 | +help (Displays help for a cargo subcommand) |
| 96 | +info (Display information about a package in the registry) |
| 97 | +init (Create a new cargo package in an existing directory) |
| 98 | +install (Install a Rust binary) |
| 99 | +locate-project (Print a JSON representation of a Cargo.toml file's location) |
| 100 | +login (Log in to a registry.) |
| 101 | +logout (Remove an API token from the registry locally) |
| 102 | +metadata (Output the resolved dependencies of a package, the concrete used versions including overrides, in machine-r…) |
| 103 | +new (Create a new cargo package at <path>) |
| 104 | +owner (Manage the owners of a crate on the registry) |
| 105 | +package (Assemble the local package into a distributable tarball) |
| 106 | +pkgid (Print a fully qualified package specification) |
| 107 | +publish (Upload a package to the registry) |
| 108 | +read-manifest (Print a JSON representation of a Cargo.toml manifest.) |
| 109 | +remove (Remove dependencies from a Cargo.toml manifest file) |
| 110 | +report (Generate and display various kinds of reports) |
| 111 | +run (Run a binary or example of the local package) |
| 112 | +rustc (Compile a package, and pass extra options to the compiler) |
| 113 | +rustdoc (Build a package's documentation, using specified custom flags.) |
| 114 | +search (Search packages in the registry. Default registry is crates.io) |
| 115 | +test (Execute all unit and integration tests and build examples of a local package) |
| 116 | +tree (Display a tree visualization of a dependency graph) |
| 117 | +uninstall (Remove a Rust binary) |
| 118 | +update (Update dependencies as recorded in the local lock file) |
| 119 | +vendor (Vendor all dependencies for a project locally) |
| 120 | +verify-project (Check correctness of crate manifest) |
| 121 | +version (Show version information) |
| 122 | +yank (Remove a pushed crate from the index)"]; |
| 123 | + |
| 124 | + let actual = complete(input, "fish"); |
| 125 | + assert_data_eq!(actual, expected); |
| 126 | +} |
| 127 | + |
| 128 | +#[cargo_test] |
| 129 | +fn zsh() { |
| 130 | + let input = "cargo \t\t"; |
| 131 | + let expected = snapbox::str![ |
| 132 | + "% cargo |
| 133 | +--color --version check install read-manifest update |
| 134 | +--config -C clean locate-project remove vendor |
| 135 | +--explain -V config login report verify-project |
| 136 | +--frozen -Z doc logout run version |
| 137 | +--help -h fetch metadata rustc yank |
| 138 | +--list -q fix new rustdoc |
| 139 | +--locked -v generate-lockfile owner search |
| 140 | +--offline add help package test |
| 141 | +--quiet bench info pkgid tree |
| 142 | +--verbose build init publish uninstall " |
| 143 | + ]; |
| 144 | + let actual = complete(input, "zsh"); |
| 145 | + assert_data_eq!(actual, expected); |
| 146 | +} |
| 147 | + |
| 148 | +fn complete(input: &str, shell: &str) -> String { |
| 149 | + let shell = shell.into(); |
| 150 | + |
| 151 | + // Load the runtime |
| 152 | + let mut runtime = match shell { |
| 153 | + "bash" => load_runtime::<completest_pty::BashRuntimeBuilder>("bash"), |
| 154 | + "elvish" => load_runtime::<completest_pty::ElvishRuntimeBuilder>("elvish"), |
| 155 | + "fish" => load_runtime::<completest_pty::FishRuntimeBuilder>("fish"), |
| 156 | + "zsh" => load_runtime::<completest_pty::ZshRuntimeBuilder>("zsh"), |
| 157 | + _ => panic!("Unsupported shell: {}", shell), |
| 158 | + }; |
| 159 | + |
| 160 | + // Exec the completion |
| 161 | + let term = completest_pty::Term::new(); |
| 162 | + let actual = runtime.complete(input, &term).unwrap(); |
| 163 | + |
| 164 | + actual |
| 165 | +} |
| 166 | + |
| 167 | +// Return the scratch directory to keep it not being dropped |
| 168 | +fn load_runtime<R: completest_pty::RuntimeBuilder>(shell: &str) -> Box<dyn completest_pty::Runtime> |
| 169 | +where |
| 170 | + <R as completest_pty::RuntimeBuilder>::Runtime: 'static, |
| 171 | +{ |
| 172 | + let home = cargo_test_support::paths::home(); |
| 173 | + |
| 174 | + let bin_path = cargo_test_support::cargo_exe(); |
| 175 | + let bin_root = bin_path.parent().unwrap().to_owned(); |
| 176 | + |
| 177 | + let mut runtime = Box::new(R::new(bin_root, home).unwrap()); |
| 178 | + |
| 179 | + match shell { |
| 180 | + "bash" => runtime |
| 181 | + .register("", "source <(CARGO_COMPLETE=bash cargo)") |
| 182 | + .unwrap(), |
| 183 | + "elvish" => runtime |
| 184 | + .register("", "eval (E:CARGO_COMPLETE=elvish cargo | slurp)") |
| 185 | + .unwrap(), |
| 186 | + "fish" => runtime |
| 187 | + .register("cargo", "source (CARGO_COMPLETE=fish cargo | psub)") |
| 188 | + .unwrap(), |
| 189 | + "zsh" => runtime |
| 190 | + .register( |
| 191 | + "cargo", |
| 192 | + "#compdef cargo |
| 193 | +source <(CARGO_COMPLETE=zsh cargo)", |
| 194 | + ) |
| 195 | + .unwrap(), |
| 196 | + _ => panic!("Unsupported shell: {}", shell), |
| 197 | + } |
| 198 | + |
| 199 | + runtime |
| 200 | +} |
0 commit comments