-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure path prefix via processor abstraction (#1226)
- Loading branch information
Showing
8 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters