Skip to content

Commit

Permalink
update from args lint tests and var names
Browse files Browse the repository at this point in the history
Signed-off-by: Talon Bowler <[email protected]>
  • Loading branch information
daghack committed Apr 24, 2024
1 parent 606efdd commit eea0cd1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 34 deletions.
49 changes: 23 additions & 26 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
if v, ok := opt.BuildArgs[metaArg.Key]; !ok {
if metaArg.Value != nil {
result, _ := shlex.ProcessWordWithMatches(*metaArg.Value, metaArgsToMap(optMetaArgs))
*metaArg.Value = result.Word
*metaArg.Value = result.Result
info.deps = result.Matched
}
} else {
Expand All @@ -250,22 +250,17 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS

// set base state for every image
for i, st := range stages {
result, err := shlex.ProcessWordWithMatches(st.BaseName, metaArgsToMap(optMetaArgs))
name := result.Word
used := result.Matched
if len(result.Unmatched) > 0 {
for unmatched := range result.Unmatched {
msg := linter.RuleUndeclaredArgInFrom.Format(unmatched)
linter.RuleUndeclaredArgInFrom.Run(opt.Warn, st.Location, msg)
}
}
nameMatch, err := shlex.ProcessWordWithMatches(st.BaseName, metaArgsToMap(optMetaArgs))
reportUnusedFromArgs(nameMatch.Unmatched, st.Location, opt.Warn)
used := nameMatch.Matched

if err != nil {
return nil, parser.WithLocation(err, st.Location)
}
if name == "" {
if nameMatch.Result == "" {
return nil, parser.WithLocation(errors.Errorf("base name (%s) should not be blank", st.BaseName), st.Location)
}
st.BaseName = name
st.BaseName = nameMatch.Result

ds := &dispatchState{
stage: st,
Expand All @@ -279,27 +274,22 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
}

if v := st.Platform; v != "" {
result, err := shlex.ProcessWordWithMatches(v, metaArgsToMap(optMetaArgs))
v := result.Word
u := result.Matched

if len(result.Unmatched) > 0 {
for unmatched := range result.Unmatched {
msg := linter.RuleUndeclaredArgInFrom.Format(unmatched)
linter.RuleUndeclaredArgInFrom.Run(opt.Warn, st.Location, msg)
}
}
platMatch, err := shlex.ProcessWordWithMatches(v, metaArgsToMap(optMetaArgs))
reportUnusedFromArgs(platMatch.Unmatched, st.Location, opt.Warn)

if err != nil {
return nil, parser.WithLocation(errors.Wrapf(err, "failed to process arguments for platform %s", v), st.Location)
return nil, parser.WithLocation(errors.Wrapf(err, "failed to process arguments for platform %s", platMatch.Result), st.Location)
}

p, err := platforms.Parse(v)
p, err := platforms.Parse(platMatch.Result)
if err != nil {
return nil, parser.WithLocation(errors.Wrapf(err, "failed to parse platform %s", v), st.Location)
return nil, parser.WithLocation(errors.Wrapf(err, "failed to parse platform %s", platMatch.Result), st.Location)
}
for k := range u {

for k := range platMatch.Matched {
used[k] = struct{}{}
}

ds.platform = &p
}

Expand Down Expand Up @@ -2011,3 +2001,10 @@ func validateCommandCasing(dockerfile *parser.Result, warn linter.LintWarnFunc)
}
}
}

func reportUnusedFromArgs(unmatched map[string]struct{}, location []parser.Range, warn linter.LintWarnFunc) {
for arg := range unmatched {
msg := linter.RuleUndeclaredArgInFrom.Format(arg)
linter.RuleUndeclaredArgInFrom.Run(warn, location, msg)
}
}
19 changes: 17 additions & 2 deletions frontend/dockerfile/dockerfile_lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,27 @@ COPY Dockerfile .
checkLinterWarnings(t, sb, dockerfile, []expectedLintWarning{})

dockerfile = []byte(`
ARG platform=linux/amd64
FROM --platform=$platform scratch
ARG BUILDPLATFORM=linux/amd64
FROM --platform=$BUILDPLATFORM scratch
COPY Dockerfile .
`)
checkLinterWarnings(t, sb, dockerfile, []expectedLintWarning{})

dockerfile = []byte(`
ARG BUILDPLATFORM=linux/amd64
FROM --platform=$BULIDPLATFORM scratch
COPY Dockerfile .
`)
checkLinterWarnings(t, sb, dockerfile, []expectedLintWarning{
{
RuleName: "UndeclaredArgInFrom",
Description: "FROM command must use declared ARGs",
Detail: "FROM argument 'BULIDPLATFORM' is not declared",
Level: 1,
Line: 3,
},
})

dockerfile = []byte(`
ARG tag=latest
FROM busybox:${tag}${version} AS b
Expand Down
4 changes: 2 additions & 2 deletions frontend/dockerfile/shell/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *Lex) ProcessWordWithMap(word string, env map[string]string) (string, er
}

type ProcessWordMatchesResult struct {
Word string
Result string
Matched map[string]struct{}
Unmatched map[string]struct{}
}
Expand All @@ -69,7 +69,7 @@ func (s *Lex) ProcessWordWithMatches(word string, env map[string]string) (Proces
sw := s.init(word, env)
word, _, err := sw.process(word)
return ProcessWordMatchesResult{
Word: word,
Result: word,
Matched: sw.matches,
Unmatched: sw.nonmatches,
}, err
Expand Down
9 changes: 5 additions & 4 deletions frontend/dockerfile/shell/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func TestProcessWithMatches(t *testing.T) {
expected string
expectedErr bool
matches map[string]struct{}
unmatched map[string]struct{}
}{
{
input: "x",
Expand Down Expand Up @@ -481,7 +482,7 @@ func TestProcessWithMatches(t *testing.T) {
c := c
t.Run(c.input, func(t *testing.T) {
result, err := shlex.ProcessWordWithMatches(c.input, c.envs)
w := result.Word
w := result.Result
matches := result.Matched
if c.expectedErr {
require.Error(t, err)
Expand Down Expand Up @@ -514,7 +515,7 @@ func TestProcessWithMatchesPlatform(t *testing.T) {
"TARGETVARIANT": "v7",
})
require.NoError(t, err)
require.Equal(t, "something-v1.2.3.linux-arm-v7.tar.gz", results.Word)
require.Equal(t, "something-v1.2.3.linux-arm-v7.tar.gz", results.Result)

results, err = shlex.ProcessWordWithMatches(release, map[string]string{
"VERSION": version,
Expand All @@ -523,7 +524,7 @@ func TestProcessWithMatchesPlatform(t *testing.T) {
"TARGETVARIANT": "",
})
require.NoError(t, err)
require.Equal(t, "something-v1.2.3.linux-arm64.tar.gz", results.Word)
require.Equal(t, "something-v1.2.3.linux-arm64.tar.gz", results.Result)

results, err = shlex.ProcessWordWithMatches(release, map[string]string{
"VERSION": version,
Expand All @@ -532,5 +533,5 @@ func TestProcessWithMatchesPlatform(t *testing.T) {
// No "TARGETVARIANT": "",
})
require.NoError(t, err)
require.Equal(t, "something-v1.2.3.linux-arm64.tar.gz", results.Word)
require.Equal(t, "something-v1.2.3.linux-arm64.tar.gz", results.Result)
}

0 comments on commit eea0cd1

Please sign in to comment.