ccpm 0.3.17

Claude Code Package Manager - A Git-based package manager for Claude agents
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
//! Integration tests for .gitignore management functionality
//!
//! These tests verify that CCPM correctly manages .gitignore files
//! based on the target.gitignore configuration setting.

use anyhow::Result;
use assert_cmd::Command;
use std::fs;
use std::path::Path;
use tempfile::TempDir;

/// Helper to create a test manifest with gitignore configuration
fn create_test_manifest(gitignore: bool, source_dir: &Path) -> String {
    // Convert path to string with forward slashes for TOML compatibility
    let source_path = source_dir.display().to_string().replace('\\', "/");
    format!(
        r#"
[sources]

[target]
agents = ".claude/agents"
snippets = ".claude/ccpm/snippets"
commands = ".claude/commands"
gitignore = {}

[agents.test-agent]
path = "{}/agents/test.md"

[snippets.test-snippet]
path = "{}/snippets/test.md"

[commands.test-command]
path = "{}/commands/test.md"
"#,
        gitignore, source_path, source_path, source_path
    )
}

/// Helper to create a test manifest without explicit gitignore setting
fn create_test_manifest_default(source_dir: &Path) -> String {
    // Convert path to string with forward slashes for TOML compatibility
    let source_path = source_dir.display().to_string().replace('\\', "/");
    format!(
        r#"
[sources]

[target]
agents = ".claude/agents"
snippets = ".claude/ccpm/snippets"
commands = ".claude/commands"

[agents.test-agent]
path = "{}/agents/test.md"
"#,
        source_path
    )
}

/// Helper to create a test lockfile with installed resources
fn create_test_lockfile() -> String {
    r#"
version = 1

[[agents]]
name = "test-agent"
path = "source/agents/test.md"
checksum = ""
installed_at = ".claude/agents/test-agent.md"

[[snippets]]
name = "test-snippet"
path = "source/snippets/test.md"
checksum = ""
installed_at = ".claude/ccpm/snippets/test-snippet.md"

[[commands]]
name = "test-command"
path = "source/commands/test.md"
checksum = ""
installed_at = ".claude/commands/test-command.md"
"#
    .to_string()
}

/// Create test source files that can be installed
fn create_test_source_files(source_dir: &Path) -> Result<()> {
    // Create the directories
    fs::create_dir_all(source_dir.join("agents"))?;
    fs::create_dir_all(source_dir.join("snippets"))?;
    fs::create_dir_all(source_dir.join("commands"))?;

    // Create source files
    fs::write(source_dir.join("agents/test.md"), "# Test Agent\n")?;
    fs::write(source_dir.join("snippets/test.md"), "# Test Snippet\n")?;
    fs::write(source_dir.join("commands/test.md"), "# Test Command\n")?;

    Ok(())
}

#[test]
fn test_gitignore_enabled_by_default() {
    ccpm::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Create manifest without explicit gitignore setting (should default to true)
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest_default(&source_dir)).unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, create_test_lockfile()).unwrap();

    // Run install command
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Check that .gitignore was created
    let gitignore_path = project_dir.join(".gitignore");
    assert!(
        gitignore_path.exists(),
        "Gitignore should be created by default"
    );

    // Check that it has the expected structure
    let content = fs::read_to_string(&gitignore_path).unwrap();
    assert!(content.contains("CCPM managed entries"));
    assert!(content.contains("# End of CCPM managed entries"));
}

#[test]
fn test_gitignore_explicitly_enabled() {
    ccpm::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Create manifest with gitignore = true
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir)).unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, create_test_lockfile()).unwrap();

    // Run install command
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Check that .gitignore was created
    let gitignore_path = project_dir.join(".gitignore");
    assert!(gitignore_path.exists(), "Gitignore should be created");

    // Verify content structure
    let content = fs::read_to_string(&gitignore_path).unwrap();
    assert!(content.contains("CCPM managed entries"));
    assert!(content.contains("CCPM managed entries - do not edit below this line"));
    assert!(content.contains("# End of CCPM managed entries"));
}

