Skip to content

Commit

Permalink
goheader: fix relative template path
Browse files Browse the repository at this point in the history
go-header template-path setting is usually a relative filepath. As a
result, the linter only worked when golangci-lint was invoked from that
folder.

We detect if the template path is relative and prefix it with config
path.

go-header_bad.go test case was modified because old files are not
checked as seen here
https://github.com/denis-tingaikin/go-header/blob/v0.4.3/analyzer.go#L59-L63
  • Loading branch information
thejan2009 committed Oct 18, 2023
1 parent f714c5d commit 0799174
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 6 deletions.
7 changes: 4 additions & 3 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,10 @@ type GofumptSettings struct {
}

type GoHeaderSettings struct {
Values map[string]map[string]string `mapstructure:"values"`
Template string `mapstructure:"template"`
TemplatePath string `mapstructure:"template-path"`
Values map[string]map[string]string `mapstructure:"values"`
Template string `mapstructure:"template"`
TemplatePath string `mapstructure:"template-path"`
LintConfigDir string `mapstructure:"lint-config-dir"`
}

type GoImportsSettings struct {
Expand Down
7 changes: 6 additions & 1 deletion pkg/golinters/goheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package golinters

import (
"go/token"
"path/filepath"
"sync"

goheader "github.com/denis-tingaikin/go-header"
Expand All @@ -21,10 +22,14 @@ func NewGoHeader(settings *config.GoHeaderSettings) *goanalysis.Linter {

conf := &goheader.Configuration{}
if settings != nil {
path := settings.TemplatePath
if path != "" && !filepath.IsAbs(path) {
path = filepath.Join(settings.LintConfigDir, path)
}
conf = &goheader.Configuration{
Values: settings.Values,
Template: settings.Template,
TemplatePath: settings.TemplatePath,
TemplatePath: path,
}
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
if stylecheckCfg != nil && stylecheckCfg.GoVersion != "" {
stylecheckCfg.GoVersion = trimGoVersion(m.cfg.Run.Go)
}

if goheaderCfg != nil {
goheaderCfg.LintConfigDir = m.cfg.GetConfigDir()
}
}

const megacheckName = "megacheck"
Expand Down
1 change: 1 addition & 0 deletions test/testdata/configs/go-header-template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MY {{title}}
6 changes: 6 additions & 0 deletions test/testdata/configs/go-header-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
linters-settings:
goheader:
template-path: go-header-template
values:
const:
title: TITLE.
2 changes: 1 addition & 1 deletion test/testdata/configs/go-header.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
linters-settings:
goheader:
template: MY {{title}}
template: "MY {{title}}"
values:
const:
title: TITLE.
6 changes: 6 additions & 0 deletions test/testdata/go-header-template_good.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*MY TITLE.*/

//golangcitest:args -Egoheader
//golangcitest:config_path testdata/configs/go-header-template.yml
//golangcitest:expected_exitcode 0
package testdata
2 changes: 1 addition & 1 deletion test/testdata/go-header_bad.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*MY TITLE!*/ // want `Expected:TITLE\., Actual: TITLE!`
/*MY TITLE?*/ // want `Expected:TITLE\., Actual: TITLE?`

//golangcitest:args -Egoheader
//golangcitest:config_path testdata/configs/go-header.yml
Expand Down

0 comments on commit 0799174

Please # to comment.