add additional tests

This commit is contained in:
Benjamin Fry 2017-10-05 10:41:36 -07:00
parent 779560b5d5
commit 5fccd1fef8
2 changed files with 197 additions and 11 deletions

View file

@ -3320,11 +3320,15 @@ impl<'a, 'b> Arg<'a, 'b> {
/// will return `0` even though the [`ArgMatches::value_of`] will return the default specified.
///
/// **NOTE:** If the user *does not* use this argument at runtime [`ArgMatches::is_present`] will
/// still return `true`. If you wish to determine whether the argument was used at runtime or
/// not, consider [`ArgMatches::occurrences_of`] which will return `0` if the argument was *not*
/// used at runtime.
/// return `true` if the variable is present in the environemnt . If you wish to determine whether
/// the argument was used at runtime or not, consider [`ArgMatches::occurrences_of`] which will
/// return `0` if the argument was *not* used at runtime.
///
/// **NOTE:** This implicitly sets [`Arg::takes_value(true)`].
///
/// **NOTE:** If [`Arg::multiple(true)`] is set then [`Arg::use_delimiter(true)`] should also be
/// set. Otherwise, only a single argument will be returned from the environment variable. The
/// default delimiter is `,` and follows all the other delimiter rules.
///
/// # Examples
///
@ -3344,7 +3348,6 @@ impl<'a, 'b> Arg<'a, 'b> {
/// "prog"
/// ]);
///
/// # env::remove_var("MY_FLAG");
/// assert_eq!(m.value_of("flag"), Some("env"));
/// ```
///
@ -3364,7 +3367,6 @@ impl<'a, 'b> Arg<'a, 'b> {
/// "prog", "--flag", "opt"
/// ]);
///
/// # env::remove_var("MY_FLAG");
/// assert_eq!(m.value_of("flag"), Some("opt"));
/// ```
///
@ -3386,9 +3388,29 @@ impl<'a, 'b> Arg<'a, 'b> {
/// "prog"
/// ]);
///
/// # env::remove_var("MY_FLAG");
/// assert_eq!(m.value_of("flag"), Some("env"));
/// ```
///
/// In this example, we show the use of multiple values in a single environment variable:
///
/// ```rust
/// # use std::env;
/// # use clap::{App, Arg};
///
/// env::set_var("MY_FLAG_MULTI", "env1,env2");
///
/// let m = App::new("prog")
/// .arg(Arg::with_name("flag")
/// .long("flag")
/// .env("MY_FLAG_MULTI")
/// .multiple(true)
/// .use_delimiter(true))
/// .get_matches_from(vec![
/// "prog"
/// ]);
///
/// assert_eq!(m.values_of("flag").unwrap().collect::<Vec<_>>(), vec!["env1", "env2"]);
/// ```
pub fn env(self, name: &'a str) -> Self {
self.env_os(OsStr::new(name))
}

View file

@ -1,22 +1,186 @@
extern crate clap;
extern crate regex;
use std::env;
use std::ffi::OsStr;
use clap::{App, Arg, ErrorKind};
use clap::{App, Arg};
#[test]
fn env_no_default() {
fn env() {
env::set_var("CLP_TEST_ENV", "env");
let r = App::new("df")
.arg(Arg::from_usage("[arg] 'some opt'").env("CLP_TEST_ENV"))
.get_matches_from_safe(vec![""]);
env::remove_var("CLP_TEST_ENV");
assert!(r.is_ok());
let m = r.unwrap();
// assert!(m.is_present("arg")); // TODO: should this be true?
assert!(m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 0);
assert_eq!(m.value_of("arg").unwrap(), "env");
}
#[test]
fn env_os() {
env::set_var("CLP_TEST_ENV", "env");
let r = App::new("df")
.arg(
Arg::from_usage("[arg] 'some opt'").env_os(OsStr::new("CLP_TEST_ENV")),
)
.get_matches_from_safe(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 0);
assert_eq!(m.value_of("arg").unwrap(), "env");
}
#[test]
fn no_env() {
// All the other tests use the presence of the Environment variable...
// we need another variable just in case one of the others is running at the same time...
env::remove_var("CLP_TEST_ENV_NONE");
let r = App::new("df")
.arg(Arg::from_usage("[arg] 'some opt'").env("CLP_TEST_ENV_NONE"))
.get_matches_from_safe(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(!m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 0);
assert_eq!(m.value_of("arg"), None);
}
#[test]
fn with_default() {
env::set_var("CLP_TEST_ENV", "env");
let r = App::new("df")
.arg(
Arg::from_usage("[arg] 'some opt'")
.env("CLP_TEST_ENV")
.default_value("default"),
)
.get_matches_from_safe(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 0);
assert_eq!(m.value_of("arg").unwrap(), "env");
}
#[test]
fn opt_user_override() {
env::set_var("CLP_TEST_ENV", "env");
let r = App::new("df")
.arg(
Arg::from_usage("--arg [FILE] 'some arg'").env("CLP_TEST_ENV"),
)
.get_matches_from_safe(vec!["", "--arg", "opt"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 1);
assert_eq!(m.value_of("arg").unwrap(), "opt");
}
#[test]
fn positionals() {
env::set_var("CLP_TEST_ENV", "env");
let r = App::new("df")
.arg(Arg::from_usage("[arg] 'some opt'").env("CLP_TEST_ENV"))
.get_matches_from_safe(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 0);
assert_eq!(m.value_of("arg").unwrap(), "env");
}
#[test]
fn positionals_user_override() {
env::set_var("CLP_TEST_ENV", "env");
let r = App::new("df")
.arg(Arg::from_usage("[arg] 'some opt'").env("CLP_TEST_ENV"))
.get_matches_from_safe(vec!["", "opt"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 1);
assert_eq!(m.value_of("arg").unwrap(), "opt");
}
#[test]
fn multiple_one() {
env::set_var("CLP_TEST_ENV", "env");
let r = App::new("df")
.arg(
Arg::from_usage("[arg] 'some opt'")
.env("CLP_TEST_ENV")
.use_delimiter(true)
.multiple(true),
)
.get_matches_from_safe(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 0);
assert_eq!(m.values_of("arg").unwrap().collect::<Vec<_>>(), vec!["env"]);
}
#[test]
fn multiple_three() {
env::set_var("CLP_TEST_ENV_MULTI1", "env1,env2,env3");
let r = App::new("df")
.arg(
Arg::from_usage("[arg] 'some opt'")
.env("CLP_TEST_ENV_MULTI1")
.use_delimiter(true)
.multiple(true),
)
.get_matches_from_safe(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 0);
assert_eq!(
m.values_of("arg").unwrap().collect::<Vec<_>>(),
vec!["env1", "env2", "env3"]
);
}
#[test]
fn multiple_no_delimiter() {
env::set_var("CLP_TEST_ENV_MULTI2", "env1 env2 env3");
let r = App::new("df")
.arg(
Arg::from_usage("[arg] 'some opt'")
.env("CLP_TEST_ENV_MULTI2")
.multiple(true),
)
.get_matches_from_safe(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 0);
assert_eq!(
m.values_of("arg").unwrap().collect::<Vec<_>>(),
vec!["env1 env2 env3"]
);
}