cargo-test-changed 0.1.1

A Cargo subcommand to run tests for changed crates and their dependents.
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
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
use std::fs;
use std::process::Command;
use tempfile::TempDir;

use crate::error::AppError;
use crate::vcs::test_utils;
use crate::vcs::{ChangeType, FileType, GitVcs, Vcs};

mod workspace_root_tests {
    use super::*;

    #[test]
    fn test_get_workspace_root_from_repo_root() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create GitVcs instance and get workspace root
        let git_vcs = GitVcs;
        let workspace_root = git_vcs.get_workspace_root(&test_repo.repo_path)?;

        // The result should match the canonical repo path
        assert_eq!(workspace_root, test_repo.repo_path.canonicalize()?);

        Ok(())
    }

    #[test]
    fn test_get_workspace_root_from_subdirectory() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create a subdirectory in the repo
        let subdir_path = test_repo.repo_path.join("subdir");
        fs::create_dir(&subdir_path)?;

        // Create GitVcs instance and get workspace root
        let git_vcs = GitVcs;
        let workspace_root = git_vcs.get_workspace_root(&subdir_path)?;

        // The result should match the canonical repo path, not the subdirectory
        assert_eq!(workspace_root, test_repo.repo_path.canonicalize()?);

        Ok(())
    }

    #[test]
    fn test_get_workspace_root_not_in_git_repo() {
        // Create a temporary directory that is not a git repo
        let temp_dir = TempDir::new().unwrap();

        // Try to get workspace root
        let git_vcs = GitVcs;
        let result = git_vcs.get_workspace_root(temp_dir.path());

        // Should return an error since this is not a git repo
        assert!(result.is_err());

        // Verify it's the expected error type
        if let Err(AppError::GitDiscoveryFailed { reason: _ }) = result {
            // Test passed
        } else {
            panic!("Expected GitDiscoveryFailed error, got: {:?}", result);
        }
    }
}

mod uncommitted_changes_tests {
    use super::*;

    // Basic scenarios
    #[test]
    fn test_no_changes_in_clean_repo() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a clean test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create and commit a file so the repo isn't empty
        test_repo.create_and_commit_file("file.txt", "content")?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should be empty since everything is committed
        assert!(changes.is_empty());

