#532 Improve date format validation to include all supported cases | Added missing valid cases

This commit is contained in:
Arkadiusz Bielewicz 2021-10-13 13:13:32 +02:00 committed by Abin Simon
parent c269d3c337
commit 8037d5f4ce
2 changed files with 41 additions and 12 deletions

View file

@ -246,8 +246,8 @@ pub fn build() -> App<'static, 'static> {
)
.arg(
Arg::with_name("classic")
.long("classic")
.help("Enable classic mode (display output similar to ls)"),
.long("classic")
.help("Enable classic mode (display output similar to ls)"),
)
.arg(
Arg::with_name("no-symlink")
@ -296,6 +296,29 @@ pub fn validate_time_format(formatter: &str) -> Result<(), String> {
loop {
match chars.next() {
Some('%') => match chars.next() {
Some('.') => match chars.next() {
Some('f') => (),
Some(n @ '3') | Some(n @ '6') | Some(n @ '9') => match chars.next() {
Some('f') => (),
Some(c) => return Err(format!("invalid format specifier: %.{}{}", n, c)),
None => return Err("missing format specifier".to_owned()),
},
Some(c) => return Err(format!("invalid format specifier: %.{}", c)),
None => return Err("missing format specifier".to_owned()),
},
Some(n @ ':') | Some(n @ '#') => match chars.next() {
Some('z') => (),
Some(c) => return Err(format!("invalid format specifier: %{}{}", n, c)),
None => return Err("missing format specifier".to_owned()),
},
Some(n @ '-') | Some(n @ '_') | Some(n @ '0') => match chars.next() {
Some('C') | Some('d') | Some('e') | Some('f') | Some('G') | Some('g')
| Some('H') | Some('I') | Some('j') | Some('k') | Some('l') | Some('M')
| Some('m') | Some('S') | Some('s') | Some('U') | Some('u') | Some('V')
| Some('W') | Some('w') | Some('Y') | Some('y') => (),
Some(c) => return Err(format!("invalid format specifier: %{}{}", n, c)),
None => return Err("missing format specifier".to_owned()),
}
Some('A') | Some('a') | Some('B') | Some('b') | Some('C') | Some('c')
| Some('D') | Some('d') | Some('e') | Some('F') | Some('f') | Some('G')
| Some('g') | Some('H') | Some('h') | Some('I') | Some('j') | Some('k')
@ -309,16 +332,6 @@ pub fn validate_time_format(formatter: &str) -> Result<(), String> {
Some(c) => return Err(format!("invalid format specifier: %{}{}", n, c)),
None => return Err("missing format specifier".to_owned()),
},
Some('.') => match chars.next() {
Some('f') => (),
Some(n @ '3') | Some(n @ '6') | Some(n @ '9') => match chars.next() {
Some('f') => (),
Some(c) => return Err(format!("invalid format specifier: %.{}{}", n, c)),
None => return Err("missing format specifier".to_owned()),
},
Some(c) => return Err(format!("invalid format specifier: %.{}", c)),
None => return Err("missing format specifier".to_owned()),
},
Some(c) => return Err(format!("invalid format specifier: %{}", c)),
None => return Err("missing format specifier".to_owned()),
},

View file

@ -608,3 +608,19 @@ fn test_date_custom_format_supports_nanos_with_length() {
.assert()
.stdout(predicate::str::is_match("testDateFormat\\.[0-9]{3}").unwrap().count(2));
}
#[test]
fn test_date_custom_format_supports_padding() {
let dir = tempdir();
dir.child("one").touch().unwrap();
dir.child("two").touch().unwrap();
cmd()
.arg("-l")
.arg("--date")
.arg("+testDateFormat%_d")
.arg("--ignore-config")
.arg(dir.path())
.assert()
.stdout(predicate::str::is_match("testDateFormat[\\s0-9]{2}").unwrap().count(2));
}