2021-08-14 00:04:49 +00:00
|
|
|
#![cfg(feature = "env")]
|
|
|
|
|
2017-10-03 23:23:03 +00:00
|
|
|
use std::env;
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
2021-11-19 20:33:11 +00:00
|
|
|
use clap::{arg, App, Arg};
|
2017-10-03 23:23:03 +00:00
|
|
|
|
|
|
|
#[test]
|
2017-10-05 17:41:36 +00:00
|
|
|
fn env() {
|
2017-10-03 23:23:03 +00:00
|
|
|
env::set_var("CLP_TEST_ENV", "env");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([arg] "some opt").env("CLP_TEST_ENV").takes_value(true))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-04 18:24:54 +00:00
|
|
|
|
2017-10-03 23:23:03 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
2017-10-05 17:41:36 +00:00
|
|
|
assert!(m.is_present("arg"));
|
|
|
|
assert_eq!(m.occurrences_of("arg"), 0);
|
2017-10-03 23:23:03 +00:00
|
|
|
assert_eq!(m.value_of("arg").unwrap(), "env");
|
|
|
|
}
|
2017-10-05 17:41:36 +00:00
|
|
|
|
2021-07-31 11:14:38 +00:00
|
|
|
#[test]
|
|
|
|
fn env_bool_literal() {
|
|
|
|
env::set_var("CLP_TEST_FLAG_TRUE", "On");
|
|
|
|
env::set_var("CLP_TEST_FLAG_FALSE", "nO");
|
|
|
|
|
|
|
|
let r = App::new("df")
|
2021-08-11 23:51:08 +00:00
|
|
|
.arg(Arg::new("present").short('p').env("CLP_TEST_FLAG_TRUE"))
|
|
|
|
.arg(Arg::new("negated").short('n').env("CLP_TEST_FLAG_FALSE"))
|
|
|
|
.arg(Arg::new("absent").short('a').env("CLP_TEST_FLAG_ABSENT"))
|
2021-07-31 11:14:38 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
|
|
|
|
2020-11-01 04:09:59 +00:00
|
|
|
assert!(r.is_ok());
|
|
|
|
let m = r.unwrap();
|
2021-07-31 11:14:38 +00:00
|
|
|
assert!(m.is_present("present"));
|
|
|
|
assert_eq!(m.occurrences_of("present"), 0);
|
|
|
|
assert_eq!(m.value_of("present"), None);
|
|
|
|
assert!(!m.is_present("negated"));
|
|
|
|
assert!(!m.is_present("absent"));
|
2020-11-01 04:09:59 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 17:41:36 +00:00
|
|
|
#[test]
|
|
|
|
fn env_os() {
|
2018-08-28 00:55:17 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_OS", "env");
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
let r = App::new("df")
|
2020-11-01 04:09:59 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2020-11-01 04:09:59 +00:00
|
|
|
.env_os(OsStr::new("CLP_TEST_ENV_OS"))
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2020-11-01 04:09:59 +00:00
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2020-11-01 04:09:59 +00:00
|
|
|
.env("CLP_TEST_ENV_NONE")
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
|
|
|
.try_get_matches_from(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 no_env_no_takes_value() {
|
|
|
|
// 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");
|
|
|
|
|
2017-10-05 17:41:36 +00:00
|
|
|
let r = App::new("df")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(arg!([arg] "some opt").env("CLP_TEST_ENV_NONE"))
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
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() {
|
2018-10-20 03:31:38 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_WD", "env");
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2018-10-20 03:31:38 +00:00
|
|
|
.env("CLP_TEST_ENV_WD")
|
2020-11-01 04:09:59 +00:00
|
|
|
.takes_value(true)
|
2017-10-05 17:41:36 +00:00
|
|
|
.default_value("default"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
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() {
|
2018-10-20 03:31:38 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_OR", "env");
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
let r = App::new("df")
|
2020-11-01 04:09:59 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(--arg [FILE] "some arg")
|
2020-11-01 04:09:59 +00:00
|
|
|
.env("CLP_TEST_ENV_OR")
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--arg", "opt"]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
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");
|
2020-04-22 08:35:20 +00:00
|
|
|
|
|
|
|
// see https://github.com/clap-rs/clap/issues/1835
|
|
|
|
let values: Vec<_> = m.values_of("arg").unwrap().collect();
|
|
|
|
assert_eq!(values, vec!["opt"]);
|
2017-10-05 17:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn positionals() {
|
2018-10-20 03:31:38 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_P", "env");
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
let r = App::new("df")
|
2020-11-01 04:09:59 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2020-11-01 04:09:59 +00:00
|
|
|
.env("CLP_TEST_ENV_P")
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
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() {
|
2018-10-20 03:31:38 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_POR", "env");
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
let r = App::new("df")
|
2020-11-01 04:09:59 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2020-11-01 04:09:59 +00:00
|
|
|
.env("CLP_TEST_ENV_POR")
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "opt"]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
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");
|
2020-04-22 08:35:20 +00:00
|
|
|
|
|
|
|
// see https://github.com/clap-rs/clap/issues/1835
|
|
|
|
let values: Vec<_> = m.values_of("arg").unwrap().collect();
|
|
|
|
assert_eq!(values, vec!["opt"]);
|
2017-10-05 17:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_one() {
|
2018-10-20 03:31:38 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_MO", "env");
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2018-10-20 03:31:38 +00:00
|
|
|
.env("CLP_TEST_ENV_MO")
|
2020-11-01 04:09:59 +00:00
|
|
|
.takes_value(true)
|
2017-10-05 17:41:36 +00:00
|
|
|
.use_delimiter(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2017-10-05 17:41:36 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
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(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2017-10-05 17:41:36 +00:00
|
|
|
.env("CLP_TEST_ENV_MULTI1")
|
2020-11-01 04:09:59 +00:00
|
|
|
.takes_value(true)
|
2017-10-05 17:41:36 +00:00
|
|
|
.use_delimiter(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2017-10-05 17:41:36 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
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(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2017-10-05 17:41:36 +00:00
|
|
|
.env("CLP_TEST_ENV_MULTI2")
|
2020-11-01 04:09:59 +00:00
|
|
|
.takes_value(true)
|
2021-06-16 05:28:25 +00:00
|
|
|
.multiple_values(true),
|
2017-10-05 17:41:36 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
|
|
|
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"]
|
|
|
|
);
|
|
|
|
}
|
2017-10-05 18:36:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn possible_value() {
|
2018-10-20 03:31:38 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_PV", "env");
|
2017-10-05 18:36:50 +00:00
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2018-10-20 03:31:38 +00:00
|
|
|
.env("CLP_TEST_ENV_PV")
|
2020-11-01 04:09:59 +00:00
|
|
|
.takes_value(true)
|
2017-10-05 18:36:50 +00:00
|
|
|
.possible_value("env"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 18:36:50 +00:00
|
|
|
|
|
|
|
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 not_possible_value() {
|
2018-10-20 03:31:38 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_NPV", "env");
|
2017-10-05 18:36:50 +00:00
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2018-10-20 03:31:38 +00:00
|
|
|
.env("CLP_TEST_ENV_NPV")
|
2020-11-01 04:09:59 +00:00
|
|
|
.takes_value(true)
|
2017-10-05 18:36:50 +00:00
|
|
|
.possible_value("never"),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 18:36:50 +00:00
|
|
|
|
|
|
|
assert!(r.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn validator() {
|
2018-10-20 03:31:38 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_VDOR", "env");
|
2017-10-05 18:36:50 +00:00
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2018-10-20 03:31:38 +00:00
|
|
|
.env("CLP_TEST_ENV_VDOR")
|
2020-11-01 04:09:59 +00:00
|
|
|
.takes_value(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.validator(|s| {
|
|
|
|
if s == "env" {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err("not equal".to_string())
|
|
|
|
}
|
2017-10-05 18:36:50 +00:00
|
|
|
}),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 18:36:50 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2018-04-05 00:45:22 +00:00
|
|
|
#[test]
|
|
|
|
fn validator_output() {
|
2018-10-20 03:31:38 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_VO", "42");
|
2018-04-05 00:45:22 +00:00
|
|
|
|
2018-07-23 19:09:42 +00:00
|
|
|
let m = App::new("df")
|
2018-04-05 00:45:22 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2018-10-20 03:31:38 +00:00
|
|
|
.env("CLP_TEST_ENV_VO")
|
2020-11-01 04:09:59 +00:00
|
|
|
.takes_value(true)
|
2018-07-23 19:09:42 +00:00
|
|
|
.validator(|s| s.parse::<i32>()),
|
2018-11-14 17:05:06 +00:00
|
|
|
)
|
|
|
|
.get_matches_from(vec![""]);
|
2018-04-05 00:45:22 +00:00
|
|
|
|
2018-07-23 19:09:42 +00:00
|
|
|
assert_eq!(m.value_of("arg").unwrap().parse(), Ok(42));
|
2018-04-05 00:45:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 18:36:50 +00:00
|
|
|
#[test]
|
|
|
|
fn validator_invalid() {
|
2018-10-20 03:31:38 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_IV", "env");
|
2017-10-05 18:36:50 +00:00
|
|
|
|
|
|
|
let r = App::new("df")
|
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2018-10-20 03:31:38 +00:00
|
|
|
.env("CLP_TEST_ENV_IV")
|
2020-11-01 04:09:59 +00:00
|
|
|
.takes_value(true)
|
2018-01-25 04:05:05 +00:00
|
|
|
.validator(|s| {
|
|
|
|
if s != "env" {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err("is equal".to_string())
|
|
|
|
}
|
2017-10-05 18:36:50 +00:00
|
|
|
}),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 18:36:50 +00:00
|
|
|
|
|
|
|
assert!(r.is_err());
|
|
|
|
}
|