Skip to content

Commit

Permalink
Extricate config stuff from linter.
Browse files Browse the repository at this point in the history
This is part of the work to make the linter more generic so it can be
run on APK packages.

Future configuration information can be marshalled into the
LinterContext another way.

Signed-off-by: Elizabeth Myers <[email protected]>
  • Loading branch information
Elizafox committed Oct 10, 2023
1 parent 4e79088 commit 497f8ca
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
4 changes: 1 addition & 3 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,6 @@ func (b *Build) BuildPackage(ctx context.Context) error {
// add the main package to the linter queue
lintTarget := linterTarget{
pkgName: b.Configuration.Package.Name,
checks: b.Configuration.Package.Checks,
}
linterQueue = append(linterQueue, lintTarget)
}
Expand Down Expand Up @@ -1112,7 +1111,6 @@ func (b *Build) BuildPackage(ctx context.Context) error {
// add the main package to the linter queue
lintTarget := linterTarget{
pkgName: sp.Name,
checks: sp.Checks,
}
linterQueue = append(linterQueue, lintTarget)
}
Expand All @@ -1130,7 +1128,7 @@ func (b *Build) BuildPackage(ctx context.Context) error {

path := filepath.Join(b.WorkspaceDir, "melange-out", lt.pkgName)
fsys := os.DirFS(path)
lctx := linter.NewLinterContext(lt.pkgName, fsys, &b.Configuration, &lt.checks)
lctx := linter.NewLinterContext(lt.pkgName, fsys)
linters := lt.checks.GetLinters()

err = lctx.LintPackageFs(fsys, linters)
Expand Down
8 changes: 2 additions & 6 deletions pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@ import (
"os"
"path/filepath"
"regexp"

"chainguard.dev/melange/pkg/config"
)

type LinterContext struct {
pkgname string
fsys fs.FS
cfg *config.Configuration
chk *config.Checks
}

func NewLinterContext(name string, fsys fs.FS, cfg *config.Configuration, chk *config.Checks) LinterContext {
return LinterContext{name, fsys, cfg, chk}
func NewLinterContext(name string, fsys fs.FS) LinterContext {
return LinterContext{name, fsys}
}

type linterFunc func(lctx LinterContext, path string, d fs.DirEntry) error
Expand Down
20 changes: 10 additions & 10 deletions pkg/linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Test_emptyLinter(t *testing.T) {
linters := cfg.Package.Checks.GetLinters()
assert.Equal(t, linters, []string{"empty"})
fsys := os.DirFS(dir)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys)
assert.Error(t, lctx.LintPackageFs(fsys, linters))
}

Expand Down Expand Up @@ -74,7 +74,7 @@ func Test_usrLocalLinter(t *testing.T) {
linters := cfg.Package.Checks.GetLinters()
assert.Equal(t, linters, []string{"usrlocal"})
fsys := os.DirFS(dir)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys)
assert.Error(t, lctx.LintPackageFs(fsys, linters))
}

Expand Down Expand Up @@ -104,7 +104,7 @@ func Test_varEmptyLinter(t *testing.T) {
linters := cfg.Package.Checks.GetLinters()
assert.Equal(t, linters, []string{"varempty"})
fsys := os.DirFS(dir)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys)
assert.Error(t, lctx.LintPackageFs(fsys, linters))
}

Expand Down Expand Up @@ -134,7 +134,7 @@ func Test_devLinter(t *testing.T) {
linters := cfg.Package.Checks.GetLinters()
assert.Equal(t, linters, []string{"dev"})
fsys := os.DirFS(dir)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys)
assert.Error(t, lctx.LintPackageFs(fsys, linters))
}

Expand Down Expand Up @@ -164,7 +164,7 @@ func Test_optLinter(t *testing.T) {
linters := cfg.Package.Checks.GetLinters()
assert.Equal(t, linters, []string{"opt"})
fsys := os.DirFS(dir)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys)
assert.Error(t, lctx.LintPackageFs(fsys, linters))
}

Expand Down Expand Up @@ -194,7 +194,7 @@ func Test_srvLinter(t *testing.T) {
linters := cfg.Package.Checks.GetLinters()
assert.Equal(t, linters, []string{"srv"})
fsys := os.DirFS(dir)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys)
assert.Error(t, lctx.LintPackageFs(fsys, linters))
}

Expand Down Expand Up @@ -236,7 +236,7 @@ func Test_tempDirLinter(t *testing.T) {
assert.NoError(t, err)
_, err = os.Create(filename)
assert.NoError(t, err)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys)
assert.Error(t, lctx.LintPackageFs(fsys, linters))
os.Remove(filename)

Expand Down Expand Up @@ -291,7 +291,7 @@ func Test_setUidGidLinter(t *testing.T) {
linters := cfg.Package.Checks.GetLinters()
assert.Equal(t, linters, []string{"setuidgid"})
fsys := os.DirFS(dir)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys)
assert.Error(t, lctx.LintPackageFs(fsys, linters))
}

Expand Down Expand Up @@ -320,7 +320,7 @@ func Test_worldWriteLinter(t *testing.T) {
linters := cfg.Package.Checks.GetLinters()
assert.Equal(t, linters, []string{"worldwrite"})
fsys := os.DirFS(dir)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys)
assert.NoError(t, lctx.LintPackageFs(fsys, linters))

// Create test file
Expand Down Expand Up @@ -377,6 +377,6 @@ func Test_disableDefaultLinter(t *testing.T) {

linters := cfg.Package.Checks.GetLinters()
fsys := os.DirFS(dir)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys)
assert.NoError(t, lctx.LintPackageFs(fsys, linters))
}

0 comments on commit 497f8ca

Please sign in to comment.