#[test]
fn test_gitignore_disabled() {
    ccpm::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Create manifest with gitignore = false
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest(false, &source_dir)).unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, create_test_lockfile()).unwrap();

    // Run install command
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Check that .gitignore was NOT created
    let gitignore_path = project_dir.join(".gitignore");
    assert!(
        !gitignore_path.exists(),
        "Gitignore should not be created when disabled"
    );
}

#[test]
fn test_gitignore_preserves_user_entries() {
    ccpm::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Create .claude directory
    fs::create_dir_all(project_dir.join(".claude")).unwrap();

    // Create existing gitignore with user entries
    let gitignore_path = project_dir.join(".gitignore");
    let user_content = r#"# User's custom comment
*.backup
user-file.txt
temp/

# CCPM managed entries - do not edit below this line
.claude/agents/old-agent.md
# End of CCPM managed entries
"#;
    fs::write(&gitignore_path, user_content).unwrap();

    // Create manifest with gitignore enabled
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir)).unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, create_test_lockfile()).unwrap();

    // Run install command
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Check that user entries are preserved
    let updated_content = fs::read_to_string(&gitignore_path).unwrap();
    assert!(updated_content.contains("# User's custom comment"));
    assert!(updated_content.contains("*.backup"));
    assert!(updated_content.contains("user-file.txt"));
    assert!(updated_content.contains("temp/"));

    // Check that CCPM section exists (entries will be based on what was actually installed)
    assert!(updated_content.contains("CCPM managed entries"));
    assert!(updated_content.contains("# End of CCPM managed entries"));
    assert!(updated_content.contains(".claude/ccpm/snippets/test-snippet.md"));
}

#[test]
fn test_gitignore_preserves_content_after_ccpm_section() {
    ccpm::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Create .claude directory
    fs::create_dir_all(project_dir.join(".claude")).unwrap();

    // Create existing gitignore with content after CCPM section
    let gitignore_path = project_dir.join(".gitignore");
    let user_content = r#"# Project gitignore
*.backup
temp/

# CCPM managed entries - do not edit below this line
.claude/agents/old-agent.md
# End of CCPM managed entries

# Additional entries after CCPM section
local-config.json
debug/
# End comment
"#;
    fs::write(&gitignore_path, user_content).unwrap();

    // Create manifest with gitignore enabled
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir)).unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, create_test_lockfile()).unwrap();

    // Run install command
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Check that all sections are preserved
    let updated_content = fs::read_to_string(&gitignore_path).unwrap();

    // Check content before CCPM section
    assert!(updated_content.contains("# Project gitignore"));
    assert!(updated_content.contains("*.backup"));
    assert!(updated_content.contains("temp/"));

    // Check CCPM section is updated
    assert!(updated_content.contains("CCPM managed entries"));
    assert!(updated_content.contains("# End of CCPM managed entries"));
    assert!(updated_content.contains(".claude/ccpm/snippets/test-snippet.md"));

    // Check content after CCPM section is preserved
    assert!(updated_content.contains("# Additional entries after CCPM section"));
    assert!(updated_content.contains("local-config.json"));
    assert!(updated_content.contains("debug/"));
    assert!(updated_content.contains("# End comment"));

    // Verify old CCPM entry is removed
    assert!(!updated_content.contains(".claude/agents/old-agent.md"));
}

#[test]
fn test_gitignore_update_command() {
    ccpm::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Create manifest
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir)).unwrap();

    // Create initial lockfile
    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, create_test_lockfile()).unwrap();

    // Run update command (which should also update gitignore)
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("update")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // Check that .gitignore exists after update
    let gitignore_path = project_dir.join(".gitignore");
    if gitignore_path.exists() {
        let content = fs::read_to_string(&gitignore_path).unwrap();
        assert!(content.contains("CCPM managed entries"));
    }
}

