+
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 .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ linters:
- godox
- gci
- tenv
- err113
- testpackage
- gochecknoglobals # store global configuration values
- forbidigo # use printf for showing progress information and verbose output
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 @@ -35,9 +35,9 @@ var (
CoverageFactor float64

// Output format flags.
JSON OutputType = "json"
Tabular OutputType = "tabular"
AvailableOutputFormats = []OutputType{JSON, Tabular}
CSV OutputType = "csv"
AvailableOutputFormats = []OutputType{Tabular, CSV}

// Coverage Run formats.
Always = "Always"
Expand Down
3 changes: 3 additions & 0 deletions grit/cmd/stat/subcommands/churn.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var churnOpts = &git.ChurnOptions{
Until: time.Time{},
Path: "",
ExcludeRegex: nil,
OutputFormat: "",
}

var (
Expand Down Expand Up @@ -64,6 +65,8 @@ func init() {
fmt.Sprintf("Specify churn sort type: [%s, %s, %s, %s]", git.Changes, git.Additions, git.Deletions, git.Commits))
flags.IntVarP(&churnOpts.Top, flag.LongTop, flag.ShortTop, git.DefaultTop, "Number of top files to display")
flags.BoolVarP(&flag.Verbose, flag.LongVerbose, flag.ShortVerbose, false, "Show detailed progress")
flags.StringVarP(&churnOpts.OutputFormat, flag.LongFormat, flag.ShortFormat, flag.Tabular,
fmt.Sprintf("Specify output format: [%s, %s]", flag.Tabular, flag.CSV))
flags.StringVar(&excludeChurnRegex, flag.LongExclude, "", "Exclude files matching regex pattern")
flags.StringSliceVarP(&extensionList, flag.LongExtensions, flag.ShortExt, nil,
"Only include files with given extensions in comma-separated list, e.g. 'go,h,c'")
Expand Down
7 changes: 4 additions & 3 deletions grit/cmd/stat/subcommands/complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var complexityOpts = complexity.Options{
Engine: complexity.Gocyclo,
ExcludeRegex: nil,
Top: 10, //nolint:mnd // default value
OutputFormat: "",
}

var excludeComplexityRegex string
Expand Down Expand Up @@ -42,9 +43,7 @@ var ComplexityCmd = &cobra.Command{ //nolint:exhaustruct // no need to set all f

fileStat = complexity.SortAndLimit(fileStat, complexityOpts)

complexity.PrintTabular(fileStat, os.Stdout)

return nil
return complexity.PrintStats(fileStat, os.Stdout, &complexityOpts)
},
}

Expand All @@ -56,4 +55,6 @@ func init() {
flags.IntVarP(&complexityOpts.Top, flag.LongTop, flag.ShortTop, git.DefaultTop, "Number of top files to display")
flags.BoolVarP(&flag.Verbose, flag.LongVerbose, flag.ShortVerbose, false, "Show detailed progress")
flags.StringVar(&excludeComplexityRegex, flag.LongExclude, "", "Exclude files matching regex pattern")
flags.StringVarP(&complexityOpts.OutputFormat, flag.LongFormat, flag.ShortFormat, flag.Tabular,
fmt.Sprintf("Specify output format: [%s, %s]", flag.Tabular, flag.CSV))
}
7 changes: 4 additions & 3 deletions grit/cmd/stat/subcommands/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var coverageOpts = &coverage.Options{
ExcludeRegex: nil,
RunCoverage: "",
CoverageFilename: "coverage.out",
OutputFormat: "",
}

var excludeCoverageRegex string
Expand Down Expand Up @@ -44,9 +45,7 @@ var CoverageCmd = &cobra.Command{ //nolint:exhaustruct // no need to set all fie

covData = coverage.SortAndLimit(covData, coverageOpts.SortBy, coverageOpts.Top)

coverage.PrintTabular(covData, os.Stdout)

return nil
return coverage.PrintStats(covData, os.Stdout, coverageOpts)
},
}

Expand All @@ -66,4 +65,6 @@ func init() {
flags.BoolVarP(&flag.Verbose, flag.LongVerbose, flag.ShortVerbose, false, "Enable verbose output")
flags.IntVarP(&coverageOpts.Top, flag.LongTop, flag.ShortTop, flag.DefaultTop, "Number of top files to display")
flags.StringVar(&excludeCoverageRegex, flag.LongExclude, "", "Exclude files matching regex pattern")
flags.StringVarP(&coverageOpts.OutputFormat, flag.LongFormat, flag.ShortFormat, flag.Tabular,
fmt.Sprintf("Specify output format: [%s, %s]", flag.Tabular, flag.CSV))
}
32 changes: 32 additions & 0 deletions pkg/complexity/print.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package complexity

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

"github.com/bndr/gotabulate"
"github.com/vbvictor/grit/grit/cmd/flag"
)

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

return nil
}

