Skip to content

Commit e7ae63a

Browse files
authored
refactor: convert more tests to Go tests (#696)
1 parent f19d8ed commit e7ae63a

3 files changed

Lines changed: 215 additions & 218 deletions

File tree

SCC-OUTPUT-REPORT.html

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
<tbody><tr>
1414
<th>Go</th>
1515
<th>34</th>
16-
<th>27183</th>
17-
<th>1780</th>
16+
<th>27374</th>
17+
<th>1796</th>
1818
<th>614</th>
19-
<th>24789</th>
20-
<th>1969</th>
21-
<th>553720</th>
22-
<th>8117</th>
19+
<th>24964</th>
20+
<th>2018</th>
21+
<th>558764</th>
22+
<th>8190</th>
2323
</tr><tr>
2424
<td>processor/constants.go</td>
2525
<td></td>
@@ -60,6 +60,16 @@
6060
<td>167</td>
6161
<td>47400</td>
6262
<td>833</td>
63+
</tr><tr>
64+
<td>main_test.go</td>
65+
<td></td>
66+
<td>890</td>
67+
<td>70</td>
68+
<td>15</td>
69+
<td>805</td>
70+
<td>228</td>
71+
<td>21740</td>
72+
<td>471</td>
6373
</tr><tr>
6474
<td>processor/workers.go</td>
6575
<td></td>
@@ -80,16 +90,6 @@
8090
<td>89</td>
8191
<td>21834</td>
8292
<td>495</td>
83-
</tr><tr>
84-
<td>main_test.go</td>
85-
<td></td>
86-
<td>699</td>
87-
<td>54</td>
88-
<td>15</td>
89-
<td>630</td>
90-
<td>179</td>
91-
<td>16696</td>
92-
<td>395</td>
9393
</tr><tr>
9494
<td>cmd/badges/main.go</td>
9595
<td></td>
@@ -364,15 +364,15 @@
364364
<tfoot><tr>
365365
<th>Total</th>
366366
<th>34</th>
367-
<th>27183</th>
368-
<th>1780</th>
367+
<th>27374</th>
368+
<th>1796</th>
369369
<th>614</th>
370-
<th>24789</th>
371-
<th>1969</th>
372-
<th>553720</th>
373-
<th>8117</th>
370+
<th>24964</th>
371+
<th>2018</th>
372+
<th>558764</th>
373+
<th>8190</th>
374374
</tr>
375375
<tr>
376-
<th colspan="9">Estimated Cost to Develop (organic) $786,261<br>Estimated Schedule Effort (organic) 12.55 months<br>Estimated People Required (organic) 5.56<br></th>
376+
<th colspan="9">Estimated Cost to Develop (organic) $792,091<br>Estimated Schedule Effort (organic) 12.59 months<br>Estimated People Required (organic) 5.59<br></th>
377377
</tr></tfoot>
378378
</table></body></html>

main_test.go

Lines changed: 192 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"regexp"
99
"slices"
10+
"strconv"
1011
"strings"
1112
"testing"
1213
)
@@ -315,6 +316,50 @@ func TestLineLength(t *testing.T) {
315316
}
316317
}
317318

