2020-01-18 12:10:07 +00:00
|
|
|
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>,
|
|
|
|
// Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and
|
|
|
|
// Andrew Hobden (@hoverbear) <andrew@hoverbear.org>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
//
|
|
|
|
// This work was derived from Structopt (https://github.com/TeXitoi/structopt)
|
|
|
|
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
|
|
|
|
// MIT/Apache 2.0 license.
|
|
|
|
|
|
|
|
mod utils;
|
|
|
|
|
|
|
|
use clap::Clap;
|
|
|
|
use utils::*;
|
|
|
|
|
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
enum Opt {
|
|
|
|
/// Fetch stuff from GitHub
|
|
|
|
Fetch {
|
|
|
|
#[clap(long)]
|
|
|
|
all: bool,
|
|
|
|
#[clap(short, long)]
|
|
|
|
/// Overwrite local branches.
|
|
|
|
force: bool,
|
|
|
|
repo: String,
|
|
|
|
},
|
|
|
|
|
|
|
|
Add {
|
|
|
|
#[clap(short, long)]
|
|
|
|
interactive: bool,
|
|
|
|
#[clap(short, long)]
|
|
|
|
verbose: bool,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fetch() {
|
|
|
|
assert_eq!(
|
|
|
|
Opt::Fetch {
|
|
|
|
all: true,
|
|
|
|
force: false,
|
|
|
|
repo: "origin".to_string()
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["test", "fetch", "--all", "origin"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt::Fetch {
|
|
|
|
all: false,
|
|
|
|
force: true,
|
|
|
|
repo: "origin".to_string()
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["test", "fetch", "-f", "origin"])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add() {
|
|
|
|
assert_eq!(
|
|
|
|
Opt::Add {
|
|
|
|
interactive: false,
|
|
|
|
verbose: false
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["test", "add"])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt::Add {
|
|
|
|
interactive: true,
|
|
|
|
verbose: true
|
|
|
|
},
|
|
|
|
Opt::parse_from(&["test", "add", "-i", "-v"])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_parse() {
|
|
|
|
let result = Opt::try_parse_from(&["test", "badcmd", "-i", "-v"]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
|
|
|
|
let result = Opt::try_parse_from(&["test", "add", "--badoption"]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
|
|
|
|
let result = Opt::try_parse_from(&["test"]);
|
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
enum Opt2 {
|
|
|
|
DoSomething { arg: String },
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// This test is specifically to make sure that hyphenated subcommands get
|
|
|
|
/// processed correctly.
|
|
|
|
fn test_hyphenated_subcommands() {
|
|
|
|
assert_eq!(
|
|
|
|
Opt2::DoSomething {
|
|
|
|
arg: "blah".to_string()
|
|
|
|
},
|
|
|
|
Opt2::parse_from(&["test", "do-something", "blah"])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
enum Opt3 {
|
|
|
|
Add,
|
|
|
|
Init,
|
|
|
|
Fetch,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_null_commands() {
|
|
|
|
assert_eq!(Opt3::Add, Opt3::parse_from(&["test", "add"]));
|
|
|
|
assert_eq!(Opt3::Init, Opt3::parse_from(&["test", "init"]));
|
|
|
|
assert_eq!(Opt3::Fetch, Opt3::parse_from(&["test", "fetch"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
#[clap(about = "Not shown")]
|
|
|
|
struct Add {
|
|
|
|
file: String,
|
|
|
|
}
|
|
|
|
/// Not shown
|
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
struct Fetch {
|
|
|
|
remote: String,
|
|
|
|
}
|
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
enum Opt4 {
|
|
|
|
// Not shown
|
|
|
|
/// Add a file
|
|
|
|
Add(Add),
|
|
|
|
Init,
|
|
|
|
/// download history from remote
|
|
|
|
Fetch(Fetch),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuple_commands() {
|
|
|
|
assert_eq!(
|
|
|
|
Opt4::Add(Add {
|
|
|
|
file: "f".to_string()
|
|
|
|
}),
|
|
|
|
Opt4::parse_from(&["test", "add", "f"])
|
|
|
|
);
|
|
|
|
assert_eq!(Opt4::Init, Opt4::parse_from(&["test", "init"]));
|
|
|
|
assert_eq!(
|
|
|
|
Opt4::Fetch(Fetch {
|
|
|
|
remote: "origin".to_string()
|
|
|
|
}),
|
|
|
|
Opt4::parse_from(&["test", "fetch", "origin"])
|
|
|
|
);
|
|
|
|
|
|
|
|
let output = get_long_help::<Opt4>();
|
|
|
|
|
|
|
|
assert!(output.contains("download history from remote"));
|
|
|
|
assert!(output.contains("Add a file"));
|
|
|
|
assert!(!output.contains("Not shown"));
|
|
|
|
}
|
|
|
|
|
2020-04-30 17:20:21 +00:00
|
|
|
#[test]
|
|
|
|
fn external_subcommand() {
|
|
|
|
#[derive(Debug, PartialEq, Clap)]
|
|
|
|
struct Opt {
|
2020-07-19 20:31:59 +00:00
|
|
|
#[clap(global = true, long)]
|
|
|
|
global: bool,
|
2020-04-30 17:20:21 +00:00
|
|
|
#[clap(subcommand)]
|
|
|
|
sub: Subcommands,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clap)]
|
|
|
|
enum Subcommands {
|
2020-05-01 12:33:36 +00:00
|
|
|
Add,
|
|
|
|
Remove,
|
2020-07-19 20:31:59 +00:00
|
|
|
Global(GlobalCmd),
|
2020-04-30 17:20:21 +00:00
|
|
|
#[clap(external_subcommand)]
|
|
|
|
Other(Vec<String>),
|
|
|
|
}
|
|
|
|
|
2020-07-19 20:31:59 +00:00
|
|
|
#[derive(Debug, PartialEq, Clap)]
|
|
|
|
struct GlobalCmd {
|
|
|
|
#[clap(from_global)]
|
|
|
|
global: bool,
|
|
|
|
}
|
|
|
|
|
2020-05-01 12:33:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt::parse_from(&["test", "add"]),
|
|
|
|
Opt {
|
2020-07-19 20:31:59 +00:00
|
|
|
global: false,
|
2020-05-01 12:33:36 +00:00
|
|
|
sub: Subcommands::Add
|
|
|
|
}
|
|
|
|
);
|
2020-04-30 17:20:21 +00:00
|
|
|
|
2020-05-01 12:33:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt::parse_from(&["test", "remove"]),
|
|
|
|
Opt {
|
2020-07-19 20:31:59 +00:00
|
|
|
global: false,
|
2020-05-01 12:33:36 +00:00
|
|
|
sub: Subcommands::Remove
|
|
|
|
}
|
|
|
|
);
|
2020-04-30 17:20:21 +00:00
|
|
|
|
2020-07-19 20:31:59 +00:00
|
|
|
assert_eq!(
|
|
|
|
Opt::parse_from(&["test", "global"]),
|
|
|
|
Opt {
|
|
|
|
global: false,
|
|
|
|
sub: Subcommands::Global(GlobalCmd { global: false })
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt::parse_from(&["test", "global", "--global"]),
|
|
|
|
Opt {
|
|
|
|
global: true,
|
|
|
|
sub: Subcommands::Global(GlobalCmd { global: true })
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-04-30 17:20:21 +00:00
|
|
|
assert!(Opt::try_parse_from(&["test"]).is_err());
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt::try_parse_from(&["test", "git", "status"]).unwrap(),
|
|
|
|
Opt {
|
2020-07-19 20:31:59 +00:00
|
|
|
global: false,
|
2020-04-30 17:20:21 +00:00
|
|
|
sub: Subcommands::Other(vec!["git".into(), "status".into()])
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn external_subcommand_os_string() {
|
|
|
|
use std::ffi::OsString;
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clap)]
|
|
|
|
struct Opt {
|
|
|
|
#[clap(subcommand)]
|
|
|
|
sub: Subcommands,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clap)]
|
|
|
|
enum Subcommands {
|
|
|
|
#[clap(external_subcommand)]
|
|
|
|
Other(Vec<OsString>),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt::try_parse_from(&["test", "git", "status"]).unwrap(),
|
|
|
|
Opt {
|
|
|
|
sub: Subcommands::Other(vec!["git".into(), "status".into()])
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(Opt::try_parse_from(&["test"]).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn external_subcommand_optional() {
|
|
|
|
#[derive(Debug, PartialEq, Clap)]
|
|
|
|
struct Opt {
|
|
|
|
#[clap(subcommand)]
|
|
|
|
sub: Option<Subcommands>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clap)]
|
|
|
|
enum Subcommands {
|
|
|
|
#[clap(external_subcommand)]
|
|
|
|
Other(Vec<String>),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Opt::try_parse_from(&["test", "git", "status"]).unwrap(),
|
|
|
|
Opt {
|
|
|
|
sub: Some(Subcommands::Other(vec!["git".into(), "status".into()]))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(Opt::try_parse_from(&["test"]).unwrap(), Opt { sub: None });
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:36:00 +00:00
|
|
|
// #[test]
|
|
|
|
// #[ignore] // FIXME (@CreepySkeleton)
|
|
|
|
// fn enum_in_enum_subsubcommand() {
|
|
|
|
// #[derive(Clap, Debug, PartialEq)]
|
|
|
|
// pub enum Opt {
|
|
|
|
// Daemon(DaemonCommand),
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[derive(Clap, Debug, PartialEq)]
|
|
|
|
// pub enum DaemonCommand {
|
|
|
|
// Start,
|
|
|
|
// Stop,
|
|
|
|
// }
|
|
|
|
|
|
|
|
// let result = Opt::try_parse_from(&["test"]);
|
|
|
|
// assert!(result.is_err());
|
|
|
|
|
|
|
|
// let result = Opt::try_parse_from(&["test", "daemon"]);
|
|
|
|
// assert!(result.is_err());
|
|
|
|
|
|
|
|
// let result = Opt::parse_from(&["test", "daemon", "start"]);
|
|
|
|
// assert_eq!(Opt::Daemon(DaemonCommand::Start), result);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// WTF??? It tests that `flatten` is essentially a synonym
|
|
|
|
// for `subcommand`. It's not documented and I doubt it was supposed to
|
|
|
|
// work this way.
|
|
|
|
// #[test]
|
|
|
|
// #[ignore]
|
|
|
|
// fn flatten_enum() {
|
|
|
|
// #[derive(Clap, Debug, PartialEq)]
|
|
|
|
// struct Opt {
|
|
|
|
// #[clap(flatten)]
|
|
|
|
// sub_cmd: SubCmd,
|
|
|
|
// }
|
|
|
|
// #[derive(Clap, Debug, PartialEq)]
|
|
|
|
// enum SubCmd {
|
|
|
|
// Foo,
|
|
|
|
// Bar,
|
|
|
|
// }
|
|
|
|
|
|
|
|
// assert!(Opt::try_parse_from(&["test"]).is_err());
|
|
|
|
// assert_eq!(
|
|
|
|
// Opt::parse_from(&["test", "foo"]),
|
|
|
|
// Opt {
|
|
|
|
// sub_cmd: SubCmd::Foo
|
|
|
|
// }
|
|
|
|
// );
|
|
|
|
// }
|