2022-07-24 23:10:31 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2022-02-14 22:09:38 +00:00
|
|
|
use clap::{CommandFactory, Parser};
|
2019-12-29 15:53:12 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
use crate::utils;
|
2019-12-29 15:53:12 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-07-28 13:53:32 +00:00
|
|
|
fn default_value() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2019-12-29 15:53:12 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_value = "3")]
|
2021-07-28 13:53:32 +00:00
|
|
|
arg: i32,
|
|
|
|
}
|
test(derive): Provide better error info
`Parser::parse_from` will call `exit` on failure and we don't just lose
backtrace information but we don't even know which of the tests running
in parallel panicked. I ran into this when experimenting with
`clap_derive` and I couldn't tell what actually failed.
So let's switch to `Parse::try_parse_from`.
Errors went from:
```
test option_option ... ok
error: Found argument 'bar' which wasn't expected, or isn't valid in this context
USAGE:
clap_derive [OPTIONS]
For more information try --help
error: test failed, to rerun pass '--test arg_enum'
```
To:
```
test option_option ... ok
test variant_with_defined_casing ... ok
test skip_variant ... ok
test default_value ... ok
test vector ... FAILED
test option_vector ... ok
failures:
---- vector stdout ----
thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto
, pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this
context", None), ("\n\n", None), ("USAGE:\n clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), ("
\n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
vector
test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass '--test arg_enum'
```
2021-10-30 14:55:50 +00:00
|
|
|
assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(&["test"]).unwrap());
|
|
|
|
assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(&["test", "1"]).unwrap());
|
2021-07-28 13:53:32 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<Opt>();
|
2021-07-28 13:53:32 +00:00
|
|
|
assert!(help.contains("[default: 3]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value_t() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2021-07-28 13:53:32 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_value_t = 3)]
|
2021-07-28 13:53:32 +00:00
|
|
|
arg: i32,
|
|
|
|
}
|
test(derive): Provide better error info
`Parser::parse_from` will call `exit` on failure and we don't just lose
backtrace information but we don't even know which of the tests running
in parallel panicked. I ran into this when experimenting with
`clap_derive` and I couldn't tell what actually failed.
So let's switch to `Parse::try_parse_from`.
Errors went from:
```
test option_option ... ok
error: Found argument 'bar' which wasn't expected, or isn't valid in this context
USAGE:
clap_derive [OPTIONS]
For more information try --help
error: test failed, to rerun pass '--test arg_enum'
```
To:
```
test option_option ... ok
test variant_with_defined_casing ... ok
test skip_variant ... ok
test default_value ... ok
test vector ... FAILED
test option_vector ... ok
failures:
---- vector stdout ----
thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto
, pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this
context", None), ("\n\n", None), ("USAGE:\n clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), ("
\n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
vector
test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass '--test arg_enum'
```
2021-10-30 14:55:50 +00:00
|
|
|
assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(&["test"]).unwrap());
|
|
|
|
assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(&["test", "1"]).unwrap());
|
2021-07-28 13:53:32 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<Opt>();
|
2021-07-28 13:53:32 +00:00
|
|
|
assert!(help.contains("[default: 3]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn auto_default_value_t() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2021-07-28 13:53:32 +00:00
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_value_t)]
|
2019-12-29 15:53:12 +00:00
|
|
|
arg: i32,
|
|
|
|
}
|
test(derive): Provide better error info
`Parser::parse_from` will call `exit` on failure and we don't just lose
backtrace information but we don't even know which of the tests running
in parallel panicked. I ran into this when experimenting with
`clap_derive` and I couldn't tell what actually failed.
So let's switch to `Parse::try_parse_from`.
Errors went from:
```
test option_option ... ok
error: Found argument 'bar' which wasn't expected, or isn't valid in this context
USAGE:
clap_derive [OPTIONS]
For more information try --help
error: test failed, to rerun pass '--test arg_enum'
```
To:
```
test option_option ... ok
test variant_with_defined_casing ... ok
test skip_variant ... ok
test default_value ... ok
test vector ... FAILED
test option_vector ... ok
failures:
---- vector stdout ----
thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto
, pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this
context", None), ("\n\n", None), ("USAGE:\n clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), ("
\n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
vector
test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass '--test arg_enum'
```
2021-10-30 14:55:50 +00:00
|
|
|
assert_eq!(Opt { arg: 0 }, Opt::try_parse_from(&["test"]).unwrap());
|
|
|
|
assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(&["test", "1"]).unwrap());
|
2019-12-29 15:53:12 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<Opt>();
|
2019-12-29 15:53:12 +00:00
|
|
|
assert!(help.contains("[default: 0]"));
|
|
|
|
}
|
2021-12-13 22:19:12 +00:00
|
|
|
|
2022-07-24 23:10:31 +00:00
|
|
|
#[test]
|
|
|
|
fn default_values_t() {
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_values_t = vec![1, 2, 3])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg1: Vec<i32>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, default_values_t = &[4, 5, 6])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg2: Vec<i32>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, default_values_t = [7, 8, 9])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg3: Vec<i32>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, default_values_t = 10..=12)]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg4: Vec<i32>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, default_values_t = vec!["hello".to_string(), "world".to_string()])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg5: Vec<String>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, default_values_t = &vec!["foo".to_string(), "bar".to_string()])]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg6: Vec<String>,
|
|
|
|
}
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg1: vec![1, 2, 3],
|
|
|
|
arg2: vec![4, 5, 6],
|
|
|
|
arg3: vec![7, 8, 9],
|
|
|
|
arg4: vec![10, 11, 12],
|
|
|
|
arg5: vec!["hello".to_string(), "world".to_string()],
|
|
|
|
arg6: vec!["foo".to_string(), "bar".to_string()],
|
|
|
|
},
|
|
|
|
Opt::try_parse_from(&["test"]).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg1: vec![1],
|
|
|
|
arg2: vec![4, 5, 6],
|
|
|
|
arg3: vec![7, 8, 9],
|
|
|
|
arg4: vec![10, 11, 12],
|
|
|
|
arg5: vec!["hello".to_string(), "world".to_string()],
|
|
|
|
arg6: vec!["foo".to_string(), "bar".to_string()],
|
|
|
|
},
|
|
|
|
Opt::try_parse_from(&["test", "1"]).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg1: vec![1, 2, 3],
|
|
|
|
arg2: vec![4, 5, 6],
|
|
|
|
arg3: vec![7, 8, 9],
|
|
|
|
arg4: vec![42, 15],
|
|
|
|
arg5: vec!["baz".to_string()],
|
|
|
|
arg6: vec!["foo".to_string(), "bar".to_string()],
|
|
|
|
},
|
|
|
|
Opt::try_parse_from(&["test", "--arg4", "42", "--arg4", "15", "--arg5", "baz"]).unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
let help = utils::get_long_help::<Opt>();
|
|
|
|
assert!(help.contains("[default: 1 2 3]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value_os_t() {
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_value_os_t = PathBuf::from("abc.def"))]
|
2022-07-24 23:10:31 +00:00
|
|
|
arg: PathBuf,
|
|
|
|
}
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: PathBuf::from("abc.def")
|
|
|
|
},
|
|
|
|
Opt::try_parse_from(&["test"]).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg: PathBuf::from("ghi")
|
|
|
|
},
|
|
|
|
Opt::try_parse_from(&["test", "ghi"]).unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
let help = utils::get_long_help::<Opt>();
|
|
|
|
assert!(help.contains("[default: abc.def]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_values_os_t() {
|
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
|
|
|
struct Opt {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(
|
2022-07-24 23:10:31 +00:00
|
|
|
default_values_os_t = vec![PathBuf::from("abc.def"), PathBuf::from("123.foo")]
|
|
|
|
)]
|
|
|
|
arg1: Vec<PathBuf>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(
|
2022-07-24 23:10:31 +00:00
|
|
|
long,
|
|
|
|
default_values_os_t = &[PathBuf::from("bar.baz")]
|
|
|
|
)]
|
|
|
|
arg2: Vec<PathBuf>,
|
|
|
|
}
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg1: vec![PathBuf::from("abc.def"), PathBuf::from("123.foo")],
|
|
|
|
arg2: vec![PathBuf::from("bar.baz")]
|
|
|
|
},
|
|
|
|
Opt::try_parse_from(&["test"]).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Opt {
|
|
|
|
arg1: vec![PathBuf::from("ghi")],
|
|
|
|
arg2: vec![PathBuf::from("baz.bar"), PathBuf::from("foo.bar")]
|
|
|
|
},
|
|
|
|
Opt::try_parse_from(&["test", "ghi", "--arg2", "baz.bar", "--arg2", "foo.bar"]).unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
let help = utils::get_long_help::<Opt>();
|
|
|
|
assert!(help.contains("[default: abc.def 123.foo]"));
|
|
|
|
}
|
|
|
|
|
2021-12-13 22:19:12 +00:00
|
|
|
#[test]
|
|
|
|
fn detect_os_variant() {
|
2022-08-29 17:42:30 +00:00
|
|
|
#![allow(deprecated)]
|
|
|
|
|
2021-12-13 22:19:12 +00:00
|
|
|
#[derive(clap::Parser)]
|
|
|
|
pub struct Options {
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(default_value_os = "123")]
|
2021-12-13 22:19:12 +00:00
|
|
|
x: String,
|
|
|
|
}
|
2022-02-14 22:04:07 +00:00
|
|
|
Options::command().debug_assert();
|
2021-12-13 22:19:12 +00:00
|
|
|
}
|