        Ok(())
    }

    #[test]
    fn test_added_file() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create an untracked file
        test_repo.create_file("new.txt", "new content")?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should have one added file
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Added);
        assert_eq!(change.file_type, FileType::File);
        assert!(change.current_path.ends_with("new.txt"));
        assert!(change.old_path.is_none());

        Ok(())
    }

    #[test]
    fn test_modified_file() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create and commit a file
        test_repo.create_and_commit_file("file.txt", "initial content")?;

        // Modify the file without committing
        test_repo.modify_file("file.txt", "modified content")?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should have one modified file
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Modified);
        assert_eq!(change.file_type, FileType::File);
        assert!(change.current_path.ends_with("file.txt"));
        assert!(change.old_path.is_none());

        Ok(())
    }

    #[test]
    fn test_deleted_file() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create and commit a file
        test_repo.create_and_commit_file("to_delete.txt", "content")?;

        // Delete the file without committing
        fs::remove_file(test_repo.repo_path.join("to_delete.txt"))?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should have one deleted file
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Removed);
        assert!(change.current_path.ends_with("to_delete.txt"));

        Ok(())
    }

    #[test]
    fn test_renamed_file() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create and commit a file
        test_repo.create_and_commit_file("original.txt", "content")?;

        // Rename the file using git mv
        Command::new("git")
            .args(["mv", "original.txt", "renamed.txt"])
            .current_dir(&test_repo.repo_path)
            .output()?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should have one renamed file
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Modified);
        assert!(change.current_path.ends_with("renamed.txt"));
        assert!(change.old_path.is_some());
        if let Some(old_path) = &change.old_path {
            assert!(old_path.ends_with("original.txt"));
        }

        Ok(())
    }

    // Complex scenarios
    #[test]
    fn test_multiple_changes() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create and commit initial files
        test_repo.create_and_commit_file("keep.txt", "initial content")?;
        test_repo.create_and_commit_file("modify.txt", "content to change")?;
        test_repo.create_and_commit_file("delete.txt", "content to delete")?;

        // Make multiple changes
        test_repo.create_file("new.txt", "new content")?;
        test_repo.modify_file("modify.txt", "modified content")?;
        fs::remove_file(test_repo.repo_path.join("delete.txt"))?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should have three changes
        assert_eq!(changes.len(), 3);

        // Find each change by path
        let added = changes.iter().find(|c| c.current_path.ends_with("new.txt"));
        let modified = changes
            .iter()
            .find(|c| c.current_path.ends_with("modify.txt"));
        let deleted = changes
            .iter()
            .find(|c| c.current_path.ends_with("delete.txt"));

        // Verify all changes were found
        assert!(added.is_some());
        assert!(modified.is_some());
        assert!(deleted.is_some());

        // Check the properties of each change
        assert_eq!(added.unwrap().change_type, ChangeType::Added);
        assert_eq!(modified.unwrap().change_type, ChangeType::Modified);
        assert_eq!(deleted.unwrap().change_type, ChangeType::Removed);

        Ok(())
    }

    #[test]
    fn test_symlink_change() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create a target file to link to
        test_repo.create_and_commit_file("target.txt", "target content")?;

        // Create a symlink to the target file
        test_repo.create_symlink("link.txt", "target.txt")?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should have one symlink change
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Added);
        assert_eq!(change.file_type, FileType::Symlink);
        assert!(change.current_path.ends_with("link.txt"));
        assert!(change.old_path.is_none());

        Ok(())
    }

    #[test]
    fn test_directory_changes() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create a new directory with a file in it
        let dir_path = test_repo.repo_path.join("new_dir");
        fs::create_dir(&dir_path)?;
        test_repo.create_file("new_dir/file_in_dir.txt", "content in dir")?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should have one file change in the directory
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Added);
        assert_eq!(change.file_type, FileType::File);
        assert!(change.current_path.ends_with("new_dir/file_in_dir.txt"));

        Ok(())
    }

    #[test]
    fn test_ignored_files() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create a .gitignore file to ignore *.log files
        test_repo.create_and_commit_file(".gitignore", "*.log\n")?;

        // Create an ignored file
        test_repo.create_file("ignored.log", "log content")?;

        // Create a non-ignored file for comparison
        test_repo.create_file("tracked.txt", "tracked content")?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should only include the non-ignored file
        assert_eq!(changes.len(), 1);

        // Verify the only change is the tracked file, not the ignored one
        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Added);
        assert!(change.current_path.ends_with("tracked.txt"));

        Ok(())
    }

    #[test]
    fn test_file_mode_change() -> Result<(), Box<dyn std::error::Error>> {
        // Skip this test on Windows as file permissions work differently
        if cfg!(windows) {
            return Ok(());
        }

        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create and commit a file
        test_repo.create_and_commit_file("script.sh", "#!/bin/sh\necho 'Hello'")?;

        // Change file permissions to make it executable
        let file_path = test_repo.repo_path.join("script.sh");
        Command::new("chmod")
            .args(["+x", file_path.to_str().unwrap()])
            .output()?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should have one modified file with changed permissions
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Modified);
        assert!(change.current_path.ends_with("script.sh"));

        Ok(())
    }

    // Uncommitted changes specific tests
    #[test]
    fn test_staged_changes() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create and stage a new file (but don't commit)
        test_repo.create_file("staged.txt", "staged content")?;
        test_repo.stage_file("staged.txt")?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should have one staged file
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Added);
        assert_eq!(change.file_type, FileType::File);
        assert!(change.current_path.ends_with("staged.txt"));

        Ok(())
    }

    #[test]
    fn test_unstaged_and_staged_changes() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create files and stage only one
        test_repo.create_file("staged.txt", "staged content")?;
        test_repo.create_file("unstaged.txt", "unstaged content")?;
        test_repo.stage_file("staged.txt")?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should have two files (one staged, one unstaged)
        assert_eq!(changes.len(), 2);

        // Find each change by path
        let staged = changes
            .iter()
            .find(|c| c.current_path.ends_with("staged.txt"));
        let unstaged = changes
            .iter()
            .find(|c| c.current_path.ends_with("unstaged.txt"));

        // Verify both changes were found
        assert!(staged.is_some());
        assert!(unstaged.is_some());

        // Both should be "Added" type
        assert_eq!(staged.unwrap().change_type, ChangeType::Added);
        assert_eq!(unstaged.unwrap().change_type, ChangeType::Added);

        Ok(())
    }

    #[test]
    fn test_empty_repository() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository without any commits
        let test_repo = test_utils::TestRepo::new()?;

        // Add a file but don't commit it
        test_repo.create_file("uncommitted.txt", "content")?;

        // Get uncommitted changes
        let git_vcs = GitVcs;
        let changes = git_vcs.get_uncommitted_changes(&test_repo.repo_path)?;

        // Should have one added file
        assert_eq!(changes.len(), 1);
        assert!(changes[0].current_path.ends_with("uncommitted.txt"));
        assert_eq!(changes[0].change_type, ChangeType::Added);

        Ok(())
    }
}

