From 003b0052bbcd7b850210a16cfeb5e0a869ce75c5 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 7 Mar 2022 14:08:56 -0600 Subject: [PATCH] test(fig): Switch to snapshot testing --- clap_complete_fig/Cargo.toml | 2 +- clap_complete_fig/tests/common.rs | 235 +++++++++ clap_complete_fig/tests/completions/fig.rs | 477 ------------------ clap_complete_fig/tests/completions/mod.rs | 28 - clap_complete_fig/tests/fig.rs | 85 ++++ .../tests/generate_completions.rs | 18 - .../tests/snapshots/aliases.fig.js | 32 ++ .../tests/snapshots/basic.fig.js | 49 ++ .../tests/snapshots/feature_sample.fig.js | 73 +++ .../tests/snapshots/quoting.fig.js | 112 ++++ .../tests/snapshots/special_commands.fig.js | 108 ++++ .../tests/snapshots/sub_subcommands.fig.js | 134 +++++ .../tests/snapshots/value_hint.fig.js | 126 +++++ clap_complete_fig/tests/value_hints.rs | 215 -------- 14 files changed, 955 insertions(+), 739 deletions(-) create mode 100644 clap_complete_fig/tests/common.rs delete mode 100644 clap_complete_fig/tests/completions/fig.rs delete mode 100644 clap_complete_fig/tests/completions/mod.rs create mode 100644 clap_complete_fig/tests/fig.rs delete mode 100644 clap_complete_fig/tests/generate_completions.rs create mode 100644 clap_complete_fig/tests/snapshots/aliases.fig.js create mode 100644 clap_complete_fig/tests/snapshots/basic.fig.js create mode 100644 clap_complete_fig/tests/snapshots/feature_sample.fig.js create mode 100644 clap_complete_fig/tests/snapshots/quoting.fig.js create mode 100644 clap_complete_fig/tests/snapshots/special_commands.fig.js create mode 100644 clap_complete_fig/tests/snapshots/sub_subcommands.fig.js create mode 100644 clap_complete_fig/tests/snapshots/value_hint.fig.js delete mode 100644 clap_complete_fig/tests/value_hints.rs diff --git a/clap_complete_fig/Cargo.toml b/clap_complete_fig/Cargo.toml index 67b05730..86d4d75f 100644 --- a/clap_complete_fig/Cargo.toml +++ b/clap_complete_fig/Cargo.toml @@ -42,4 +42,4 @@ clap = { path = "../", version = "3.1.0", default-features = false, features = [ clap_complete = { path = "../clap_complete", version = "3.1.0" } [dev-dependencies] -pretty_assertions = "1.0" +snapbox = { version = "0.2", features = ["diff"] } diff --git a/clap_complete_fig/tests/common.rs b/clap_complete_fig/tests/common.rs new file mode 100644 index 00000000..5d12b4c4 --- /dev/null +++ b/clap_complete_fig/tests/common.rs @@ -0,0 +1,235 @@ +pub fn basic_command(name: &'static str) -> clap::Command<'static> { + clap::Command::new(name) + .arg(clap::Arg::new("config").short('c').global(true)) + .arg(clap::Arg::new("v").short('v').conflicts_with("config")) + .subcommand( + clap::Command::new("test") + .about("Subcommand") + .arg(clap::Arg::new("debug").short('d')), + ) +} + +pub fn feature_sample_command(name: &'static str) -> clap::Command<'static> { + clap::Command::new(name) + .version("3.0") + .propagate_version(true) + .about("Tests completions") + .arg( + clap::Arg::new("file") + .value_hint(clap::ValueHint::FilePath) + .help("some input file"), + ) + .arg( + clap::Arg::new("config") + .help("some config file") + .short('c') + .visible_short_alias('C') + .long("config") + .visible_alias("conf"), + ) + .arg(clap::Arg::new("choice").possible_values(["first", "second"])) + .subcommand( + clap::Command::new("test").about("tests things").arg( + clap::Arg::new("case") + .long("case") + .takes_value(true) + .help("the case to test"), + ), + ) +} + +pub fn special_commands_command(name: &'static str) -> clap::Command<'static> { + feature_sample_command(name) + .subcommand( + clap::Command::new("some_cmd") + .about("tests other things") + .arg( + clap::Arg::new("config") + .long("--config") + .takes_value(true) + .help("the other case to test"), + ), + ) + .subcommand(clap::Command::new("some-cmd-with-hyphens").alias("hyphen")) +} + +pub fn quoting_command(name: &'static str) -> clap::Command<'static> { + clap::Command::new(name) + .version("3.0") + .arg( + clap::Arg::new("single-quotes") + .long("single-quotes") + .help("Can be 'always', 'auto', or 'never'"), + ) + .arg( + clap::Arg::new("double-quotes") + .long("double-quotes") + .help("Can be \"always\", \"auto\", or \"never\""), + ) + .arg( + clap::Arg::new("backticks") + .long("backticks") + .help("For more information see `echo test`"), + ) + .arg( + clap::Arg::new("backslash") + .long("backslash") + .help("Avoid '\\n'"), + ) + .arg( + clap::Arg::new("brackets") + .long("brackets") + .help("List packages [filter]"), + ) + .arg( + clap::Arg::new("expansions") + .long("expansions") + .help("Execute the shell command with $SHELL"), + ) + .subcommands([ + clap::Command::new("cmd-single-quotes").about("Can be 'always', 'auto', or 'never'"), + clap::Command::new("cmd-double-quotes") + .about("Can be \"always\", \"auto\", or \"never\""), + clap::Command::new("cmd-backticks").about("For more information see `echo test`"), + clap::Command::new("cmd-backslash").about("Avoid '\\n'"), + clap::Command::new("cmd-brackets").about("List packages [filter]"), + clap::Command::new("cmd-expansions").about("Execute the shell command with $SHELL"), + ]) +} + +pub fn aliases_command(name: &'static str) -> clap::Command<'static> { + clap::Command::new(name) + .version("3.0") + .about("testing bash completions") + .arg( + clap::Arg::new("flag") + .short('f') + .visible_short_alias('F') + .long("flag") + .visible_alias("flg") + .help("cmd flag"), + ) + .arg( + clap::Arg::new("option") + .short('o') + .visible_short_alias('O') + .long("option") + .visible_alias("opt") + .help("cmd option") + .takes_value(true), + ) + .arg(clap::Arg::new("positional")) +} + +pub fn sub_subcommands_command(name: &'static str) -> clap::Command<'static> { + feature_sample_command(name).subcommand( + clap::Command::new("some_cmd") + .about("top level subcommand") + .subcommand( + clap::Command::new("sub_cmd").about("sub-subcommand").arg( + clap::Arg::new("config") + .long("--config") + .takes_value(true) + .possible_values([clap::PossibleValue::new("Lest quotes aren't escaped.")]) + .help("the other case to test"), + ), + ), + ) +} + +pub fn value_hint_command(name: &'static str) -> clap::Command<'static> { + clap::Command::new(name) + .trailing_var_arg(true) + .arg( + clap::Arg::new("choice") + .long("choice") + .possible_values(["bash", "fish", "zsh"]), + ) + .arg( + clap::Arg::new("unknown") + .long("unknown") + .value_hint(clap::ValueHint::Unknown), + ) + .arg( + clap::Arg::new("other") + .long("other") + .value_hint(clap::ValueHint::Other), + ) + .arg( + clap::Arg::new("path") + .long("path") + .short('p') + .value_hint(clap::ValueHint::AnyPath), + ) + .arg( + clap::Arg::new("file") + .long("file") + .short('f') + .value_hint(clap::ValueHint::FilePath), + ) + .arg( + clap::Arg::new("dir") + .long("dir") + .short('d') + .value_hint(clap::ValueHint::DirPath), + ) + .arg( + clap::Arg::new("exe") + .long("exe") + .short('e') + .value_hint(clap::ValueHint::ExecutablePath), + ) + .arg( + clap::Arg::new("cmd_name") + .long("cmd-name") + .value_hint(clap::ValueHint::CommandName), + ) + .arg( + clap::Arg::new("cmd") + .long("cmd") + .short('c') + .value_hint(clap::ValueHint::CommandString), + ) + .arg( + clap::Arg::new("command_with_args") + .takes_value(true) + .multiple_values(true) + .value_hint(clap::ValueHint::CommandWithArguments), + ) + .arg( + clap::Arg::new("user") + .short('u') + .long("user") + .value_hint(clap::ValueHint::Username), + ) + .arg( + clap::Arg::new("host") + .short('h') + .long("host") + .value_hint(clap::ValueHint::Hostname), + ) + .arg( + clap::Arg::new("url") + .long("url") + .value_hint(clap::ValueHint::Url), + ) + .arg( + clap::Arg::new("email") + .long("email") + .value_hint(clap::ValueHint::EmailAddress), + ) +} + +pub fn assert_matches_path( + expected_path: impl AsRef, + gen: impl clap_complete::Generator, + mut cmd: clap::Command, + name: &str, +) { + let mut buf = vec![]; + clap_complete::generate(gen, &mut cmd, name, &mut buf); + + snapbox::Assert::new() + .action_env("SNAPSHOTS") + .matches_path(expected_path, buf); +} diff --git a/clap_complete_fig/tests/completions/fig.rs b/clap_complete_fig/tests/completions/fig.rs deleted file mode 100644 index e06790b9..00000000 --- a/clap_complete_fig/tests/completions/fig.rs +++ /dev/null @@ -1,477 +0,0 @@ -use super::*; -use crate::Fig; - -fn build_app() -> Command<'static> { - build_app_with_name("myapp") -} - -fn build_app_with_name(s: &'static str) -> Command<'static> { - Command::new(s) - .version("3.0") - .propagate_version(true) - .about("Tests completions") - .arg( - Arg::new("file") - .value_hint(ValueHint::FilePath) - .help("some input file"), - ) - .subcommand( - Command::new("test").about("tests things").arg( - Arg::new("case") - .long("case") - .takes_value(true) - .help("the case to test"), - ), - ) -} - -#[test] -fn fig() { - let mut cmd = build_app(); - common(Fig, &mut cmd, "myapp", FIG); -} - -static FIG: &str = r#"const completion: Fig.Spec = { - name: "myapp", - description: "Tests completions", - subcommands: [ - { - name: "test", - description: "tests things", - options: [ - { - name: "--case", - description: "the case to test", - args: { - name: "case", - isOptional: true, - }, - }, - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - ], - }, - { - name: "help", - description: "Print this message or the help of the given subcommand(s)", - options: [ - ], - args: { - name: "subcommand", - isOptional: true, - }, - }, - ], - options: [ - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - ], - args: { - name: "file", - isOptional: true, - template: "filepaths", - }, -}; - -export default completion; -"#; - -#[test] -fn fig_with_special_commands() { - let mut cmd = build_app_special_commands(); - common(Fig, &mut cmd, "my_app", FIG_SPECIAL_CMDS); -} - -fn build_app_special_commands() -> Command<'static> { - build_app_with_name("my_app") - .subcommand( - Command::new("some_cmd").about("tests other things").arg( - Arg::new("config") - .long("--config") - .takes_value(true) - .help("the other case to test"), - ), - ) - .subcommand(Command::new("some-cmd-with-hyphens").alias("hyphen")) -} - -static FIG_SPECIAL_CMDS: &str = r#"const completion: Fig.Spec = { - name: "my_app", - description: "Tests completions", - subcommands: [ - { - name: "test", - description: "tests things", - options: [ - { - name: "--case", - description: "the case to test", - args: { - name: "case", - isOptional: true, - }, - }, - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - ], - }, - { - name: "some_cmd", - description: "tests other things", - options: [ - { - name: "--config", - description: "the other case to test", - args: { - name: "config", - isOptional: true, - }, - }, - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - ], - }, - { - name: "some-cmd-with-hyphens", - options: [ - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - ], - }, - { - name: "help", - description: "Print this message or the help of the given subcommand(s)", - options: [ - ], - args: { - name: "subcommand", - isOptional: true, - }, - }, - ], - options: [ - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - ], - args: { - name: "file", - isOptional: true, - template: "filepaths", - }, -}; - -export default completion; -"#; - -#[test] -fn fig_with_special_help() { - let mut cmd = build_app_special_help(); - common(Fig, &mut cmd, "my_app", FIG_SPECIAL_HELP); -} - -fn build_app_special_help() -> Command<'static> { - Command::new("my_app") - .version("3.0") - .arg( - Arg::new("single-quotes") - .long("single-quotes") - .help("Can be 'always', 'auto', or 'never'"), - ) - .arg( - Arg::new("double-quotes") - .long("double-quotes") - .help("Can be \"always\", \"auto\", or \"never\""), - ) - .arg( - Arg::new("backticks") - .long("backticks") - .help("For more information see `echo test`"), - ) - .arg(Arg::new("backslash").long("backslash").help("Avoid '\\n'")) - .arg( - Arg::new("brackets") - .long("brackets") - .help("List packages [filter]"), - ) - .arg( - Arg::new("expansions") - .long("expansions") - .help("Execute the shell command with $SHELL"), - ) -} - -static FIG_SPECIAL_HELP: &str = r#"const completion: Fig.Spec = { - name: "my_app", - description: "", - options: [ - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - { - name: "--single-quotes", - description: "Can be 'always', 'auto', or 'never'", - }, - { - name: "--double-quotes", - description: "Can be \"always\", \"auto\", or \"never\"", - }, - { - name: "--backticks", - description: "For more information see `echo test`", - }, - { - name: "--backslash", - description: "Avoid '\\n'", - }, - { - name: "--brackets", - description: "List packages [filter]", - }, - { - name: "--expansions", - description: "Execute the shell command with $SHELL", - }, - ], -}; - -export default completion; -"#; - -#[test] -fn fig_with_aliases() { - let mut cmd = build_app_with_aliases(); - common(Fig, &mut cmd, "cmd", FIG_ALIASES); -} - -fn build_app_with_aliases() -> Command<'static> { - Command::new("cmd") - .version("3.0") - .about("testing bash completions") - .arg( - Arg::new("flag") - .short('f') - .visible_short_alias('F') - .long("flag") - .visible_alias("flg") - .help("cmd flag"), - ) - .arg( - Arg::new("option") - .short('o') - .visible_short_alias('O') - .long("option") - .visible_alias("opt") - .help("cmd option") - .takes_value(true), - ) - .arg(Arg::new("positional")) -} - -static FIG_ALIASES: &str = r#"const completion: Fig.Spec = { - name: "cmd", - description: "testing bash completions", - options: [ - { - name: ["-o", "-O", "--option", "--opt"], - description: "cmd option", - args: { - name: "option", - isOptional: true, - }, - }, - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - { - name: ["-f", "-F", "--flag", "--flg"], - description: "cmd flag", - }, - ], - args: { - name: "positional", - isOptional: true, - }, -}; - -export default completion; -"#; - -#[test] -fn fig_with_sub_subcommands() { - let mut cmd = build_app_sub_subcommands(); - common(Fig, &mut cmd, "my_app", FIG_SUB_SUBCMDS); -} - -fn build_app_sub_subcommands() -> Command<'static> { - build_app_with_name("my_app").subcommand( - Command::new("some_cmd") - .about("top level subcommand") - .subcommand( - Command::new("sub_cmd").about("sub-subcommand").arg( - Arg::new("config") - .long("--config") - .takes_value(true) - .help("the other case to test"), - ), - ), - ) -} - -static FIG_SUB_SUBCMDS: &str = r#"const completion: Fig.Spec = { - name: "my_app", - description: "Tests completions", - subcommands: [ - { - name: "test", - description: "tests things", - options: [ - { - name: "--case", - description: "the case to test", - args: { - name: "case", - isOptional: true, - }, - }, - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - ], - }, - { - name: "some_cmd", - description: "top level subcommand", - subcommands: [ - { - name: "sub_cmd", - description: "sub-subcommand", - options: [ - { - name: "--config", - description: "the other case to test", - args: { - name: "config", - isOptional: true, - }, - }, - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - ], - }, - { - name: "help", - description: "Print this message or the help of the given subcommand(s)", - options: [ - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - ], - args: { - name: "subcommand", - isOptional: true, - }, - }, - ], - options: [ - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - ], - }, - { - name: "help", - description: "Print this message or the help of the given subcommand(s)", - options: [ - ], - args: { - name: "subcommand", - isOptional: true, - }, - }, - ], - options: [ - { - name: ["-h", "--help"], - description: "Print help information", - }, - { - name: ["-V", "--version"], - description: "Print version information", - }, - ], - args: { - name: "file", - isOptional: true, - template: "filepaths", - }, -}; - -export default completion; -"#; diff --git a/clap_complete_fig/tests/completions/mod.rs b/clap_complete_fig/tests/completions/mod.rs deleted file mode 100644 index 4368c063..00000000 --- a/clap_complete_fig/tests/completions/mod.rs +++ /dev/null @@ -1,28 +0,0 @@ -use clap::{Arg, Command, ValueHint}; -use clap_complete::{generate, Generator}; -use std::fmt; - -mod fig; - -#[derive(PartialEq, Eq)] -pub struct PrettyString<'a>(pub &'a str); - -impl<'a> fmt::Debug for PrettyString<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(self.0) - } -} - -macro_rules! assert_eq { - ($left:expr, $right:expr) => { - pretty_assertions::assert_eq!(PrettyString($left), PrettyString($right)); - }; -} - -pub fn common(gen: G, cmd: &mut Command, name: &str, fixture: &str) { - let mut buf = vec![]; - generate(gen, cmd, name, &mut buf); - let string = String::from_utf8(buf).unwrap(); - - assert_eq!(&string, fixture); -} diff --git a/clap_complete_fig/tests/fig.rs b/clap_complete_fig/tests/fig.rs new file mode 100644 index 00000000..d93db7c8 --- /dev/null +++ b/clap_complete_fig/tests/fig.rs @@ -0,0 +1,85 @@ +mod common; + +#[test] +fn basic() { + let name = "my-app"; + let cmd = common::basic_command(name); + common::assert_matches_path( + "tests/snapshots/basic.fig.js", + clap_complete_fig::Fig, + cmd, + name, + ); +} + +#[test] +fn feature_sample() { + let name = "my-app"; + let cmd = common::feature_sample_command(name); + common::assert_matches_path( + "tests/snapshots/feature_sample.fig.js", + clap_complete_fig::Fig, + cmd, + name, + ); +} + +#[test] +fn special_commands() { + let name = "my-app"; + let cmd = common::special_commands_command(name); + common::assert_matches_path( + "tests/snapshots/special_commands.fig.js", + clap_complete_fig::Fig, + cmd, + name, + ); +} + +#[test] +fn quoting() { + let name = "my-app"; + let cmd = common::quoting_command(name); + common::assert_matches_path( + "tests/snapshots/quoting.fig.js", + clap_complete_fig::Fig, + cmd, + name, + ); +} + +#[test] +fn aliases() { + let name = "my-app"; + let cmd = common::aliases_command(name); + common::assert_matches_path( + "tests/snapshots/aliases.fig.js", + clap_complete_fig::Fig, + cmd, + name, + ); +} + +#[test] +fn sub_subcommands() { + let name = "my-app"; + let cmd = common::sub_subcommands_command(name); + common::assert_matches_path( + "tests/snapshots/sub_subcommands.fig.js", + clap_complete_fig::Fig, + cmd, + name, + ); +} + +#[test] +fn value_hint() { + let name = "my-app"; + let cmd = common::value_hint_command(name); + common::assert_matches_path( + "tests/snapshots/value_hint.fig.js", + clap_complete_fig::Fig, + cmd, + name, + ); +} diff --git a/clap_complete_fig/tests/generate_completions.rs b/clap_complete_fig/tests/generate_completions.rs deleted file mode 100644 index 99a7f665..00000000 --- a/clap_complete_fig/tests/generate_completions.rs +++ /dev/null @@ -1,18 +0,0 @@ -use clap::{Arg, Command}; -use clap_complete::generate; -use clap_complete_fig::Fig; -use std::io; - -#[test] -fn generate_completions() { - let mut cmd = Command::new("test_app") - .arg(Arg::new("config").short('c').global(true)) - .arg(Arg::new("v").short('v').conflicts_with("config")) - .subcommand( - Command::new("test") - .about("Subcommand") - .arg(Arg::new("debug").short('d')), - ); - - generate(Fig, &mut cmd, "test_app", &mut io::sink()); -} diff --git a/clap_complete_fig/tests/snapshots/aliases.fig.js b/clap_complete_fig/tests/snapshots/aliases.fig.js new file mode 100644 index 00000000..16f61717 --- /dev/null +++ b/clap_complete_fig/tests/snapshots/aliases.fig.js @@ -0,0 +1,32 @@ +const completion: Fig.Spec = { + name: "my-app", + description: "testing bash completions", + options: [ + { + name: ["-o", "-O", "--option", "--opt"], + description: "cmd option", + args: { + name: "option", + isOptional: true, + }, + }, + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + { + name: ["-f", "-F", "--flag", "--flg"], + description: "cmd flag", + }, + ], + args: { + name: "positional", + isOptional: true, + }, +}; + +export default completion; diff --git a/clap_complete_fig/tests/snapshots/basic.fig.js b/clap_complete_fig/tests/snapshots/basic.fig.js new file mode 100644 index 00000000..4bcc271f --- /dev/null +++ b/clap_complete_fig/tests/snapshots/basic.fig.js @@ -0,0 +1,49 @@ +const completion: Fig.Spec = { + name: "my-app", + description: "", + subcommands: [ + { + name: "test", + description: "Subcommand", + options: [ + { + name: "-d", + }, + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: "-c", + }, + ], + }, + { + name: "help", + description: "Print this message or the help of the given subcommand(s)", + options: [ + { + name: "-c", + }, + ], + args: { + name: "subcommand", + isOptional: true, + }, + }, + ], + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: "-c", + }, + { + name: "-v", + }, + ], +}; + +export default completion; diff --git a/clap_complete_fig/tests/snapshots/feature_sample.fig.js b/clap_complete_fig/tests/snapshots/feature_sample.fig.js new file mode 100644 index 00000000..5d69d01a --- /dev/null +++ b/clap_complete_fig/tests/snapshots/feature_sample.fig.js @@ -0,0 +1,73 @@ +const completion: Fig.Spec = { + name: "my-app", + description: "Tests completions", + subcommands: [ + { + name: "test", + description: "tests things", + options: [ + { + name: "--case", + description: "the case to test", + args: { + name: "case", + isOptional: true, + }, + }, + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + ], + }, + { + name: "help", + description: "Print this message or the help of the given subcommand(s)", + options: [ + ], + args: { + name: "subcommand", + isOptional: true, + }, + }, + ], + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + { + name: ["-c", "-C", "--config", "--conf"], + description: "some config file", + }, + ], + args: [ +{ + name: "file", + isOptional: true, + template: "filepaths", + }, +{ + name: "choice", + isOptional: true, + suggestions: [ + { + name: "first", + }, + { + name: "second", + }, + ] + }, + ] +}; + +export default completion; diff --git a/clap_complete_fig/tests/snapshots/quoting.fig.js b/clap_complete_fig/tests/snapshots/quoting.fig.js new file mode 100644 index 00000000..89cd2528 --- /dev/null +++ b/clap_complete_fig/tests/snapshots/quoting.fig.js @@ -0,0 +1,112 @@ +const completion: Fig.Spec = { + name: "my-app", + description: "", + subcommands: [ + { + name: "cmd-single-quotes", + description: "Can be 'always', 'auto', or 'never'", + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + ], + }, + { + name: "cmd-double-quotes", + description: "Can be /"always/", /"auto/", or /"never/"", + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + ], + }, + { + name: "cmd-backticks", + description: "For more information see `echo test`", + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + ], + }, + { + name: "cmd-backslash", + description: "Avoid '//n'", + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + ], + }, + { + name: "cmd-brackets", + description: "List packages [filter]", + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + ], + }, + { + name: "cmd-expansions", + description: "Execute the shell command with $SHELL", + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + ], + }, + { + name: "help", + description: "Print this message or the help of the given subcommand(s)", + options: [ + ], + args: { + name: "subcommand", + isOptional: true, + }, + }, + ], + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + { + name: "--single-quotes", + description: "Can be 'always', 'auto', or 'never'", + }, + { + name: "--double-quotes", + description: "Can be /"always/", /"auto/", or /"never/"", + }, + { + name: "--backticks", + description: "For more information see `echo test`", + }, + { + name: "--backslash", + description: "Avoid '//n'", + }, + { + name: "--brackets", + description: "List packages [filter]", + }, + { + name: "--expansions", + description: "Execute the shell command with $SHELL", + }, + ], +}; + +export default completion; diff --git a/clap_complete_fig/tests/snapshots/special_commands.fig.js b/clap_complete_fig/tests/snapshots/special_commands.fig.js new file mode 100644 index 00000000..3ae3da48 --- /dev/null +++ b/clap_complete_fig/tests/snapshots/special_commands.fig.js @@ -0,0 +1,108 @@ +const completion: Fig.Spec = { + name: "my-app", + description: "Tests completions", + subcommands: [ + { + name: "test", + description: "tests things", + options: [ + { + name: "--case", + description: "the case to test", + args: { + name: "case", + isOptional: true, + }, + }, + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + ], + }, + { + name: "some_cmd", + description: "tests other things", + options: [ + { + name: "--config", + description: "the other case to test", + args: { + name: "config", + isOptional: true, + }, + }, + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + ], + }, + { + name: "some-cmd-with-hyphens", + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + ], + }, + { + name: "help", + description: "Print this message or the help of the given subcommand(s)", + options: [ + ], + args: { + name: "subcommand", + isOptional: true, + }, + }, + ], + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + { + name: ["-c", "-C", "--config", "--conf"], + description: "some config file", + }, + ], + args: [ +{ + name: "file", + isOptional: true, + template: "filepaths", + }, +{ + name: "choice", + isOptional: true, + suggestions: [ + { + name: "first", + }, + { + name: "second", + }, + ] + }, + ] +}; + +export default completion; diff --git a/clap_complete_fig/tests/snapshots/sub_subcommands.fig.js b/clap_complete_fig/tests/snapshots/sub_subcommands.fig.js new file mode 100644 index 00000000..4f3a8a98 --- /dev/null +++ b/clap_complete_fig/tests/snapshots/sub_subcommands.fig.js @@ -0,0 +1,134 @@ +const completion: Fig.Spec = { + name: "my-app", + description: "Tests completions", + subcommands: [ + { + name: "test", + description: "tests things", + options: [ + { + name: "--case", + description: "the case to test", + args: { + name: "case", + isOptional: true, + }, + }, + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + ], + }, + { + name: "some_cmd", + description: "top level subcommand", + subcommands: [ + { + name: "sub_cmd", + description: "sub-subcommand", + options: [ + { + name: "--config", + description: "the other case to test", + args: { + name: "config", + isOptional: true, + suggestions: [ + { + name: "Lest quotes aren't escaped.", + }, + ] + }, + }, + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + ], + }, + { + name: "help", + description: "Print this message or the help of the given subcommand(s)", + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + ], + args: { + name: "subcommand", + isOptional: true, + }, + }, + ], + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + ], + }, + { + name: "help", + description: "Print this message or the help of the given subcommand(s)", + options: [ + ], + args: { + name: "subcommand", + isOptional: true, + }, + }, + ], + options: [ + { + name: ["-h", "--help"], + description: "Print help information", + }, + { + name: ["-V", "--version"], + description: "Print version information", + }, + { + name: ["-c", "-C", "--config", "--conf"], + description: "some config file", + }, + ], + args: [ +{ + name: "file", + isOptional: true, + template: "filepaths", + }, +{ + name: "choice", + isOptional: true, + suggestions: [ + { + name: "first", + }, + { + name: "second", + }, + ] + }, + ] +}; + +export default completion; diff --git a/clap_complete_fig/tests/snapshots/value_hint.fig.js b/clap_complete_fig/tests/snapshots/value_hint.fig.js new file mode 100644 index 00000000..9fe463d8 --- /dev/null +++ b/clap_complete_fig/tests/snapshots/value_hint.fig.js @@ -0,0 +1,126 @@ +const completion: Fig.Spec = { + name: "my-app", + description: "", + options: [ + { + name: "--choice", + args: { + name: "choice", + isOptional: true, + suggestions: [ + { + name: "bash", + }, + { + name: "fish", + }, + { + name: "zsh", + }, + ] + }, + }, + { + name: "--unknown", + args: { + name: "unknown", + isOptional: true, + }, + }, + { + name: "--other", + args: { + name: "other", + isOptional: true, + }, + }, + { + name: ["-p", "--path"], + args: { + name: "path", + isOptional: true, + template: "filepaths", + }, + }, + { + name: ["-f", "--file"], + args: { + name: "file", + isOptional: true, + template: "filepaths", + }, + }, + { + name: ["-d", "--dir"], + args: { + name: "dir", + isOptional: true, + template: "folders", + }, + }, + { + name: ["-e", "--exe"], + args: { + name: "exe", + isOptional: true, + template: "filepaths", + }, + }, + { + name: "--cmd-name", + args: { + name: "cmd_name", + isOptional: true, + isCommand: true, + }, + }, + { + name: ["-c", "--cmd"], + args: { + name: "cmd", + isOptional: true, + isCommand: true, + }, + }, + { + name: ["-u", "--user"], + args: { + name: "user", + isOptional: true, + }, + }, + { + name: ["-h", "--host"], + args: { + name: "host", + isOptional: true, + }, + }, + { + name: "--url", + args: { + name: "url", + isOptional: true, + }, + }, + { + name: "--email", + args: { + name: "email", + isOptional: true, + }, + }, + { + name: "--help", + description: "Print help information", + }, + ], + args: { + name: "command_with_args", + isVariadic: true, + isOptional: true, + isCommand: true, + }, +}; + +export default completion; diff --git a/clap_complete_fig/tests/value_hints.rs b/clap_complete_fig/tests/value_hints.rs deleted file mode 100644 index 08388f08..00000000 --- a/clap_complete_fig/tests/value_hints.rs +++ /dev/null @@ -1,215 +0,0 @@ -use clap::{Arg, Command, ValueHint}; -use clap_complete_fig::Fig; -use completions::common; - -mod completions; - -pub fn build_app_with_value_hints() -> Command<'static> { - Command::new("my_app") - .disable_version_flag(true) - .trailing_var_arg(true) - .arg( - Arg::new("choice") - .long("choice") - .possible_values(["bash", "fish", "zsh"]), - ) - .arg( - Arg::new("unknown") - .long("unknown") - .value_hint(ValueHint::Unknown), - ) - .arg(Arg::new("other").long("other").value_hint(ValueHint::Other)) - .arg( - Arg::new("path") - .long("path") - .short('p') - .value_hint(ValueHint::AnyPath), - ) - .arg( - Arg::new("file") - .long("file") - .short('f') - .value_hint(ValueHint::FilePath), - ) - .arg( - Arg::new("dir") - .long("dir") - .short('d') - .value_hint(ValueHint::DirPath), - ) - .arg( - Arg::new("exe") - .long("exe") - .short('e') - .value_hint(ValueHint::ExecutablePath), - ) - .arg( - Arg::new("cmd_name") - .long("cmd-name") - .value_hint(ValueHint::CommandName), - ) - .arg( - Arg::new("cmd") - .long("cmd") - .short('c') - .value_hint(ValueHint::CommandString), - ) - .arg( - Arg::new("command_with_args") - .takes_value(true) - .multiple_values(true) - .value_hint(ValueHint::CommandWithArguments), - ) - .arg( - Arg::new("user") - .short('u') - .long("user") - .value_hint(ValueHint::Username), - ) - .arg( - Arg::new("host") - .short('h') - .long("host") - .value_hint(ValueHint::Hostname), - ) - .arg(Arg::new("url").long("url").value_hint(ValueHint::Url)) - .arg( - Arg::new("email") - .long("email") - .value_hint(ValueHint::EmailAddress), - ) -} - -static FIG_VALUE_HINTS: &str = r#"const completion: Fig.Spec = { - name: "my_app", - description: "", - options: [ - { - name: "--choice", - args: { - name: "choice", - isOptional: true, - suggestions: [ - { - name: "bash", - }, - { - name: "fish", - }, - { - name: "zsh", - }, - ] - }, - }, - { - name: "--unknown", - args: { - name: "unknown", - isOptional: true, - }, - }, - { - name: "--other", - args: { - name: "other", - isOptional: true, - }, - }, - { - name: ["-p", "--path"], - args: { - name: "path", - isOptional: true, - template: "filepaths", - }, - }, - { - name: ["-f", "--file"], - args: { - name: "file", - isOptional: true, - template: "filepaths", - }, - }, - { - name: ["-d", "--dir"], - args: { - name: "dir", - isOptional: true, - template: "folders", - }, - }, - { - name: ["-e", "--exe"], - args: { - name: "exe", - isOptional: true, - template: "filepaths", - }, - }, - { - name: "--cmd-name", - args: { - name: "cmd_name", - isOptional: true, - isCommand: true, - }, - }, - { - name: ["-c", "--cmd"], - args: { - name: "cmd", - isOptional: true, - isCommand: true, - }, - }, - { - name: ["-u", "--user"], - args: { - name: "user", - isOptional: true, - }, - }, - { - name: ["-h", "--host"], - args: { - name: "host", - isOptional: true, - }, - }, - { - name: "--url", - args: { - name: "url", - isOptional: true, - }, - }, - { - name: "--email", - args: { - name: "email", - isOptional: true, - }, - }, - { - name: "--help", - description: "Print help information", - }, - ], - args: { - name: "command_with_args", - isVariadic: true, - isOptional: true, - isCommand: true, - }, -}; - -export default completion; -"#; - -#[test] -fn fig_with_value_hints() { - let mut cmd = build_app_with_value_hints(); - common(Fig, &mut cmd, "my_app", FIG_VALUE_HINTS); -}