From a9287491e94cd6762ca7433db694de4af8dd5de7 Mon Sep 17 00:00:00 2001 From: Ethan Reesor Date: Fri, 16 Feb 2024 10:59:45 -0600 Subject: [PATCH 1/6] dev: enable --fix for go-header --- go.mod | 2 +- go.sum | 4 ++-- pkg/golinters/goheader.go | 12 ++++++++++++ test/testdata/fix/in/go-header.go | 6 ++++++ test/testdata/fix/out/go-header.go | 6 ++++++ 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 test/testdata/fix/in/go-header.go create mode 100644 test/testdata/fix/out/go-header.go diff --git a/go.mod b/go.mod index cad2143c8cbe..1e7000f67935 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,7 @@ require ( github.com/ckaznocha/intrange v0.1.0 github.com/curioswitch/go-reassign v0.2.0 github.com/daixiang0/gci v0.12.3 - github.com/denis-tingaikin/go-header v0.4.3 + github.com/denis-tingaikin/go-header v0.4.4-0.20240216000427-8715c00948e1 github.com/esimonov/ifshort v1.0.4 github.com/fatih/color v1.16.0 github.com/firefart/nonamedreturns v1.0.4 diff --git a/go.sum b/go.sum index 9088c3540c62..00d0eed4ee28 100644 --- a/go.sum +++ b/go.sum @@ -127,8 +127,8 @@ github.com/daixiang0/gci v0.12.3/go.mod h1:xtHP9N7AHdNvtRNfcx9gwTDfw7FRJx4bZUsiE github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/denis-tingaikin/go-header v0.4.3 h1:tEaZKAlqql6SKCY++utLmkPLd6K8IBM20Ha7UVm+mtU= -github.com/denis-tingaikin/go-header v0.4.3/go.mod h1:0wOCWuN71D5qIgE2nz9KrKmuYBAC2Mra5RassOIQ2/c= +github.com/denis-tingaikin/go-header v0.4.4-0.20240216000427-8715c00948e1 h1:iRrzLnD65fQulU4R+4DTnq4fanBj7XnFMXekgBYsAfU= +github.com/denis-tingaikin/go-header v0.4.4-0.20240216000427-8715c00948e1/go.mod h1:mMenU5bWrok6Wl2UsZjy+1okegmwQ3UgWl4V1D8gjlY= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= diff --git a/pkg/golinters/goheader.go b/pkg/golinters/goheader.go index d3cfefa90b28..c2e2fcc95d54 100644 --- a/pkg/golinters/goheader.go +++ b/pkg/golinters/goheader.go @@ -87,6 +87,7 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) ([]goanalysi continue } + i = a.Analyze(&goheader.Target{File: file, Path: path}) issue := result.Issue{ Pos: token.Position{ Line: i.Location().Line + 1, @@ -97,6 +98,17 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) ([]goanalysi FromLinter: goHeaderName, } + if fix := i.Fix(); fix != nil { + issue.LineRange = &result.Range{ + From: issue.Line(), + To: issue.Line() + len(fix.Actual) - 1, + } + issue.Replacement = &result.Replacement{ + NeedOnlyDelete: len(fix.Expected) == 0, + NewLines: fix.Expected, + } + } + issues = append(issues, goanalysis.NewIssue(&issue, pass)) } diff --git a/test/testdata/fix/in/go-header.go b/test/testdata/fix/in/go-header.go new file mode 100644 index 000000000000..3893d14f0b5b --- /dev/null +++ b/test/testdata/fix/in/go-header.go @@ -0,0 +1,6 @@ +/*MY TITLE!*/ + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header.yml +package p diff --git a/test/testdata/fix/out/go-header.go b/test/testdata/fix/out/go-header.go new file mode 100644 index 000000000000..258c954c2c86 --- /dev/null +++ b/test/testdata/fix/out/go-header.go @@ -0,0 +1,6 @@ +/*MY TITLE.*/ + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header.yml +package p From 4583130d32e8ddfe6d9292c1159bb6e301cb4c5e Mon Sep 17 00:00:00 2001 From: Ethan Reesor Date: Fri, 16 Feb 2024 11:28:42 -0600 Subject: [PATCH 2/6] More tests --- test/fix_test.go | 11 +++++++++-- test/testdata/configs/go-header-fix.yml | 9 +++++++++ test/testdata/fix/in/go-header.go | 6 ------ test/testdata/fix/in/go-header_1.go | 6 ++++++ test/testdata/fix/in/go-header_2.go | 8 ++++++++ test/testdata/fix/in/go-header_3.go | 10 ++++++++++ test/testdata/fix/out/go-header.go | 6 ------ test/testdata/fix/out/go-header_1.go | 8 ++++++++ test/testdata/fix/out/go-header_2.go | 8 ++++++++ test/testdata/fix/out/go-header_3.go | 10 ++++++++++ test/testshared/directives.go | 8 +++++++- 11 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 test/testdata/configs/go-header-fix.yml delete mode 100644 test/testdata/fix/in/go-header.go create mode 100644 test/testdata/fix/in/go-header_1.go create mode 100644 test/testdata/fix/in/go-header_2.go create mode 100644 test/testdata/fix/in/go-header_3.go delete mode 100644 test/testdata/fix/out/go-header.go create mode 100644 test/testdata/fix/out/go-header_1.go create mode 100644 test/testdata/fix/out/go-header_2.go create mode 100644 test/testdata/fix/out/go-header_3.go diff --git a/test/fix_test.go b/test/fix_test.go index 87dff82dbe3d..53917f4ba95d 100644 --- a/test/fix_test.go +++ b/test/fix_test.go @@ -4,6 +4,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "testing" "github.com/stretchr/testify/require" @@ -71,7 +72,7 @@ func TestFix(t *testing.T) { expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input))) require.NoError(t, err) - require.Equal(t, string(expectedOutput), string(output)) + require.Equal(t, trimTrailingSpaces(string(expectedOutput)), trimTrailingSpaces(string(output))) }) } } @@ -110,7 +111,13 @@ func TestFix_pathPrefix(t *testing.T) { expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input))) require.NoError(t, err) - require.Equal(t, string(expectedOutput), string(output)) + require.Equal(t, trimTrailingSpaces(string(expectedOutput)), trimTrailingSpaces(string(output))) }) } } + +var reTrailingSpaces = regexp.MustCompile(`(?m)(\s+)$`) + +func trimTrailingSpaces(s string) string { + return reTrailingSpaces.ReplaceAllString(s, "") +} diff --git a/test/testdata/configs/go-header-fix.yml b/test/testdata/configs/go-header-fix.yml new file mode 100644 index 000000000000..fa1371e3b1ee --- /dev/null +++ b/test/testdata/configs/go-header-fix.yml @@ -0,0 +1,9 @@ +linters-settings: + goheader: + values: + const: + AUTHOR: The Awesome Project Authors + template: |- + Copyright 2024 {{ AUTHOR }} + + Use of this source code is governed by LICENSE diff --git a/test/testdata/fix/in/go-header.go b/test/testdata/fix/in/go-header.go deleted file mode 100644 index 3893d14f0b5b..000000000000 --- a/test/testdata/fix/in/go-header.go +++ /dev/null @@ -1,6 +0,0 @@ -/*MY TITLE!*/ - -//golangcitest:args -Egoheader -//golangcitest:expected_exitcode 0 -//golangcitest:config_path testdata/configs/go-header.yml -package p diff --git a/test/testdata/fix/in/go-header_1.go b/test/testdata/fix/in/go-header_1.go new file mode 100644 index 000000000000..7fd59b97a29c --- /dev/null +++ b/test/testdata/fix/in/go-header_1.go @@ -0,0 +1,6 @@ +// Copyright 1999 The Awesome Project Authors + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testdata/fix/in/go-header_2.go b/test/testdata/fix/in/go-header_2.go new file mode 100644 index 000000000000..ed70ffd4ee24 --- /dev/null +++ b/test/testdata/fix/in/go-header_2.go @@ -0,0 +1,8 @@ +/* Copyright 1999 The Awesome Project Authors + +Use of this source code is governed */ + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testdata/fix/in/go-header_3.go b/test/testdata/fix/in/go-header_3.go new file mode 100644 index 000000000000..2961f0d5a9ab --- /dev/null +++ b/test/testdata/fix/in/go-header_3.go @@ -0,0 +1,10 @@ +/* +Copyright 1999 The Awesome + +Use of this source code is governed by LICENSE +*/ + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testdata/fix/out/go-header.go b/test/testdata/fix/out/go-header.go deleted file mode 100644 index 258c954c2c86..000000000000 --- a/test/testdata/fix/out/go-header.go +++ /dev/null @@ -1,6 +0,0 @@ -/*MY TITLE.*/ - -//golangcitest:args -Egoheader -//golangcitest:expected_exitcode 0 -//golangcitest:config_path testdata/configs/go-header.yml -package p diff --git a/test/testdata/fix/out/go-header_1.go b/test/testdata/fix/out/go-header_1.go new file mode 100644 index 000000000000..4484c537472d --- /dev/null +++ b/test/testdata/fix/out/go-header_1.go @@ -0,0 +1,8 @@ +// Copyright 2024 The Awesome Project Authors +// +// Use of this source code is governed by LICENSE + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testdata/fix/out/go-header_2.go b/test/testdata/fix/out/go-header_2.go new file mode 100644 index 000000000000..80a336a47a8e --- /dev/null +++ b/test/testdata/fix/out/go-header_2.go @@ -0,0 +1,8 @@ +/* Copyright 2024 The Awesome Project Authors + +Use of this source code is governed by LICENSE */ + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testdata/fix/out/go-header_3.go b/test/testdata/fix/out/go-header_3.go new file mode 100644 index 000000000000..040cad6e8758 --- /dev/null +++ b/test/testdata/fix/out/go-header_3.go @@ -0,0 +1,10 @@ +/* +Copyright 2024 The Awesome Project Authors + +Use of this source code is governed by LICENSE +*/ + +//golangcitest:args -Egoheader +//golangcitest:expected_exitcode 0 +//golangcitest:config_path testdata/configs/go-header-fix.yml +package p diff --git a/test/testshared/directives.go b/test/testshared/directives.go index 01c011bb436f..34f37daf3a15 100644 --- a/test/testshared/directives.go +++ b/test/testshared/directives.go @@ -59,7 +59,13 @@ func ParseTestDirectives(tb testing.TB, sourcePath string) *RunContext { continue } - if !strings.HasPrefix(line, "//golangcitest:") { + switch { + case strings.HasPrefix(line, "//golangcitest:"): + // Ok + case !strings.Contains(line, "golangcitest"): + // Assume this is a regular comment (required for go-header tests) + continue + default: require.Failf(tb, "invalid prefix of comment line %s", line) } From 1bc60d929799a1142cecedbd6349000480300c54 Mon Sep 17 00:00:00 2001 From: Ethan Reesor Date: Fri, 16 Feb 2024 11:29:33 -0600 Subject: [PATCH 3/6] Oops --- pkg/golinters/goheader.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/golinters/goheader.go b/pkg/golinters/goheader.go index c2e2fcc95d54..5ed5d9d8ded6 100644 --- a/pkg/golinters/goheader.go +++ b/pkg/golinters/goheader.go @@ -87,7 +87,6 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) ([]goanalysi continue } - i = a.Analyze(&goheader.Target{File: file, Path: path}) issue := result.Issue{ Pos: token.Position{ Line: i.Location().Line + 1, From d968b23d6132783fc0694ca35d2acb1f75535ecf Mon Sep 17 00:00:00 2001 From: Ethan Reesor Date: Fri, 16 Feb 2024 11:34:28 -0600 Subject: [PATCH 4/6] Revert fix_test changes --- test/fix_test.go | 11 ++--------- test/testdata/fix/out/go-header_1.go | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/test/fix_test.go b/test/fix_test.go index 53917f4ba95d..87dff82dbe3d 100644 --- a/test/fix_test.go +++ b/test/fix_test.go @@ -4,7 +4,6 @@ import ( "os" "os/exec" "path/filepath" - "regexp" "testing" "github.com/stretchr/testify/require" @@ -72,7 +71,7 @@ func TestFix(t *testing.T) { expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input))) require.NoError(t, err) - require.Equal(t, trimTrailingSpaces(string(expectedOutput)), trimTrailingSpaces(string(output))) + require.Equal(t, string(expectedOutput), string(output)) }) } } @@ -111,13 +110,7 @@ func TestFix_pathPrefix(t *testing.T) { expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input))) require.NoError(t, err) - require.Equal(t, trimTrailingSpaces(string(expectedOutput)), trimTrailingSpaces(string(output))) + require.Equal(t, string(expectedOutput), string(output)) }) } } - -var reTrailingSpaces = regexp.MustCompile(`(?m)(\s+)$`) - -func trimTrailingSpaces(s string) string { - return reTrailingSpaces.ReplaceAllString(s, "") -} diff --git a/test/testdata/fix/out/go-header_1.go b/test/testdata/fix/out/go-header_1.go index 4484c537472d..e8f7529b627c 100644 --- a/test/testdata/fix/out/go-header_1.go +++ b/test/testdata/fix/out/go-header_1.go @@ -1,5 +1,5 @@ // Copyright 2024 The Awesome Project Authors -// +// // Use of this source code is governed by LICENSE //golangcitest:args -Egoheader From 29a4492ae163c4e02cf746143411529e2bf6bfa2 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 25 Feb 2024 17:34:47 +0100 Subject: [PATCH 5/6] chore: update to v0.5.0 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 1e7000f67935..c6c699982c98 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,7 @@ require ( github.com/ckaznocha/intrange v0.1.0 github.com/curioswitch/go-reassign v0.2.0 github.com/daixiang0/gci v0.12.3 - github.com/denis-tingaikin/go-header v0.4.4-0.20240216000427-8715c00948e1 + github.com/denis-tingaikin/go-header v0.5.0 github.com/esimonov/ifshort v1.0.4 github.com/fatih/color v1.16.0 github.com/firefart/nonamedreturns v1.0.4 diff --git a/go.sum b/go.sum index 00d0eed4ee28..7f787b4e088c 100644 --- a/go.sum +++ b/go.sum @@ -127,8 +127,8 @@ github.com/daixiang0/gci v0.12.3/go.mod h1:xtHP9N7AHdNvtRNfcx9gwTDfw7FRJx4bZUsiE github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/denis-tingaikin/go-header v0.4.4-0.20240216000427-8715c00948e1 h1:iRrzLnD65fQulU4R+4DTnq4fanBj7XnFMXekgBYsAfU= -github.com/denis-tingaikin/go-header v0.4.4-0.20240216000427-8715c00948e1/go.mod h1:mMenU5bWrok6Wl2UsZjy+1okegmwQ3UgWl4V1D8gjlY= +github.com/denis-tingaikin/go-header v0.5.0 h1:SRdnP5ZKvcO9KKRP1KJrhFR3RrlGuD+42t4429eC9k8= +github.com/denis-tingaikin/go-header v0.5.0/go.mod h1:mMenU5bWrok6Wl2UsZjy+1okegmwQ3UgWl4V1D8gjlY= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= From 756e8554c4b8c3c90cbabfefdfbd6ed468944073 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 25 Feb 2024 17:40:15 +0100 Subject: [PATCH 6/6] review: add autofix attribute --- pkg/lint/lintersdb/manager.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 707a0bd15664..3a972c7a43aa 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -486,6 +486,7 @@ func (m *Manager) GetAllSupportedLinterConfigs() []*linter.Config { linter.NewConfig(golinters.NewGoHeader(goheaderCfg)). WithSince("v1.28.0"). WithPresets(linter.PresetStyle). + WithAutoFix(). WithURL("https://github.com/denis-tingaikin/go-header"), linter.NewConfig(golinters.NewGoimports(goimportsCfg)).