mod commit_diff_tests {
    use super::*;

    // Basic scenarios
    #[test]
    fn test_no_changes_between_same_commits() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create and commit a file
        let commit_hash = test_repo.create_and_commit_file("file.txt", "content")?;

        // Get changes between the same commit
        let git_vcs = GitVcs;
        let changes =
            git_vcs.get_changes_between(&test_repo.repo_path, &commit_hash, Some(&commit_hash))?;

        // Should be empty - no changes between the same commit
        assert!(changes.is_empty());

        Ok(())
    }

    #[test]
    fn test_added_file_between_commits() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // First commit
        let commit1 = test_repo.create_and_commit_file("file1.txt", "content")?;

        // Add a new file and commit
        test_repo.create_file("file2.txt", "content of file 2")?;
        test_repo.stage_all()?;
        let commit2 = test_repo.commit("Second commit")?;

        // Get changes between the two commits
        let git_vcs = GitVcs;
        let changes =
            git_vcs.get_changes_between(&test_repo.repo_path, &commit1, Some(&commit2))?;

        // Should have one added file
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Added);
        assert_eq!(change.file_type, FileType::File);
        assert!(change.current_path.ends_with("file2.txt"));
        assert!(change.old_path.is_none());

        Ok(())
    }

    #[test]
    fn test_modified_file_between_commits() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // First commit
        let commit1 = test_repo.create_and_commit_file("file.txt", "initial content")?;

        // Modify the file and commit
        test_repo.modify_file("file.txt", "modified content")?;
        test_repo.stage_all()?;
        let commit2 = test_repo.commit("Modify file")?;

        // Get changes between the two commits
        let git_vcs = GitVcs;
        let changes =
            git_vcs.get_changes_between(&test_repo.repo_path, &commit1, Some(&commit2))?;

        // Should have one modified file
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Modified);
        assert_eq!(change.file_type, FileType::File);
        assert!(change.current_path.ends_with("file.txt"));
        assert!(change.old_path.is_none());

        Ok(())
    }

    #[test]
    fn test_deleted_file_between_commits() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // First commit with two files
        test_repo.create_file("keep.txt", "content to keep")?;
        test_repo.create_file("delete.txt", "content to delete")?;
        test_repo.stage_all()?;
        let commit1 = test_repo.commit("Initial commit")?;

        // Delete one file and commit
        fs::remove_file(test_repo.repo_path.join("delete.txt"))?;
        test_repo.stage_all()?;
        let commit2 = test_repo.commit("Delete file")?;

        // Get changes between the two commits
        let git_vcs = GitVcs;
        let changes =
            git_vcs.get_changes_between(&test_repo.repo_path, &commit1, Some(&commit2))?;

        // Should have one deleted file
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Removed);
        assert!(change.current_path.ends_with("delete.txt"));

        Ok(())
    }

    #[test]
    fn test_renamed_file_between_commits() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // First commit
        test_repo.create_file("original.txt", "content")?;
        test_repo.stage_all()?;
        let commit1 = test_repo.commit("Initial commit")?;

        // Rename file and commit
        Command::new("git")
            .args(["mv", "original.txt", "renamed.txt"])
            .current_dir(&test_repo.repo_path)
            .output()?;
        test_repo.stage_all()?;
        let commit2 = test_repo.commit("Rename file")?;

        // Get changes between the two commits
        let git_vcs = GitVcs;
        let changes =
            git_vcs.get_changes_between(&test_repo.repo_path, &commit1, Some(&commit2))?;

        // Should have one changed file that was renamed
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Modified);
        assert!(change.current_path.ends_with("renamed.txt"));
        assert!(change.old_path.is_some());
        if let Some(old_path) = &change.old_path {
            assert!(old_path.ends_with("original.txt"));
        }

        Ok(())
    }

    // Complex scenarios
    #[test]
    fn test_multiple_changes_between_commits() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // First commit with initial files
        test_repo.create_file("keep.txt", "content to keep")?;
        test_repo.create_file("modify.txt", "content to change")?;
        test_repo.create_file("delete.txt", "content to delete")?;
        test_repo.stage_all()?;
        let commit1 = test_repo.commit("Initial commit")?;

        // Make multiple changes
        test_repo.create_file("new.txt", "new content")?;
        test_repo.modify_file("modify.txt", "modified content")?;
        fs::remove_file(test_repo.repo_path.join("delete.txt"))?;
        test_repo.stage_all()?;
        let commit2 = test_repo.commit("Multiple changes")?;

        // Get changes between the two commits
        let git_vcs = GitVcs;
        let changes =
            git_vcs.get_changes_between(&test_repo.repo_path, &commit1, Some(&commit2))?;

        // Should have three changes
        assert_eq!(changes.len(), 3);

        // Find each change by path
        let added = changes.iter().find(|c| c.current_path.ends_with("new.txt"));
        let modified = changes
            .iter()
            .find(|c| c.current_path.ends_with("modify.txt"));
        let deleted = changes
            .iter()
            .find(|c| c.current_path.ends_with("delete.txt"));

        // Verify all changes were found
        assert!(added.is_some());
        assert!(modified.is_some());
        assert!(deleted.is_some());

        // Check the properties of each change
        assert_eq!(added.unwrap().change_type, ChangeType::Added);
        assert_eq!(modified.unwrap().change_type, ChangeType::Modified);
        assert_eq!(deleted.unwrap().change_type, ChangeType::Removed);

        Ok(())
    }

    #[test]
    fn test_symlink_between_commits() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // First commit with a regular file
        test_repo.create_file("target.txt", "target content")?;
        test_repo.stage_all()?;
        let commit1 = test_repo.commit("Add target file")?;

        // Create a symlink to the target file
        test_repo.create_symlink("link.txt", "target.txt")?;
        test_repo.stage_all()?;
        let commit2 = test_repo.commit("Add symlink")?;

        // Get changes between the two commits
        let git_vcs = GitVcs;
        let changes =
            git_vcs.get_changes_between(&test_repo.repo_path, &commit1, Some(&commit2))?;

        // Should have one added symlink
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Added);
        assert_eq!(change.file_type, FileType::Symlink);
        assert!(change.current_path.ends_with("link.txt"));
        assert!(change.old_path.is_none());

        Ok(())
    }

    #[test]
    fn test_directory_between_commits() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // First commit
        let commit1 = test_repo.create_and_commit_file("file.txt", "content")?;

        // Create a new directory with a file in it
        let dir_path = test_repo.repo_path.join("new_dir");
        fs::create_dir(&dir_path)?;
        test_repo.create_file("new_dir/file_in_dir.txt", "content in dir")?;
        test_repo.stage_all()?;
        let commit2 = test_repo.commit("Add directory with file")?;

        // Get changes between the two commits
        let git_vcs = GitVcs;
        let changes =
            git_vcs.get_changes_between(&test_repo.repo_path, &commit1, Some(&commit2))?;

        // Should have one file change in the directory
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Added);
        assert_eq!(change.file_type, FileType::File);
        assert!(change.current_path.ends_with("new_dir/file_in_dir.txt"));

        Ok(())
    }

    #[test]
    fn test_ignored_file_between_commits() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create a .gitignore file in the first commit
        test_repo.create_file(".gitignore", "*.log\n")?;
        test_repo.stage_all()?;
        let commit1 = test_repo.commit("Add gitignore")?;

        // Add an ignored file and a tracked file
        test_repo.create_file("ignored.log", "log content")?;
        test_repo.create_file("tracked.txt", "tracked content")?;

        // Stage and commit only the tracked file
        // (the ignored file should not appear in git status)
        test_repo.stage_all()?;
        let commit2 = test_repo.commit("Add tracked file")?;

        // Get changes between the two commits
        let git_vcs = GitVcs;
        let changes =
            git_vcs.get_changes_between(&test_repo.repo_path, &commit1, Some(&commit2))?;

        // Should only include the tracked file, not the ignored one
        assert_eq!(changes.len(), 1);

        // Verify the only change is the tracked file, not the ignored one
        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Added);
        assert!(change.current_path.ends_with("tracked.txt"));

        Ok(())
    }

    #[test]
    fn test_file_mode_change_between_commits() -> Result<(), Box<dyn std::error::Error>> {
        // Skip this test on Windows as file permissions work differently
        if cfg!(windows) {
            return Ok(());
        }

        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create a file in the first commit
        test_repo.create_file("script.sh", "#!/bin/sh\necho 'Hello'")?;
        test_repo.stage_all()?;
        let commit1 = test_repo.commit("Add script")?;

        // Change file permissions to make it executable
        let file_path = test_repo.repo_path.join("script.sh");
        Command::new("chmod")
            .args(["+x", file_path.to_str().unwrap()])
            .output()?;
        test_repo.stage_all()?;
        let commit2 = test_repo.commit("Make script executable")?;

        // Get changes between the two commits
        let git_vcs = GitVcs;
        let changes =
            git_vcs.get_changes_between(&test_repo.repo_path, &commit1, Some(&commit2))?;

        // Should have one modified file with changed permissions
        assert_eq!(changes.len(), 1);

        let change = &changes[0];
        assert_eq!(change.change_type, ChangeType::Modified);
        assert!(change.current_path.ends_with("script.sh"));

        Ok(())
    }

    // Commit diff specific tests
    #[test]
    fn test_changes_with_default_head() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // First commit
        let commit1 = test_repo.create_and_commit_file("file1.txt", "content")?;

        // Add another file and commit
        test_repo.create_file("file2.txt", "content")?;
        test_repo.stage_all()?;
        test_repo.commit("Second commit")?;

        // Get changes between first commit and HEAD (without specifying to_ref)
        let git_vcs = GitVcs;
        let changes = git_vcs.get_changes_between(&test_repo.repo_path, &commit1, None)?;

        // Should have one added file
        assert_eq!(changes.len(), 1);
        assert!(changes[0].current_path.ends_with("file2.txt"));
        assert_eq!(changes[0].change_type, ChangeType::Added);

        Ok(())
    }

    #[test]
    fn test_changes_on_different_branch() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Initial commit on main branch
        let main_commit = test_repo.create_and_commit_file("main.txt", "main content")?;

        // Create and checkout a new branch
        Command::new("git")
            .args(["checkout", "-b", "feature-branch"])
            .current_dir(&test_repo.repo_path)
            .output()?;

        // Add a file only on the feature branch
        test_repo.create_file("feature.txt", "feature content")?;
        test_repo.stage_all()?;
        let feature_commit = test_repo.commit("Feature commit")?;

        // Get changes between main commit and feature branch commit
        let git_vcs = GitVcs;
        let changes = git_vcs.get_changes_between(
            &test_repo.repo_path,
            &main_commit,
            Some(&feature_commit),
        )?;

        // Should have one added file on the feature branch
        assert_eq!(changes.len(), 1);
        assert!(changes[0].current_path.ends_with("feature.txt"));
        assert_eq!(changes[0].change_type, ChangeType::Added);

        Ok(())
    }

    #[test]
    fn test_changes_with_merge_commit() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Initial commit on main branch
        let main_commit = test_repo.create_and_commit_file("main.txt", "main content")?;

        // Create and checkout a feature branch
        Command::new("git")
            .args(["checkout", "-b", "feature-branch"])
            .current_dir(&test_repo.repo_path)
            .output()?;

        // Add a file on the feature branch
        test_repo.create_file("feature.txt", "feature content")?;
        test_repo.stage_all()?;
        let _ = test_repo.commit("Feature commit")?;

        // Go back to main and add a different file
        Command::new("git")
            .args(["checkout", "master"])
            .current_dir(&test_repo.repo_path)
            .output()?;
        test_repo.create_file("main2.txt", "more main content")?;
        test_repo.stage_all()?;
        test_repo.commit("Second main commit")?;

        // Merge feature branch into main
        Command::new("git")
            .args(["merge", "feature-branch", "-m", "Merge feature branch"])
            .current_dir(&test_repo.repo_path)
            .output()?;

        // Get commit hash of the merge commit (HEAD)
        let merge_commit = Command::new("git")
            .args(["rev-parse", "HEAD"])
            .current_dir(&test_repo.repo_path)
            .output()?;
        let merge_commit_hash = String::from_utf8(merge_commit.stdout)?.trim().to_string();

        // Get changes between initial commit and merge commit
        let git_vcs = GitVcs;
        let changes = git_vcs.get_changes_between(
            &test_repo.repo_path,
            &main_commit,
            Some(&merge_commit_hash),
        )?;

        // Should have two added files (main2.txt and feature.txt)
        assert_eq!(changes.len(), 2);

        // Find each change by path
        let main_file = changes
            .iter()
            .find(|c| c.current_path.ends_with("main2.txt"));
        let feature_file = changes
            .iter()
            .find(|c| c.current_path.ends_with("feature.txt"));

        // Verify both changes were found
        assert!(main_file.is_some());
        assert!(feature_file.is_some());

        // Both should be "Added" type
        assert_eq!(main_file.unwrap().change_type, ChangeType::Added);
        assert_eq!(feature_file.unwrap().change_type, ChangeType::Added);

        Ok(())
    }

    #[test]
    fn test_changes_with_invalid_reference() {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new().unwrap();

        // Try to get changes with an invalid reference
        let git_vcs = GitVcs;
        let result = git_vcs.get_changes_between(&test_repo.repo_path, "non-existent-ref", None);

        // Should return an error
        assert!(result.is_err());

        // Verify it's the expected error type
        if let Err(AppError::GitOperationFailed {
            operation: _,
            reason: _,
        }) = result
        {
            // Test passed
        } else {
            panic!("Expected GitOperationFailed error, got: {:?}", result);
        }
    }

    #[test]
    fn test_changes_with_ancestry_path() -> Result<(), Box<dyn std::error::Error>> {
        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Initial commit
        let initial_commit = test_repo.create_and_commit_file("common.txt", "common content")?;

        // Create two branches with different files

        // First branch
        Command::new("git")
            .args(["checkout", "-b", "branch1"])
            .current_dir(&test_repo.repo_path)
            .output()?;
        test_repo.create_file("branch1.txt", "branch1 content")?;
        test_repo.stage_all()?;
        let branch1_commit = test_repo.commit("Branch1 commit")?;

        // Go back to initial commit and create second branch
        Command::new("git")
            .args(["checkout", &initial_commit])
            .current_dir(&test_repo.repo_path)
            .output()?;
        Command::new("git")
            .args(["checkout", "-b", "branch2"])
            .current_dir(&test_repo.repo_path)
            .output()?;
        test_repo.create_file("branch2.txt", "branch2 content")?;
        test_repo.stage_all()?;
        let branch2_commit = test_repo.commit("Branch2 commit")?;

        // Get changes between the two branch tips
        let git_vcs = GitVcs;
        let changes = git_vcs.get_changes_between(
            &test_repo.repo_path,
            &branch1_commit,
            Some(&branch2_commit),
        )?;

        // Should show changes in branch2 compared to branch1
        // Since they diverge, this will typically show both:
        // 1. The added file in branch2
        // 2. The "removal" of the branch1 file (since it doesn't exist in branch2)
        assert_eq!(changes.len(), 2);

        // Find each change by type and path
        let branch1_file = changes
            .iter()
            .find(|c| c.current_path.ends_with("branch1.txt"));
        let branch2_file = changes
            .iter()
            .find(|c| c.current_path.ends_with("branch2.txt"));

        // Verify both changes were found
        assert!(branch1_file.is_some());
        assert!(branch2_file.is_some());

        // The branch1 file should be removed, the branch2 file should be added
        assert_eq!(branch1_file.unwrap().change_type, ChangeType::Removed);
        assert_eq!(branch2_file.unwrap().change_type, ChangeType::Added);

        Ok(())
    }

    #[test]
    fn test_submodule_changes() -> Result<(), Box<dyn std::error::Error>> {
        // This test is more complex and may require setup of a separate repository
        // to use as a submodule. For simplicity, we'll just check if the function
        // doesn't crash when a submodule might be present.

        // Skip if git version is too old for submodule support
        // (This is a simplified version, in practice you might want to check the actual git version)
        let git_version = Command::new("git").args(["--version"]).output()?;
        if !String::from_utf8_lossy(&git_version.stdout).contains("git version") {
            return Ok(());
        }

        // Setup a test repository
        let test_repo = test_utils::TestRepo::new()?;

        // Create a commit so we have something to compare against
        let commit1 = test_repo.create_and_commit_file("file.txt", "content")?;

        // Try adding an empty directory that would be a submodule mount point
        // (This won't actually create a submodule but tests the code path)
        let submodule_dir = test_repo.repo_path.join("submodule");
        fs::create_dir(&submodule_dir)?;
        test_repo.create_file("submodule/.git", "gitdir: ../.git/modules/submodule")?;
        test_repo.stage_all()?;
        let commit2 = test_repo.commit("Add potential submodule")?;

        // Get changes between the two commits
        let git_vcs = GitVcs;
        let _ = git_vcs.get_changes_between(&test_repo.repo_path, &commit1, Some(&commit2))?;

        // We don't assert specific behavior since we're not actually creating a submodule,
        // we just ensure the function runs without crashing

        Ok(())
    }
}