func PrintTabular(results []*FileStat, out io.Writer) {
_, _ = io.WriteString(out, "\nCode complexity analysis results:\n")

Expand All @@ -20,3 +37,18 @@ func PrintTabular(results []*FileStat, out io.Writer) {

_, _ = io.WriteString(out, table.Render("grid"))
}

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

_ = writer.Write([]string{"FILEPATH", "COMPLEXITY"})

for _, result := range results {
record := []string{
result.Path,
strconv.FormatFloat(result.AvgComplexity, 'f', 2, 64),
}
_ = writer.Write(record)
}
}
74 changes: 70 additions & 4 deletions pkg/complexity/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package complexity

import (
"bytes"
"strings"
"encoding/csv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPrintTabular(t *testing.T) {
Expand Down Expand Up @@ -54,9 +57,72 @@ func TestPrintTabular(t *testing.T) {

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)
}
assert.Contains(t, output, exp, "Expected output to contain %q", exp)
}
})
}
}

func TestPrintCSV(t *testing.T) {
testCases := []struct {
name string
input []*FileStat
expected [][]string
}{
{
name: "single file complexity",
input: []*FileStat{
{
Path: "main.go",
AvgComplexity: 4,
},
},
expected: [][]string{
{"FILEPATH", "COMPLEXITY"},
{"main.go", "4.00"},
},
},
{
name: "multiple files complexity",
input: []*FileStat{
{
Path: "path/to/foo.go",
AvgComplexity: 2,
},
{
Path: "bar.go",
AvgComplexity: 6,
},
},
expected: [][]string{
{"FILEPATH", "COMPLEXITY"},
{"path/to/foo.go", "2.00"},
{"bar.go", "6.00"},
},
},
{
name: "empty input",
input: []*FileStat{},
expected: [][]string{
{"FILEPATH", "COMPLEXITY"},
},
},
}

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

PrintCSV(tc.input, &buf)

reader := csv.NewReader(bytes.NewReader(buf.Bytes()))
output, err := reader.ReadAll()
require.NoError(t, err, "Failed to parse CSV output")

assert.Equal(t, len(tc.expected), len(output), "Row count mismatch")

for i, expectedRow := range tc.expected {
assert.ElementsMatch(t, expectedRow, output[i])
}
})
}
Expand Down
1 change: 1 addition & 0 deletions pkg/complexity/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Options struct {
Engine string
ExcludeRegex *regexp.Regexp
Top int
OutputFormat string
}

var ErrUnsupportedEngine = errors.New("unsupported complexity engine")
Expand Down
14 changes: 14 additions & 0 deletions pkg/coverage/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@ import (
"io"

"github.com/bndr/gotabulate"
"github.com/vbvictor/grit/grit/cmd/flag"
)

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

return nil
}

func PrintTabular(results []*FileCoverage, out io.Writer) {
fmt.Fprintf(out, "\nCode coverage analysis results:\n")

Expand Down
1 change: 1 addition & 0 deletions pkg/coverage/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Options struct {
ExcludeRegex *regexp.Regexp
RunCoverage string
CoverageFilename string
OutputFormat string
}

func PopulateOpts(opts *Options, excludeRegex string) error {
Expand Down
34 changes: 31 additions & 3 deletions pkg/git/print.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package git

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

"github.com/bndr/gotabulate"
"github.com/vbvictor/grit/grit/cmd/flag"
)

func PrintStats(results []*ChurnChunk, out io.Writer, opts *ChurnOptions) error {
printTable(results, out, opts)
switch opts.OutputFormat {
case flag.CSV:
printCSV(results, out, opts)
case flag.Tabular:
printTable(results, out, opts)
default:
return fmt.Errorf("unsupported output format: %s", opts.OutputFormat)
}

return nil
}

func printTable(results []*ChurnChunk, out io.Writer, opts *ChurnOptions) {
fmt.Fprintf(out, "\nTop %d most modified files by %s:\n", opts.Top, opts.SortBy)

data := make([][]interface{}, len(results))
data := make([][]any, len(results))

for i, result := range results {
data[i] = []interface{}{result.Churn, result.Added, result.Removed, result.Commits, result.File}
data[i] = []any{result.Churn, result.Added, result.Removed, result.Commits, result.File}
}

table := gotabulate.Create(data)
Expand All @@ -28,3 +38,21 @@ func printTable(results []*ChurnChunk, out io.Writer, opts *ChurnOptions) {

_, _ = io.WriteString(out, table.Render("grid"))
}

func printCSV(results []*ChurnChunk, out io.Writer, _ *ChurnOptions) {
writer := csv.NewWriter(out)
defer writer.Flush()

_ = writer.Write([]string{"FILEPATH", "CHANGES", "ADDED", "DELETED", "COMMITS"})

for _, result := range results {
record := []string{
result.File,
strconv.Itoa(result.Churn),
strconv.Itoa(result.Added),
strconv.Itoa(result.Removed),
strconv.Itoa(result.Commits),
}
_ = writer.Write(record)
}
}
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载