2021-08-10 12:27:49 +00:00
|
|
|
#![cfg(not(windows))]
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
use clap::{ErrorKind, Parser};
|
2021-08-10 12:27:49 +00:00
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::os::unix::ffi::OsStringExt;
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug, PartialEq, Eq)]
|
2021-08-10 12:27:49 +00:00
|
|
|
struct Positional {
|
|
|
|
arg: String,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug, PartialEq, Eq)]
|
2021-08-10 12:27:49 +00:00
|
|
|
struct Named {
|
2022-07-22 14:19:14 +00:00
|
|
|
#[clap(short, long)]
|
2021-08-10 12:27:49 +00:00
|
|
|
arg: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_positional() {
|
|
|
|
let m = Positional::try_parse_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
|
2021-08-10 12:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_option_short_space() {
|
|
|
|
let m = Named::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("-a"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
|
2021-08-10 12:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_option_short_equals() {
|
|
|
|
let m = Named::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
|
|
|
|
]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
|
2021-08-10 12:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_option_short_no_space() {
|
|
|
|
let m = Named::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
|
|
|
|
]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
|
2021-08-10 12:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_option_long_space() {
|
|
|
|
let m = Named::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("--arg"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
|
2021-08-10 12:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_strict_option_long_equals() {
|
|
|
|
let m = Named::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
|
|
|
|
]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
|
2021-08-10 12:27:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug, PartialEq, Eq)]
|
2021-08-10 12:27:49 +00:00
|
|
|
struct PositionalOs {
|
|
|
|
arg: OsString,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug, PartialEq, Eq)]
|
2021-08-10 12:27:49 +00:00
|
|
|
struct NamedOs {
|
2022-07-22 14:19:14 +00:00
|
|
|
#[clap(short, long)]
|
2021-08-10 12:27:49 +00:00
|
|
|
arg: OsString,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_positional() {
|
|
|
|
let r = PositionalOs::try_parse_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
|
|
|
|
assert_eq!(
|
|
|
|
r.unwrap(),
|
|
|
|
PositionalOs {
|
|
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_option_short_space() {
|
|
|
|
let r = NamedOs::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("-a"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
r.unwrap(),
|
|
|
|
NamedOs {
|
|
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_option_short_equals() {
|
|
|
|
let r = NamedOs::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
r.unwrap(),
|
|
|
|
NamedOs {
|
|
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_option_short_no_space() {
|
|
|
|
let r = NamedOs::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
r.unwrap(),
|
|
|
|
NamedOs {
|
|
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_option_long_space() {
|
|
|
|
let r = NamedOs::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("--arg"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
r.unwrap(),
|
|
|
|
NamedOs {
|
|
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_utf8_option_long_equals() {
|
|
|
|
let r = NamedOs::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
r.unwrap(),
|
|
|
|
NamedOs {
|
|
|
|
arg: OsString::from_vec(vec![0xe9])
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Debug, PartialEq, Parser)]
|
2021-08-10 12:27:49 +00:00
|
|
|
enum External {
|
|
|
|
#[clap(external_subcommand)]
|
|
|
|
Other(Vec<String>),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn refuse_invalid_utf8_subcommand_with_allow_external_subcommands() {
|
|
|
|
let m = External::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
OsString::from("normal"),
|
|
|
|
]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
|
2021-08-10 12:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn refuse_invalid_utf8_subcommand_args_with_allow_external_subcommands() {
|
|
|
|
let m = External::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("subcommand"),
|
|
|
|
OsString::from("normal"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
OsString::from("--another_normal"),
|
|
|
|
]);
|
|
|
|
assert!(m.is_err());
|
2022-01-25 22:19:28 +00:00
|
|
|
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
|
2021-08-10 12:27:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Debug, PartialEq, Parser)]
|
2021-08-10 12:27:49 +00:00
|
|
|
enum ExternalOs {
|
|
|
|
#[clap(external_subcommand)]
|
|
|
|
Other(Vec<OsString>),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn allow_invalid_utf8_subcommand_args_with_allow_external_subcommands() {
|
|
|
|
let m = ExternalOs::try_parse_from(vec![
|
|
|
|
OsString::from(""),
|
|
|
|
OsString::from("subcommand"),
|
|
|
|
OsString::from("normal"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
OsString::from("--another_normal"),
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
m.unwrap(),
|
|
|
|
ExternalOs::Other(vec![
|
|
|
|
OsString::from("subcommand"),
|
|
|
|
OsString::from("normal"),
|
|
|
|
OsString::from_vec(vec![0xe9]),
|
|
|
|
OsString::from("--another_normal"),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|