+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ go.work.sum
dev_data
test_data
plots
tmp

# Test results
testdata/charts
Expand Down
4 changes: 2 additions & 2 deletions grit/cmd/flag/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ const (
LongSince = "since"
LongUntil = "until"
LongFormat = "format"
LongEngine = "engine"
LongEngine = "complexity-engine"
LongRunCoverage = "run-tests"
LongFileCoverage = "coverage"
LongFileCoverage = "coverage-file"

// Flag shortcuts.
ShortTop = "t"
Expand Down
18 changes: 17 additions & 1 deletion grit/cmd/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package report

import (
"fmt"
"io"
"os"
"path/filepath"
"time"
Expand All @@ -19,6 +20,7 @@ var (
top int
since string
until string
outputFormat string
)

var churnOpts = &git.ChurnOptions{
Expand Down Expand Up @@ -104,7 +106,7 @@ var ReportCmd = &cobra.Command{
fileScores = report.SortAndLimit(report.CalculateScores(fileScores, reportOpts), top)
flag.LogIfVerbose("Got %d file scores\n", len(fileScores))

return report.PrintStats(fileScores, os.Stdout, reportOpts)
return printReport(fileScores, os.Stdout, &reportOpts, outputFormat)
},
}

Expand All @@ -130,4 +132,18 @@ func init() {

// Report specific flags
flag.PerfectCoverageFlag(flags, &reportOpts.PerfectCoverage)
flag.OutputFormatFlag(flags, &outputFormat)
}

func printReport(results []*report.FileScore, out io.Writer, opts *report.Options, format string) error {
switch format {
case flag.CSV:
report.PrintCSV(results, out, opts)
case flag.Tabular:
report.PrintTabular(results, out, opts)
default:
return fmt.Errorf("unsupported output format: %s", format)
}

return nil
}
33 changes: 26 additions & 7 deletions pkg/report/print.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package report

import (
"encoding/csv"
"fmt"
"io"

"github.com/bndr/gotabulate"
)

func PrintStats(results []*FileScore, out io.Writer, opts Options) error {
return printTabular(results, out, opts)
}

func printTabular(results []*FileScore, out io.Writer, opts Options) error {
func PrintTabular(results []*FileScore, out io.Writer, opts *Options) {
fmt.Fprintf(out, "\nCode health analysis results (top %d):\n", opts.Top)

data := make([][]any, len(results))
Expand All @@ -30,8 +27,30 @@ func printTabular(results []*FileScore, out io.Writer, opts Options) error {
table.SetAlign("left")

if _, err := io.WriteString(out, table.Render("grid")); err != nil {
return fmt.Errorf("failed to write grid: %w", err)
return
}
}

func PrintCSV(results []*FileScore, out io.Writer, _ *Options) {
writer := csv.NewWriter(out)
defer writer.Flush()

return nil
// Write headers
if err := writer.Write([]string{"FILEPATH", "SCORE", "CHURN", "COMPLEXITY", "COVERAGE"}); err != nil {
return
}

// Write data
for _, result := range results {
record := []string{
result.File,
fmt.Sprintf("%.2f", result.Score),
fmt.Sprintf("%.2f", result.Churn),
fmt.Sprintf("%.2f", result.Complexity),
fmt.Sprintf("%.2f", result.Coverage),
}
if err := writer.Write(record); err != nil {
return
}
}
}
74 changes: 69 additions & 5 deletions pkg/report/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,78 @@ func TestPrintTabular(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer

err := printTabular(tc.input, &buf, Options{})
require.NoError(t, err)
PrintTabular(tc.input, &buf, &Options{})

output := buf.String()
for _, exp := range tc.expected {
if !strings.Contains(output, exp) {
t.Errorf("Expected output to contain %q, but it didn't.\nGot: %s", exp, output)
}
require.Contains(t, output, exp,
"Expected output to contain %q, but it didn't.\nGot: %s", exp, output)
}
})
}
}

func TestPrintCSV(t *testing.T) {
testCases := []struct {
name string
input []*FileScore
expected []string
}{
{
name: "single file score",
input: []*FileScore{
{
File: "main.go",
Coverage: 75.5,
Complexity: 4.2,
Churn: 100,
Score: 42.5,
},
},
expected: []string{
"FILEPATH,SCORE,CHURN,COMPLEXITY,COVERAGE",
"main.go,42.50,100.00,4.20,75.50",
},
},
{
name: "multiple files score",
input: []*FileScore{
{
File: "path/to/foo.go",
Coverage: 90.0,
Complexity: 2.5,
Churn: 50,
Score: 20.5,
},
{
File: "bar.go",
Coverage: 60.5,
Complexity: 6.0,
Churn: 150,
Score: 85.2,
},
},
expected: []string{
"FILEPATH,SCORE,CHURN,COMPLEXITY,COVERAGE",
"path/to/foo.go,20.50,50.00,2.50,90.00",
"bar.go,85.20,150.00,6.00,60.50",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer

PrintCSV(tc.input, &buf, &Options{})

output := buf.String()
lines := strings.Split(output, "\n")

require.Equal(t, len(tc.expected), len(lines)-1) // -1 for trailing newline

for i, exp := range tc.expected {
require.Equal(t, exp, lines[i])
}
})
}
Expand Down
10 changes: 10 additions & 0 deletions test/gritrepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ func TestGritBasicFunctionality(t *testing.T) {

RunGritTests(t, tests)
}

/*
func newGritReportValidator(expectedOutputs ...string) OutputValidator {
return func(t *testing.T, stdout, _ string) bool {
t.Helper()

return false
}
}
*/
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载