#[test]
fn test_gitignore_handles_external_paths() {
    ccpm::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();

    // Create manifest
    let manifest_path = project_dir.join("ccpm.toml");
    let manifest_content = r#"
[sources]
test-source = "https://github.com/test/repo.git"

[target]
gitignore = true

[scripts.external-script]
source = "test-source"
path = "scripts/test.sh"
version = "v1.0.0"
"#;
    fs::write(&manifest_path, manifest_content).unwrap();

    // Create lockfile with resource installed outside .claude
    let lockfile_content = r#"
version = 1

[[sources]]
name = "test-source"
url = "https://github.com/test/repo.git"
commit = "abc123"

[[scripts]]
name = "external-script"
source = "test-source"
url = "https://github.com/test/repo.git"
path = "scripts/test.sh"
version = "v1.0.0"
resolved_commit = "abc123"
checksum = "sha256:test"
installed_at = "scripts/external.sh"

[[agents]]
name = "internal-agent"
source = "test-source"
url = "https://github.com/test/repo.git"
path = "agents/test.md"
version = "v1.0.0"
resolved_commit = "abc123"
checksum = "sha256:test"
installed_at = ".claude/agents/internal.md"
"#;
    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, lockfile_content).unwrap();

    // Create directories
    fs::create_dir_all(project_dir.join(".claude/agents")).unwrap();
    fs::create_dir_all(project_dir.join("scripts")).unwrap();

    // Create resource files
    fs::write(project_dir.join("scripts/external.sh"), "#!/bin/bash\n").unwrap();
    fs::write(
        project_dir.join(".claude/agents/internal.md"),
        "# Internal\n",
    )
    .unwrap();

    // Run install command
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Check gitignore content
    let gitignore_path = project_dir.join(".gitignore");
    if gitignore_path.exists() {
        let content = fs::read_to_string(&gitignore_path).unwrap();
        // External path should use ../
        assert!(
            content.contains("../scripts/external.sh"),
            "External paths should use ../ prefix"
        );
        // Internal path should use /
        assert!(
            content.contains(".claude/agents/internal.md"),
            "Internal paths should use / prefix"
        );
    }
}

#[test]
fn test_gitignore_empty_lockfile() {
    ccpm::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Create manifest
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir)).unwrap();

    // Create empty lockfile
    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, "version = 1\n").unwrap();

    // Run install command
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Check that .gitignore is created even with no resources
    let gitignore_path = project_dir.join(".gitignore");
    assert!(
        gitignore_path.exists(),
        "Gitignore should be created even with empty lockfile"
    );

    let content = fs::read_to_string(&gitignore_path).unwrap();
    assert!(content.contains("CCPM managed entries"));
    assert!(content.contains("# End of CCPM managed entries"));
}

#[test]
fn test_gitignore_idempotent() {
    ccpm::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Create manifest
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir)).unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, create_test_lockfile()).unwrap();

    // Run install command
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Get content after first run
    let gitignore_path = project_dir.join(".gitignore");
    let first_content = if gitignore_path.exists() {
        fs::read_to_string(&gitignore_path).unwrap()
    } else {
        String::new()
    };

    // Run again
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Get content after second run
    let second_content = if gitignore_path.exists() {
        fs::read_to_string(&gitignore_path).unwrap()
    } else {
        String::new()
    };

    // Content should be the same (idempotent)
    assert_eq!(
        first_content, second_content,
        "Gitignore should be idempotent"
    );
}

