Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
1837 lines (1519 loc) · 52.1 KB
/
Copy pathmain_test.go
File metadata and controls
1837 lines (1519 loc) · 52.1 KB
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
package main
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/josephschmitt/pj/internal/discover"
"github.com/josephschmitt/pj/internal/icons"
)
var (
// Binary path will be set in TestMain
binaryPath string
)
// TestMain builds the binary before running tests and cleans up after
func TestMain(m *testing.M) {
// Build the binary
binaryName := "pj-test-binary"
if os.Getenv("GOOS") == "windows" || filepath.Ext(os.Args[0]) == ".exe" {
binaryName += ".exe"
}
binaryPath = filepath.Join(os.TempDir(), binaryName)
build := exec.Command("go", "build", "-o", binaryPath)
if err := build.Run(); err != nil {
_, _ = os.Stderr.WriteString("Failed to build binary: " + err.Error() + "\n")
os.Exit(1)
}
// Run tests
code := m.Run()
// Cleanup
_ = os.Remove(binaryPath)
os.Exit(code)
}
// testEnv holds test environment directories
type testEnv struct {
configDir string
cacheDir string
t *testing.T
}
// setupTestEnv creates a test environment with isolated config and cache
func setupTestEnv(t *testing.T) *testEnv {
t.Helper()
tmpConfig := t.TempDir()
tmpCache := t.TempDir()
// Create an empty config file to override defaults
configDir := filepath.Join(tmpConfig, "pj")
if err := os.MkdirAll(configDir, 0755); err != nil {
t.Fatal(err)
}
configPath := filepath.Join(configDir, "config.yaml")
// Use old format with icons field for backward compatibility
emptyConfig := `search_paths: []
markers:
- .git
- go.mod
- package.json
- Cargo.toml
- pyproject.toml
- Makefile
- flake.nix
- Dockerfile
max_depth: 3
excludes:
- node_modules
- .terraform
- vendor
- .git
- target
- dist
- build
cache_ttl: 300
icons:
.git: "\ue65d"
go.mod: "\U000f07d3"
package.json: "\U000f0399"
Cargo.toml: "\ue68b"
pyproject.toml: "\ue606"
Makefile: "\ue673"
flake.nix: "\ue843"
Dockerfile: "\ue7b0"
`
if err := os.WriteFile(configPath, []byte(emptyConfig), 0644); err != nil {
t.Fatal(err)
}
return &testEnv{
configDir: tmpConfig,
cacheDir: tmpCache,
t: t,
}
}
// runPJ runs the pj binary with arguments in the test environment
func (env *testEnv) runPJ(args ...string) (string, string, error) {
env.t.Helper()
cmd := exec.Command(binaryPath, args...)
cmd.Env = append(os.Environ(),
"XDG_CONFIG_HOME="+env.configDir,
"XDG_CACHE_HOME="+env.cacheDir,
)
// Capture stdout and stderr
var stdout, stderr strings.Builder
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
// runPJWithStdin runs the pj binary with stdin input in the test environment
func (env *testEnv) runPJWithStdin(stdin string, args ...string) (string, string, error) {
env.t.Helper()
cmd := exec.Command(binaryPath, args...)
cmd.Env = append(os.Environ(),
"XDG_CONFIG_HOME="+env.configDir,
"XDG_CACHE_HOME="+env.cacheDir,
)
// Set up stdin
cmd.Stdin = strings.NewReader(stdin)
// Capture stdout and stderr
var stdout, stderr strings.Builder
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
// Helper to run the pj binary with arguments (for simple tests)
func runPJ(t *testing.T, args ...string) (string, string, error) {
t.Helper()
env := setupTestEnv(t)
return env.runPJ(args...)
}
// Helper to create a test project structure
func createTestProject(t *testing.T, base, name string, markers ...string) string {
t.Helper()
dir := filepath.Join(base, name)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
for _, marker := range markers {
path := filepath.Join(dir, marker)
if strings.HasSuffix(marker, "/") {
if err := os.MkdirAll(path, 0755); err != nil {
t.Fatal(err)
}
} else {
if err := os.WriteFile(path, []byte{}, 0644); err != nil {
t.Fatal(err)
}
}
}
return dir
}
func TestCLI_Version(t *testing.T) {
stdout, _, err := runPJ(t, "--version")
if err != nil {
t.Fatalf("--version failed: %v", err)
}
if !strings.Contains(stdout, "pj version") {
t.Errorf("--version output = %q, should contain 'pj version'", stdout)
}
}
func TestCLI_Discovery(t *testing.T) {
tmpDir := t.TempDir()
// Create test projects
createTestProject(t, tmpDir, "project1", ".git/")
createTestProject(t, tmpDir, "project2", "go.mod")
createTestProject(t, tmpDir, "project3", "package.json")
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache")
if err != nil {
t.Fatalf("pj failed: %v\nStderr: %s", err, stderr)
}
// Verify all projects are found
if !strings.Contains(stdout, "project1") {
t.Error("Output should contain project1")
}
if !strings.Contains(stdout, "project2") {
t.Error("Output should contain project2")
}
if !strings.Contains(stdout, "project3") {
t.Error("Output should contain project3")
}
// Verify output contains full paths
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 3 {
t.Errorf("Expected 3 lines of output, got %d", len(lines))
}
for _, line := range lines {
if !strings.HasPrefix(line, tmpDir) {
t.Errorf("Output line %q should start with tmpDir %q", line, tmpDir)
}
}
}
func TestCLI_CacheFlow(t *testing.T) {
tmpDir := t.TempDir()
env := setupTestEnv(t)
// Create test project
createTestProject(t, tmpDir, "cached-project", ".git/")
// First run with --no-cache to force discovery
stdout1, stderr1, err := env.runPJ("-p", tmpDir, "--no-cache", "-v")
if err != nil {
t.Fatalf("First run failed: %v\nStderr: %s", err, stderr1)
}
if !strings.Contains(stdout1, "cached-project") {
t.Error("First run should find cached-project")
}
// Wait a moment to ensure different timestamps
time.Sleep(100 * time.Millisecond)
// Second run without --no-cache should hit cache
stdout2, stderr2, err := env.runPJ("-p", tmpDir, "-v")
if err != nil {
t.Fatalf("Second run failed: %v\nStderr: %s", err, stderr2)
}
if !strings.Contains(stderr2, "Using cached results") {
t.Errorf("Second run should use cache\nStderr: %s", stderr2)
}
if !strings.Contains(stdout2, "cached-project") {
t.Error("Second run should find cached-project from cache")
}
// Clear cache
_, stderr3, err := env.runPJ("-p", tmpDir, "--clear-cache", "-v")
if err != nil {
t.Fatalf("Clear cache failed: %v\nStderr: %s", err, stderr3)
}
if !strings.Contains(stderr3, "Cache cleared") {
t.Errorf("Clear cache should confirm clearing\nStderr: %s", stderr3)
}
// Third run after clear should not hit cache
stdout4, stderr4, err := env.runPJ("-p", tmpDir, "-v")
if err != nil {
t.Fatalf("Third run failed: %v\nStderr: %s", err, stderr4)
}
if strings.Contains(stderr4, "Using cached results") {
t.Errorf("Third run should not use cache after clearing\nStderr: %s", stderr4)
}
if !strings.Contains(stdout4, "cached-project") {
t.Error("Third run should find cached-project via fresh discovery")
}
}
func TestCLI_Icons(t *testing.T) {
tmpDir := t.TempDir()
// Create test project with go.mod
createTestProject(t, tmpDir, "go-project", "go.mod")
// Run without icons flag
stdout1, _, err := runPJ(t, "-p", tmpDir, "--no-cache")
if err != nil {
t.Fatalf("pj without icons failed: %v", err)
}
// Should not contain icon characters
lines1 := strings.Split(strings.TrimSpace(stdout1), "\n")
if len(lines1) != 1 {
t.Fatalf("Expected 1 project, got %d", len(lines1))
}
// Run with icons flag
stdout2, _, err := runPJ(t, "-p", tmpDir, "--no-cache", "--icons")
if err != nil {
t.Fatalf("pj with icons failed: %v", err)
}
lines2 := strings.Split(strings.TrimSpace(stdout2), "\n")
if len(lines2) != 1 {
t.Fatalf("Expected 1 project with icons, got %d", len(lines2))
}
// With --icons, the line should start with an icon followed by a space
// The icon is a Unicode character (multi-byte), so we can check if the line is longer
if len(lines2[0]) <= len(lines1[0]) {
t.Error("Output with --icons should be longer than without (should include icon)")
}
// The lines should both end with the project path
if !strings.HasSuffix(lines1[0], "go-project") {
t.Error("Output without icons should end with project name")
}
if !strings.HasSuffix(lines2[0], "go-project") {
t.Error("Output with icons should end with project name")
}
}
func TestCLI_PathFlag(t *testing.T) {
tmpDir1 := t.TempDir()
tmpDir2 := t.TempDir()
// Create projects in different directories
createTestProject(t, tmpDir1, "project-a", ".git/")
createTestProject(t, tmpDir2, "project-b", ".git/")
// Run with multiple -p flags
stdout, stderr, err := runPJ(t, "-p", tmpDir1, "-p", tmpDir2, "--no-cache")
if err != nil {
t.Fatalf("pj with multiple paths failed: %v\nStderr: %s", err, stderr)
}
// Should find both projects
if !strings.Contains(stdout, "project-a") {
t.Error("Output should contain project-a")
}
if !strings.Contains(stdout, "project-b") {
t.Error("Output should contain project-b")
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 2 {
t.Errorf("Expected 2 projects from 2 paths, got %d", len(lines))
}
}
func TestCLI_MaxDepth(t *testing.T) {
tmpDir := t.TempDir()
// Create nested projects
createTestProject(t, tmpDir, "level1", ".git/")
createTestProject(t, filepath.Join(tmpDir, "level1"), "level2", ".git/")
createTestProject(t, filepath.Join(tmpDir, "level1", "level2"), "level3", ".git/")
// Run with max depth of 1 (should only find level1)
stdout, stderr, err := runPJ(t, "-p", tmpDir, "-d", "1", "--no-cache")
if err != nil {
t.Fatalf("pj with max depth failed: %v\nStderr: %s", err, stderr)
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 1 {
t.Errorf("With max depth 1, expected 1 project, got %d\nOutput: %s", len(lines), stdout)
}
if !strings.Contains(stdout, "level1") {
t.Error("Output should contain level1")
}
if strings.Contains(stdout, "level2") || strings.Contains(stdout, "level3") {
t.Error("Output should not contain level2 or level3 with max depth 1")
}
}
func TestCLI_Excludes(t *testing.T) {
tmpDir := t.TempDir()
// Create projects, one in node_modules
createTestProject(t, tmpDir, "project1", ".git/")
nodeModulesDir := filepath.Join(tmpDir, "node_modules")
if err := os.MkdirAll(nodeModulesDir, 0755); err != nil {
t.Fatal(err)
}
createTestProject(t, nodeModulesDir, "excluded-project", ".git/")
// Run normally (node_modules is in default excludes)
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache")
if err != nil {
t.Fatalf("pj failed: %v\nStderr: %s", err, stderr)
}
if !strings.Contains(stdout, "project1") {
t.Error("Output should contain project1")
}
if strings.Contains(stdout, "excluded-project") {
t.Error("Output should not contain excluded-project from node_modules")
}
}
func TestCLI_PrioritySorting(t *testing.T) {
tmpDir := t.TempDir()
// Create projects with different marker priorities
// go.mod has priority 10, .git has priority 1
createTestProject(t, tmpDir, "high-priority", "go.mod")
createTestProject(t, tmpDir, "low-priority", ".git/")
// Default sort is priority descending
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache")
if err != nil {
t.Fatalf("pj failed: %v\nStderr: %s", err, stderr)
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 2 {
t.Fatalf("Expected 2 projects, got %d", len(lines))
}
// High priority (go.mod) should come first
if !strings.Contains(lines[0], "high-priority") {
t.Errorf("First line should be high-priority project, got: %s", lines[0])
}
if !strings.Contains(lines[1], "low-priority") {
t.Errorf("Second line should be low-priority project, got: %s", lines[1])
}
}
func TestCLI_StdinBasic(t *testing.T) {
tmpDir := t.TempDir()
env := setupTestEnv(t)
createTestProject(t, tmpDir, "stdin-project", ".git/")
stdin := tmpDir + "\n"
stdout, stderr, err := env.runPJWithStdin(stdin, "--no-cache")
if err != nil {
t.Fatalf("pj with stdin failed: %v\nStderr: %s", err, stderr)
}
if !strings.Contains(stdout, "stdin-project") {
t.Errorf("Output should contain stdin-project, got: %s", stdout)
}
}
func TestCLI_StdinMultiplePaths(t *testing.T) {
tmpDir1 := t.TempDir()
tmpDir2 := t.TempDir()
env := setupTestEnv(t)
createTestProject(t, tmpDir1, "project-a", "go.mod")
createTestProject(t, tmpDir2, "project-b", "package.json")
stdin := tmpDir1 + "\n" + tmpDir2 + "\n"
stdout, stderr, err := env.runPJWithStdin(stdin, "--no-cache")
if err != nil {
t.Fatalf("pj with stdin failed: %v\nStderr: %s", err, stderr)
}
if !strings.Contains(stdout, "project-a") {
t.Error("Output should contain project-a")
}
if !strings.Contains(stdout, "project-b") {
t.Error("Output should contain project-b")
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 2 {
t.Errorf("Expected 2 projects, got %d", len(lines))
}
}
func TestCLI_StdinWithIcons(t *testing.T) {
tmpDir := t.TempDir()
env := setupTestEnv(t)
createTestProject(t, tmpDir, "go-project", "go.mod")
stdin := tmpDir + "\n"
stdout, stderr, err := env.runPJWithStdin(stdin, "--no-cache", "--icons")
if err != nil {
t.Fatalf("pj with stdin and icons failed: %v\nStderr: %s", err, stderr)
}
if !strings.Contains(stdout, "go-project") {
t.Error("Output should contain go-project")
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines[0]) <= len(tmpDir)+len("/go-project") {
t.Error("Output with --icons should include icon character")
}
}
func TestCLI_StdinInvalidPaths(t *testing.T) {
tmpDir := t.TempDir()
env := setupTestEnv(t)
createTestProject(t, tmpDir, "valid-project", ".git/")
stdin := tmpDir + "\n/nonexistent/path\n"
stdout, stderr, err := env.runPJWithStdin(stdin, "--no-cache", "-v")
if err != nil {
t.Fatalf("pj with stdin failed: %v\nStderr: %s", err, stderr)
}
if !strings.Contains(stdout, "valid-project") {
t.Error("Output should contain valid-project")
}
if !strings.Contains(stderr, "warning: skipping invalid path") {
t.Error("Stderr should contain warning about invalid path")
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 1 {
t.Errorf("Expected 1 project (invalid path should be skipped), got %d", len(lines))
}
}
func TestCLI_StdinInvalidPathsSilent(t *testing.T) {
tmpDir := t.TempDir()
env := setupTestEnv(t)
createTestProject(t, tmpDir, "valid-project", ".git/")
stdin := tmpDir + "\n/nonexistent/path\n"
stdout, stderr, err := env.runPJWithStdin(stdin, "--no-cache")
if err != nil {
t.Fatalf("pj with stdin failed: %v\nStderr: %s", err, stderr)
}
if !strings.Contains(stdout, "valid-project") {
t.Error("Output should contain valid-project")
}
if strings.Contains(stderr, "warning") {
t.Error("Stderr should not contain warnings without -v flag")
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 1 {
t.Errorf("Expected 1 project (invalid path should be silently skipped), got %d", len(lines))
}
}
func TestCLI_StdinSkipsCache(t *testing.T) {
tmpDir := t.TempDir()
env := setupTestEnv(t)
createTestProject(t, tmpDir, "project", ".git/")
stdin := tmpDir + "\n"
stdout1, stderr1, err := env.runPJWithStdin(stdin, "-v")
if err != nil {
t.Fatalf("First stdin run failed: %v\nStderr: %s", err, stderr1)
}
if !strings.Contains(stdout1, "project") {
t.Error("First run should find project")
}
if strings.Contains(stderr1, "Using cached results") {
t.Error("stdin mode should not use cache on first run")
}
time.Sleep(100 * time.Millisecond)
stdout2, stderr2, err := env.runPJWithStdin(stdin, "-v")
if err != nil {
t.Fatalf("Second stdin run failed: %v\nStderr: %s", err, stderr2)
}
if !strings.Contains(stdout2, "project") {
t.Error("Second run should find project")
}
if strings.Contains(stderr2, "Using cached results") {
t.Error("stdin mode should never use cache, even on subsequent runs")
}
}
func TestCLI_StdinEmptyInput(t *testing.T) {
tmpDir := t.TempDir()
env := setupTestEnv(t)
createTestProject(t, tmpDir, "project", ".git/")
stdin := "\n\n\n"
stdout, stderr, err := env.runPJWithStdin(stdin, "-p", tmpDir, "--no-cache")
if err != nil {
t.Fatalf("pj with empty stdin failed: %v\nStderr: %s", err, stderr)
}
if !strings.Contains(stdout, "project") {
t.Error("Empty stdin should fall back to normal behavior with -p flag")
}
}
func TestCLI_NoNested(t *testing.T) {
tmpDir := t.TempDir()
// Create nested structure:
// tmpDir/
// outer/
// .git/
// apps/
// app1/
// package.json
// packages/
// pkg1/
// go.mod
outerDir := createTestProject(t, tmpDir, "outer", ".git/")
appsDir := filepath.Join(outerDir, "apps")
if err := os.MkdirAll(appsDir, 0755); err != nil {
t.Fatal(err)
}
createTestProject(t, appsDir, "app1", "package.json")
packagesDir := filepath.Join(outerDir, "packages")
if err := os.MkdirAll(packagesDir, 0755); err != nil {
t.Fatal(err)
}
createTestProject(t, packagesDir, "pkg1", "go.mod")
// Default behavior (nested discovery enabled) should find all 3 projects
stdout1, stderr1, err := runPJ(t, "-p", tmpDir, "--no-cache")
if err != nil {
t.Fatalf("pj without --no-nested failed: %v\nStderr: %s", err, stderr1)
}
lines1 := strings.Split(strings.TrimSpace(stdout1), "\n")
if len(lines1) != 3 {
t.Errorf("Without --no-nested, expected 3 projects, got %d\nOutput: %s", len(lines1), stdout1)
}
if !strings.Contains(stdout1, "outer") {
t.Error("Output should contain outer")
}
if !strings.Contains(stdout1, "app1") {
t.Error("Output should contain app1")
}
if !strings.Contains(stdout1, "pkg1") {
t.Error("Output should contain pkg1")
}
// With --no-nested, should only find outer
stdout2, stderr2, err := runPJ(t, "-p", tmpDir, "--no-cache", "--no-nested")
if err != nil {
t.Fatalf("pj with --no-nested failed: %v\nStderr: %s", err, stderr2)
}
lines2 := strings.Split(strings.TrimSpace(stdout2), "\n")
if len(lines2) != 1 {
t.Errorf("With --no-nested, expected 1 project, got %d\nOutput: %s", len(lines2), stdout2)
}
if !strings.Contains(stdout2, "outer") {
t.Error("Output should contain outer")
}
if strings.Contains(stdout2, "app1") {
t.Error("Output should not contain app1 with --no-nested")
}
if strings.Contains(stdout2, "pkg1") {
t.Error("Output should not contain pkg1 with --no-nested")
}
}
func TestCLI_LabelsDefault(t *testing.T) {
tmpDir := t.TempDir()
createTestProject(t, tmpDir, "go-project", "go.mod")
// --labels without a value should default to "label"
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache", "--labels")
if err != nil {
t.Fatalf("pj --labels (no value) failed: %v\nStderr: %s", err, stderr)
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 1 {
t.Fatalf("Expected 1 project, got %d", len(lines))
}
if !strings.HasPrefix(lines[0], "go ") {
t.Errorf("Output should start with 'go ' label, got: %s", lines[0])
}
if !strings.HasSuffix(lines[0], "go-project") {
t.Errorf("Output should end with project name, got: %s", lines[0])
}
}
func TestCLI_LabelsLabel(t *testing.T) {
tmpDir := t.TempDir()
createTestProject(t, tmpDir, "go-project", "go.mod")
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache", "--labels", "label")
if err != nil {
t.Fatalf("pj --labels label failed: %v\nStderr: %s", err, stderr)
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 1 {
t.Fatalf("Expected 1 project, got %d", len(lines))
}
if !strings.HasPrefix(lines[0], "go ") {
t.Errorf("Output should start with 'go ' label, got: %s", lines[0])
}
if !strings.HasSuffix(lines[0], "go-project") {
t.Errorf("Output should end with project name, got: %s", lines[0])
}
}
func TestCLI_LabelsDisplay(t *testing.T) {
tmpDir := t.TempDir()
createTestProject(t, tmpDir, "go-project", "go.mod")
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache", "--labels", "display")
if err != nil {
t.Fatalf("pj --labels display failed: %v\nStderr: %s", err, stderr)
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 1 {
t.Fatalf("Expected 1 project, got %d", len(lines))
}
if !strings.HasPrefix(lines[0], "Go ") {
t.Errorf("Output should start with 'Go ' label, got: %s", lines[0])
}
}
func TestCLI_LabelsWithIcons(t *testing.T) {
tmpDir := t.TempDir()
createTestProject(t, tmpDir, "go-project", "go.mod")
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache", "--labels", "label", "--icons")
if err != nil {
t.Fatalf("pj --labels --icons failed: %v\nStderr: %s", err, stderr)
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 1 {
t.Fatalf("Expected 1 project, got %d", len(lines))
}
// Should have both icon and label (label is raw, no brackets)
if !strings.Contains(lines[0], "go ") {
t.Errorf("Output should contain 'go' label, got: %s", lines[0])
}
if !strings.HasSuffix(lines[0], "go-project") {
t.Errorf("Output should end with project name, got: %s", lines[0])
}
}
func TestCLI_JSONOutput(t *testing.T) {
tmpDir := t.TempDir()
// Create test projects with different markers
createTestProject(t, tmpDir, "go-project", "go.mod")
createTestProject(t, tmpDir, "js-project", "package.json")
createTestProject(t, tmpDir, "rust-project", "Cargo.toml")
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache", "--json")
if err != nil {
t.Fatalf("pj --json failed: %v\nStderr: %s", err, stderr)
}
// Parse JSON output
var result struct {
Projects []struct {
Path string `json:"path"`
Name string `json:"name"`
Marker string `json:"marker"`
MarkerLabel string `json:"markerLabel"`
MarkerDisplayLabel string `json:"markerDisplayLabel,omitempty"`
Icon string `json:"icon,omitempty"`
} `json:"projects"`
}
if err := json.Unmarshal([]byte(stdout), &result); err != nil {
t.Fatalf("Failed to parse JSON output: %v\nOutput: %s", err, stdout)
}
// Verify we found all 3 projects
if len(result.Projects) != 3 {
t.Errorf("Expected 3 projects in JSON, got %d", len(result.Projects))
}
// Verify each project has required fields
for i, proj := range result.Projects {
if proj.Path == "" {
t.Errorf("Project %d missing path field", i)
}
if proj.Name == "" {
t.Errorf("Project %d missing name field", i)
}
if proj.Marker == "" {
t.Errorf("Project %d missing marker field", i)
}
if proj.MarkerLabel == "" {
t.Errorf("Project %d missing markerLabel field", i)
}
if !strings.HasPrefix(proj.Path, tmpDir) {
t.Errorf("Project %d path should start with tmpDir", i)
}
}
// Verify specific project markers and their labels
foundLabels := make(map[string]string)
foundDisplayLabels := make(map[string]string)
for _, proj := range result.Projects {
foundLabels[proj.Marker] = proj.MarkerLabel
foundDisplayLabels[proj.Marker] = proj.MarkerDisplayLabel
}
expectedMarkerLabels := map[string]string{
"go.mod": "go",
"package.json": "nodejs",
"Cargo.toml": "rust",
}
for marker, expectedLabel := range expectedMarkerLabels {
if foundLabels[marker] != expectedLabel {
t.Errorf("MarkerLabel for %s = %q, want %q", marker, foundLabels[marker], expectedLabel)
}
}
expectedDisplayLabels := map[string]string{
"go.mod": "Go",
"package.json": "NodeJS",
"Cargo.toml": "Rust",
}
for marker, expectedDisplayLabel := range expectedDisplayLabels {
if foundDisplayLabels[marker] != expectedDisplayLabel {
t.Errorf("MarkerDisplayLabel for %s = %q, want %q", marker, foundDisplayLabels[marker], expectedDisplayLabel)
}
}
}
func TestCLI_JSONOutputWithIcons(t *testing.T) {
tmpDir := t.TempDir()
// Create test project
createTestProject(t, tmpDir, "go-project", "go.mod")
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache", "--json", "--icons")
if err != nil {
t.Fatalf("pj --json --icons failed: %v\nStderr: %s", err, stderr)
}
// Parse JSON output
var result struct {
Projects []struct {
Path string `json:"path"`
Name string `json:"name"`
Marker string `json:"marker"`
Icon string `json:"icon,omitempty"`
Color string `json:"color,omitempty"`
} `json:"projects"`
}
if err := json.Unmarshal([]byte(stdout), &result); err != nil {
t.Fatalf("Failed to parse JSON output: %v\nOutput: %s", err, stdout)
}
if len(result.Projects) != 1 {
t.Fatalf("Expected 1 project, got %d", len(result.Projects))
}
proj := result.Projects[0]
// Verify icon field is populated when --icons is used
if proj.Icon == "" {
t.Error("Icon field should be populated when --icons flag is used")
}
// Verify color field is populated when --icons is used (even without --ansi)
if proj.Color == "" {
t.Error("Color field should be populated when --icons flag is used")
}
if proj.Color != "cyan" {
t.Errorf("Color = %q, want %q (go.mod default)", proj.Color, "cyan")
}
// Verify marker is go.mod
if proj.Marker != "go.mod" {
t.Errorf("Expected marker to be go.mod, got %s", proj.Marker)
}
// Verify name is correct
if proj.Name != "go-project" {
t.Errorf("Expected name to be go-project, got %s", proj.Name)
}
// Verify markerLabel is always present in JSON (even raw access)
var rawResult map[string][]map[string]interface{}
if err := json.Unmarshal([]byte(stdout), &rawResult); err != nil {
t.Fatalf("Failed to parse raw JSON output: %v", err)
}
rawProj := rawResult["projects"][0]
if _, exists := rawProj["markerLabel"]; !exists {
t.Error("markerLabel field should always be present in JSON output")
}
}
func TestCLI_JSONOutputWithoutIcons(t *testing.T) {
tmpDir := t.TempDir()
// Create test project
createTestProject(t, tmpDir, "project", "package.json")
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache", "--json")
if err != nil {
t.Fatalf("pj --json failed: %v\nStderr: %s", err, stderr)
}
// Parse JSON output
var result struct {
Projects []struct {
Path string `json:"path"`
Name string `json:"name"`
Marker string `json:"marker"`
Icon string `json:"icon,omitempty"`
} `json:"projects"`
}
if err := json.Unmarshal([]byte(stdout), &result); err != nil {
t.Fatalf("Failed to parse JSON output: %v\nOutput: %s", err, stdout)
}
if len(result.Projects) != 1 {
t.Fatalf("Expected 1 project, got %d", len(result.Projects))
}
proj := result.Projects[0]
// Verify icon field is empty when --icons is not used
if proj.Icon != "" {
t.Error("Icon field should be empty when --icons flag is not used")
}
// Verify other fields are populated
if proj.Path == "" || proj.Name == "" || proj.Marker == "" {
t.Error("Path, name, and marker fields should be populated")
}
}
func TestCLI_JSONEmpty(t *testing.T) {
tmpDir := t.TempDir()
// Don't create any projects
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache", "--json")
if err != nil {
t.Fatalf("pj --json with no projects failed: %v\nStderr: %s", err, stderr)
}
// Parse JSON output
var result struct {
Projects []struct {
Path string `json:"path"`
Name string `json:"name"`
Marker string `json:"marker"`
Icon string `json:"icon,omitempty"`
} `json:"projects"`
}
if err := json.Unmarshal([]byte(stdout), &result); err != nil {
t.Fatalf("Failed to parse JSON output: %v\nOutput: %s", err, stdout)
}
// Verify empty array
if len(result.Projects) != 0 {
t.Errorf("Expected 0 projects, got %d", len(result.Projects))
}
}
func TestCLI_AnsiIcons(t *testing.T) {
tmpDir := t.TempDir()
createTestProject(t, tmpDir, "go-project", "go.mod")
// Run with --icons --ansi
stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache", "--icons", "--ansi")
if err != nil {
t.Fatalf("pj --icons --ansi failed: %v\nStderr: %s", err, stderr)
}
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 1 {