| 2503 | } |
| 2504 | |
| 2505 | func TestTimestampFlagApply_MultipleFormats(t *testing.T) { |
| 2506 | now := time.Now().UTC() |
| 2507 | |
| 2508 | testCases := []struct { |
| 2509 | caseName string |
| 2510 | layoutsPrecisions map[string]time.Duration |
| 2511 | expRes time.Time |
| 2512 | expErrValidation func(err error) (validation error) |
| 2513 | }{ |
| 2514 | { |
| 2515 | caseName: "all_valid_layouts", |
| 2516 | layoutsPrecisions: map[string]time.Duration{ |
| 2517 | time.RFC3339: time.Second, |
| 2518 | time.DateTime: time.Second, |
| 2519 | time.RFC1123: time.Second, |
| 2520 | }, |
| 2521 | expRes: now.Truncate(time.Second), |
| 2522 | }, |
| 2523 | { |
| 2524 | caseName: "one_invalid_layout", |
| 2525 | layoutsPrecisions: map[string]time.Duration{ |
| 2526 | time.RFC3339: time.Second, |
| 2527 | time.DateTime: time.Second, |
| 2528 | "foo": 0, |
| 2529 | }, |
| 2530 | expRes: now.Truncate(time.Second), |
| 2531 | }, |
| 2532 | { |
| 2533 | caseName: "multiple_invalid_layouts", |
| 2534 | layoutsPrecisions: map[string]time.Duration{ |
| 2535 | time.RFC3339: time.Second, |
| 2536 | "foo": 0, |
| 2537 | time.DateTime: time.Second, |
| 2538 | "bar": 0, |
| 2539 | }, |
| 2540 | expRes: now.Truncate(time.Second), |
| 2541 | }, |
| 2542 | { |
| 2543 | caseName: "all_invalid_layouts", |
| 2544 | layoutsPrecisions: map[string]time.Duration{ |
| 2545 | "foo": 0, |
| 2546 | "2024-08-07 74:01:82Z-100": 0, |
| 2547 | "25:70": 0, |
| 2548 | "": 0, |
| 2549 | }, |
| 2550 | expErrValidation: func(err error) error { |
| 2551 | if err == nil { |
| 2552 | return errors.New("got nil err") |
| 2553 | } |
| 2554 | |
| 2555 | found := regexp.MustCompile(`(cannot parse ".+" as ".*")|(extra text: ".+")`).Match([]byte(err.Error())) |
| 2556 | if !found { |
| 2557 | return fmt.Errorf("given error does not satisfy pattern: %w", err) |
| 2558 | } |
| 2559 | |
| 2560 | return nil |
| 2561 | }, |
| 2562 | }, |