2022-09-02 12:37:37 +00:00
|
|
|
use clap::builder::PossibleValue;
|
2024-05-24 15:13:50 +00:00
|
|
|
use snapbox::prelude::*;
|
2022-09-02 12:37:37 +00:00
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn basic_command(name: &'static str) -> clap::Command {
|
2022-03-07 20:03:46 +00:00
|
|
|
clap::Command::new(name)
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(
|
|
|
|
clap::Arg::new("config")
|
|
|
|
.short('c')
|
|
|
|
.global(true)
|
|
|
|
.action(clap::ArgAction::SetTrue),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("v")
|
|
|
|
.short('v')
|
|
|
|
.conflicts_with("config")
|
|
|
|
.action(clap::ArgAction::SetTrue),
|
|
|
|
)
|
2022-03-07 20:03:46 +00:00
|
|
|
.subcommand(
|
2024-02-16 22:16:36 +00:00
|
|
|
clap::Command::new("test")
|
|
|
|
.about("Subcommand\nwith a second line")
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("debug")
|
|
|
|
.short('d')
|
|
|
|
.action(clap::ArgAction::Count),
|
|
|
|
),
|
2022-03-07 20:03:46 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn feature_sample_command(name: &'static str) -> clap::Command {
|
2022-03-07 20:03:46 +00:00
|
|
|
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")
|
2022-06-07 18:48:48 +00:00
|
|
|
.action(clap::ArgAction::Count)
|
2022-03-07 20:03:46 +00:00
|
|
|
.help("some config file")
|
|
|
|
.short('c')
|
|
|
|
.visible_short_alias('C')
|
|
|
|
.long("config")
|
|
|
|
.visible_alias("conf"),
|
|
|
|
)
|
2022-05-24 01:16:02 +00:00
|
|
|
.arg(clap::Arg::new("choice").value_parser(["first", "second"]))
|
2022-03-07 20:03:46 +00:00
|
|
|
.subcommand(
|
|
|
|
clap::Command::new("test").about("tests things").arg(
|
|
|
|
clap::Arg::new("case")
|
|
|
|
.long("case")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::Set)
|
2022-03-07 20:03:46 +00:00
|
|
|
.help("the case to test"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn special_commands_command(name: &'static str) -> clap::Command {
|
2022-03-07 20:03:46 +00:00
|
|
|
feature_sample_command(name)
|
|
|
|
.subcommand(
|
|
|
|
clap::Command::new("some_cmd")
|
|
|
|
.about("tests other things")
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("config")
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("config")
|
2022-03-17 20:35:52 +00:00
|
|
|
.hide(true)
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::Set)
|
2022-03-18 13:46:20 +00:00
|
|
|
.require_equals(true)
|
2022-03-07 20:03:46 +00:00
|
|
|
.help("the other case to test"),
|
2022-03-17 20:35:52 +00:00
|
|
|
)
|
2022-08-03 16:20:07 +00:00
|
|
|
.arg(clap::Arg::new("path").num_args(1..)),
|
2022-03-07 20:03:46 +00:00
|
|
|
)
|
|
|
|
.subcommand(clap::Command::new("some-cmd-with-hyphens").alias("hyphen"))
|
2022-03-17 20:35:52 +00:00
|
|
|
.subcommand(clap::Command::new("some-hidden-cmd").hide(true))
|
2022-03-07 20:03:46 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn quoting_command(name: &'static str) -> clap::Command {
|
2022-03-07 20:03:46 +00:00
|
|
|
clap::Command::new(name)
|
|
|
|
.version("3.0")
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("single-quotes")
|
|
|
|
.long("single-quotes")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::SetTrue)
|
2022-03-07 20:03:46 +00:00
|
|
|
.help("Can be 'always', 'auto', or 'never'"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("double-quotes")
|
|
|
|
.long("double-quotes")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::SetTrue)
|
2022-03-07 20:03:46 +00:00
|
|
|
.help("Can be \"always\", \"auto\", or \"never\""),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("backticks")
|
|
|
|
.long("backticks")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::SetTrue)
|
2022-03-07 20:03:46 +00:00
|
|
|
.help("For more information see `echo test`"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("backslash")
|
|
|
|
.long("backslash")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::SetTrue)
|
2022-03-07 20:03:46 +00:00
|
|
|
.help("Avoid '\\n'"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("brackets")
|
|
|
|
.long("brackets")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::SetTrue)
|
2022-03-07 20:03:46 +00:00
|
|
|
.help("List packages [filter]"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("expansions")
|
|
|
|
.long("expansions")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::SetTrue)
|
2022-03-07 20:03:46 +00:00
|
|
|
.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"),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn aliases_command(name: &'static str) -> clap::Command {
|
2022-03-07 20:03:46 +00:00
|
|
|
clap::Command::new(name)
|
|
|
|
.version("3.0")
|
|
|
|
.about("testing bash completions")
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("flag")
|
|
|
|
.short('f')
|
|
|
|
.visible_short_alias('F')
|
|
|
|
.long("flag")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::SetTrue)
|
2022-03-07 20:03:46 +00:00
|
|
|
.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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::Set),
|
2022-03-07 20:03:46 +00:00
|
|
|
)
|
|
|
|
.arg(clap::Arg::new("positional"))
|
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn sub_subcommands_command(name: &'static str) -> clap::Command {
|
2022-03-07 20:03:46 +00:00
|
|
|
feature_sample_command(name).subcommand(
|
|
|
|
clap::Command::new("some_cmd")
|
|
|
|
.about("top level subcommand")
|
2022-09-27 04:18:24 +00:00
|
|
|
.visible_alias("some_cmd_alias")
|
2022-03-07 20:03:46 +00:00
|
|
|
.subcommand(
|
|
|
|
clap::Command::new("sub_cmd").about("sub-subcommand").arg(
|
|
|
|
clap::Arg::new("config")
|
2022-05-04 20:38:06 +00:00
|
|
|
.long("config")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::Set)
|
2022-09-02 12:37:37 +00:00
|
|
|
.value_parser([
|
|
|
|
PossibleValue::new("Lest quotes, aren't escaped.")
|
|
|
|
.help("help,with,comma"),
|
|
|
|
PossibleValue::new("Second to trigger display of options"),
|
|
|
|
])
|
2022-03-07 20:03:46 +00:00
|
|
|
.help("the other case to test"),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn value_hint_command(name: &'static str) -> clap::Command {
|
2022-03-07 20:03:46 +00:00
|
|
|
clap::Command::new(name)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("choice")
|
|
|
|
.long("choice")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["bash", "fish", "zsh"]),
|
2022-03-07 20:03:46 +00:00
|
|
|
)
|
|
|
|
.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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(clap::ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..)
|
2022-09-06 20:41:23 +00:00
|
|
|
.trailing_var_arg(true)
|
2022-03-07 20:03:46 +00:00
|
|
|
.value_hint(clap::ValueHint::CommandWithArguments),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("user")
|
|
|
|
.short('u')
|
|
|
|
.long("user")
|
|
|
|
.value_hint(clap::ValueHint::Username),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("host")
|
2022-08-08 21:08:47 +00:00
|
|
|
.short('H')
|
2022-03-07 20:03:46 +00:00
|
|
|
.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),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn value_terminator_command(name: &'static str) -> clap::Command {
|
2023-01-09 10:50:58 +00:00
|
|
|
clap::Command::new(name).arg(
|
|
|
|
clap::Arg::new("arguments")
|
|
|
|
.help("multi-valued argument with a value terminator")
|
|
|
|
.num_args(1..)
|
|
|
|
.value_terminator(";"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn two_multi_valued_arguments_command(name: &'static str) -> clap::Command {
|
2023-01-10 08:14:52 +00:00
|
|
|
clap::Command::new(name)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("first")
|
|
|
|
.help("first multi-valued argument")
|
|
|
|
.num_args(1..),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
clap::Arg::new("second")
|
|
|
|
.help("second multi-valued argument")
|
|
|
|
.raw(true),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn subcommand_last(name: &'static str) -> clap::Command {
|
2023-05-10 18:21:01 +00:00
|
|
|
clap::Command::new(name)
|
|
|
|
.arg(clap::Arg::new("free").last(true))
|
|
|
|
.subcommands([clap::Command::new("foo"), clap::Command::new("bar")])
|
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn assert_matches(
|
2024-05-24 15:13:50 +00:00
|
|
|
expected: impl IntoData,
|
2022-03-07 20:03:46 +00:00
|
|
|
gen: impl clap_complete::Generator,
|
|
|
|
mut cmd: clap::Command,
|
2022-08-15 19:29:46 +00:00
|
|
|
name: &'static str,
|
2022-03-07 20:03:46 +00:00
|
|
|
) {
|
|
|
|
let mut buf = vec![];
|
|
|
|
clap_complete::generate(gen, &mut cmd, name, &mut buf);
|
|
|
|
|
|
|
|
snapbox::Assert::new()
|
2024-05-24 15:13:50 +00:00
|
|
|
.action_env(snapbox::assert::DEFAULT_ACTION_ENV)
|
2022-09-02 12:35:12 +00:00
|
|
|
.normalize_paths(false)
|
2024-05-24 15:13:50 +00:00
|
|
|
.eq(buf, expected);
|
2022-03-07 20:03:46 +00:00
|
|
|
}
|
2023-07-20 19:42:58 +00:00
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn register_example<R: completest::RuntimeBuilder>(context: &str, name: &str) {
|
2024-01-15 18:20:57 +00:00
|
|
|
use completest::Runtime as _;
|
|
|
|
|
2024-05-24 15:13:50 +00:00
|
|
|
let scratch = snapbox::dir::DirRoot::mutable_temp().unwrap();
|
2023-07-21 00:42:03 +00:00
|
|
|
let scratch_path = scratch.path().unwrap();
|
|
|
|
|
2024-01-15 18:20:57 +00:00
|
|
|
let shell_name = R::name();
|
2023-07-21 19:15:26 +00:00
|
|
|
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.join("tests/snapshots/home")
|
2023-07-28 17:48:36 +00:00
|
|
|
.join(context)
|
2023-07-20 19:42:58 +00:00
|
|
|
.join(name)
|
|
|
|
.join(shell_name);
|
2023-07-21 19:15:26 +00:00
|
|
|
println!("Compiling");
|
|
|
|
let manifest_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
|
2023-07-28 11:10:04 +00:00
|
|
|
let bin_path = snapbox::cmd::compile_example(
|
|
|
|
name,
|
|
|
|
[
|
|
|
|
"--manifest-path",
|
|
|
|
manifest_path.to_str().unwrap(),
|
|
|
|
// Unconditionally include to avoid completion file tests failing based on the how
|
|
|
|
// `cargo test` is invoked
|
|
|
|
"--features=unstable-dynamic",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-21 19:15:26 +00:00
|
|
|
println!("Compiled");
|
2023-07-20 19:42:58 +00:00
|
|
|
let bin_root = bin_path.parent().unwrap().to_owned();
|
|
|
|
|
2023-07-28 18:28:30 +00:00
|
|
|
let mut registration = std::process::Command::new(&bin_path);
|
|
|
|
match context {
|
|
|
|
"static" => registration.args([format!("--generate={shell_name}")]),
|
2024-08-12 15:40:41 +00:00
|
|
|
"dynamic-command" => registration.args(["complete", shell_name]),
|
2024-08-12 15:53:24 +00:00
|
|
|
"dynamic-env" => registration.env("COMPLETE", shell_name),
|
2023-07-28 18:28:30 +00:00
|
|
|
_ => unreachable!("unsupported context {}", context),
|
|
|
|
};
|
|
|
|
let registration = registration.output().unwrap();
|
2023-07-20 19:42:58 +00:00
|
|
|
assert!(
|
|
|
|
registration.status.success(),
|
|
|
|
"{}",
|
|
|
|
String::from_utf8_lossy(®istration.stderr)
|
|
|
|
);
|
|
|
|
let registration = std::str::from_utf8(®istration.stdout).unwrap();
|
|
|
|
assert!(!registration.is_empty());
|
|
|
|
|
2024-01-15 18:20:57 +00:00
|
|
|
let mut runtime = R::new(bin_root, scratch_path.to_owned()).unwrap();
|
2023-07-20 19:42:58 +00:00
|
|
|
|
|
|
|
runtime.register(name, registration).unwrap();
|
2023-07-21 00:42:03 +00:00
|
|
|
|
2023-07-24 15:56:51 +00:00
|
|
|
snapbox::assert_subset_eq(home, scratch_path);
|
2023-07-21 00:42:03 +00:00
|
|
|
|
|
|
|
scratch.close().unwrap();
|
2023-07-20 19:42:58 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn load_runtime<R: completest::RuntimeBuilder>(
|
2023-07-28 17:48:36 +00:00
|
|
|
context: &str,
|
|
|
|
name: &str,
|
2024-01-15 18:20:57 +00:00
|
|
|
) -> Box<dyn completest::Runtime>
|
|
|
|
where
|
|
|
|
<R as completest::RuntimeBuilder>::Runtime: 'static,
|
|
|
|
{
|
|
|
|
let shell_name = R::name();
|
2023-07-21 19:15:26 +00:00
|
|
|
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.join("tests/snapshots/home")
|
2023-07-28 17:48:36 +00:00
|
|
|
.join(context)
|
2023-07-20 19:42:58 +00:00
|
|
|
.join(name)
|
|
|
|
.join(shell_name);
|
2024-05-24 15:13:50 +00:00
|
|
|
let scratch = snapbox::dir::DirRoot::mutable_temp()
|
2023-07-21 00:42:03 +00:00
|
|
|
.unwrap()
|
|
|
|
.with_template(&home)
|
|
|
|
.unwrap();
|
|
|
|
let home = scratch.path().unwrap().to_owned();
|
2023-07-21 19:15:26 +00:00
|
|
|
println!("Compiling");
|
|
|
|
let manifest_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
|
2023-07-28 11:10:04 +00:00
|
|
|
let bin_path = snapbox::cmd::compile_example(
|
|
|
|
name,
|
|
|
|
[
|
|
|
|
"--manifest-path",
|
|
|
|
manifest_path.to_str().unwrap(),
|
|
|
|
// Unconditionally include to avoid completion file tests failing based on the how
|
|
|
|
// `cargo test` is invoked
|
|
|
|
"--features=unstable-dynamic",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-21 19:15:26 +00:00
|
|
|
println!("Compiled");
|
2023-07-20 19:42:58 +00:00
|
|
|
let bin_root = bin_path.parent().unwrap().to_owned();
|
|
|
|
|
2024-01-15 18:20:57 +00:00
|
|
|
let runtime = R::with_home(bin_root, home).unwrap();
|
2023-07-21 00:42:03 +00:00
|
|
|
|
|
|
|
Box::new(ScratchRuntime {
|
|
|
|
_scratch: scratch,
|
2024-01-15 18:20:57 +00:00
|
|
|
runtime: Box::new(runtime),
|
2023-07-21 00:42:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-21 19:15:26 +00:00
|
|
|
#[derive(Debug)]
|
2023-07-21 00:42:03 +00:00
|
|
|
struct ScratchRuntime {
|
2024-05-24 15:13:50 +00:00
|
|
|
_scratch: snapbox::dir::DirRoot,
|
2023-07-21 00:42:03 +00:00
|
|
|
runtime: Box<dyn completest::Runtime>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl completest::Runtime for ScratchRuntime {
|
|
|
|
fn home(&self) -> &std::path::Path {
|
|
|
|
self.runtime.home()
|
|
|
|
}
|
|
|
|
|
2023-07-21 19:42:49 +00:00
|
|
|
fn register(&mut self, name: &str, content: &str) -> std::io::Result<()> {
|
2023-07-21 00:42:03 +00:00
|
|
|
self.runtime.register(name, content)
|
|
|
|
}
|
|
|
|
|
2023-07-21 19:42:49 +00:00
|
|
|
fn complete(&mut self, input: &str, term: &completest::Term) -> std::io::Result<String> {
|
2023-07-20 19:44:54 +00:00
|
|
|
let output = self.runtime.complete(input, term)?;
|
|
|
|
// HACK: elvish prints and clears this message when a completer takes too long which is
|
|
|
|
// dependent on a lot of factors, making this show up or no sometimes (especially if we
|
|
|
|
// aren't clearing the screen properly for fish)
|
2023-07-27 01:48:02 +00:00
|
|
|
let output = output.replace("\nCOMPLETING argument\n", "\n");
|
2023-07-20 19:44:54 +00:00
|
|
|
Ok(output)
|
2023-07-21 00:42:03 +00:00
|
|
|
}
|
2023-07-20 19:42:58 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 19:59:40 +00:00
|
|
|
pub(crate) fn has_command(command: &str) -> bool {
|
2023-07-21 19:15:26 +00:00
|
|
|
let output = match std::process::Command::new(command)
|
|
|
|
.arg("--version")
|
|
|
|
.output()
|
|
|
|
{
|
|
|
|
Ok(output) => output,
|
|
|
|
Err(e) => {
|
|
|
|
// CI is expected to support all of the commands
|
2024-06-25 18:18:15 +00:00
|
|
|
if is_ci() && cfg!(target_os = "linux") {
|
2023-07-21 19:15:26 +00:00
|
|
|
panic!(
|
|
|
|
"expected command `{}` to be somewhere in PATH: {}",
|
|
|
|
command, e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if !output.status.success() {
|
|
|
|
panic!(
|
|
|
|
"expected command `{}` to be runnable, got error {}:\n\
|
|
|
|
stderr:{}\n\
|
|
|
|
stdout:{}\n",
|
|
|
|
command,
|
|
|
|
output.status,
|
|
|
|
String::from_utf8_lossy(&output.stderr),
|
|
|
|
String::from_utf8_lossy(&output.stdout)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
|
|
println!(
|
|
|
|
"$ {command} --version
|
|
|
|
{}",
|
|
|
|
stdout
|
|
|
|
);
|
|
|
|
if cfg!(target_os = "macos") && stdout.starts_with("GNU bash, version 3") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if cfg!(target_os = "macos") && command == "zsh" {
|
|
|
|
// HACK: At least on CI, the prompt override is not working
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2023-07-20 19:42:58 +00:00
|
|
|
/// Whether or not this running in a Continuous Integration environment.
|
|
|
|
fn is_ci() -> bool {
|
|
|
|
// Consider using `tracked_env` instead of option_env! when it is stabilized.
|
|
|
|
// `tracked_env` will handle changes, but not require rebuilding the macro
|
|
|
|
// itself like option_env does.
|
|
|
|
option_env!("CI").is_some() || option_env!("TF_BUILD").is_some()
|
|
|
|
}
|