Skip to content

Commit

Permalink
Merge pull request #5047 from epage/nu
Browse files Browse the repository at this point in the history
refactor(test): Move nushell tests to completest
  • Loading branch information
epage committed Jul 27, 2023
2 parents 053c778 + abdb1d5 commit 74109e5
Show file tree
Hide file tree
Showing 9 changed files with 670 additions and 387 deletions.
18 changes: 10 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion clap_complete/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ unicode-xid = { version = "0.2.2", optional = true }
snapbox = { version = "0.4.11", features = ["diff", "path", "examples"] }
# Cutting out `filesystem` feature
trycmd = { version = "0.14.16", default-features = false, features = ["color-auto", "diff", "examples"] }
completest = "0.0.14"
completest = "0.0.16"
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "derive", "help"] }

[[example]]
Expand Down
117 changes: 63 additions & 54 deletions clap_complete/tests/testsuite/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,61 +296,21 @@ pub fn assert_matches_path(
.matches_path(expected_path, buf);
}

pub fn has_command(command: &str) -> bool {
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
if is_ci() && cfg!(linux) {
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
}

#[cfg(unix)]
pub fn register_example(name: &str, shell: completest::Shell) {
let scratch = snapbox::path::PathFixture::mutable_temp().unwrap();
let scratch_path = scratch.path().unwrap();

let shell_name = shell.name();
let home = std::path::Path::new("tests/snapshots/home")
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/snapshots/home")
.join(name)
.join(shell_name);
let bin_path = snapbox::cmd::compile_example(name, []).unwrap();
println!("Compiling");
let manifest_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
let bin_path =
snapbox::cmd::compile_example(name, ["--manifest-path", manifest_path.to_str().unwrap()])
.unwrap();
println!("Compiled");
let bin_root = bin_path.parent().unwrap().to_owned();

let registration = std::process::Command::new(&bin_path)
Expand All @@ -374,35 +334,39 @@ pub fn register_example(name: &str, shell: completest::Shell) {
scratch.close().unwrap();
}

#[cfg(unix)]
pub fn load_runtime(name: &str, shell: completest::Shell) -> Box<dyn completest::Runtime> {
let shell_name = shell.name();
let home = std::path::Path::new("tests/snapshots/home")
let home = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/snapshots/home")
.join(name)
.join(shell_name);
let scratch = snapbox::path::PathFixture::mutable_temp()
.unwrap()
.with_template(&home)
.unwrap();
let home = scratch.path().unwrap().to_owned();
let bin_path = snapbox::cmd::compile_example(name, []).unwrap();
println!("Compiling");
let manifest_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
let bin_path =
snapbox::cmd::compile_example(name, ["--manifest-path", manifest_path.to_str().unwrap()])
.unwrap();
println!("Compiled");
let bin_root = bin_path.parent().unwrap().to_owned();

let runtime = shell.with_home(bin_root, home);
let runtime = shell.with_home(bin_root, home).unwrap();

Box::new(ScratchRuntime {
_scratch: scratch,
runtime,
})
}

#[cfg(unix)]
#[derive(Debug)]
struct ScratchRuntime {
_scratch: snapbox::path::PathFixture,
runtime: Box<dyn completest::Runtime>,
}

#[cfg(unix)]
impl completest::Runtime for ScratchRuntime {
fn home(&self) -> &std::path::Path {
self.runtime.home()
Expand All @@ -422,6 +386,51 @@ impl completest::Runtime for ScratchRuntime {
}
}

pub fn has_command(command: &str) -> bool {
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
if is_ci() && cfg!(linux) {
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
}

/// 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.
Expand Down
9 changes: 2 additions & 7 deletions clap_complete_nushell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ clap = { path = "../", version = "4.0.0", default-features = false, features = [
clap_complete = { path = "../clap_complete", version = "4.0.0" }

[dev-dependencies]
snapbox = { version = "0.4.11", features = ["diff"] }
snapbox = { version = "0.4.11", features = ["diff", "examples", "path"] }
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "help"] }
nu-cli = "0.78.0"
nu-command = "0.78.0"
nu-parser = "0.78.0"
nu-protocol = "0.78.0"
nu-test-support = "0.78.0"
reedline = "0.18.0"
completest = { version = "0.0.16", features = ["nu"] }
170 changes: 170 additions & 0 deletions clap_complete_nushell/examples/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
use clap_complete::generate;
use clap_complete_nushell::Nushell;

fn main() {
let matches = cli().get_matches();
if matches.contains_id("generate") {
let mut cmd = cli();
generate(Nushell, &mut cmd, "test", &mut std::io::stdout());
} else {
println!("{:?}", matches);
}
}

fn cli() -> clap::Command {
clap::Command::new("test")
.version("3.0")
.propagate_version(true)
.args([
clap::Arg::new("global")
.long("global")
.global(true)
.action(clap::ArgAction::SetTrue)
.help("everywhere"),
clap::Arg::new("generate").long("generate").help("generate"),
])
.subcommands([
clap::Command::new("action").args([
clap::Arg::new("set-true")
.long("set-true")
.action(clap::ArgAction::SetTrue)
.help("bool"),
clap::Arg::new("set")
.long("set")
.action(clap::ArgAction::Set)
.help("value"),
clap::Arg::new("count")
.long("count")
.action(clap::ArgAction::Count)
.help("number"),
clap::Arg::new("choice")
.long("choice")
.value_parser(["first", "second"])
.help("enum"),
]),
clap::Command::new("quote")
.args([
clap::Arg::new("single-quotes")
.long("single-quotes")
.action(clap::ArgAction::SetTrue)
.help("Can be 'always', 'auto', or 'never'"),
clap::Arg::new("double-quotes")
.long("double-quotes")
.action(clap::ArgAction::SetTrue)
.help("Can be \"always\", \"auto\", or \"never\""),
clap::Arg::new("backticks")
.long("backticks")
.action(clap::ArgAction::SetTrue)
.help("For more information see `echo test`"),
clap::Arg::new("backslash")
.long("backslash")
.action(clap::ArgAction::SetTrue)
.help("Avoid '\\n'"),
clap::Arg::new("brackets")
.long("brackets")
.action(clap::ArgAction::SetTrue)
.help("List packages [filter]"),
clap::Arg::new("expansions")
.long("expansions")
.action(clap::ArgAction::SetTrue)
.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"),
]),
clap::Command::new("value").args([
clap::Arg::new("delim").long("delim").value_delimiter(','),
clap::Arg::new("tuple").long("tuple").num_args(2),
clap::Arg::new("require-eq")
.long("require-eq")
.require_equals(true),
clap::Arg::new("term").num_args(1..).value_terminator(";"),
]),
clap::Command::new("pacman").subcommands([
clap::Command::new("one").long_flag("one").short_flag('o'),
clap::Command::new("two").long_flag("two").short_flag('t'),
]),
clap::Command::new("last")
.args([clap::Arg::new("first"), clap::Arg::new("free").last(true)]),
clap::Command::new("alias").args([
clap::Arg::new("flag")
.short('f')
.visible_short_alias('F')
.long("flag")
.action(clap::ArgAction::SetTrue)
.visible_alias("flg")
.help("cmd flag"),
clap::Arg::new("option")
.short('o')
.visible_short_alias('O')
.long("option")
.visible_alias("opt")
.help("cmd option")
.action(clap::ArgAction::Set),
clap::Arg::new("positional"),
]),
clap::Command::new("hint").args([
clap::Arg::new("choice")
.long("choice")
.action(clap::ArgAction::Set)
.value_parser(["bash", "fish", "zsh"]),
clap::Arg::new("unknown")
.long("unknown")
.value_hint(clap::ValueHint::Unknown),
clap::Arg::new("other")
.long("other")
.value_hint(clap::ValueHint::Other),
clap::Arg::new("path")
.long("path")
.short('p')
.value_hint(clap::ValueHint::AnyPath),
clap::Arg::new("file")
.long("file")
.short('f')
.value_hint(clap::ValueHint::FilePath),
clap::Arg::new("dir")
.long("dir")
.short('d')
.value_hint(clap::ValueHint::DirPath),
clap::Arg::new("exe")
.long("exe")
.short('e')
.value_hint(clap::ValueHint::ExecutablePath),
clap::Arg::new("cmd_name")
.long("cmd-name")
.value_hint(clap::ValueHint::CommandName),
clap::Arg::new("cmd")
.long("cmd")
.short('c')
.value_hint(clap::ValueHint::CommandString),
clap::Arg::new("command_with_args")
.action(clap::ArgAction::Set)
.num_args(1..)
.trailing_var_arg(true)
.value_hint(clap::ValueHint::CommandWithArguments),
clap::Arg::new("user")
.short('u')
.long("user")
.value_hint(clap::ValueHint::Username),
clap::Arg::new("host")
.short('H')
.long("host")
.value_hint(clap::ValueHint::Hostname),
clap::Arg::new("url")
.long("url")
.value_hint(clap::ValueHint::Url),
clap::Arg::new("email")
.long("email")
.value_hint(clap::ValueHint::EmailAddress),
]),
])
}
Loading

0 comments on commit 74109e5

Please sign in to comment.