calver 1.1.6

Calver: A lightweight command-line tool for effortless Calendar Versioning increments.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
/// Internal version module containing core data structures.
///
/// This module houses the main data structures used by the calver tool,
/// particularly the `Version` struct which handles calendar version
/// generation and management.
mod git;
mod version;

use calver::{Cli, Commands};
use clap::Parser;
use std::process;

use git::find_last_tag_for_date;
use version::Version;

/// Main entry point for the calver command-line tool.
///
/// This function serves as the primary orchestrator for the calendar versioning
/// tool. It handles command-line argument parsing, delegates to appropriate
/// handlers based on the selected subcommand, and manages error conditions
/// with proper exit codes.
///
/// # Program Flow
///
/// 1. Parse command-line arguments using clap
/// 2. Match against available commands (currently only `Bump`)
/// 3. Create a `Version` instance with provided parameters
/// 4. Handle automatic patch increment if `--last` is provided
/// 5. Generate and output the final version string
/// 6. Exit with appropriate code (0 for success, 1 for errors)
///
/// # Error Handling
///
/// The program uses proper error handling with meaningful exit codes:
/// - Exit code 0: Successful version generation
/// - Exit code 1: Error during patch calculation (e.g., overflow)
///
/// Errors are printed to stderr while successful output goes to stdout,
/// allowing for proper shell scripting and piping.
///
/// # Examples
///
/// Basic usage:
/// ```bash
/// $ calver bump
/// 2024.03.15-0
/// ```
///
/// With custom format:
/// ```bash
/// $ calver bump --format "%Y%m%d" --separator "_v"
/// 20240315_v0
/// ```
///
/// Auto-increment from last version:
/// ```bash
/// $ calver bump --last "2024.03.15-3"
/// 2024.03.15-4
/// ```
///
/// Error handling:
/// ```bash
/// $ calver bump --last "2024.03.15-65535"
/// Error: The patch calculated exceeds the maximum allowed value (65535)
/// $ echo $?
/// 1
/// ```
fn main() {
    // Parse command-line arguments using clap's derive macros
    let cli = Cli::parse();

    // Handle the parsed command
    match cli.command {
        Commands::Bump {
            format,
            separator,
            git_path,
            patch,
            last,
        } => {
            // Create a new version instance with the provided parameters
            let mut version = Version::new(format, separator, Some(patch));

            // Handle Git repository integration
            if let Some(repo_path) = git_path {
                // Validate repository
                if !git::is_valid_repository(&repo_path) {
                    eprintln!("Error: '{}' is not a valid Git repository", repo_path);
                    process::exit(1);
                }

                // Get the latest tag for today from the repository
                match find_last_tag_for_date(&repo_path, &version) {
                    Ok(Some(latest_tag)) => {
                        // Found a matching tag, use it to set the patch
                        match version.set_patch_from_last(&latest_tag) {
                            Ok(()) => {
                                println!("{}", version.generate());
                            }
                            Err(e) => {
                                eprintln!("Error: {}", e);
                                process::exit(1);
                            }
                        }
                    }
                    Ok(None) => {
                        // No matching tag found for today, start with patch 0
                        println!("{}", version.generate());
                    }
                    Err(e) => {
                        eprintln!("Error reading Git repository: {}", e);
                        process::exit(1);
                    }
                }
            }
            // Handle automatic patch increment if a previous version is provided
            else if let Some(last_version) = last {
                // Attempt to set the patch based on the last version
                match version.set_patch_from_last(&last_version) {
                    Ok(()) => {
                        println!("{}", version.generate());
                    }
                    Err(e) => {
                        eprintln!("Error: {}", e);
                        process::exit(1);
                    }
                }
            } else {
                // No last version provided, use the explicit patch number
                // and generate the version directly
                println!("{}", version.generate());
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::Parser;

    #[test]
    fn test_cli_parsing_bump_command() {
        let args = vec!["calver", "bump"];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump {
                format,
                separator,
                git_path,
                patch,
                last,
            } => {
                assert_eq!(format, None);
                assert_eq!(separator, None);
                assert_eq!(git_path, None);
                assert_eq!(patch, 0);
                assert_eq!(last, None);
            }
        }
    }

    #[test]
    fn test_cli_parsing_bump_with_format() {
        let args = vec!["calver", "bump", "--format", "%Y.%m"];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump {
                format,
                separator,
                git_path,
                patch,
                last,
            } => {
                assert_eq!(format, Some("%Y.%m".to_string()));
                assert_eq!(separator, None);
                assert_eq!(git_path, None);
                assert_eq!(patch, 0);
                assert_eq!(last, None);
            }
        }
    }

    #[test]
    fn test_cli_parsing_bump_with_separator() {
        let args = vec!["calver", "bump", "--separator", "_"];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump {
                format,
                separator,
                git_path,
                patch,
                last,
            } => {
                assert_eq!(format, None);
                assert_eq!(separator, Some("_".to_string()));
                assert_eq!(git_path, None);
                assert_eq!(patch, 0);
                assert_eq!(last, None);
            }
        }
    }

    #[test]
    fn test_cli_parsing_bump_with_patch() {
        let args = vec!["calver", "bump", "--patch", "5"];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump {
                format,
                separator,
                git_path,
                patch,
                last,
            } => {
                assert_eq!(format, None);
                assert_eq!(separator, None);
                assert_eq!(git_path, None);
                assert_eq!(patch, 5);
                assert_eq!(last, None);
            }
        }
    }

    #[test]
    fn test_cli_parsing_bump_with_all_options() {
        let args = vec![
            "calver",
            "bump",
            "--format",
            "%Y.%W",
            "--separator",
            ".",
            "--patch",
            "10",
        ];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump {
                format,
                separator,
                git_path,
                patch,
                last,
            } => {
                assert_eq!(format, Some("%Y.%W".to_string()));
                assert_eq!(separator, Some(".".to_string()));
                assert_eq!(git_path, None);
                assert_eq!(patch, 10);
                assert_eq!(last, None);
            }
        }
    }

    #[test]
    fn test_cli_parsing_bump_with_short_flags() {
        let args = vec!["calver", "bump", "-f", "%Y-%m", "-s", "_", "-p", "3"];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump {
                format,
                separator,
                git_path,
                patch,
                last,
            } => {
                assert_eq!(format, Some("%Y-%m".to_string()));
                assert_eq!(separator, Some("_".to_string()));
                assert_eq!(git_path, None);
                assert_eq!(patch, 3);
                assert_eq!(last, None);
            }
        }
    }

    #[test]
    fn test_cli_invalid_patch_value() {
        let args = vec!["calver", "bump", "--patch", "invalid"];
        let result = Cli::try_parse_from(args);
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_negative_patch_value() {
        let args = vec!["calver", "bump", "--patch", "-1"];
        let result = Cli::try_parse_from(args);
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_patch_overflow() {
        let args = vec!["calver", "bump", "--patch", "65536"]; // u16::MAX + 1
        let result = Cli::try_parse_from(args);
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_help_contains_description() {
        use clap::CommandFactory;
        let mut cmd = Cli::command();
        let help = cmd.render_help();
        let help_str = help.to_string();
        assert!(help_str.contains("Calendar Versioning"));
        assert!(help_str.contains("bump"));
    }

    #[test]
    fn test_cli_version_flag() {
        let args = vec!["calver", "--version"];
        let result = Cli::try_parse_from(args);

        assert!(result.is_err());
    }

    #[test]
    fn test_cli_no_subcommand() {
        let args = vec!["calver"];
        let result = Cli::try_parse_from(args);
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_invalid_subcommand() {
        let args = vec!["calver", "invalid"];
        let result = Cli::try_parse_from(args);
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_bump_with_empty_format() {
        temp_env::with_vars(
            [
                ("CALVER_FORMAT", None::<&str>),
                ("CALVER_SEPARATOR", None::<&str>),
                ("CALVER_PATCH", None::<&str>),
                ("CALVER_LAST", None::<&str>),
                ("CALVER_GIT_PATH", None::<&str>),
            ],
            || {
                let args = vec!["calver", "bump", "--format", ""];
                let cli = Cli::try_parse_from(args).unwrap();

                match cli.command {
                    Commands::Bump {
                        format,
                        separator,
                        git_path,
                        patch,
                        last,
                    } => {
                        assert_eq!(format, Some("".to_string()));
                        assert_eq!(separator, None);
                        assert_eq!(git_path, None);
                        assert_eq!(patch, 0);
                        assert_eq!(last, None);
                    }
                }
            },
        );
    }

    #[test]
    fn test_cli_bump_with_empty_separator() {
        temp_env::with_vars(
            [
                ("CALVER_FORMAT", None::<&str>),
                ("CALVER_SEPARATOR", None::<&str>),
                ("CALVER_PATCH", None::<&str>),
                ("CALVER_LAST", None::<&str>),
                ("CALVER_GIT_PATH", None::<&str>),
            ],
            || {
                let args = vec!["calver", "bump", "--separator", ""];
                let cli = Cli::try_parse_from(args).unwrap();

                match cli.command {
                    Commands::Bump {
                        format,
                        separator,
                        git_path,
                        patch,
                        last,
                    } => {
                        assert_eq!(format, None);
                        assert_eq!(separator, Some("".to_string()));
                        assert_eq!(git_path, None);
                        assert_eq!(patch, 0);
                        assert_eq!(last, None);
                    }
                }
            },
        );
    }

    #[test]
    fn test_cli_bump_with_max_patch() {
        temp_env::with_vars(
            [
                ("CALVER_FORMAT", None::<&str>),
                ("CALVER_SEPARATOR", None::<&str>),
                ("CALVER_PATCH", None::<&str>),
                ("CALVER_LAST", None::<&str>),
                ("CALVER_GIT_PATH", None::<&str>),
            ],
            || {
                let args = vec!["calver", "bump", "--patch", "65535"]; // u16::MAX
                let cli = Cli::try_parse_from(args).unwrap();

                match cli.command {
                    Commands::Bump {
                        format,
                        separator,
                        git_path,
                        patch,
                        last,
                    } => {
                        assert_eq!(format, None);
                        assert_eq!(separator, None);
                        assert_eq!(git_path, None);
                        assert_eq!(patch, 65535);
                        assert_eq!(last, None);
                    }
                }
            },
        );
    }

    #[test]
    fn test_cli_parsing_with_complex_format() {
        let args = vec!["calver", "bump", "--format", "%Y.%m.%d.%H.%M"];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump {
                format,
                separator,
                git_path,
                patch,
                last,
            } => {
                assert_eq!(format, Some("%Y.%m.%d.%H.%M".to_string()));
                assert_eq!(separator, None);
                assert_eq!(git_path, None);
                assert_eq!(patch, 0);
                assert_eq!(last, None);
            }
        }
    }

    #[test]
    fn test_environment_variables() {
        temp_env::with_vars(
            [
                ("CALVER_FORMAT", Some("%Y.%W")),
                ("CALVER_SEPARATOR", Some("_")),
                ("CALVER_PATCH", Some("7")),
            ],
            || {
                let args = vec!["calver", "bump"];
                let cli = Cli::try_parse_from(args).unwrap();

                match cli.command {
                    Commands::Bump {
                        format,
                        separator,
                        git_path,
                        patch,
                        last,
                    } => {
                        assert_eq!(format, Some("%Y.%W".to_string()));
                        assert_eq!(separator, Some("_".to_string()));
                        assert_eq!(git_path, None);
                        assert_eq!(patch, 7);
                        assert_eq!(last, None);
                    }
                }
            },
        );
    }

    #[test]
    fn test_cli_args_override_env() {
        temp_env::with_vars([("CALVER_FORMAT", Some("%Y.%W"))], || {
            let args = vec!["calver", "bump", "--format", "%Y.%m.%d"];
            let cli = Cli::try_parse_from(args).unwrap();

            match cli.command {
                Commands::Bump { format, .. } => {
                    assert_eq!(format, Some("%Y.%m.%d".to_string()));
                }
            }
        });
    }

    #[test]
    fn test_cli_parsing_bump_with_git_path() {
        let args = vec!["calver", "bump", "--git-path", "/path/to/repo"];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump {
                format,
                separator,
                git_path,
                patch,
                last,
            } => {
                assert_eq!(format, None);
                assert_eq!(separator, None);
                assert_eq!(git_path, Some("/path/to/repo".to_string()));
                assert_eq!(patch, 0);
                assert_eq!(last, None);
            }
        }
    }

    #[test]
    fn test_cli_parsing_bump_with_git_path_short_flag() {
        let args = vec!["calver", "bump", "-g", "."];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump {
                format,
                separator,
                git_path,
                patch,
                last,
            } => {
                assert_eq!(format, None);
                assert_eq!(separator, None);
                assert_eq!(git_path, Some(".".to_string()));
                assert_eq!(patch, 0);
                assert_eq!(last, None);
            }
        }
    }

    #[test]
    fn test_cli_parsing_bump_with_git_path_and_format() {
        let args = vec![
            "calver",
            "bump",
            "--git-path",
            "/repo",
            "--format",
            "%Y%m%d",
            "--separator",
            "_",
        ];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump {
                format,
                separator,
                git_path,
                patch,
                last,
            } => {
                assert_eq!(format, Some("%Y%m%d".to_string()));
                assert_eq!(separator, Some("_".to_string()));
                assert_eq!(git_path, Some("/repo".to_string()));
                assert_eq!(patch, 0);
                assert_eq!(last, None);
            }
        }
    }

    #[test]
    fn test_cli_git_path_conflicts_with_patch() {
        let args = vec!["calver", "bump", "--git-path", ".", "--patch", "5"];
        let result = Cli::try_parse_from(args);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("cannot be used with"));
    }

    #[test]
    fn test_cli_git_path_conflicts_with_last() {
        let args = vec![
            "calver",
            "bump",
            "--git-path",
            ".",
            "--last",
            "2024.03.15-3",
        ];
        let result = Cli::try_parse_from(args);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("cannot be used with"));
    }

    #[test]
    fn test_cli_git_path_with_empty_string() {
        let args = vec!["calver", "bump", "--git-path", ""];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump { git_path, .. } => {
                assert_eq!(git_path, Some("".to_string()));
            }
        }
    }

    #[test]
    fn test_cli_git_path_with_relative_path() {
        let args = vec!["calver", "bump", "--git-path", "../other-repo"];
        let cli = Cli::try_parse_from(args).unwrap();

        match cli.command {
            Commands::Bump { git_path, .. } => {
                assert_eq!(git_path, Some("../other-repo".to_string()));
            }
        }
    }

    #[test]
    fn test_environment_variable_git_path() {
        temp_env::with_vars([("CALVER_GIT_PATH", Some("/env/repo"))], || {
            let args = vec!["calver", "bump"];
            let cli = Cli::try_parse_from(args).unwrap();

            match cli.command {
                Commands::Bump { git_path, .. } => {
                    assert_eq!(git_path, Some("/env/repo".to_string()));
                }
            }
        });
    }

    #[test]
    fn test_cli_args_override_env_git_path() {
        temp_env::with_vars([("CALVER_GIT_PATH", Some("/env/repo"))], || {
            let args = vec!["calver", "bump", "--git-path", "/cli/repo"];
            let cli = Cli::try_parse_from(args).unwrap();

            match cli.command {
                Commands::Bump { git_path, .. } => {
                    assert_eq!(git_path, Some("/cli/repo".to_string()));
                }
            }
        });
    }
}