#[test]
fn test_gitignore_switch_enabled_disabled() {
    ccpm::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Start with gitignore enabled
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir)).unwrap();

    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, create_test_lockfile()).unwrap();

    // Run install with gitignore enabled
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    let gitignore_path = project_dir.join(".gitignore");
    assert!(gitignore_path.exists(), "Gitignore should be created");

    // Now disable gitignore
    fs::write(&manifest_path, create_test_manifest(false, &source_dir)).unwrap();

    // Run install again
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Gitignore should still exist (we don't delete it)
    assert!(
        gitignore_path.exists(),
        "Gitignore should still exist when disabled"
    );

    // Re-enable gitignore
    fs::write(&manifest_path, create_test_manifest(true, &source_dir)).unwrap();

    // Add a user entry to the existing gitignore
    let content = fs::read_to_string(&gitignore_path).unwrap();
    let modified_content = content.replace(
        "# CCPM managed entries",
        "user-custom.txt\n\n# CCPM managed entries",
    );
    fs::write(&gitignore_path, modified_content).unwrap();

    // Run install again
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Check that user entry is preserved
    let final_content = fs::read_to_string(&gitignore_path).unwrap();
    assert!(
        final_content.contains("user-custom.txt"),
        "User entries should be preserved when re-enabling"
    );
}

#[test]
fn test_gitignore_actually_ignored_by_git() {
    ccpm::test_utils::init_test_logging(None);
    use std::process::Command as StdCommand;

    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Initialize a git repository
    StdCommand::new("git")
        .arg("init")
        .current_dir(project_dir)
        .output()
        .expect("Failed to initialize git repo");

    // Create manifest with gitignore enabled
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir)).unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, create_test_lockfile()).unwrap();

    // Run install command to create gitignore and install files
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Verify files were installed
    assert!(project_dir.join(".claude/agents/test-agent.md").exists());
    assert!(
        project_dir
            .join(".claude/ccpm/snippets/test-snippet.md")
            .exists()
    );
    assert!(
        project_dir
            .join(".claude/commands/test-command.md")
            .exists()
    );

    // Stage all files for git
    StdCommand::new("git")
        .arg("add")
        .arg(".")
        .current_dir(project_dir)
        .output()
        .expect("Failed to stage files");

    // Check git status to see what files are staged
    let output = StdCommand::new("git")
        .arg("status")
        .arg("--porcelain")
        .current_dir(project_dir)
        .output()
        .expect("Failed to get git status");

    let status = String::from_utf8_lossy(&output.stdout);

    // Verify that installed CCPM files are NOT staged (ignored by git)
    assert!(
        !status.contains("agents/test-agent.md"),
        "Agent file should be ignored by git\nGit status:\n{}",
        status
    );
    assert!(
        !status.contains("snippets/test-snippet.md"),
        "Snippet file should be ignored by git\nGit status:\n{}",
        status
    );
    assert!(
        !status.contains("commands/test-command.md"),
        "Command file should be ignored by git\nGit status:\n{}",
        status
    );

    // Verify that the gitignore file itself IS staged
    assert!(
        status.contains(".gitignore"),
        "Gitignore file should be tracked by git\nGit status:\n{}",
        status
    );

    // Verify that manifest and lockfile ARE staged
    assert!(
        status.contains("ccpm.toml"),
        "Manifest should be tracked by git\nGit status:\n{}",
        status
    );
    assert!(
        status.contains("ccpm.lock"),
        "Lockfile should be tracked by git\nGit status:\n{}",
        status
    );

    // Also test with git check-ignore to be explicit
    let check_agent = StdCommand::new("git")
        .arg("check-ignore")
        .arg(".claude/agents/test-agent.md")
        .current_dir(project_dir)
        .output()
        .expect("Failed to check-ignore");

    // git check-ignore returns 0 if the file is ignored
    assert!(
        check_agent.status.success(),
        "Agent file should be ignored by git check-ignore"
    );

    let check_snippet = StdCommand::new("git")
        .arg("check-ignore")
        .arg(".claude/ccpm/snippets/test-snippet.md")
        .current_dir(project_dir)
        .output()
        .expect("Failed to check-ignore");

    assert!(
        check_snippet.status.success(),
        "Snippet file should be ignored by git check-ignore"
    );

    let check_command = StdCommand::new("git")
        .arg("check-ignore")
        .arg(".claude/commands/test-command.md")
        .current_dir(project_dir)
        .output()
        .expect("Failed to check-ignore");

    assert!(
        check_command.status.success(),
        "Command file should be ignored by git check-ignore"
    );
}