319+
func TestFormatHTML(t *testing.T) {
320+
t.Parallel()
321+
output, err := runSCC("--format", "html")
322+
if err != nil {
323+
t.Fatal(err)
324+
}
325+
if !strings.Contains(output, "<title>scc html output</title>") {
326+
t.Fatalf("html format test failed, output:\n%s", output)
327+
}
328+
}
329+
330+
func TestFormatHTMLTable(t *testing.T) {
331+
t.Parallel()
332+
output, err := runSCC("--format", "html-table")
333+
if err != nil {
334+
t.Fatal(err)
335+
}
336+
if !strings.Contains(output, `<table id="scc-table">`) {
337+
t.Fatalf("html-table format test failed, output:\n%s", output)
338+
}
339+
}
340+
341+
func TestFormatSQL(t *testing.T) {
342+
t.Parallel()
343+
output, err := runSCC("--format", "sql")
344+
if err != nil {
345+
t.Fatal(err)
346+
}
347+
if !strings.Contains(output, "create table metadata ( -- github.com/boyter/scc") {
348+
t.Fatalf("sql format test failed, output:\n%s", output)
349+
}
350+
}
351+
352+
func TestFormatSQLInsert(t *testing.T) {
353+
t.Parallel()
354+
output, err := runSCC("--format", "sql-insert")
355+
if err != nil {
356+
t.Fatal(err)
357+
}
358+
if !strings.Contains(output, "begin transaction;\ninsert into t values(") {
359+
t.Fatalf("sql-insert format test failed, output:\n%s", output)
360+
}
361+
}
362+
318363
func TestMultipleFormatStdout(t *testing.T) {
319364
output, err := runSCC("--format-multi", "tabular:stdout,html:stdout,csv:stdout,sql:stdout")
320365
if err != nil {
@@ -519,7 +564,7 @@ func TestDeterministicOutput(t *testing.T) {
519564
if err != nil {
520565
t.Fatal(err)
521566
}
522-
for range 20 {
567+
for range 10 {
523568
output2, err := runSCC(".")
524569
if err != nil {
525570
t.Fatal(err)
@@ -530,6 +575,152 @@ func TestDeterministicOutput(t *testing.T) {
530575
}
531576
}
532577

578+
func TestDuplicates(t *testing.T) {
579+
for range 10 {
580+
output, err := runSCC("-f", "json", "-d", "./examples/duplicates/")
581+
if err != nil {
582+
t.Fatal(err)
583+
}
584+
if !strings.Contains(output, `"Count":1`) {
585+
t.Fatalf("duplicates check failed, output:\n%s", output)
586+
}
587+
}
588+
}
589+
590+
func TestCountAs(t *testing.T) {
591+
testCases := []struct {
592+
countAs string
593+
expected []string
594+
}{
595+
{
596+
countAs: "jsp:html",
597+
expected: []string{"HTML"},
598+
},
599+
{
600+
countAs: "JsP:html",
601+
expected: []string{"HTML"},
602+
},
603+
{
604+
countAs: "jsp:j2",
605+
expected: []string{"Jinja"},
606+
},
607+
{
608+
countAs: "jsp:html,new:java",
609+
expected: []string{"HTML", "Java"},
610+
},
611+
{
612+
countAs: "jsp:html,new:C Header",
613+
expected: []string{"HTML", "C Header"},
614+
},
615+
}
616+
617+
for _, tc := range testCases {
618+
output, err := runSCC("-f", "csv", "--count-as", tc.countAs, "./examples/countas/")
619+
if err != nil {
620+
t.Fatal(err)
621+
}
622+
for _, expectedLang := range tc.expected {
623+
if !strings.Contains(output, expectedLang+",") {
624+
t.Errorf("count as failed, count as: %s, output:\n%s", tc.countAs, output)
625+
}
626+
}
627+
}
628+
}
629+
630+
func TestRemapUnknown(t *testing.T) {
631+
t.Parallel()
632+
output, err := runSCC("-f", "csv", "--remap-unknown", "-*- C++ -*-:C Header", "./examples/remap/unknown")
633+
if err != nil {
634+
t.Fatal(err)
635+
}
636+
if !strings.Contains(output, "C Header,") {
637+
t.Fatalf("remap unknown failed, output:\n%s", output)
638+
}
639+
}
640+
641+
func TestRemapAll(t *testing.T) {
642+
t.Parallel()
643+
output, err := runSCC("-f", "csv", "--remap-all", "-*- C++ -*-:C Header", "./examples/remap/java.java")
644+
if err != nil {
645+
t.Fatal(err)
646+
}
647+
if !strings.Contains(output, "C Header,") {
648+
t.Fatalf("remap all failed, output:\n%s", output)
649+
}
650+
}
651+
652+
func TestCocomoProjectType(t *testing.T) {
653+
projectTypes := []string{"organic", "semi-detached", "embedded", "custom,1,1,1,1"}
654+
for _, typ := range projectTypes {
655+
output, err := runSCC("--cocomo-project-type", typ)
656+
if err != nil {
657+
t.Fatal(err)
658+
}
659+
if !strings.Contains(output, fmt.Sprintf("Estimated Cost to Develop (%s)", typ)) ||
660+
!strings.Contains(output, fmt.Sprintf("Estimated Schedule Effort (%s)", typ)) ||
661+
!strings.Contains(output, fmt.Sprintf("Estimated People Required (%s)", typ)) {
662+
t.Errorf("check cocomo project type failed: %s", typ)
663+
}
664+
}
665+
}
666+
667+
func TestCocomoProjectTypeFallback(t *testing.T) {
668+
unknownTypes := []string{"doesnotexist", "custom,1,1,1"}
669+
for _, typ := range unknownTypes {
670+
output, err := runSCC("--cocomo-project-type", typ)
671+
if err != nil {
672+
t.Fatal(err)
673+
}
674+
if !strings.Contains(output, "Estimated Cost to Develop (organic)") ||
675+
!strings.Contains(output, "Estimated Schedule Effort (organic)") ||
676+
!strings.Contains(output, "Estimated People Required (organic)") {
677+
t.Errorf("check cocomo project type fallback failed: %s", typ)
678+
}
679+
}
680+
}
681+
682+
func TestOutputBytes(t *testing.T) {
683+
jsonOutput, err := runSCC("-f", "json")
684+
if err != nil {
685+
t.Fatal(err)
686+
}
687+
if !strings.Contains(jsonOutput, `"Bytes":`) {
688+
t.Errorf("json output does not contain `Bytes` field, output:\n%s", jsonOutput)
689+
}
690+
691+
output, err := runSCC()
692+
if err != nil {
693+
t.Fatal(err)
694+
}
695+
if !strings.Contains(output, "megabytes") {
696+
t.Errorf("output does not contain `megabytes`, output:\n%s", output)
697+
}
698+
}
699+
700+
func TestFileGCCount(t *testing.T) {
701+
const target = "./examples/duplicates"
702+
files, err := os.ReadDir(target)
703+
if err != nil {
704+
t.Fatal(err)
705+
}
706+
707+
output, err := runSCC("--file-gc-count", strconv.Itoa(len(files)-1), "-v", target)
708+
if err != nil {
709+
t.Fatal(err)
710+
}
711+
if !strings.Contains(output, "read file limit exceeded GC re-enabled") {
712+
t.Errorf("test file GC count failed, file count: %d, limit: %d", len(files), len(files)-1)
713+
}
714+
715+
output, err = runSCC("--file-gc-count", strconv.Itoa(len(files)+1), "-v", target)
716+
if err != nil {
717+
t.Fatal(err)
718+
}
719+
if strings.Contains(output, "read file limit exceeded GC re-enabled") {
720+
t.Errorf("test file GC count failed, file count: %d, limit: %d", len(files), len(files)+1)
721+
}
722+
}
723+
533724
func TestLanguageNameTruncate(t *testing.T) {
534725
output, err := runSCC("examples/language")
535726
if err != nil {

0 commit comments

Comments
 (0)