#[cfg(test)]
mod integration_tests {
    use std::process::Command;

    fn run_calver_command(args: &[&str]) -> (String, String, i32) {
        let output = Command::new("target/debug/calver")
            .args(args)
            .output()
            .expect("Failed to execute calver command");

        let stdout = String::from_utf8_lossy(&output.stdout).to_string();
        let stderr = String::from_utf8_lossy(&output.stderr).to_string();
        let exit_code = output.status.code().unwrap_or(-1);

        (stdout, stderr, exit_code)
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_integration_basic_bump() {
        let (stdout, stderr, exit_code) = run_calver_command(&["bump"]);

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        // Check that the output contains a date in the format YYYY.MM.DD-0
        let expected_output = format!("{}-0", chrono::Utc::now().format("%Y.%m.%d"));
        assert!(stdout.contains(&expected_output));
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_integration_with_custom_format() {
        let (stdout, stderr, exit_code) = run_calver_command(&[
            "bump",
            "--format",
            "%Y.%m",
            "--separator",
            "_",
            "--patch",
            "5",
        ]);

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        let expected_output = format!("{}_5", chrono::Utc::now().format("%Y.%m"));
        assert!(stdout.contains(&expected_output));
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_with_last_parameter_success() {
        // Integration test: Successful execution with --last parameter
        let now = chrono::Utc::now().format("%Y.%m.%d");
        let last = format!("{}-3", now);
        let (stdout, stderr, exit_code) = run_calver_command(&["bump", "--last", &last]);

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        let expected_output = format!("{}-4", now);
        assert!(stdout.contains(&expected_output));
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_with_last_parameter_not_today() {
        let (stdout, stderr, exit_code) = run_calver_command(&["bump", "--last", "2025.09.15-3"]);

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        let expected_output = format!("{}-0", chrono::Utc::now().format("%Y.%m.%d"));
        assert!(stdout.contains(&expected_output));
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_with_last_parameter_overflow_error() {
        // Integration test: Error handling for patch overflow
        let now = chrono::Utc::now().format("%Y.%m.%d");
        let last = format!("{}-{}", now, u16::MAX);
        let (stdout, stderr, exit_code) = run_calver_command(&["bump", "--last", &last]);

        assert_eq!(exit_code, 1);
        assert!(stdout.is_empty());
        assert!(stderr.contains("Error:"));
        assert!(stderr.contains("exceeds the maximum allowed value"));
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_with_patch_and_last_parameter() {
        let (stdout, stderr, exit_code) =
            run_calver_command(&["bump", "--patch", "5", "--last", "2025.09.15-3"]);

        assert_eq!(exit_code, 2);
        assert!(stdout.is_empty());
        assert!(stderr.contains("error:"));
        assert!(stderr.contains("cannot be used with"));
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_with_git_path_invalid_repository() {
        let (stdout, stderr, exit_code) =
            run_calver_command(&["bump", "--git-path", "/non/existent/repo"]);

        assert_eq!(exit_code, 1);
        assert!(stdout.is_empty());
        assert!(stderr.contains("Error:"));
        assert!(stderr.contains("not a valid Git repository"));
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_with_git_path_valid_repo_no_tags() {
        use git2::Repository;
        use tempfile::TempDir;

        // Create a temporary Git repository
        let temp_dir = TempDir::new().unwrap();
        let repo = Repository::init(temp_dir.path()).unwrap();

        // Configure repository
        let mut config = repo.config().unwrap();
        config.set_str("user.name", "Test User").unwrap();
        config.set_str("user.email", "test@example.com").unwrap();

        // Create initial commit
        let sig = repo.signature().unwrap();
        let tree_id = {
            let mut index = repo.index().unwrap();
            index.write_tree().unwrap()
        };
        let tree = repo.find_tree(tree_id).unwrap();
        repo.commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])
            .unwrap();

        let (stdout, stderr, exit_code) =
            run_calver_command(&["bump", "--git-path", temp_dir.path().to_str().unwrap()]);

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        // Should generate version with patch 0 (no tags found)
        let expected_output = format!("{}-0", chrono::Utc::now().format("%Y.%m.%d"));
        assert!(stdout.contains(&expected_output));
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_with_git_path_with_matching_tags() {
        use git2::Repository;
        use tempfile::TempDir;

        // Create a temporary Git repository
        let temp_dir = TempDir::new().unwrap();
        let repo = Repository::init(temp_dir.path()).unwrap();

        // Configure repository
        let mut config = repo.config().unwrap();
        config.set_str("user.name", "Test User").unwrap();
        config.set_str("user.email", "test@example.com").unwrap();

        // Create initial commit
        let sig = repo.signature().unwrap();
        let tree_id = {
            let mut index = repo.index().unwrap();
            index.write_tree().unwrap()
        };
        let tree = repo.find_tree(tree_id).unwrap();
        let commit = repo
            .commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])
            .unwrap();
        let commit_obj = repo.find_commit(commit).unwrap();

        // Create tags for today with different patch numbers
        let today = chrono::Utc::now().format("%Y.%m.%d").to_string();
        repo.tag_lightweight(&format!("{}-0", today), commit_obj.as_object(), false)
            .unwrap();
        repo.tag_lightweight(&format!("{}-1", today), commit_obj.as_object(), false)
            .unwrap();
        repo.tag_lightweight(&format!("{}-2", today), commit_obj.as_object(), false)
            .unwrap();

        let (stdout, stderr, exit_code) =
            run_calver_command(&["bump", "--git-path", temp_dir.path().to_str().unwrap()]);

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        // Should increment from the highest tag (2) to 3
        let expected_output = format!("{}-3", today);
        assert!(stdout.contains(&expected_output));
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_with_git_path_custom_format() {
        use git2::Repository;
        use tempfile::TempDir;

        // Create a temporary Git repository
        let temp_dir = TempDir::new().unwrap();
        let repo = Repository::init(temp_dir.path()).unwrap();

        // Configure repository
        let mut config = repo.config().unwrap();
        config.set_str("user.name", "Test User").unwrap();
        config.set_str("user.email", "test@example.com").unwrap();

        // Create initial commit
        let sig = repo.signature().unwrap();
        let tree_id = {
            let mut index = repo.index().unwrap();
            index.write_tree().unwrap()
        };
        let tree = repo.find_tree(tree_id).unwrap();
        let commit = repo
            .commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])
            .unwrap();
        let commit_obj = repo.find_commit(commit).unwrap();

        // Create tags with custom format
        let today = chrono::Utc::now().format("%Y%m%d").to_string();
        repo.tag_lightweight(&format!("{}_v0", today), commit_obj.as_object(), false)
            .unwrap();
        repo.tag_lightweight(&format!("{}_v1", today), commit_obj.as_object(), false)
            .unwrap();

        let (stdout, stderr, exit_code) = run_calver_command(&[
            "bump",
            "--git-path",
            temp_dir.path().to_str().unwrap(),
            "--format",
            "%Y%m%d",
            "--separator",
            "_v",
        ]);

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        // Should increment from the highest tag (1) to 2
        let expected_output = format!("{}_v2", today);
        assert!(stdout.contains(&expected_output));
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_with_git_path_mixed_date_tags() {
        use git2::Repository;
        use tempfile::TempDir;

        // Create a temporary Git repository
        let temp_dir = TempDir::new().unwrap();
        let repo = Repository::init(temp_dir.path()).unwrap();

        // Configure repository
        let mut config = repo.config().unwrap();
        config.set_str("user.name", "Test User").unwrap();
        config.set_str("user.email", "test@example.com").unwrap();

        // Create initial commit
        let sig = repo.signature().unwrap();
        let tree_id = {
            let mut index = repo.index().unwrap();
            index.write_tree().unwrap()
        };
        let tree = repo.find_tree(tree_id).unwrap();
        let commit = repo
            .commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])
            .unwrap();
        let commit_obj = repo.find_commit(commit).unwrap();

        // Create tags for different dates
        let today = chrono::Utc::now().format("%Y.%m.%d").to_string();
        repo.tag_lightweight("2024.01.01-10", commit_obj.as_object(), false)
            .unwrap();
        repo.tag_lightweight(&format!("{}-0", today), commit_obj.as_object(), false)
            .unwrap();
        repo.tag_lightweight("2024.12.31-5", commit_obj.as_object(), false)
            .unwrap();

        let (stdout, stderr, exit_code) =
            run_calver_command(&["bump", "--git-path", temp_dir.path().to_str().unwrap()]);

        assert_eq!(exit_code, 0);
        assert!(stderr.is_empty());
        // Should only consider today's tags and increment from 0 to 1
        let expected_output = format!("{}-1", today);
        assert!(stdout.contains(&expected_output));
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_with_git_path_current_directory() {
        // Test using "." as the git path
        let (stdout, stderr, exit_code) = run_calver_command(&["bump", "--git-path", "."]);

        // This test may succeed or fail depending on whether the current directory is a Git repo
        // We just verify that the command executes and produces appropriate output
        if exit_code == 0 {
            assert!(stderr.is_empty());
            assert!(!stdout.is_empty());
        } else {
            assert!(stderr.contains("Error:"));
        }
    }

    #[test]
    #[cfg_attr(tarpaulin, ignore)]
    fn test_main_with_git_path_tag_overflow() {
        use git2::Repository;
        use tempfile::TempDir;

        // Create a temporary Git repository
        let temp_dir = TempDir::new().unwrap();
        let repo = Repository::init(temp_dir.path()).unwrap();

        // Configure repository
        let mut config = repo.config().unwrap();
        config.set_str("user.name", "Test User").unwrap();
        config.set_str("user.email", "test@example.com").unwrap();

        // Create initial commit
        let sig = repo.signature().unwrap();
        let tree_id = {
            let mut index = repo.index().unwrap();
            index.write_tree().unwrap()
        };
        let tree = repo.find_tree(tree_id).unwrap();
        let commit = repo
            .commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])
            .unwrap();
        let commit_obj = repo.find_commit(commit).unwrap();

        // Create a tag with maximum patch number
        let today = chrono::Utc::now().format("%Y.%m.%d").to_string();
        repo.tag_lightweight(
            &format!("{}-{}", today, u16::MAX),
            commit_obj.as_object(),
            false,
        )
        .unwrap();

        let (stdout, stderr, exit_code) =
            run_calver_command(&["bump", "--git-path", temp_dir.path().to_str().unwrap()]);

        assert_eq!(exit_code, 1);
        assert!(stdout.is_empty());
        assert!(stderr.contains("Error:"));
        assert!(stderr.contains("exceeds the maximum allowed value"));
    }
}