-
-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathmain_test.go
More file actions
943 lines (867 loc) · 22.7 KB
/
main_test.go
File metadata and controls
943 lines (867 loc) · 22.7 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"slices"
"strconv"
"strings"
"testing"
)
const sccTestFlag string = "-test.main"
var sccBinPath = os.Args[0]
func TestMain(m *testing.M) {
idx := slices.Index(os.Args, sccTestFlag)
if idx != -1 {
os.Args = slices.Delete(os.Args, idx, idx+1)
main()
return
}
os.Exit(m.Run())
}
func runSCC(args ...string) (string, error) {
args = slices.Insert(args, 0, sccTestFlag)
cmd := exec.Command(sccBinPath, args...)
res, err := cmd.CombinedOutput()
return string(res), err
}
func TestNoGitIgnore(t *testing.T) {
tmpDir := t.TempDir()
ignoreFileName := filepath.Join(tmpDir, ".gitignore")
err := os.WriteFile(ignoreFileName, []byte("ignored.xml\n"), 0644)
if err != nil {
t.Fatal(err)
}
xmlFileName := filepath.Join(tmpDir, "ignored.xml")
err = os.WriteFile(xmlFileName, []byte(`<?xml version="1.0" encoding="UTF-8"?>`), 0644)
if err != nil {
t.Fatal(err)
}
output, err := runSCC(tmpDir)
if err != nil {
t.Fatal(err)
}
if strings.Contains(output, "XML") {
t.Fatalf("test --no-gitignore failed, output:\n%s", output)
}
output, err = runSCC("--no-gitignore", tmpDir)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "XML") {
t.Fatalf("test --no-gitignore failed, output:\n%s", output)
}
}
func TestIssue82(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/82
output1, err := runSCC(".")
if err != nil {
t.Fatal(err)
}
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
output2, err := runSCC(pwd)
if err != nil {
t.Fatal(err)
}
if output1 != output2 {
t.Fatalf("`./scc .` not equal to `./scc ${PWD}`")
}
}
func TestIncludeExt(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/108
output, err := runSCC("--include-ext", "go", "examples/language")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "Go") || strings.Contains(output, "Java") {
t.Fatalf("include-ext check failed, output:\n%s", output)
}
}
func TestIssue115(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/115
output, err := runSCC("examples/issue115/.test/file")
if err != nil {
t.Fatal(err)
}
if strings.Contains(output, "Perl") {
t.Fatalf("Should not print Perl, output:\n%s", output)
}
}
func TestIssue120(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/120
output, err := runSCC("-i", "java", "./examples/issue120")
if err != nil {
t.Fatal(err)
}
if strings.Contains(output, "Perl") {
t.Fatal("extension param should ignore Shebang")
}
}
func TestIssue152(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/152
output, err := runSCC("-i", "css", "./examples/issue152/")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "CSS") {
t.Fatalf("`-i css` extension check failed, output:\n%s", output)
}
}
func TestIssue250(t *testing.T) {
// Regression issue https://github.com/boyter/scc/issues/250
output1, err := runSCC("--exclude-dir", "examples/")
if err != nil {
t.Fatal(err)
}
output2, err := runSCC("--exclude-dir", "examples")
if err != nil {
t.Fatal(err)
}
if output1 != output2 {
t.Fatalf("examples exclude-dir check failed, output1:\n%s, output2:\n%s", output1, output2)
}
}
func TestIssue259(t *testing.T) {
// Regression issue https://github.com/boyter/scc/issues/259
output, err := runSCC("-f", "csv", "--exclude-ext", "go")
if err != nil {
t.Fatal(err)
}
if strings.Contains(output, "Go,") {
t.Fatalf("exclude-ext check failed, output:\n%s", output)
}
}
func TestIssue260(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/260
_, err := runSCC("-d", "examples/issue260/")
if err != nil {
t.Fatalf("duplicate empty crash: %v", err)
}
}
func TestIssue345(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/345
const expectedOutput = "C++,4,3,1,0,0,76,1,0"
output, err := runSCC("-f", "csv", "--no-scc-ignore", "examples/issue345/")
if err != nil {
t.Fatal(err)
}
lines := strings.Split(output, "\n")
if len(lines) < 2 {
t.Fatalf("wrong output: %s", output)
}
if lines[1] != expectedOutput {
t.Fatalf("got: %s, want: %s", lines[1], expectedOutput)
}
}
func TestIssue379(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/379
const expectedOutput = "Python,7,4,2,1,1,83,1,0"
output, err := runSCC("-f", "csv", "--no-scc-ignore", "examples/issue379/")
if err != nil {
t.Fatal(err)
}
lines := strings.Split(output, "\n")
if len(lines) < 2 {
t.Fatalf("wrong output: %s", output)
}
if lines[1] != expectedOutput {
t.Fatalf("got: %s, want: %s", lines[1], expectedOutput)
}
}
func TestIssue457(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/457
output, err := runSCC("-M", ".*")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "0.000 megabytes") {
t.Fatalf("Issue 457 test failed, output:\n%s", output)
}
}
func TestIssue564(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/564
const expectedPythonOutput = "Python,3,3,0,0,0,84,3,0"
const expectedGoOutput = "Go,6,4,0,2,0,58,2,0"
output, err := runSCC("-f", "csv", "--no-scc-ignore", "examples/issue564/")
if err != nil {
t.Fatal(err)
}
lines := strings.Split(output, "\n")
if len(lines) < 3 {
t.Fatalf("wrong output: %s", output)
}
if lines[1] != expectedPythonOutput {
t.Fatalf("got: %s, want: %s", lines[1], expectedPythonOutput)
}
if lines[2] != expectedGoOutput {
t.Fatalf("got: %s, want: %s", lines[2], expectedGoOutput)
}
}
func TestIssue610(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/610
const expectedOutput = "TypeScript,11,7,2,2,1,214,1,0"
output, err := runSCC("-f", "csv", "--no-scc-ignore", "examples/issue610/")
if err != nil {
t.Fatal(err)
}
lines := strings.Split(output, "\n")
if len(lines) < 2 {
t.Fatalf("wrong output: %s", output)
}
if lines[1] != expectedOutput {
t.Fatalf("got: %s, want: %s", lines[1], expectedOutput)
}
}
func TestIssue339(t *testing.T) {
t.Parallel()
// Regression issue https://github.com/boyter/scc/issues/339
output, err := runSCC("-f", "csv", "--no-scc-ignore", "examples/issue339/")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "MATLAB") {
t.Errorf("can not find MATLAB, output: %s", output)
}
if !strings.Contains(output, "Objective C") {
t.Errorf("can not find Objective C, output:\n%s", output)
}
}
func TestInvalidOption(t *testing.T) {
t.Parallel()
output, err := runSCC("--not-a-real-option")
if err == nil {
t.Fatal("scc should exit with error code")
}
if !strings.Contains(output, "Error: unknown flag: --not-a-real-option") {
t.Fatalf("scc should report invalid options, output:\n%s", output)
}
}
func TestFileFlagSyntax(t *testing.T) {
tmpDir := t.TempDir()
flagsFileName := filepath.Join(tmpDir, "flags.txt")
// include \n, \r\n and no line terminators
testCases := []string{
"go.mod\ngo.sum\nLICENSE\n",
"go.mod\r\ngo.sum\r\nLICENSE\r\n",
"go.mod\ngo.sum\nLICENSE",
"go.mod\r\ngo.sum\r\nLICENSE",
"go.mod\ngo.sum\r\nLICENSE",
}
for _, tc := range testCases {
err := os.WriteFile(flagsFileName, []byte(tc), 0644)
if err != nil {
t.Fatal(err)
}
_, err = runSCC("@" + flagsFileName)
if err != nil {
t.Errorf("flag syntax faild: %q, %v", tc, err)
}
}
}
func TestLineLength(t *testing.T) {
t.Parallel()
output, err := runSCC("-m")
if err != nil {
t.Fatal(err)
}
if strings.Count(output, "MaxLine / MeanLine") < 2 {
t.Fatalf("line length test failed, output:\n%s", output)
}
}
func TestFormatHTML(t *testing.T) {
t.Parallel()
output, err := runSCC("--format", "html")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "<title>scc html output</title>") {
t.Fatalf("html format test failed, output:\n%s", output)
}
}
func TestFormatHTMLTable(t *testing.T) {
t.Parallel()
output, err := runSCC("--format", "html-table")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, `<table id="scc-table">`) {
t.Fatalf("html-table format test failed, output:\n%s", output)
}
}
func TestFormatSQL(t *testing.T) {
t.Parallel()
output, err := runSCC("--format", "sql")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "create table metadata ( -- github.com/boyter/scc") {
t.Fatalf("sql format test failed, output:\n%s", output)
}
}
func TestFormatSQLInsert(t *testing.T) {
t.Parallel()
output, err := runSCC("--format", "sql-insert")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "begin transaction;\ninsert into t values(") {
t.Fatalf("sql-insert format test failed, output:\n%s", output)
}
}
func TestMultipleFormatStdout(t *testing.T) {
output, err := runSCC("--format-multi", "tabular:stdout,html:stdout,csv:stdout,sql:stdout")
if err != nil {
t.Fatal(err)
}
tabularPattern := regexp.MustCompile(`Processed .+? bytes, .+? megabytes \(SI\)`)
if !tabularPattern.MatchString(output) {
t.Errorf("multi-format tabular failed, output:\n%s", output)
}
if !strings.Contains(output, `<html lang="en"><head><meta charset="utf-8" /><title>scc html output</title>`) {
t.Errorf("multi-format html failed, output:\n%s", output)
}
if !strings.Contains(output, "Language,Lines,Code,Comments,Blanks,Complexity,Bytes,Files,ULOC") {
t.Errorf("multi-format csv failed, output:\n%s", output)
}
sqlPattern := regexp.MustCompile(`insert into t values\(.+?\);`)
if !sqlPattern.MatchString(output) {
t.Errorf("multi-format sql failed, output:\n%s", output)
}
}
func TestMultipleFormatWriteFile(t *testing.T) {
tmpDir := t.TempDir()
outputTabular := filepath.Join(tmpDir, "output.tab")
outputWide := filepath.Join(tmpDir, "output.wide")
outputJSON1 := filepath.Join(tmpDir, "output.json")
outputJSON2 := filepath.Join(tmpDir, "output2.json")
outputCSV := filepath.Join(tmpDir, "output.csv")
outputYAML := filepath.Join(tmpDir, "output.yaml")
outputHTML := filepath.Join(tmpDir, "output.html")
outputHTMLTable := filepath.Join(tmpDir, "output_table.html")
outputSQL := filepath.Join(tmpDir, "output.sql")
multiFormatArgs := fmt.Sprintf(
"tabular:%s,wide:%s,json:%s,json2:%s,csv:%s,cloc-yaml:%s,html:%s,html-table:%s,sql:%s",
outputTabular,
outputWide,
outputJSON1,
outputJSON2,
outputCSV,
outputYAML,
outputHTML,
outputHTMLTable,
outputSQL,
)
_, err := runSCC("--format-multi", multiFormatArgs)
if err != nil {
t.Fatal(err)
}
if info, err := os.Stat(outputTabular); err != nil || info.Size() <= 0 {
t.Fatal("tabular write file test failed")
}
if info, err := os.Stat(outputWide); err != nil || info.Size() <= 0 {
t.Fatal("wide write file test failed")
}
if info, err := os.Stat(outputJSON1); err != nil || info.Size() <= 0 {
t.Fatal("json write file test failed")
}
if info, err := os.Stat(outputJSON2); err != nil || info.Size() <= 0 {
t.Fatal("json2 write file test failed")
}
if info, err := os.Stat(outputCSV); err != nil || info.Size() <= 0 {
t.Fatal("csv write file test failed")
}
if info, err := os.Stat(outputYAML); err != nil || info.Size() <= 0 {
t.Fatal("cloc-yaml write file test failed")
}
if info, err := os.Stat(outputHTML); err != nil || info.Size() <= 0 {
t.Fatal("html write file test failed")
}
if info, err := os.Stat(outputHTMLTable); err != nil || info.Size() <= 0 {
t.Fatal("html-table write file test failed")
}
if info, err := os.Stat(outputSQL); err != nil || info.Size() <= 0 {
t.Fatal("sql write file test failed")
}
}
func TestRecursivelyIgnore(t *testing.T) {
tmpDir := t.TempDir()
err := os.WriteFile(filepath.Join(tmpDir, ".gitignore"), []byte("ignore-git.txt\n"), 0644)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(filepath.Join(tmpDir, ".ignore"), []byte("vendor/\nignore.txt\n"), 0644)
if err != nil {
t.Fatal(err)
}
err = os.Mkdir(filepath.Join(tmpDir, "ignore"), 0755)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(filepath.Join(tmpDir, "ignore", "README.md"), []byte("Files in here are to ensure that .ignore and .gitignore work recursively\n"), 0644)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(filepath.Join(tmpDir, "ignore", "ignore.txt"), []byte("testing\n"), 0644)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(filepath.Join(tmpDir, "ignore", "ignore-git.txt"), []byte("git\ntesting\n"), 0644)
if err != nil {
t.Fatal(err)
}
output, err := runSCC("--by-file", "--no-scc-ignore", tmpDir)
if err != nil {
t.Fatal(err)
}
if strings.Contains(output, "ignore.txt") || strings.Contains(output, "ignore-git.txt") {
t.Errorf("ignore recursive filter failed, output:\n%s", output)
}
output, err = runSCC("--by-file", "--no-scc-ignore", "--no-ignore", tmpDir)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "ignore.txt") || strings.Contains(output, "ignore-git.txt") {
t.Errorf("ignore recursive filter failed, output:\n%s", output)
}
output, err = runSCC("--by-file", "--no-scc-ignore", "--no-gitignore", tmpDir)
if err != nil {
t.Fatal(err)
}
if strings.Contains(output, "ignore.txt") || !strings.Contains(output, "ignore-git.txt") {
t.Errorf("ignore recursive filter failed, output:\n%s", output)
}
output, err = runSCC("--by-file", "--no-scc-ignore", "--no-ignore", "--no-gitignore", tmpDir)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "ignore.txt") || !strings.Contains(output, "ignore-git.txt") {
t.Errorf("ignore recursive filter failed, output:\n%s", output)
}
}
func TestMultipleGitIgnore(t *testing.T) {
tmpDir := t.TempDir()
err := os.WriteFile(filepath.Join(tmpDir, ".gitignore"), []byte("ignore.txt\n"), 0644)
if err != nil {
t.Fatal(err)
}
err = os.Mkdir(filepath.Join(tmpDir, "ignore"), 0755)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(filepath.Join(tmpDir, "ignore", ".gitignore"), []byte("ignore.java\n"), 0644)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(filepath.Join(tmpDir, "ignore", "ignore.java"), []byte("//test\n"), 0644)
if err != nil {
t.Fatal(err)
}
output, err := runSCC(tmpDir)
if err != nil {
t.Fatal(err)
}
if strings.Contains(output, "Java") {
t.Fatalf("multiple gitignore failed, output:\n%s", output)
}
}
func TestFlagSuggestion(t *testing.T) {
t.Parallel()
testCases := []struct {
args []string
expectedOutput string
}{
{
args: []string{"--farmat"},
expectedOutput: "The most similar flag of --farmat is:\n\t--format\n",
},
{
args: []string{"--no-gignore"},
expectedOutput: "The most similar flags of --no-gignore are:\n\t--no-ignore\n\t--no-gitignore\n",
},
}
for _, tc := range testCases {
output, err := runSCC(tc.args...)
if err == nil {
t.Fatal("scc should exit with error code")
}
if !strings.Contains(output, tc.expectedOutput) {
t.Errorf("wrong suggestion for %v, want: %s, got: %s", tc.args, tc.expectedOutput, output)
}
}
}
func TestDeterministicOutput(t *testing.T) {
output, err := runSCC(".")
if err != nil {
t.Fatal(err)
}
for range 10 {
output2, err := runSCC(".")
if err != nil {
t.Fatal(err)
}
if output != output2 {
t.Fatalf("want:\n%s, got:\n%s", output, output2)
}
}
}
func TestDuplicates(t *testing.T) {
for range 10 {
output, err := runSCC("-f", "json", "-d", "./examples/duplicates/")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, `"Count":1`) {
t.Fatalf("duplicates check failed, output:\n%s", output)
}
}
}
func TestCountAs(t *testing.T) {
testCases := []struct {
countAs string
expected []string
}{
{
countAs: "jsp:html",
expected: []string{"HTML"},
},
{
countAs: "JsP:html",
expected: []string{"HTML"},
},
{
countAs: "jsp:j2",
expected: []string{"Jinja"},
},
{
countAs: "jsp:html,new:java",
expected: []string{"HTML", "Java"},
},
{
countAs: "jsp:html,new:C Header",
expected: []string{"HTML", "C Header"},
},
}
for _, tc := range testCases {
output, err := runSCC("-f", "csv", "--count-as", tc.countAs, "./examples/countas/")
if err != nil {
t.Fatal(err)
}
for _, expectedLang := range tc.expected {
if !strings.Contains(output, expectedLang+",") {
t.Errorf("count as failed, count as: %s, output:\n%s", tc.countAs, output)
}
}
}
}
func TestRemapUnknown(t *testing.T) {
t.Parallel()
output, err := runSCC("-f", "csv", "--remap-unknown", "-*- C++ -*-:C Header", "./examples/remap/unknown")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "C Header,") {
t.Fatalf("remap unknown failed, output:\n%s", output)
}
}
func TestRemapAll(t *testing.T) {
t.Parallel()
output, err := runSCC("-f", "csv", "--remap-all", "-*- C++ -*-:C Header", "./examples/remap/java.java")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "C Header,") {
t.Fatalf("remap all failed, output:\n%s", output)
}
}
func TestCocomoProjectType(t *testing.T) {
projectTypes := []string{"organic", "semi-detached", "embedded", "custom,1,1,1,1"}
for _, typ := range projectTypes {
output, err := runSCC("--cocomo-project-type", typ)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, fmt.Sprintf("Estimated Cost to Develop (%s)", typ)) ||
!strings.Contains(output, fmt.Sprintf("Estimated Schedule Effort (%s)", typ)) ||
!strings.Contains(output, fmt.Sprintf("Estimated People Required (%s)", typ)) {
t.Errorf("check cocomo project type failed: %s", typ)
}
}
}
func TestCocomoProjectTypeFallback(t *testing.T) {
unknownTypes := []string{"doesnotexist", "custom,1,1,1"}
for _, typ := range unknownTypes {
output, err := runSCC("--cocomo-project-type", typ)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "Estimated Cost to Develop (organic)") ||
!strings.Contains(output, "Estimated Schedule Effort (organic)") ||
!strings.Contains(output, "Estimated People Required (organic)") {
t.Errorf("check cocomo project type fallback failed: %s", typ)
}
}
}
func TestOutputBytes(t *testing.T) {
jsonOutput, err := runSCC("-f", "json")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(jsonOutput, `"Bytes":`) {
t.Errorf("json output does not contain `Bytes` field, output:\n%s", jsonOutput)
}
output, err := runSCC()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "megabytes") {
t.Errorf("output does not contain `megabytes`, output:\n%s", output)
}
}
func TestFileGCCount(t *testing.T) {
const target = "./examples/duplicates"
files, err := os.ReadDir(target)
if err != nil {
t.Fatal(err)
}
output, err := runSCC("--file-gc-count", strconv.Itoa(len(files)-1), "-v", target)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, "read file limit exceeded GC re-enabled") {
t.Errorf("test file GC count failed, file count: %d, limit: %d", len(files), len(files)-1)
}
output, err = runSCC("--file-gc-count", strconv.Itoa(len(files)+1), "-v", target)
if err != nil {
t.Fatal(err)
}
if strings.Contains(output, "read file limit exceeded GC re-enabled") {
t.Errorf("test file GC count failed, file count: %d, limit: %d", len(files), len(files)+1)
}
}
func TestIncludeSymlinks(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping symlink test on Windows due to privilege requirements")
}
tmpDir, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
dirA := filepath.Join(tmpDir, "a")
dirB := filepath.Join(tmpDir, "b")
if err := os.Mkdir(dirA, 0755); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(dirB, 0755); err != nil {
t.Fatal(err)
}
const fileName = "source.go"
if err := os.WriteFile(filepath.Join(dirA, fileName), []byte("package main\n"), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dirB, fileName), []byte("package main\n"), 0644); err != nil {
t.Fatal(err)
}
// link to another dir, should be counted under --include-symlinks
if err := os.Symlink(filepath.Join(dirB, fileName), filepath.Join(dirA, "link1.go")); err != nil {
t.Fatal(err)
}
// link to the same dir, this should be ignored in all times
if err := os.Symlink(filepath.Join(dirA, fileName), filepath.Join(dirA, "link2.go")); err != nil {
t.Fatal(err)
}
output, err := runSCC("-f", "json", "--no-scc-ignore", dirA)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, `"Count":1`) {
t.Errorf("count without symlink failed, output:\n%s", output)
}
output, err = runSCC("-f", "json", "--no-scc-ignore", "--include-symlinks", dirA)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(output, `"Count":2`) {
t.Errorf("count includes symlink failed, output:\n%s", output)
}
}
func TestLanguageNameTruncate(t *testing.T) {
output, err := runSCC("examples/language")
if err != nil {
t.Fatal(err)
}
if strings.Count(output, "Bitbucket Pipe…") != 1 {
t.Errorf("`Bitbucket Pipeline` truncate test failed")
}
if strings.Count(output, "CloudFormation…") != 2 {
t.Errorf("`CloudFormation (JSON)` and `CloudFormation (YAML)` truncate test failed")
}
}
func TestSpecificLanguages(t *testing.T) {
languages := [...]string{
"ABNF",
"Alchemist",
"Algol 68",
"Alloy",
"Amber",
"Apex",
"ArkTs",
"Arturo",
"Astro",
"AWK",
"BASH",
"Bean",
"Bicep",
"Bitbucket Pipeline",
"Blueprint",
"Boo",
"Bosque",
"Bru",
"C",
"C3",
"C Header",
"C Shell",
"C#",
"C++",
"C++ Header",
"Cairo",
"Cangjie",
"Chapel",
"Circom",
"Clipper",
"Clojure",
"CMake",
"Cuda",
"Cypher",
"D2",
"DAML",
"DM",
"Docker ignore",
"Dockerfile",
"DOT",
"Elixir Template",
"Elm",
"EmiT",
"F#",
"Factor",
"Flow9",
"FSL",
"Futhark",
"FXML",
"Gemfile",
"Gleam",
"Go",
"Go+",
"Godot Scene",
"GraphQL",
"Gremlin",
"Gwion",
"HAML",
"Hare",
"Haskell",
"HCL",
"ignore",
"INI",
"Java",
"JavaScript",
"JCL",
"JSON5",
"JSONC",
"jq",
"Korn Shell",
"Koto",
"LALRPOP",
"License",
"LiveScript",
"LLVM IR",
"Lua",
"Luau",
"Luna",
"MLIR",
"Makefile",
"Metal",
"Mojo",
"Monkey C",
"Moonbit",
"Nature",
"Nushell",
"OpenQASM",
"OpenTofu",
"Perl",
"Pkl",
"Plain Text",
"POML",
"PostScript",
"Proto",
"Python",
"Q#",
"R",
"Racket",
"Rakefile",
"RAML",
"Rebol",
"Redscript",
"Rich Text Format",
"Scallop",
"Seed7",
"Shell",
"Sieve",
"Slang",
"Slint",
"Smalltalk",
"Snakemake",
"Stan",
"Systemd",
"Tact",
"Teal",
"Tera",
"Templ",
"Terraform",
"TOML",
"TOON",
"TTCN-3",
"TypeScript",
"TypeSpec",
"Typst",
"Up",
"Vala",
"Vim Script",
"Web Services Description Language",
"WebGPU Enhanced Shading Language",
"wenyan",
"Wren",
"XHTML",
"XMake",
"XML Schema",
"YAML",
"Yarn",
"Zig",
"ZoKrates",
"Zsh",
}
output, err := runSCC("-f", "csv", "examples/language")
if err != nil {
t.Fatal(err)
}
for _, language := range languages {
if !strings.Contains(output, language+",") {
t.Errorf("language not found in output: %v", language)
}
}
}