refactor(complete): Split tests up

This makes them easier to read and and easier to manually invoke
This commit is contained in:
Ed Page 2023-07-20 14:28:09 -05:00
parent 5c362fe5b1
commit 28be38b3a7
4 changed files with 26 additions and 15 deletions

4
Cargo.lock generated
View file

@ -646,9 +646,9 @@ checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "completest"
version = "0.0.2"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf3a6911a195bda4c8bd5dd36360da54081f542c5c64858b27d4de0456996eed"
checksum = "6feacc4465b103a9c3ee1ca2de3fc8336822145bed52ede7cd80f9b32a1ab329"
dependencies = [
"ptyprocess",
"vt100",

View file

@ -43,7 +43,7 @@ unicode-xid = { version = "0.2.2", optional = true }
snapbox = { version = "0.4.11", features = ["diff", "examples"] }
# Cutting out `filesystem` feature
trycmd = { version = "0.14.16", default-features = false, features = ["color-auto", "diff", "examples"] }
completest = "0.0.2"
completest = "0.0.3"
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "derive", "help"] }
[[example]]

View file

@ -1,3 +1,5 @@
PS1='% '
. /etc/bash_completion
_test() {
local i cur prev opts cmd
COMPREPLY=()
@ -904,3 +906,4 @@ _test() {
}
complete -F _test -o nosort -o bashdefault -o default test

View file

@ -138,17 +138,13 @@ fn subcommand_last() {
#[test]
#[cfg(unix)]
fn complete() {
fn register_completion() {
if !has_command("bash") {
return;
}
let shell = "bash";
let home = std::path::Path::new(env!("CARGO_TARGET_TMPDIR"))
.join(format!("clap_complete_{shell}_home"));
let _ = std::fs::remove_dir_all(&home);
let home = std::path::Path::new("tests/snapshots/home/test/bash").to_owned();
let bin_path = snapbox::cmd::compile_example("test", []).unwrap();
let bin_root = bin_path.parent().unwrap().to_owned();
@ -163,20 +159,32 @@ fn complete() {
);
let registration = std::str::from_utf8(&registration.stdout).unwrap();
assert!(!registration.is_empty());
snapbox::Assert::new()
.action_env("SNAPSHOTS")
.normalize_paths(false)
.matches_path(format!("tests/snapshots/test.{shell}"), registration);
let term = completest::Term::new();
let runtime = completest::BashRuntime::new(bin_root, home).unwrap();
runtime.register("test", registration).unwrap();
}
#[test]
#[cfg(unix)]
fn complete() {
if !has_command("bash") {
return;
}
let shell = "bash";
let home = std::path::PathBuf::from(format!("tests/snapshots/home/test/{shell}"));
let bin_path = snapbox::cmd::compile_example("test", []).unwrap();
let bin_root = bin_path.parent().unwrap().to_owned();
let term = completest::Term::new();
let runtime = completest::BashRuntime::with_home(bin_root, home);
let input = "test \t\t";
let expected = r#"%
-h --global --help action value last hint
-V --generate --version quote pacman alias help "#;
let actual = runtime.complete("test \t\t", &term).unwrap();
let actual = runtime.complete(input, &term).unwrap();
snapbox::assert_eq(expected, actual);
}