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;
|
|
|
|
|
2022-06-10 01:03:28 +00:00
|
|
|
use clap::{arg, builder::FalseyValueParser, Arg, ArgAction, Command};
|
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");
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-07-26 00:17:01 +00:00
|
|
|
.arg(
|
|
|
|
arg!([arg] "some opt")
|
|
|
|
.env("CLP_TEST_ENV")
|
|
|
|
.action(ArgAction::Set),
|
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-04 18:24:54 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-03 23:23:03 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-11-30 02:26:37 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.value_source("arg").unwrap(),
|
|
|
|
clap::parser::ValueSource::EnvVariable
|
|
|
|
);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"env"
|
|
|
|
);
|
2017-10-03 23:23:03 +00:00
|
|
|
}
|
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");
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2022-06-10 01:03:28 +00:00
|
|
|
.arg(
|
|
|
|
Arg::new("present")
|
|
|
|
.short('p')
|
|
|
|
.env("CLP_TEST_FLAG_TRUE")
|
|
|
|
.action(ArgAction::SetTrue)
|
|
|
|
.value_parser(FalseyValueParser::new()),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("negated")
|
|
|
|
.short('n')
|
|
|
|
.env("CLP_TEST_FLAG_FALSE")
|
|
|
|
.action(ArgAction::SetTrue)
|
|
|
|
.value_parser(FalseyValueParser::new()),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::new("absent")
|
|
|
|
.short('a')
|
|
|
|
.env("CLP_TEST_FLAG_ABSENT")
|
|
|
|
.action(ArgAction::SetTrue)
|
|
|
|
.value_parser(FalseyValueParser::new()),
|
|
|
|
)
|
2021-07-31 11:14:38 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2020-11-01 04:09:59 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(*m.get_one::<bool>("present").expect("defaulted by clap"));
|
|
|
|
assert!(!*m.get_one::<bool>("negated").expect("defaulted by clap"));
|
|
|
|
assert!(!*m.get_one::<bool>("absent").expect("defaulted by clap"));
|
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
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2020-11-01 04:09:59 +00:00
|
|
|
.arg(
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([arg] "some opt")
|
2022-09-13 19:55:12 +00:00
|
|
|
.env(OsStr::new("CLP_TEST_ENV_OS"))
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set),
|
2020-11-01 04:09:59 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-05 17:41:36 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"env"
|
|
|
|
);
|
2017-10-05 17:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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");
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::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_NONE")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set),
|
2020-11-01 04:09:59 +00:00
|
|
|
)
|
|
|
|
.try_get_matches_from(vec![""]);
|
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2020-11-01 04:09:59 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(!m.contains_id("arg"));
|
2022-11-30 02:26:37 +00:00
|
|
|
assert_eq!(m.value_source("arg"), None);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("arg").map(|v| v.as_str()), None);
|
2020-11-01 04:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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");
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::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
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-05 17:41:36 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(!m.contains_id("arg"));
|
2022-11-30 02:26:37 +00:00
|
|
|
assert_eq!(m.value_source("arg"), None);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(m.get_one::<String>("arg").map(|v| v.as_str()), None);
|
2017-10-05 17:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2017-10-05 17:41:36 +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_WD")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
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
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-05 17:41:36 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-11-30 02:26:37 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.value_source("arg").unwrap(),
|
|
|
|
clap::parser::ValueSource::EnvVariable
|
|
|
|
);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"env"
|
|
|
|
);
|
2017-10-05 17:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set),
|
2020-11-01 04:09:59 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "--arg", "opt"]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-05 17:41:36 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-11-30 02:26:37 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.value_source("arg").unwrap(),
|
|
|
|
clap::parser::ValueSource::CommandLine
|
|
|
|
);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"opt"
|
|
|
|
);
|
2020-04-22 08:35:20 +00:00
|
|
|
|
|
|
|
// see https://github.com/clap-rs/clap/issues/1835
|
2022-05-24 15:16:50 +00:00
|
|
|
let values: Vec<_> = m
|
|
|
|
.get_many::<String>("arg")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect();
|
2020-04-22 08:35:20 +00:00
|
|
|
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
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set),
|
2020-11-01 04:09:59 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec![""]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-05 17:41:36 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-11-30 02:26:37 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.value_source("arg").unwrap(),
|
|
|
|
clap::parser::ValueSource::EnvVariable
|
|
|
|
);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"env"
|
|
|
|
);
|
2017-10-05 17:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set),
|
2020-11-01 04:09:59 +00:00
|
|
|
)
|
2018-10-19 20:42:13 +00:00
|
|
|
.try_get_matches_from(vec!["", "opt"]);
|
2017-10-05 17:41:36 +00:00
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-05 17:41:36 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-11-30 02:26:37 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.value_source("arg").unwrap(),
|
|
|
|
clap::parser::ValueSource::CommandLine
|
|
|
|
);
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"opt"
|
|
|
|
);
|
2020-04-22 08:35:20 +00:00
|
|
|
|
|
|
|
// see https://github.com/clap-rs/clap/issues/1835
|
2022-05-24 15:16:50 +00:00
|
|
|
let values: Vec<_> = m
|
|
|
|
.get_many::<String>("arg")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect();
|
2020-04-22 08:35:20 +00:00
|
|
|
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
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2017-10-05 17:41:36 +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_MO")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 15:59:42 +00:00
|
|
|
.value_delimiter(',')
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
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
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-05 17:41:36 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_many::<String>("arg")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
vec!["env"]
|
|
|
|
);
|
2017-10-05 17:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_three() {
|
|
|
|
env::set_var("CLP_TEST_ENV_MULTI1", "env1,env2,env3");
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2017-10-05 17:41:36 +00:00
|
|
|
.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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 15:59:42 +00:00
|
|
|
.value_delimiter(',')
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
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
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-05 17:41:36 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2017-10-05 17:41:36 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("arg")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2017-10-05 17:41:36 +00:00
|
|
|
vec!["env1", "env2", "env3"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_no_delimiter() {
|
|
|
|
env::set_var("CLP_TEST_ENV_MULTI2", "env1 env2 env3");
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2017-10-05 17:41:36 +00:00
|
|
|
.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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-08-03 16:20:07 +00:00
|
|
|
.num_args(1..),
|
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
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-05 17:41:36 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2017-10-05 17:41:36 +00:00
|
|
|
assert_eq!(
|
2022-05-24 15:16:50 +00:00
|
|
|
m.get_many::<String>("arg")
|
|
|
|
.unwrap()
|
|
|
|
.map(|v| v.as_str())
|
|
|
|
.collect::<Vec<_>>(),
|
2017-10-05 17:41:36 +00:00
|
|
|
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
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2017-10-05 18:36:50 +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_PV")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["env"]),
|
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
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-05 18:36:50 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"env"
|
|
|
|
);
|
2017-10-05 18:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2017-10-05 18:36:50 +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_NPV")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-24 01:16:02 +00:00
|
|
|
.value_parser(["never"]),
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-05-23 20:42:34 +00:00
|
|
|
fn value_parser() {
|
2018-10-20 03:31:38 +00:00
|
|
|
env::set_var("CLP_TEST_ENV_VDOR", "env");
|
2017-10-05 18:36:50 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2017-10-05 18:36:50 +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_VDOR")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-23 20:42:34 +00:00
|
|
|
.value_parser(|s: &str| -> Result<String, String> {
|
2018-01-25 04:05:05 +00:00
|
|
|
if s == "env" {
|
2022-05-23 20:42:34 +00:00
|
|
|
Ok(s.to_owned())
|
2018-01-25 04:05:05 +00:00
|
|
|
} else {
|
2022-05-23 20:42:34 +00:00
|
|
|
Err("not equal".to_owned())
|
2018-01-25 04:05:05 +00:00
|
|
|
}
|
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
|
|
|
|
2021-12-27 19:57:38 +00:00
|
|
|
assert!(r.is_ok(), "{}", r.unwrap_err());
|
2017-10-05 18:36:50 +00:00
|
|
|
let m = r.unwrap();
|
2022-06-10 01:03:28 +00:00
|
|
|
assert!(m.contains_id("arg"));
|
2022-05-24 15:16:50 +00:00
|
|
|
assert_eq!(
|
|
|
|
m.get_one::<String>("arg").map(|v| v.as_str()).unwrap(),
|
|
|
|
"env"
|
|
|
|
);
|
2017-10-05 18:36:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 00:45:22 +00:00
|
|
|
#[test]
|
2022-05-23 20:42:34 +00:00
|
|
|
fn value_parser_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
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let m = Command::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")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-23 20:42:34 +00:00
|
|
|
.value_parser(clap::value_parser!(i32)),
|
2018-11-14 17:05:06 +00:00
|
|
|
)
|
2021-12-27 18:56:12 +00:00
|
|
|
.try_get_matches_from(vec![""])
|
|
|
|
.unwrap();
|
2018-04-05 00:45:22 +00:00
|
|
|
|
2022-05-23 20:42:34 +00:00
|
|
|
assert_eq!(*m.get_one::<i32>("arg").unwrap(), 42);
|
2018-04-05 00:45:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 18:36:50 +00:00
|
|
|
#[test]
|
2022-05-23 20:42:34 +00:00
|
|
|
fn value_parser_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
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let r = Command::new("df")
|
2017-10-05 18:36:50 +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_IV")
|
2022-07-26 00:17:01 +00:00
|
|
|
.action(ArgAction::Set)
|
2022-05-23 20:42:34 +00:00
|
|
|
.value_parser(|s: &str| -> Result<String, String> {
|
2018-01-25 04:05:05 +00:00
|
|
|
if s != "env" {
|
2022-05-23 20:42:34 +00:00
|
|
|
Ok(s.to_owned())
|
2018-01-25 04:05:05 +00:00
|
|
|
} 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());
|
|
|
|
}
|