Skip to content

Commit

Permalink
reorganize and clean up rule check tests
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 30, 2024
1 parent c953238 commit be30f61
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 48 deletions.
8 changes: 4 additions & 4 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
}

newword, unmatched, err := opt.shlex.ProcessWord(word, env)
reportUnmatchedVariables(cmd, d.buildArgs, env, unmatched, lint)
reportUnmatchedVariables(cmd, d.buildArgs, env, unmatched, &opt)
return newword, err
})
if err != nil {
Expand All @@ -851,7 +851,7 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
lex := shell.NewLex('\\')
lex.SkipProcessQuotes = true
newword, unmatched, err := lex.ProcessWord(word, env)
reportUnmatchedVariables(cmd, d.buildArgs, env, unmatched, lint)
reportUnmatchedVariables(cmd, d.buildArgs, env, unmatched, &opt)
return newword, err
})
if err != nil {
Expand Down Expand Up @@ -2162,7 +2162,7 @@ func validateStageNames(stages []instructions.Stage, lint *linter.Linter) {
}
}

func reportUnmatchedVariables(cmd instructions.Command, buildArgs []instructions.KeyValuePairOptional, unmatched map[string]struct{}, lint *linter.Linter) {
func reportUnmatchedVariables(cmd instructions.Command, buildArgs []instructions.KeyValuePairOptional, env []string, unmatched map[string]struct{}, opt *dispatchOpt) {
if len(unmatched) == 0 {
return
}
Expand All @@ -2183,7 +2183,7 @@ func reportUnmatchedVariables(cmd instructions.Command, buildArgs []instructions
}
match, _ := suggest.Search(cmdVar, options, true)
msg := linter.RuleUndefinedVar.Format(cmdVar, match)
lint.Run(&linter.RuleUndefinedVar, cmd.Location(), msg)
opt.lint.Run(&linter.RuleUndefinedVar, cmd.Location(), msg)
}
}

Expand Down
83 changes: 39 additions & 44 deletions frontend/dockerfile/dockerfile_lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,31 @@ var lintTests = integration.TestFuncs(
)

func testRuleCheckOption(t *testing.T, sb integration.Sandbox) {
dockerfile := []byte(`
#check=skip=all
dockerfile := []byte(`#check=skip=all
FROM scratch as base
copy Dockerfile .
`)
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})

// dockerfile = []byte(`
//#check=skip=all;error=true
//FROM scratch as base
//copy Dockerfile .
//`)
// checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
dockerfile = []byte(`#check=skip=all;error=true
FROM scratch as base
copy Dockerfile .
`)
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})

dockerfile = []byte(`
#check=skip=FileConsistentCommandCasing
dockerfile = []byte(`#check=skip=FileConsistentCommandCasing,FromAsCasing
FROM scratch as base
copy Dockerfile .
`)
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})

dockerfile = []byte(`#check=skip=FileConsistentCommandCasing,FromAsCasing;error=true
FROM scratch as base
copy Dockerfile .
`)
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})

dockerfile = []byte(`#check=skip=FileConsistentCommandCasing
FROM scratch as base
copy Dockerfile .
`)
Expand All @@ -67,45 +76,31 @@ copy Dockerfile .
RuleName: "FromAsCasing",
Description: "The 'as' keyword should match the case of the 'from' keyword",
Detail: "'as' and 'FROM' keywords' casing do not match",
Line: 3,
Line: 2,
Level: 1,
},
},
})

// dockerfile = []byte(`
//#check=skip=FileConsistentCommandCasing,FromAsCasing
//FROM scratch as base
//copy Dockerfile .
//`)
// checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})

// dockerfile = []byte(`
//#check=skip=FileConsistentCommandCasing;error=true
//FROM scratch as base
//copy Dockerfile .
//`)
// checkLinterWarnings(t, sb, &lintTestParams{
// Dockerfile: dockerfile,
// Warnings: []expectedLintWarning{
// {
// RuleName: "FromAsCasing",
// Description: "The 'as' keyword should match the case of the 'from' keyword",
// Detail: "'as' and 'FROM' keywords' casing do not match",
// Line: 3,
// Level: 1,
// },
// },
// StreamBuildErr: "failed to solve: 'as' and 'FROM' keywords' casing do not match",
// UnmarshalBuildErr: "'as' and 'FROM' keywords' casing do not match",
// BuildErrLocation: 4,
// })
// dockerfile = []byte(`
//#check=skip=FileConsistentCommandCasing;error=true
//FROM scratch AS base
//copy Dockerfile .
//`)
// checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
dockerfile = []byte(`#check=skip=FileConsistentCommandCasing;error=true
FROM scratch as base
copy Dockerfile .
`)
checkLinterWarnings(t, sb, &lintTestParams{
Dockerfile: dockerfile,
Warnings: []expectedLintWarning{
{
RuleName: "FromAsCasing",
Description: "The 'as' keyword should match the case of the 'from' keyword",
Detail: "'as' and 'FROM' keywords' casing do not match",
Line: 2,
Level: 1,
},
},
StreamBuildErr: "failed to solve: lint violation found for rules: FromAsCasing",
UnmarshalBuildErr: "lint violation found for rules: FromAsCasing",
BuildErrLocation: 2,
})
}

func testStageName(t *testing.T, sb integration.Sandbox) {
Expand Down

0 comments on commit be30f61

Please sign in to comment.