Skip to content

Commit

Permalink
Configure path prefix via processor abstraction (#1226)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilner committed Jul 10, 2020
1 parent 916213c commit 6550984
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ output:
# make issues output unique by line, default is true
uniq-by-line: true

# add a prefix to the output file references; default is no prefix
path-prefix: ""


# all available settings of specific linters
linters-settings:
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
fs.BoolVar(&oc.PrintLinterName, "print-linter-name", true, wh("Print linter name in issue line"))
fs.BoolVar(&oc.UniqByLine, "uniq-by-line", true, wh("Make issues output unique by line"))
fs.BoolVar(&oc.PrintWelcomeMessage, "print-welcome", false, wh("Print welcome message"))
fs.StringVar(&oc.PathPrefix, "path-prefix", "", wh("Path prefix to add to output"))
hideFlag("print-welcome") // no longer used

// Run config
Expand Down
9 changes: 5 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,11 @@ type Config struct {
Output struct {
Format string
Color string
PrintIssuedLine bool `mapstructure:"print-issued-lines"`
PrintLinterName bool `mapstructure:"print-linter-name"`
UniqByLine bool `mapstructure:"uniq-by-line"`
PrintWelcomeMessage bool `mapstructure:"print-welcome"`
PrintIssuedLine bool `mapstructure:"print-issued-lines"`
PrintLinterName bool `mapstructure:"print-linter-name"`
UniqByLine bool `mapstructure:"uniq-by-line"`
PrintWelcomeMessage bool `mapstructure:"print-welcome"`
PathPrefix string `mapstructure:"path-prefix"`
}

LintersSettings LintersSettings `mapstructure:"linters-settings"`
Expand Down
1 change: 1 addition & 0 deletions pkg/lint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lint
processors.NewSourceCode(lineCache, log.Child("source_code")),
processors.NewPathShortener(),
getSeverityRulesProcessor(&cfg.Severity, log, lineCache),
processors.NewPathPrefixer(cfg.Output.PathPrefix),
},
Log: log,
}, nil
Expand Down
37 changes: 37 additions & 0 deletions pkg/result/processors/path_prefixer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package processors

import (
"path"

"github.com/golangci/golangci-lint/pkg/result"
)

// PathPrefixer adds a customizable prefix to every output path
type PathPrefixer struct {
prefix string
}

var _ Processor = new(PathPrefixer)

// NewPathPrefixer returns a new path prefixer for the provided string
func NewPathPrefixer(prefix string) *PathPrefixer {
return &PathPrefixer{prefix: prefix}
}

// Name returns the name of this processor
func (*PathPrefixer) Name() string {
return "path_prefixer"
}

// Process adds the prefix to each path
func (p *PathPrefixer) Process(issues []result.Issue) ([]result.Issue, error) {
if p.prefix != "" {
for i := range issues {
issues[i].Pos.Filename = path.Join(p.prefix, issues[i].Pos.Filename)
}
}
return issues, nil
}

// Finish is implemented to satisfy the Processor interface
func (*PathPrefixer) Finish() {}
37 changes: 37 additions & 0 deletions pkg/result/processors/path_prefixer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package processors

import (
"go/token"
"testing"

"github.com/stretchr/testify/require"

"github.com/golangci/golangci-lint/pkg/result"
)

func TestPathPrefixer_Process(t *testing.T) {
paths := func(ps ...string) (issues []result.Issue) {
for _, p := range ps {
issues = append(issues, result.Issue{Pos: token.Position{Filename: p}})
}
return
}
for _, tt := range []struct {
name, prefix string
issues, want []result.Issue
}{
{"empty prefix", "", paths("some/path", "cool"), paths("some/path", "cool")},
{"prefix", "ok", paths("some/path", "cool"), paths("ok/some/path", "ok/cool")},
{"prefix slashed", "ok/", paths("some/path", "cool"), paths("ok/some/path", "ok/cool")},
} {
t.Run(tt.name, func(t *testing.T) {
r := require.New(t)

p := NewPathPrefixer(tt.prefix) //nolint:scopelint
got, err := p.Process(tt.issues) //nolint:scopelint
r.NoError(err, "prefixer should never error")

r.Equal(got, tt.want) //nolint:scopelint
})
}
}
19 changes: 19 additions & 0 deletions test/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,22 @@ func TestDisallowedOptionsInConfig(t *testing.T) {
r.RunWithYamlConfig(c.cfg, withCommonRunArgs(args...)...).ExpectExitCode(exitcodes.Failure)
}
}

func TestPathPrefix(t *testing.T) {
for _, tt := range []struct {
Name string
Args []string
Pattern string
}{
{"empty", nil, "^testdata/withTests/"},
{"prefixed", []string{"--path-prefix=cool"}, "^cool/testdata/withTests"},
} {
t.Run(tt.Name, func(t *testing.T) {
testshared.NewLintRunner(t).Run(
append(tt.Args, getTestDataDir("withTests"))..., //nolint:scopelint
).ExpectOutputRegexp(
tt.Pattern, //nolint:scopelint
)
})
}
}
6 changes: 6 additions & 0 deletions test/testshared/testshared.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func (r *RunResult) ExpectExitCode(possibleCodes ...int) *RunResult {
return r
}

// ExpectOutputRegexp can be called with either a string or compiled regexp
func (r *RunResult) ExpectOutputRegexp(s interface{}) *RunResult {
assert.Regexp(r.t, s, r.output, "exit code is %d", r.exitCode)
return r
}

func (r *RunResult) ExpectOutputContains(s string) *RunResult {
assert.Contains(r.t, r.output, s, "exit code is %d", r.exitCode)
return r
Expand Down

0 comments on commit 6550984

Please sign in to comment.