Skip to content

Commit

Permalink
Merge pull request #3544 from epage/trycmd
Browse files Browse the repository at this point in the history
test: Switch completions/man to snapshot testing
  • Loading branch information
epage authored Mar 7, 2022
2 parents 17193bd + 7c86eeb commit 9e3b661
Show file tree
Hide file tree
Showing 76 changed files with 4,340 additions and 2,559 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ criterion = "0.3.2"
trybuild = "1.0.18"
rustversion = "1"
# Cutting out `filesystem` feature
trycmd = { version = "0.12", default-features = false, features = ["color-auto", "diff", "examples"] }
trycmd = { version = "0.13", default-features = false, features = ["color-auto", "diff", "examples"] }

[[example]]
name = "demo"
Expand Down
1 change: 1 addition & 0 deletions clap_complete/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ clap = { path = "../", version = "3.1.0", default-features = false, features = [

[dev-dependencies]
pretty_assertions = "1.0"
snapbox = { version = "0.2", features = ["diff"] }
clap = { path = "../", version = "3.1.0", default-features = false, features = ["std", "derive"] }

[features]
Expand Down
85 changes: 85 additions & 0 deletions clap_complete/tests/bash.rs
Original file line number Diff line number Diff line change
@@ -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.bash.log",
clap_complete::shells::Bash,
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.bash.log",
clap_complete::shells::Bash,
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.bash.log",
clap_complete::shells::Bash,
cmd,
name,
);
}

#[test]
fn quoting() {
let name = "my-app";
let cmd = common::quoting_command(name);
common::assert_matches_path(
"tests/snapshots/quoting.bash.log",
clap_complete::shells::Bash,
cmd,
name,
);
}

#[test]
fn aliases() {
let name = "my-app";
let cmd = common::aliases_command(name);
common::assert_matches_path(
"tests/snapshots/aliases.bash.log",
clap_complete::shells::Bash,
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.bash.log",
clap_complete::shells::Bash,
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.bash.log",
clap_complete::shells::Bash,
cmd,
name,
);
}
235 changes: 235 additions & 0 deletions clap_complete/tests/common.rs
Original file line number Diff line number Diff line change
@@ -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<std::path::Path>,
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);
}
Loading

0 comments on commit 9e3b661

Please sign in to comment.