From 553801d5b69b7d3d5c5a35eb2ce4154a353506b1 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 4 Sep 2024 09:47:21 -0500 Subject: [PATCH] refactor(complete): Make it easier to compare tests --- clap_complete/tests/testsuite/bash.rs | 52 +++++++++++-------------- clap_complete/tests/testsuite/elvish.rs | 32 +++++++-------- clap_complete/tests/testsuite/fish.rs | 32 +++++++-------- clap_complete/tests/testsuite/zsh.rs | 32 +++++++-------- 4 files changed, 68 insertions(+), 80 deletions(-) diff --git a/clap_complete/tests/testsuite/bash.rs b/clap_complete/tests/testsuite/bash.rs index 9448018b3b9e..249df2767b25 100644 --- a/clap_complete/tests/testsuite/bash.rs +++ b/clap_complete/tests/testsuite/bash.rs @@ -2,6 +2,9 @@ use snapbox::assert_data_eq; use crate::common; +const CMD: &str = "bash"; +type RuntimeBuilder = completest_pty::BashRuntimeBuilder; + #[test] fn basic() { let name = "my-app"; @@ -138,19 +141,18 @@ fn subcommand_last() { #[test] #[cfg(unix)] fn register_completion() { - common::register_example::("static", "exhaustive"); + common::register_example::("static", "exhaustive"); } #[test] #[cfg(unix)] fn complete_static_toplevel() { - if !common::has_command("bash") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("static", "exhaustive"); + let mut runtime = common::load_runtime::("static", "exhaustive"); let input = "exhaustive \t\t"; let expected = snapbox::str![[r#" @@ -165,13 +167,12 @@ fn complete_static_toplevel() { #[test] #[cfg(unix)] fn complete_static_file_hint() { - if !common::has_command("bash") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("static", "exhaustive"); + let mut runtime = common::load_runtime::("static", "exhaustive"); let input = "exhaustive hint --file test\t"; let expected = snapbox::str!["exhaustive hint --file test % exhaustive hint --file tests/"]; @@ -182,13 +183,12 @@ fn complete_static_file_hint() { #[test] #[cfg(unix)] fn complete_static_dir_hint() { - if !common::has_command("bash") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("static", "exhaustive"); + let mut runtime = common::load_runtime::("static", "exhaustive"); { use std::fs::File; @@ -235,13 +235,12 @@ fn complete_static_dir_hint() { #[test] #[cfg(unix)] fn complete_file_hint_escaping() { - if !common::has_command("bash") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("static", "exhaustive"); + let mut runtime = common::load_runtime::("static", "exhaustive"); { use std::fs::File; @@ -265,13 +264,12 @@ fn complete_file_hint_escaping() { #[test] #[cfg(unix)] fn complete_static_other_hint() { - if !common::has_command("bash") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("static", "exhaustive"); + let mut runtime = common::load_runtime::("static", "exhaustive"); let input = "exhaustive hint --other \t"; let expected = snapbox::str!["exhaustive hint --other % exhaustive hint --other "]; @@ -282,19 +280,18 @@ fn complete_static_other_hint() { #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn register_dynamic_env() { - common::register_example::("dynamic-env", "exhaustive"); + common::register_example::("dynamic-env", "exhaustive"); } #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_toplevel() { - if !common::has_command("bash") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive \t\t"; let expected = snapbox::str![[r#" @@ -309,13 +306,12 @@ fn complete_dynamic_env_toplevel() { #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_quoted_help() { - if !common::has_command("bash") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive quote \t\t"; let expected = snapbox::str![[r#" @@ -332,13 +328,12 @@ fn complete_dynamic_env_quoted_help() { #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_option_value() { - if !common::has_command("bash") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive action --choice=\t\t"; let expected = snapbox::str!["% "]; @@ -354,13 +349,12 @@ fn complete_dynamic_env_option_value() { #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_quoted_value() { - if !common::has_command("bash") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive quote --choice \t\t"; let expected = snapbox::str![[r#" diff --git a/clap_complete/tests/testsuite/elvish.rs b/clap_complete/tests/testsuite/elvish.rs index b0018a1b6688..53a89362a1ca 100644 --- a/clap_complete/tests/testsuite/elvish.rs +++ b/clap_complete/tests/testsuite/elvish.rs @@ -1,6 +1,9 @@ use crate::common; use snapbox::assert_data_eq; +const CMD: &str = "elvish"; +type RuntimeBuilder = completest_pty::ElvishRuntimeBuilder; + #[test] fn basic() { let name = "my-app"; @@ -137,19 +140,18 @@ fn subcommand_last() { #[test] #[cfg(unix)] fn register_completion() { - common::register_example::("static", "exhaustive"); + common::register_example::("static", "exhaustive"); } #[test] #[cfg(unix)] fn complete_static_toplevel() { - if !common::has_command("elvish") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("static", "exhaustive"); + let mut runtime = common::load_runtime::("static", "exhaustive"); let input = "exhaustive \t"; let expected = snapbox::str![[r#" @@ -177,19 +179,18 @@ value value #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn register_dynamic_env() { - common::register_example::("dynamic-env", "exhaustive"); + common::register_example::("dynamic-env", "exhaustive"); } #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_toplevel() { - if !common::has_command("elvish") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive \t"; let expected = snapbox::str![[r#" @@ -205,13 +206,12 @@ fn complete_dynamic_env_toplevel() { #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_quoted_help() { - if !common::has_command("elvish") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive quote \t"; let expected = snapbox::str![[r#" @@ -229,13 +229,12 @@ fn complete_dynamic_env_quoted_help() { #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_option_value() { - if !common::has_command("elvish") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive action --choice=\t"; let expected = snapbox::str![[r#" @@ -259,13 +258,12 @@ fn complete_dynamic_env_option_value() { #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_quoted_value() { - if !common::has_command("elvish") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive quote --choice \t"; let expected = snapbox::str![[r#" diff --git a/clap_complete/tests/testsuite/fish.rs b/clap_complete/tests/testsuite/fish.rs index 9dccb53c1297..01d75e59d2d9 100644 --- a/clap_complete/tests/testsuite/fish.rs +++ b/clap_complete/tests/testsuite/fish.rs @@ -1,6 +1,9 @@ use crate::common; use snapbox::assert_data_eq; +const CMD: &str = "fish"; +type RuntimeBuilder = completest_pty::FishRuntimeBuilder; + #[test] fn basic() { let name = "my-app"; @@ -137,19 +140,18 @@ fn subcommand_last() { #[test] #[cfg(unix)] fn register_completion() { - common::register_example::("static", "exhaustive"); + common::register_example::("static", "exhaustive"); } #[test] #[cfg(unix)] fn complete() { - if !common::has_command("fish") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("static", "exhaustive"); + let mut runtime = common::load_runtime::("static", "exhaustive"); let input = "exhaustive \t"; let expected = snapbox::str![[r#" @@ -172,19 +174,18 @@ another shell (something with a space) bash (bash (shell)) fish (fish shell #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn register_dynamic_env() { - common::register_example::("dynamic-env", "exhaustive"); + common::register_example::("dynamic-env", "exhaustive"); } #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_toplevel() { - if !common::has_command("fish") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive \t\t"; let expected = snapbox::str![[r#" @@ -202,13 +203,12 @@ last -V (Print ve #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_quoted_help() { - if !common::has_command("fish") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive quote \t\t"; let expected = snapbox::str![[r#" @@ -241,13 +241,12 @@ help (Print this message or the help of the given subcommand(s)) #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_option_value() { - if !common::has_command("fish") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive action --choice=\t\t"; let expected = snapbox::str![[r#" @@ -266,13 +265,12 @@ fn complete_dynamic_env_option_value() { #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_quoted_value() { - if !common::has_command("fish") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive quote --choice \t\t"; let expected = snapbox::str![[r#" diff --git a/clap_complete/tests/testsuite/zsh.rs b/clap_complete/tests/testsuite/zsh.rs index 4bc30a4d16dd..a1e7ca4e1a27 100644 --- a/clap_complete/tests/testsuite/zsh.rs +++ b/clap_complete/tests/testsuite/zsh.rs @@ -2,6 +2,9 @@ use snapbox::assert_data_eq; use crate::common; +const CMD: &str = "zsh"; +type RuntimeBuilder = completest_pty::ZshRuntimeBuilder; + #[test] fn basic() { let name = "my-app"; @@ -138,19 +141,18 @@ fn subcommand_last() { #[test] #[cfg(unix)] fn register_completion() { - common::register_example::("static", "exhaustive"); + common::register_example::("static", "exhaustive"); } #[test] #[cfg(unix)] fn complete() { - if !common::has_command("zsh") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("static", "exhaustive"); + let mut runtime = common::load_runtime::("static", "exhaustive"); let input = "exhaustive \t"; let expected = snapbox::str![[r#" @@ -165,19 +167,18 @@ pacman action alias value quote hint last -- #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn register_dynamic_env() { - common::register_example::("dynamic-env", "exhaustive"); + common::register_example::("dynamic-env", "exhaustive"); } #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_toplevel() { - if !common::has_command("zsh") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive \t\t"; let expected = snapbox::str![[r#" @@ -192,13 +193,12 @@ fn complete_dynamic_env_toplevel() { #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_quoted_help() { - if !common::has_command("zsh") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive quote \t\t"; let expected = snapbox::str![[r#" @@ -215,13 +215,12 @@ fn complete_dynamic_env_quoted_help() { #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_option_value() { - if !common::has_command("zsh") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive action --choice=\t\t"; let expected = snapbox::str![[r#" @@ -240,13 +239,12 @@ fn complete_dynamic_env_option_value() { #[test] #[cfg(all(unix, feature = "unstable-dynamic"))] fn complete_dynamic_env_quoted_value() { - if !common::has_command("zsh") { + if !common::has_command(CMD) { return; } let term = completest::Term::new(); - let mut runtime = - common::load_runtime::("dynamic-env", "exhaustive"); + let mut runtime = common::load_runtime::("dynamic-env", "exhaustive"); let input = "exhaustive quote --choice \t\t"; let expected = snapbox::str![[r#"