#[test]
fn test_gitignore_disabled_files_not_ignored_by_git() {
    ccpm::test_utils::init_test_logging(None);
    use std::process::Command as StdCommand;

    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Initialize a git repository
    StdCommand::new("git")
        .arg("init")
        .current_dir(project_dir)
        .output()
        .expect("Failed to initialize git repo");

    // Create manifest with gitignore DISABLED
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest(false, &source_dir)).unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, create_test_lockfile()).unwrap();

    // Run install command (should NOT create gitignore)
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Verify files were installed
    assert!(project_dir.join(".claude/agents/test-agent.md").exists());
    assert!(
        project_dir
            .join(".claude/ccpm/snippets/test-snippet.md")
            .exists()
    );
    assert!(
        project_dir
            .join(".claude/commands/test-command.md")
            .exists()
    );

    // Stage all files for git
    StdCommand::new("git")
        .arg("add")
        .arg(".")
        .current_dir(project_dir)
        .output()
        .expect("Failed to stage files");

    // Check git status to see what files are staged
    let output = StdCommand::new("git")
        .arg("status")
        .arg("--porcelain")
        .current_dir(project_dir)
        .output()
        .expect("Failed to get git status");

    let status = String::from_utf8_lossy(&output.stdout);

    // When gitignore is disabled, installed files SHOULD be staged (NOT ignored)
    assert!(
        status.contains("agents/test-agent.md"),
        "Agent file should NOT be ignored when gitignore is disabled\nGit status:\n{}",
        status
    );
    assert!(
        status.contains("snippets/test-snippet.md"),
        "Snippet file should NOT be ignored when gitignore is disabled\nGit status:\n{}",
        status
    );
    assert!(
        status.contains("commands/test-command.md"),
        "Command file should NOT be ignored when gitignore is disabled\nGit status:\n{}",
        status
    );

    // Also test with git check-ignore to be explicit
    let check_agent = StdCommand::new("git")
        .arg("check-ignore")
        .arg(".claude/agents/test-agent.md")
        .current_dir(project_dir)
        .output()
        .expect("Failed to check-ignore");

    // git check-ignore returns non-zero if the file is NOT ignored
    assert!(
        !check_agent.status.success(),
        "Agent file should NOT be ignored when gitignore is disabled"
    );
}

#[test]
fn test_gitignore_malformed_existing() {
    ccpm::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

    // Create source files
    create_test_source_files(&source_dir).unwrap();

    // Create .claude directory
    fs::create_dir_all(project_dir.join(".claude")).unwrap();

    // Create malformed gitignore (missing end marker)
    let gitignore_path = project_dir.join(".gitignore");
    let malformed_content = r#"# Some content
user-file.txt

# CCPM managed entries - do not edit below this line
/old/entry.md
# Missing end marker!
"#;
    fs::write(&gitignore_path, malformed_content).unwrap();

    // Create manifest and lockfile
    let manifest_path = project_dir.join("ccpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir)).unwrap();

    let lockfile_path = project_dir.join("ccpm.lock");
    fs::write(&lockfile_path, create_test_lockfile()).unwrap();

    // Run install command
    Command::cargo_bin("ccpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .arg("--force")
        .current_dir(project_dir)
        .assert();

    // Check that gitignore was properly recreated
    let updated_content = fs::read_to_string(&gitignore_path).unwrap();
    assert!(updated_content.contains("# End of CCPM managed entries"));
    assert!(updated_content.contains("user-file.txt"));
    assert!(updated_content.contains("CCPM managed entries"));
}