Skip to content

Commit

Permalink
Add test method for DetectLint
Browse files Browse the repository at this point in the history
Signed-off-by: Talon Bowler <talon.bowler@docker.com>
  • Loading branch information
daghack committed May 28, 2024
1 parent 78e0a40 commit 34a421b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
7 changes: 3 additions & 4 deletions frontend/dockerfile/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type LinterRuleI interface {
Run(warn LintWarnFunc, location []parser.Range, txt ...string) error
Run(warn LintWarnFunc, location []parser.Range, txt ...string)
SetSkip(bool)
}

Expand All @@ -24,16 +24,15 @@ func (rule *LinterRule[F]) SetSkip(skip bool) {
rule.Skip = skip
}

func (rule *LinterRule[F]) Run(warn LintWarnFunc, location []parser.Range, txt ...string) error {
func (rule *LinterRule[F]) Run(warn LintWarnFunc, location []parser.Range, txt ...string) {
if rule.Skip {
return nil
return
}
if len(txt) == 0 {
txt = []string{rule.Description}
}
short := strings.Join(txt, " ")
warn(rule.Name, rule.Description, rule.URL, short, location)
return nil
}

func LintFormatShort(rulename, msg string, startLine int) string {
Expand Down
65 changes: 65 additions & 0 deletions frontend/dockerfile/parser/directives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,68 @@ RUN ls
_, _, _, ok = DetectSyntax([]byte(dt))
require.False(t, ok)
}

func TestDetectLint(t *testing.T) {
t.Parallel()

dt := `#lint = skip=all // opts
FROM busybox
`
ref, cmdline, loc, ok := DetectLint([]byte(dt))
require.True(t, ok)
require.Equal(t, ref, "skip=all")
require.Equal(t, cmdline, "skip=all // opts")
require.Equal(t, 1, loc[0].Start.Line)
require.Equal(t, 1, loc[0].End.Line)

dt = `#!/bin/sh
# lint = skip=all
FROM busybox
`
ref, _, loc, ok = DetectLint([]byte(dt))
require.True(t, ok)
require.Equal(t, ref, "skip=all")
require.Equal(t, 2, loc[0].Start.Line)
require.Equal(t, 2, loc[0].End.Line)

dt = `#!/bin/sh
# lint = skip=all
`
_, _, _, ok = DetectLint([]byte(dt))
require.False(t, ok)

dt = `FROM busybox
RUN ls
`
ref, cmdline, _, ok = DetectLint([]byte(dt))
require.False(t, ok)
require.Equal(t, ref, "")
require.Equal(t, cmdline, "")

dt = `//lint=skip=all
//key=value`
ref, _, _, ok = DetectLint([]byte(dt))
require.True(t, ok)
require.Equal(t, ref, "skip=all")

dt = `#!/bin/sh
//lint=skip=all`
ref, _, _, ok = DetectLint([]byte(dt))
require.True(t, ok)
require.Equal(t, ref, "skip=all")

dt = `{"lint": "skip=all"}`
ref, _, _, ok = DetectLint([]byte(dt))
require.True(t, ok)
require.Equal(t, ref, "skip=all")

dt = `{"lint": "foo"`
_, _, _, ok = DetectLint([]byte(dt))
require.False(t, ok)

dt = `{"lint": "foo"}
# syntax=bar`
_, _, _, ok = DetectLint([]byte(dt))
require.False(t, ok)
}

0 comments on commit 34a421b

Please sign in to comment.