-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: sharable config presets #2241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package config | ||
|
||
type Config struct { | ||
Run Run | ||
Run Run | ||
Presets []string | ||
|
||
Output Output | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package config | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/golangci/golangci-lint/pkg/logutils" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFileReader_Read(t *testing.T) { | ||
cases := map[string]Config{ | ||
"preset_parent.yml": { | ||
Linters: Linters{ | ||
EnableAll: true, | ||
Fast: true, | ||
Enable: []string{"deadcode", "errcheck", "gosimple"}, | ||
}, | ||
}, | ||
"preset_single.yml": { | ||
Linters: Linters{ | ||
Fast: true, | ||
Enable: []string{"errcheck", "gosimple", "govet"}, | ||
Disable: []string{"deadcode"}, | ||
}, | ||
Presets: []string{"./preset_parent.yml"}, | ||
}, | ||
"preset_double.yml": { | ||
Linters: Linters{ | ||
Enable: []string{"errcheck", "gosimple", "govet"}, | ||
Disable: []string{"deadcode"}, | ||
}, | ||
Output: Output{ | ||
PrintIssuedLine: true, | ||
}, | ||
Presets: []string{"./preset_parent.yml", "./preset_single.yml"}, | ||
}, | ||
"preset_order.yml": { | ||
Linters: Linters{ | ||
Fast: true, | ||
Disable: []string{"deadcode", "errcheck", "gosimple", "govet"}, | ||
}, | ||
Presets: []string{"./preset_parent.yml", "./preset_single.yml", "./disable_all.yml"}, | ||
}, | ||
} | ||
|
||
for cfgFile, expected := range cases { | ||
t.Run(cfgFile, func(t *testing.T) { | ||
cfg := readConfig(t, cfgFile) | ||
assert.Equal(t, expected, cfg) | ||
}) | ||
} | ||
} | ||
|
||
func TestFileReader_Read_Module(t *testing.T) { | ||
cfg := readConfig(t, "preset_module.yml") | ||
assert.NotEmpty(t, cfg.Linters.Enable) | ||
} | ||
|
||
func readConfig(t *testing.T, fn string) Config { | ||
var cfg, cmdCfg Config | ||
cmdCfg.Run.Config = filepath.Join("..", "..", "test", "testdata", "configs", fn) | ||
log := logutils.NewMockLog() | ||
log.On("Infof", mock.Anything, mock.Anything).Maybe() | ||
|
||
r := NewFileReader(&cfg, &cmdCfg, log) | ||
err := r.Read() | ||
require.NoError(t, err) | ||
return cfg | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
linters: | ||
disable: | ||
- deadcode | ||
- errcheck | ||
- gosimple | ||
- govet |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
linters: | ||
# overwrite the value in preset_single.yml | ||
fast: false | ||
|
||
# add a new value | ||
output: | ||
print-issued-lines: true | ||
|
||
presets: | ||
- ./preset_parent.yml | ||
- ./preset_single.yml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# TODO: host a preset in the golangci org? | ||
presets: | ||
- github.com/thepwagner/golangci-lint-yaml | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could it be located in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, but I'm not sold on that path: the goal of this test is to exercise loading the configuration from a go module. Using a
I figured since #1141 mentions "presets like Any repo with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. In my case I would like have any github or https path (not exactly the go module or some convention of names). presets:
- local/path/config.yml
- https://raw.githubusercontent.com/golangci/golangci-lint/master/.pre-commit-hooks.yaml
- github:golangci/golangci-lint/master/subfolder/docs/common.yaml There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Per #2137 (comment) , I don't think
That might be useful, e.g. to have multiple configurations in a single repository. Would it be acceptable for that path to also be a valid go module? Again, I wanted to avoid adding outgoing network calls, authentication etc. Only using files provided by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It make sense. Thank you for the hint. In this case then I would propose first support only local path and think about go mod solution in separate PR. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
presets: | ||
- ./preset_parent.yml | ||
- ./preset_single.yml | ||
# appears last, overwriting previously enabled checks | ||
- ./disable_all.yml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
linters: | ||
enable-all: true | ||
fast: true | ||
enable: | ||
- deadcode | ||
- errcheck | ||
- gosimple |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
linters: | ||
# overwrite a value in preset_parent.yml | ||
enable-all: false | ||
# enable and disable a linter | ||
enable: | ||
- govet | ||
disable: | ||
- deadcode | ||
|
||
presets: | ||
- ./preset_parent.yml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It means the local path always should be started with
./
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or
../
. The pattern for distinguishing modules from local paths is inspired by the right hand side of replace directives ingo.mod
./
might also be a valid prefix to trigger file loading that Imisseddid not think would be used.