Skip to content

Commit

Permalink
add testdata for promlinter
Browse files Browse the repository at this point in the history
Signed-off-by: yeya24 <yb532204897@gmail.com>
  • Loading branch information
yeya24 committed Mar 29, 2021
1 parent 0917a92 commit 806664f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 5 deletions.
14 changes: 14 additions & 0 deletions .golangci.example.yml
Expand Up @@ -339,6 +339,20 @@ linters-settings:
simple: true
range-loops: true # Report preallocation suggestions on range loops, true by default
for-loops: false # Report preallocation suggestions on for loops, false by default
promlinter:
# Promlinter cannot infer all metrics name in static analysis. Enable strict mode
# will also include the errors caused by failing to parse the args.
strict: false
disabled-linters:
# Please refer to https://github.com/yeya24/promlinter#usage for detailed usage.
# - "Help"
# - "MetricUnits"
# - "Counter"
# - "HistogramSummaryReserved"
# - "MetricTypeInName"
# - "ReservedChars"
# - "CamelCase"
# - "lintUnitAbbreviations"
predeclared:
# comma-separated list of predeclared identifiers to not report on
ignore: ""
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -73,7 +73,7 @@ require (
github.com/ultraware/whitespace v0.0.4
github.com/uudashr/gocognit v1.0.1
github.com/valyala/quicktemplate v1.6.3
github.com/yeya24/promlinter v0.0.0-20201120174540-eec9e2ee3b40
github.com/yeya24/promlinter v0.0.0-20210328235706-000c7d74ddb3
golang.org/x/text v0.3.4 // indirect
golang.org/x/tools v0.1.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/config/config.go
Expand Up @@ -452,9 +452,11 @@ type ForbidigoSettings struct {
type PredeclaredSettings struct {
Ignore string `mapstructure:"ignore"`
Qualified bool `mapstructure:"q"`
}

type PromlinterSettings struct {
Strict bool `mapstructure:"strict"`
Strict bool `mapstructure:"strict"`
DisabledLinters []string `mapstructure:"disabled-linters"`
}

var defaultLintersSettings = LintersSettings{
Expand Down
8 changes: 6 additions & 2 deletions pkg/golinters/promlinter.go
Expand Up @@ -30,6 +30,7 @@ func NewPromlinter() *goanalysis.Linter {
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
strict := lintCtx.Cfg.LintersSettings.Promlinter.Strict
disabledLinters := lintCtx.Cfg.LintersSettings.Promlinter.DisabledLinters

analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
files := make([]*ast.File, 0)
Expand All @@ -41,7 +42,10 @@ func NewPromlinter() *goanalysis.Linter {

files = append(files, f)
}
issues := promlinter.Run(pass.Fset, files, strict)
issues := promlinter.RunLint(pass.Fset, files, promlinter.Setting{
Strict: strict,
DisabledLintFuncs: disabledLinters,
})

if len(issues) == 0 {
return nil, nil
Expand All @@ -66,5 +70,5 @@ func NewPromlinter() *goanalysis.Linter {
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}).WithLoadMode(goanalysis.LoadModeNone)
}
1 change: 0 additions & 1 deletion pkg/lint/lintersdb/manager.go
Expand Up @@ -371,7 +371,6 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithURL("https://github.com/charithe/durationcheck"),
linter.NewConfig(golinters.NewPromlinter()).
WithPresets(linter.PresetStyle).
WithAutoFix().
WithURL("https://github.com/yeya24/promlinter"),
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
Expand Down
34 changes: 34 additions & 0 deletions test/testdata/promlinter.go
@@ -0,0 +1,34 @@
//args: -Epromlinter
package testdata

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
_ = promauto.NewCounterVec(
prometheus.CounterOpts{ // Metric: test_metric_name Error: counter metrics should have "_total" suffix
Name: "test_metric_name",
Help: "test help text",
}, []string{},
)

_ = promauto.NewCounterVec(
prometheus.CounterOpts{ // Metric: test_metric_total Error: no help text
Name: "test_metric_total",
}, []string{},
)

_ = promauto.NewCounterVec(
prometheus.CounterOpts{ // Metric: metric_type_in_name_counter_total Error: metric name should not include type 'counter'
Name: "metric_type_in_name_counter_total",
Help: "foo",
}, []string{},
)

_ = prometheus.NewHistogram(prometheus.HistogramOpts{ // Metric: test_duration_milliseconds Error: use base unit "seconds" instead of "milliseconds"
Name: "test_duration_milliseconds",
Help: "",
})
)

0 comments on commit 806664f

Please sign in to comment.