Skip to content

Commit 09f1a48

Browse files
authored
Merge pull request moby#5046 from daghack/consolidate-case-checks
Consolidate instruction casing lint rules
2 parents 2ee263a + 8cbb98e commit 09f1a48

File tree

7 files changed

+40
-176
lines changed

7 files changed

+40
-176
lines changed

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,22 +2162,15 @@ func validateCommandCasing(dockerfile *parser.Result, lint *linter.Linter) {
21622162
// Here, we check both if the command is consistent per command (ie, "CMD" or "cmd", not "Cmd")
21632163
// as well as ensuring that the casing is consistent throughout the dockerfile by comparing the
21642164
// command to the casing of the majority of commands.
2165-
if !isSelfConsistentCasing(node.Value) {
2166-
msg := linter.RuleConsistentInstructionCasing.Format(node.Value)
2165+
var correctCasing string
2166+
if isMajorityLower && strings.ToLower(node.Value) != node.Value {
2167+
correctCasing = "lowercase"
2168+
} else if !isMajorityLower && strings.ToUpper(node.Value) != node.Value {
2169+
correctCasing = "uppercase"
2170+
}
2171+
if correctCasing != "" {
2172+
msg := linter.RuleConsistentInstructionCasing.Format(node.Value, correctCasing)
21672173
lint.Run(&linter.RuleConsistentInstructionCasing, node.Location(), msg)
2168-
} else {
2169-
var msg string
2170-
var needsLintWarn bool
2171-
if isMajorityLower && strings.ToUpper(node.Value) == node.Value {
2172-
msg = linter.RuleFileConsistentCommandCasing.Format(node.Value, "lowercase")
2173-
needsLintWarn = true
2174-
} else if !isMajorityLower && strings.ToLower(node.Value) == node.Value {
2175-
msg = linter.RuleFileConsistentCommandCasing.Format(node.Value, "uppercase")
2176-
needsLintWarn = true
2177-
}
2178-
if needsLintWarn {
2179-
lint.Run(&linter.RuleFileConsistentCommandCasing, node.Location(), msg)
2180-
}
21812174
}
21822175
}
21832176
}

frontend/dockerfile/dockerfile_lint_test.go

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ var lintTests = integration.TestFuncs(
2828
testStageName,
2929
testNoEmptyContinuation,
3030
testConsistentInstructionCasing,
31-
testFileConsistentCommandCasing,
3231
testDuplicateStageName,
3332
testReservedStageName,
3433
testJSONArgsRecommended,
@@ -97,19 +96,19 @@ copy Dockerfile .
9796
`)
9897
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
9998

100-
dockerfile = []byte(`#check=skip=FileConsistentCommandCasing,FromAsCasing
99+
dockerfile = []byte(`#check=skip=ConsistentInstructionCasing,FromAsCasing
101100
FROM scratch as base
102101
copy Dockerfile .
103102
`)
104103
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
105104

106-
dockerfile = []byte(`#check=skip=FileConsistentCommandCasing,FromAsCasing;error=true
105+
dockerfile = []byte(`#check=skip=ConsistentInstructionCasing,FromAsCasing;error=true
107106
FROM scratch as base
108107
copy Dockerfile .
109108
`)
110109
checkLinterWarnings(t, sb, &lintTestParams{Dockerfile: dockerfile})
111110

112-
dockerfile = []byte(`#check=skip=FileConsistentCommandCasing
111+
dockerfile = []byte(`#check=skip=ConsistentInstructionCasing
113112
FROM scratch as base
114113
copy Dockerfile .
115114
`)
@@ -127,7 +126,7 @@ copy Dockerfile .
127126
},
128127
})
129128

