Skip to content

Commit

Permalink
Make improvements/suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Myers <elizabeth.myers@chainguard.dev>
  • Loading branch information
Elizafox committed Sep 28, 2023
1 parent 9dccbef commit 01477a3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,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, &b.Configuration, &lt.checks)
lctx := linter.NewLinterContext(lt.pkgName, fsys, &b.Configuration, &lt.checks)
linters := lt.checks.GetLinters()

err = lctx.LintPackageFs(fsys, linters)
Expand Down
44 changes: 35 additions & 9 deletions pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ package linter
import (
"debug/elf"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"

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

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

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

type linterFunc func(lctx LinterContext, path string, d fs.DirEntry) error
Expand Down Expand Up @@ -179,10 +181,34 @@ func strippedLinter(lctx LinterContext, path string, d fs.DirEntry) error {
return nil
}

file, err := elf.Open(filepath.Join(lctx.realfspath, path))
reader, err := lctx.fsys.Open(path)
if err != nil {
return fmt.Errorf("Could not open file for reading: %v", err)
}
defer reader.Close()

// XXX(Elizafox) - fs.Open doesn't support the ReaderAt interface so we copy it to a temp file.
// This sucks but what can you do?
tempfile, err := os.CreateTemp("", "melange.XXXXX")
if err != nil {
return fmt.Errorf("Could not create temporary file: %v", err)
}
defer os.Remove(tempfile.Name())

_, err = io.Copy(tempfile, reader)
if err != nil {
return fmt.Errorf("Could not write to temporary file: %v", err)
}

_, err = tempfile.Seek(0, 0)
if err != nil {
return fmt.Errorf("Could not rewind temporary file: %v", err)
}

file, err := elf.NewFile(tempfile)
if err != nil {
// We don't particularly care if this fails, it means it's probably not an ELF file
fmt.Printf("WARNING: Could not open file '%s' as executable: %v\n", path, err)
fmt.Printf("WARNING: Could not open file %q as executable: %v\n", path, err)
return nil
}
defer file.Close()
Expand Down Expand Up @@ -251,7 +277,7 @@ func (lctx LinterContext) LintPackageFs(fsys fs.FS, linters []string) error {

err = linter.LinterFunc(lctx, path, d)
if err != nil {
return fmt.Errorf("Linter %s failed at path \"%s\": %w; suggest: %s", linterName, path, err, linter.Explain)
return fmt.Errorf("Linter %s failed at path %q: %w; suggest: %s", linterName, path, err, linter.Explain)
}
}

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, dir, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
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, dir, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
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, dir, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
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, dir, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
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, dir, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
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, dir, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
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, dir, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
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, dir, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
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, dir, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
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, dir, &cfg, &cfg.Package.Checks)
lctx := NewLinterContext(cfg.Package.Name, fsys, &cfg, &cfg.Package.Checks)
assert.NoError(t, lctx.LintPackageFs(fsys, linters))
}

0 comments on commit 01477a3

Please sign in to comment.