Skip to content

Commit

Permalink
Merge acfa038 into ed35f7b
Browse files Browse the repository at this point in the history
  • Loading branch information
boyter committed Jan 6, 2020
2 parents ed35f7b + acfa038 commit 59687e6
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 3 deletions.
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -93,7 +93,7 @@ func main() {
"format",
"f",
"tabular",
"set output format [tabular, wide, json, csv, cloc-yaml]",
"set output format [tabular, wide, json, csv, cloc-yaml, html, html-table]",
)
flags.StringSliceVarP(
&processor.AllowListExtensions,
Expand Down
121 changes: 120 additions & 1 deletion processor/formatters.go
Expand Up @@ -26,7 +26,6 @@ var shortNameTruncate = 20
var tabularShortFormatHeadNoComplexity = "%-22s %11s %11s %10s %11s %9s\n"
var tabularShortFormatBodyNoComplexity = "%-22s %11d %11d %10d %11d %9d\n"
var tabularShortFormatFileNoComplexity = "%-34s %11d %10d %11d %9d\n"
var shortFormatFileTrucateNoComplexity = 33
var longNameTruncate = 22

var tabularWideBreak = "─────────────────────────────────────────────────────────────────────────────────────────────────────────────\n"
Expand Down Expand Up @@ -284,6 +283,122 @@ func toCSV(input chan *FileJob) string {
return b.String()
}

func toHtml(input chan *FileJob) string {
return `<html lang="en"><head><meta charset="utf-8" /><title>scc html output</title><style>table { border-collapse: collapse; }td, th { border: 1px solid #999; padding: 0.5rem; text-align: left;}</style></head><body>` +
toHtmlTable(input) +
`</body></html>`
}

func toHtmlTable(input chan *FileJob) string {
languages := map[string]LanguageSummary{}
var sumFiles, sumLines, sumCode, sumComment, sumBlank, sumComplexity int64 = 0, 0, 0, 0, 0, 0

for res := range input {
sumFiles++
sumLines += res.Lines
sumCode += res.Code
sumComment += res.Comment
sumBlank += res.Blank
sumComplexity += res.Complexity

_, ok := languages[res.Language]

if !ok {
files := []*FileJob{}
files = append(files, res)

languages[res.Language] = LanguageSummary{
Name: res.Language,
Lines: res.Lines,
Code: res.Code,
Comment: res.Comment,
Blank: res.Blank,
Complexity: res.Complexity,
Count: 1,
Files: files,
}
} else {
tmp := languages[res.Language]
files := append(tmp.Files, res)

languages[res.Language] = LanguageSummary{
Name: res.Language,
Lines: tmp.Lines + res.Lines,
Code: tmp.Code + res.Code,
Comment: tmp.Comment + res.Comment,
Blank: tmp.Blank + res.Blank,
Complexity: tmp.Complexity + res.Complexity,
Count: tmp.Count + 1,
Files: files,
}
}
}

language := []LanguageSummary{}
for _, summary := range languages {
language = append(language, summary)
}

language = sortLanguageSummary(language)

var str strings.Builder

str.WriteString(`<table id="scc-table">
<thead><tr>
<th>Language</th>
<th>Files</th>
<th>Lines</th>
<th>Blank</th>
<th>Comment</th>
<th>Code</th>
<th>Complexity</th>
</tr></thead>
<tbody>`)

for _, r := range language {
str.WriteString(fmt.Sprintf(`<tr>
<th>%s</th>
<th>%d</th>
<th>%d</th>
<th>%d</th>
<th>%d</th>
<th>%d</th>
<th>%d</th>
</tr>`, r.Name, len(r.Files), r.Lines, r.Blank, r.Comment, r.Code, r.Complexity))

if Files {
sortSummaryFiles(&r)

for _, res := range r.Files {
str.WriteString(fmt.Sprintf(`<tr>
<td>%s</td>
<td></td>
<td>%d</td>
<td>%d</td>
<td>%d</td>
<td>%d</td>
<td>%d</td>
</tr>`, res.Location, res.Lines, res.Blank, res.Comment, res.Code, res.Complexity))
}
}

}

str.WriteString(fmt.Sprintf(`</tbody>
<tfoot><tr>
<th>Total</th>
<th>%d</th>
<th>%d</th>
<th>%d</th>
<th>%d</th>
<th>%d</th>
<th>%d</th>
</tr></tfoot>
</table>`, sumFiles, sumLines, sumBlank, sumComment, sumCode, sumComplexity))

return str.String()
}

func fileSummarize(input chan *FileJob) string {
switch {
case More || strings.ToLower(Format) == "wide":
Expand All @@ -294,6 +409,10 @@ func fileSummarize(input chan *FileJob) string {
return toClocYAML(input)
case strings.ToLower(Format) == "csv":
return toCSV(input)
case strings.ToLower(Format) == "html":
return toHtml(input)
case strings.ToLower(Format) == "html-table":
return toHtmlTable(input)
}

return fileSummarizeShort(input)
Expand Down
118 changes: 118 additions & 0 deletions processor/formatters_test.go
Expand Up @@ -30,6 +30,18 @@ func TestPrintError(t *testing.T) {
printError("Testing print error")
}

func TestPrintWarnF(t *testing.T) {
printWarnf("Testing print error")
}

func TestPrintDebugF(t *testing.T) {
printDebugf("Testing print error")
}

func TestPrintTraceF(t *testing.T) {
printTracef("Testing print error")
}

func TestGetFormattedTime(t *testing.T) {
res := getFormattedTime()

Expand Down Expand Up @@ -610,6 +622,60 @@ func TestFileSummarizeYml(t *testing.T) {
}
}

func TestFileSummarizeHtml(t *testing.T) {
inputChan := make(chan *FileJob, 1000)
inputChan <- &FileJob{
Language: "Go",
Filename: "bbbb.go",
Extension: "go",
Location: "./",
Bytes: 1000,
Lines: 1000,
Code: 1000,
Comment: 1000,
Blank: 1000,
Complexity: 1000,
WeightedComplexity: 1000,
Binary: false,
}

close(inputChan)
Format = "html"
More = false
res := fileSummarize(inputChan)

if !strings.Contains(res, `<th>1000`) {
t.Error("Expected HTML return", res)
}
}

func TestFileSummarizeHtmlTable(t *testing.T) {
inputChan := make(chan *FileJob, 1000)
inputChan <- &FileJob{
Language: "Go",
Filename: "bbbb.go",
Extension: "go",
Location: "./",
Bytes: 1000,
Lines: 1000,
Code: 1000,
Comment: 1000,
Blank: 1000,
Complexity: 1000,
WeightedComplexity: 1000,
Binary: false,
}

close(inputChan)
Format = "html-table"
More = false
res := fileSummarize(inputChan)

if !strings.Contains(res, `<th>1000`) {
t.Error("Expected HTML-table return", res)
}
}

func TestFileSummarizeDefault(t *testing.T) {
inputChan := make(chan *FileJob, 1000)
inputChan <- &FileJob{
Expand Down Expand Up @@ -839,6 +905,58 @@ func TestGetTabularWideBreak(t *testing.T) {
Ci = false
}

func TestToHTML(t *testing.T) {
inputChan := make(chan *FileJob, 1000)
inputChan <- &FileJob{
Language: "Go",
Filename: "bbbb.go",
Extension: "go",
Location: "./",
Bytes: 1000,
Lines: 1000,
Code: 1000,
Comment: 1000,
Blank: 1000,
Complexity: 1000,
WeightedComplexity: 1000,
Binary: false,
}
close(inputChan)
res := toHtml(inputChan)

if !strings.Contains(res, `<html lang="en">`) {
t.Error("Expected to have HTML wrapper")
}
}

func TestToHTMLTable(t *testing.T) {
inputChan := make(chan *FileJob, 1000)
inputChan <- &FileJob{
Language: "Go",
Filename: "bbbb.go",
Extension: "go",
Location: "./",
Bytes: 1000,
Lines: 1000,
Code: 1000,
Comment: 1000,
Blank: 1000,
Complexity: 1000,
WeightedComplexity: 1000,
Binary: false,
}
close(inputChan)
res := toHtmlTable(inputChan)

if strings.Contains(res, `<html lang="en">`) {
t.Error("Expected to not have wrapper")
}

if !strings.Contains(res, `<table id="scc-table">`) {
t.Error("Expected to have table element")
}
}

// When using columise ~28726 ns/op
// When using optimised ~14293 ns/op
func BenchmarkFileSummerize(b *testing.B) {
Expand Down
2 changes: 1 addition & 1 deletion processor/processor.go
Expand Up @@ -15,7 +15,7 @@ import (
)

// The version of the application
var Version = "2.10.1"
var Version = "2.11.0"

// Flags set via the CLI which control how the output is displayed

Expand Down
18 changes: 18 additions & 0 deletions test-all.sh
Expand Up @@ -453,6 +453,24 @@ else
exit
fi

if ./scc --format html | grep -q "html"; then
echo -e "${GREEN}PASSED html output test"
else
echo -e "${RED}======================================================="
echo -e "FAILED Should be able to output to html"
echo -e "=======================================================${NC}"
exit
fi

if ./scc --format html-table | grep -q "table"; then
echo -e "${GREEN}PASSED html-table output test"
else
echo -e "${RED}======================================================="
echo -e "FAILED Should be able to output to html-table"
echo -e "=======================================================${NC}"
exit
fi

# Try out specific languages
for i in 'Bosque ' 'Flow9 ' 'Bitbucket Pipeline ' 'Docker ignore ' 'Q# ' 'Futhark ' 'Alloy ' 'Wren ' 'Monkey C ' 'Alchemist ' 'Luna ' 'ignore ' 'XML Schema ' 'Web Services' 'Go ' 'Java ' 'Boo ' 'License ' 'BASH ' 'C Shell ' 'Korn Shell ' 'Makefile ' 'Shell ' 'Zsh ' 'Rakefile ' 'Gemfile ' 'Dockerfile '
do
Expand Down

0 comments on commit 59687e6

Please sign in to comment.