130-
dockerfile = []byte(`#check=skip=FileConsistentCommandCasing;error=true
129+
dockerfile = []byte(`#check=skip=ConsistentInstructionCasing;error=true
131130
FROM scratch as base
132131
copy Dockerfile .
133132
`)
@@ -168,7 +167,7 @@ copy Dockerfile .
168167
UnmarshalBuildErr: "lint violation found for rules: FromAsCasing",
169168
BuildErrLocation: 2,
170169
FrontendAttrs: map[string]string{
171-
"build-arg:BUILDKIT_DOCKERFILE_CHECK": "skip=FileConsistentCommandCasing;error=true",
170+
"build-arg:BUILDKIT_DOCKERFILE_CHECK": "skip=ConsistentInstructionCasing;error=true",
172171
},
173172
})
174173

@@ -275,52 +274,50 @@ FROM scratch AS base2
275274
Warnings: []expectedLintWarning{
276275
{
277276
RuleName: "ConsistentInstructionCasing",
278-
Description: "Instructions should be in consistent casing (all lower or all upper)",
277+
Description: "All commands within the Dockerfile should use the same casing (either upper or lower)",
279278
URL: "https://docs.docker.com/go/dockerfile/rule/consistent-instruction-casing/",
280-
Detail: "Command 'From' should be consistently cased",
279+
Detail: "Command 'From' should match the case of the command majority (uppercase)",
281280
Level: 1,
282281
Line: 3,
283282
},
284283
},
285284
})
286285

287286
dockerfile = []byte(`
288-
# warning: 'FROM' should be either lowercased or uppercased
289-
frOM scratch as base
290-
from scratch as base2
287+
FROM scratch
288+
# warning: 'copy' should match command majority's casing (uppercase)
289+
copy Dockerfile /foo
290+
COPY Dockerfile /bar
291291
`)
292+
292293
checkLinterWarnings(t, sb, &lintTestParams{
293294
Dockerfile: dockerfile,
294295
Warnings: []expectedLintWarning{
295296
{
296297
RuleName: "ConsistentInstructionCasing",
297-
Description: "Instructions should be in consistent casing (all lower or all upper)",
298+
Description: "All commands within the Dockerfile should use the same casing (either upper or lower)",
298299
URL: "https://docs.docker.com/go/dockerfile/rule/consistent-instruction-casing/",
299-
Detail: "Command 'frOM' should be consistently cased",
300-
Line: 3,
300+
Detail: "Command 'copy' should match the case of the command majority (uppercase)",
301+
Line: 4,
301302
Level: 1,
302303
},
303304
},
304305
})
305-
}
306306

307-
func testFileConsistentCommandCasing(t *testing.T, sb integration.Sandbox) {
308-
dockerfile := []byte(`
309-
FROM scratch
310-
# warning: 'copy' should match command majority's casing (uppercase)
311-
copy Dockerfile /foo
312-
COPY Dockerfile /bar
307+
dockerfile = []byte(`
308+
# warning: 'frOM' should be either lowercased or uppercased
309+
frOM scratch as base
310+
from scratch as base2
313311
`)
314-
315312
checkLinterWarnings(t, sb, &lintTestParams{
316313
Dockerfile: dockerfile,
317314
Warnings: []expectedLintWarning{
318315
{
319-
RuleName: "FileConsistentCommandCasing",
316+
RuleName: "ConsistentInstructionCasing",
320317
Description: "All commands within the Dockerfile should use the same casing (either upper or lower)",
321-
URL: "https://docs.docker.com/go/dockerfile/rule/file-consistent-command-casing/",
322-
Detail: "Command 'copy' should match the case of the command majority (uppercase)",
323-
Line: 4,
318+
URL: "https://docs.docker.com/go/dockerfile/rule/consistent-instruction-casing/",
319+
Detail: "Command 'frOM' should match the case of the command majority (lowercase)",
320+
Line: 3,
324321
Level: 1,
325322
},
326323
},
@@ -336,9 +333,9 @@ copy Dockerfile /bar
336333
Dockerfile: dockerfile,
337334
Warnings: []expectedLintWarning{
338335
{
339-
RuleName: "FileConsistentCommandCasing",
336+
RuleName: "ConsistentInstructionCasing",
340337
Description: "All commands within the Dockerfile should use the same casing (either upper or lower)",
341-
URL: "https://docs.docker.com/go/dockerfile/rule/file-consistent-command-casing/",
338+
URL: "https://docs.docker.com/go/dockerfile/rule/consistent-instruction-casing/",
342339
Detail: "Command 'COPY' should match the case of the command majority (lowercase)",
343340
Line: 4,
344341
Level: 1,
@@ -357,9 +354,9 @@ COPY Dockerfile /baz
357354
Dockerfile: dockerfile,
358355
Warnings: []expectedLintWarning{
359356
{
360-
RuleName: "FileConsistentCommandCasing",
357+
RuleName: "ConsistentInstructionCasing",
361358
Description: "All commands within the Dockerfile should use the same casing (either upper or lower)",
362-
URL: "https://docs.docker.com/go/dockerfile/rule/file-consistent-command-casing/",
359+
URL: "https://docs.docker.com/go/dockerfile/rule/consistent-instruction-casing/",
363360
Detail: "Command 'from' should match the case of the command majority (uppercase)",
364361
Line: 3,
365362
Level: 1,
@@ -378,9 +375,9 @@ copy Dockerfile /baz
378375
Dockerfile: dockerfile,
379376
Warnings: []expectedLintWarning{
380377
{
381-
RuleName: "FileConsistentCommandCasing",
378+
RuleName: "ConsistentInstructionCasing",
382379
Description: "All commands within the Dockerfile should use the same casing (either upper or lower)",
383-
URL: "https://docs.docker.com/go/dockerfile/rule/file-consistent-command-casing/",
380+
URL: "https://docs.docker.com/go/dockerfile/rule/consistent-instruction-casing/",
384381
Detail: "Command 'FROM' should match the case of the command majority (lowercase)",
385382
Line: 3,
386383
Level: 1,

frontend/dockerfile/docs/rules/_index.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ $ docker build --check .
4242
</tr>
4343
<tr>
4444
<td><a href="./consistent-instruction-casing/">ConsistentInstructionCasing</a></td>
45-
<td>Instructions should be in consistent casing (all lower or all upper)</td>
46-
</tr>
47-
<tr>
48-
<td><a href="./file-consistent-command-casing/">FileConsistentCommandCasing</a></td>
4945
<td>All commands within the Dockerfile should use the same casing (either upper or lower)</td>
5046
</tr>
5147
<tr>

frontend/dockerfile/docs/rules/consistent-instruction-casing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: ConsistentInstructionCasing
3-
description: Instructions should be in consistent casing (all lower or all upper)
3+
description: All commands within the Dockerfile should use the same casing (either upper or lower)
44
aliases:
55
- /go/dockerfile/rule/consistent-instruction-casing/
66
---

frontend/dockerfile/docs/rules/file-consistent-command-casing.md

Lines changed: 0 additions & 61 deletions
This file was deleted.

frontend/dockerfile/linter/docs/FileConsistentCommandCasing.md

Lines changed: 0 additions & 53 deletions
This file was deleted.

frontend/dockerfile/linter/ruleset.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,10 @@ var (
2929
return "Empty continuation line"
3030
},
3131
}
32-
RuleConsistentInstructionCasing = LinterRule[func(string) string]{
32+
RuleConsistentInstructionCasing = LinterRule[func(string, string) string]{
3333
Name: "ConsistentInstructionCasing",
34-
Description: "Instructions should be in consistent casing (all lower or all upper)",
35-
URL: "https://docs.docker.com/go/dockerfile/rule/consistent-instruction-casing/",
36-
Format: func(command string) string {
37-
return fmt.Sprintf("Command '%s' should be consistently cased", command)
38-
},
39-
}
40-
RuleFileConsistentCommandCasing = LinterRule[func(string, string) string]{
41-
Name: "FileConsistentCommandCasing",
4234
Description: "All commands within the Dockerfile should use the same casing (either upper or lower)",
43-
URL: "https://docs.docker.com/go/dockerfile/rule/file-consistent-command-casing/",
35+
URL: "https://docs.docker.com/go/dockerfile/rule/consistent-instruction-casing/",
4436
Format: func(violatingCommand, correctCasing string) string {
4537
return fmt.Sprintf("Command '%s' should match the case of the command majority (%s)", violatingCommand, correctCasing)
4638
},

0 commit comments

Comments
 (0)