Skip to content

Commit

Permalink
Separate out package and build lints
Browse files Browse the repository at this point in the history
There are currently no separate lints, but there will soon be. Prepare
for this eventuality now.

Signed-off-by: Elizabeth Myers <[email protected]>
  • Loading branch information
Elizafox committed Oct 26, 2023
1 parent 818204c commit 2ccb5ff
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Lint() *cobra.Command {
linterSet := map[string]struct{}{}

// Get all default linters, ignoring disabled ones
for _, e := range linter_defaults.DefaultLinters {
for _, e := range linter_defaults.GetDefaultLinters(linter_defaults.LintersApk) {
if !slices.Contains(disabled, e) {
linterSet[e] = struct{}{}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (p *Package) FullCopyright() string {
// Computes the list of package or subpackage linters, taking into account default linters.
// This includes the default linters as well, unless disabled.
func (chk *Checks) GetLinters() []string {
linters := linter_defaults.DefaultLinters
linters := linter_defaults.GetDefaultLinters(linter_defaults.LintersBuild)

// Enable non-default linters
for _, v := range chk.Enabled {
Expand Down
38 changes: 37 additions & 1 deletion pkg/linter/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,20 @@

package defaults

var DefaultLinters = []string{
import (
"slices"
)

type LinterSet int

const (
LintersDefault LinterSet = iota
LintersBuild LinterSet = iota
LintersApk LinterSet = iota
)

// Default linters run regardless of whether we are dealing with an APK or build
var defaultLinters = []string{
"dev",
"empty",
"opt",
Expand All @@ -32,3 +45,26 @@ var DefaultLinters = []string{
"varempty",
"worldwrite",
}

// Linters run by default on builds but not on APKs
var defaultBuildLinters = []string{}

// Linters run by default on APKs but not during build
var defaultApkLinters = []string{}

// Get the set of default linters for a given task
func GetDefaultLinters(linterSet LinterSet) (linters []string) {
linters = slices.Clone(defaultLinters)
switch linterSet {
case LintersDefault:
// Do nothing
case LintersBuild:
linters = append(linters, slices.Clone(defaultBuildLinters)...)
case LintersApk:
linters = append(linters, slices.Clone(defaultApkLinters)...)
default:
panic("Invalid linter set called")
}

return
}
6 changes: 3 additions & 3 deletions pkg/linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

func checksOnly(onlyLint string) config.Checks {
checksDisabled := []string{}
for _, lint := range linter_defaults.DefaultLinters {
for _, lint := range linter_defaults.GetDefaultLinters(linter_defaults.LintersBuild) {
if lint != onlyLint {
checksDisabled = append(checksDisabled, lint)
}
Expand Down Expand Up @@ -607,11 +607,11 @@ func Test_lintApk(t *testing.T) {
called := false
assert.NoError(t, LintApk(ctx, filepath.Join("testdata", "hello-wolfi-2.12.1-r1.apk"), func(err error) {
called = true
}, linter_defaults.DefaultLinters))
}, linter_defaults.GetDefaultLinters(linter_defaults.LintersApk)))
assert.False(t, called)

assert.NoError(t, LintApk(ctx, filepath.Join("testdata", "kubeflow-pipelines-2.1.3-r7.apk"), func(err error) {
called = true
}, linter_defaults.DefaultLinters))
}, linter_defaults.GetDefaultLinters(linter_defaults.LintersApk)))
assert.True(t, called)
}

0 comments on commit 2ccb5ff

Please sign in to comment.