Skip to content

Commit 16551e0

Browse files
authored
feat: automate file generation and add CI tests (#688)
1 parent c9512c0 commit 16551e0

7 files changed

Lines changed: 130 additions & 67 deletions

File tree

.github/workflows/go.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,30 @@ jobs:
1111
build:
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/checkout@v5
14+
- uses: actions/checkout@v6
1515

1616
- name: Set up Go
1717
uses: actions/setup-go@v6
1818
with:
1919
go-version-file: './go.mod'
20+
21+
- name: Format Check
22+
run: |
23+
go fmt ./...
24+
if ! git diff --color=always --exit-code -- . ':!vendor'; then
25+
echo "::error::Found changed files after 'go fmt ./...'."
26+
echo "Please run 'go fmt ./...' and check in all changes."
27+
exit 1
28+
fi
29+
30+
- name: Generate Check
31+
run: |
32+
go generate
33+
if ! git diff --color=always --exit-code -- . ':!vendor'; then
34+
echo "::error::Found changed files after 'go generate'."
35+
echo "Please run 'go generate' and check in all changes."
36+
exit 1
37+
fi
2038
2139
- name: Build
2240
run: go build -v ./...

SCC-OUTPUT-REPORT.html

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
</tr></thead>
1313
<tbody><tr>
1414
<th>Go</th>
15-
<th>32</th>
16-
<th>26344</th>
17-
<th>1679</th>
18-
<th>584</th>
19-
<th>24081</th>
20-
<th>1856</th>
21-
<th>530745</th>
22-
<th>7733</th>
15+
<th>34</th>
16+
<th>26934</th>
17+
<th>1752</th>
18+
<th>614</th>
19+
<th>24568</th>
20+
<th>1952</th>
21+
<th>547792</th>
22+
<th>8014</th>
2323
</tr><tr>
2424
<td>processor/constants.go</td>
2525
<td></td>
@@ -73,13 +73,13 @@
7373
</tr><tr>
7474
<td>processor/processor.go</td>
7575
<td></td>
76-
<td>704</td>
76+
<td>705</td>
7777
<td>151</td>
7878
<td>123</td>
79-
<td>430</td>
79+
<td>431</td>
8080
<td>85</td>
81-
<td>20885</td>
82-
<td>469</td>
81+
<td>20926</td>
82+
<td>470</td>
8383
</tr><tr>
8484
<td>main_test.go</td>
8585
<td></td>
@@ -103,13 +103,13 @@
103103
</tr><tr>
104104
<td>main.go</td>
105105
<td></td>
106-
<td>542</td>
107-
<td>17</td>
108-
<td>10</td>
109-
<td>515</td>
110-
<td>24</td>
111-
<td>13082</td>
112-
<td>339</td>
106+
<td>559</td>
107+
<td>19</td>
108+
<td>12</td>
109+
<td>528</td>
110+
<td>27</td>
111+
<td>13470</td>
112+
<td>349</td>
113113
</tr><tr>
114114
<td>processor/detector_test.go</td>
115115
<td></td>
@@ -120,6 +120,16 @@
120120
<td>109</td>
121121
<td>7784</td>
122122
<td>162</td>
123+
</tr><tr>
124+
<td>mcp.go</td>
125+
<td></td>
126+
<td>409</td>
127+
<td>44</td>
128+
<td>25</td>
129+
<td>340</td>
130+
<td>57</td>
131+
<td>12733</td>
132+
<td>293</td>
123133
</tr><tr>
124134
<td>cmd/badges/main_test.go</td>
125135
<td></td>
@@ -210,6 +220,16 @@
210220
<td>17</td>
211221
<td>5311</td>
212222
<td>117</td>
223+
</tr><tr>
224+
<td>processor/processor_test.go</td>
225+
<td></td>
226+
<td>156</td>
227+
<td>36</td>
228+
<td>1</td>
229+
<td>119</td>
230+
<td>22</td>
231+
<td>2741</td>
232+
<td>70</td>
213233
</tr><tr>
214234
<td>processor/file.go</td>
215235
<td></td>
@@ -220,16 +240,6 @@
220240
<td>43</td>
221241
<td>3527</td>
222242
<td>94</td>
223-
</tr><tr>
224-
<td>processor/processor_test.go</td>
225-
<td></td>
226-
<td>151</td>
227-
<td>36</td>
228-
<td>1</td>
229-
<td>114</td>
230-
<td>21</td>
231-
<td>2573</td>
232-
<td>66</td>
233243
</tr><tr>
234244
<td>processor/structs_test.go</td>
235245
<td></td>
@@ -240,6 +250,16 @@
240250
<td>16</td>
241251
<td>3323</td>
242252
<td>96</td>
253+
</tr><tr>
254+
<td>processor/result.go</td>
255+
<td></td>
256+
<td>135</td>
257+
<td>24</td>
258+
<td>3</td>
259+
<td>108</td>
260+
<td>31</td>
261+
<td>3150</td>
262+
<td>90</td>
243263
</tr><tr>
244264
<td>processor/trace_test.go</td>
245265
<td></td>
@@ -250,6 +270,16 @@
250270
<td>15</td>
251271
<td>2771</td>
252272
<td>80</td>
273+
</tr><tr>
274+
<td>scripts/include.go</td>
275+
<td></td>
276+
<td>115</td>
277+
<td>22</td>
278+
<td>5</td>
279+
<td>88</td>
280+
<td>23</td>
281+
<td>2707</td>
282+
<td>73</td>
253283
</tr><tr>
254284
<td>processor/trace.go</td>
255285
<td></td>
@@ -260,16 +290,6 @@
260290
<td>9</td>
261291
<td>1957</td>
262292
<td>58</td>
263-
</tr><tr>
264-
<td>scripts/include.go</td>
265-
<td></td>
266-
<td>92</td>
267-
<td>19</td>
268-
<td>5</td>
269-
<td>68</td>
270-
<td>19</td>
271-
<td>2140</td>
272-
<td>64</td>
273293
</tr><tr>
274294
<td>processor/similar_flags.go</td>
275295
<td></td>
@@ -343,16 +363,16 @@
343363
</tr></tbody>
344364
<tfoot><tr>
345365
<th>Total</th>
346-
<th>32</th>
347-
<th>26344</th>
348-
<th>1679</th>
349-
<th>584</th>
350-
<th>24081</th>
351-
<th>1856</th>
352-
<th>530745</th>
353-
<th>7733</th>
366+
<th>34</th>
367+
<th>26934</th>
368+
<th>1752</th>
369+
<th>614</th>
370+
<th>24568</th>
371+
<th>1952</th>
372+
<th>547792</th>
373+
<th>8014</th>
354374
</tr>
355375
<tr>
356-
<th colspan="9">Estimated Cost to Develop (organic) $762,699<br>Estimated Schedule Effort (organic) 12.41 months<br>Estimated People Required (organic) 5.46<br></th>
376+
<th colspan="9">Estimated Cost to Develop (organic) $778,903<br>Estimated Schedule Effort (organic) 12.51 months<br>Estimated People Required (organic) 5.53<br></th>
357377
</tr></tfoot>
358378
</table></body></html>

mcp.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ Use by_file with sort=complexity to find the most complex files in a project.`),
8787
}
8888

8989
type mcpAnalyzeResponse struct {
90-
Path string `json:"path"`
91-
Languages []mcpLanguageResult `json:"languages"`
92-
Totals mcpTotals `json:"totals"`
93-
COCOMO *mcpCOCOMO `json:"cocomo,omitempty"`
94-
LOCOMO *mcpLOCOMO `json:"locomo,omitempty"`
95-
FileCount int64 `json:"totalFiles"`
96-
TotalLines int64 `json:"totalLines"`
97-
TotalCode int64 `json:"totalCode"`
90+
Path string `json:"path"`
91+
Languages []mcpLanguageResult `json:"languages"`
92+
Totals mcpTotals `json:"totals"`
93+
COCOMO *mcpCOCOMO `json:"cocomo,omitempty"`
94+
LOCOMO *mcpLOCOMO `json:"locomo,omitempty"`
95+
FileCount int64 `json:"totalFiles"`
96+
TotalLines int64 `json:"totalLines"`
97+
TotalCode int64 `json:"totalCode"`
9898
}
9999

100100
type mcpLanguageResult struct {

processor/processor.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package processor
44

55
import (
66
"fmt"
7+
"io"
78
"os"
89
"path/filepath"
910
"regexp"
@@ -595,7 +596,7 @@ func LanguageDatabase() map[string]Language {
595596
return languageDatabase
596597
}
597598

598-
func printLanguages() {
599+
func PrintLanguages(dst io.Writer) {
599600
names := make([]string, 0, len(languageDatabase))
600601
for key := range languageDatabase {
601602
names = append(names, key)
@@ -606,7 +607,7 @@ func printLanguages() {
606607
})
607608

608609
for _, name := range names {
609-
fmt.Printf("%s (%s)\n", name, strings.Join(append(languageDatabase[name].Extensions, languageDatabase[name].FileNames...), ","))
610+
_, _ = fmt.Fprintf(dst, "%s (%s)\n", name, strings.Join(append(languageDatabase[name].Extensions, languageDatabase[name].FileNames...), ","))
610611
}
611612
}
612613

@@ -618,7 +619,7 @@ var ulocLanguageCount = map[string]map[string]struct{}{}
618619
// Process is the main entry point of the command line it sets everything up and starts running
619620
func Process() {
620621
if Languages {
621-
printLanguages()
622+
PrintLanguages(os.Stdout)
622623
return
623624
}
624625

processor/processor_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package processor
44

55
import (
6+
"strings"
67
"testing"
78
)
89

@@ -85,7 +86,11 @@ func TestProcessFlags(t *testing.T) {
8586
}
8687

8788
func TestPrintLanguages(t *testing.T) {
88-
printLanguages()
89+
result := &strings.Builder{}
90+
PrintLanguages(result)
91+
if !strings.Contains(result.String(), "Go Template (tmpl,gohtml,gotxt)\n") {
92+
t.Fatal("printLanguages test failed")
93+
}
8994
}
9095

9196
func TestProcess(t *testing.T) {

scripts/include.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import (
1717
jsoniter "github.com/json-iterator/go"
1818
)
1919

20-
const constantsFile = "./processor/constants.go"
20+
const (
21+
constantsFile = "./processor/constants.go"
22+
languagesListFile = "./LANGUAGES.md"
23+
)
2124

2225
var json = jsoniter.ConfigCompatibleWithStandardLibrary
2326

@@ -69,7 +72,7 @@ func generateConstants() error {
6972
return fmt.Errorf("failed to format code: %v", err)
7073
}
7174

72-
out, err := os.OpenFile(constantsFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
75+
out, err := os.OpenFile(constantsFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
7376
if err != nil {
7477
return fmt.Errorf("failed to open constants file: %v", err)
7578
}
@@ -84,9 +87,29 @@ func generateConstants() error {
8487
return nil
8588
}
8689

90+
func generateLanguagesList() error {
91+
out, err := os.OpenFile(languagesListFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
92+
if err != nil {
93+
return fmt.Errorf("failed to open languages list file: %v", err)
94+
}
95+
defer func(file *os.File) {
96+
_ = file.Close()
97+
}(out)
98+
99+
_, _ = out.WriteString("```\n")
100+
processor.PrintLanguages(out)
101+
_, _ = out.WriteString("```\n")
102+
103+
return nil
104+
}
105+
87106
func main() {
88107
if err := generateConstants(); err != nil {
89108
fmt.Fprintf(os.Stderr, "failed to generate constants: %v\n", err)
90109
os.Exit(1)
91110
}
111+
if err := generateLanguagesList(); err != nil {
112+
fmt.Fprintf(os.Stderr, "failed to generate languages list: %v\n", err)
113+
os.Exit(1)
114+
}
92115
}

test-all.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ fi
3030
echo "Building application..."
3131
go build -ldflags="-s -w" || exit
3232

33-
echo '```' > LANGUAGES.md
34-
./scc --languages >> LANGUAGES.md
35-
echo '```' >> LANGUAGES.md
36-
3733

3834
echo "Building HTML report..."
3935

0 commit comments

Comments
 (0)