Skip to content

Commit b7486e5

Browse files
authored
Merge pull request #2647 from daghack/print-warning-count
build: print out the number of warnings after completing a rule check
2 parents 48faab5 + 806ccd3 commit b7486e5

File tree

3 files changed

+202
-5
lines changed

3 files changed

+202
-5
lines changed

commands/build.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -891,17 +891,29 @@ func printResult(w io.Writer, f *controllerapi.CallFunc, res map[string]string)
891891
case "subrequests.describe":
892892
return 0, printValue(w, subrequests.PrintDescribe, subrequests.SubrequestsDescribeDefinition.Version, f.Format, res)
893893
case "lint":
894-
err := printValue(w, lint.PrintLintViolations, lint.SubrequestLintDefinition.Version, f.Format, res)
895-
if err != nil {
896-
return 0, err
897-
}
898-
899894
lintResults := lint.LintResults{}
900895
if result, ok := res["result.json"]; ok {
901896
if err := json.Unmarshal([]byte(result), &lintResults); err != nil {
902897
return 0, err
903898
}
904899
}
900+
901+
warningCount := len(lintResults.Warnings)
902+
if f.Format != "json" && warningCount > 0 {
903+
var warningCountMsg string
904+
if warningCount == 1 {
905+
warningCountMsg = "1 warning has been found!"
906+
} else if warningCount > 1 {
907+
warningCountMsg = fmt.Sprintf("%d warnings have been found!", warningCount)
908+
}
909+
fmt.Fprintf(w, "Check complete, %s\n", warningCountMsg)
910+
}
911+
912+
err := printValue(w, lint.PrintLintViolations, lint.SubrequestLintDefinition.Version, f.Format, res)
913+
if err != nil {
914+
return 0, err
915+
}
916+
905917
if lintResults.Error != nil {
906918
// Print the error message and the source
907919
// Normally, we would use `errdefs.WithSource` to attach the source to the

tests/bake.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
5959
testBakeCallCheckFlag,
6060
testBakeCallMetadata,
6161
testBakeMultiPlatform,
62+
testBakeCheckCallOutput,
6263
}
6364

6465
func testBakePrint(t *testing.T, sb integration.Sandbox) {
@@ -1274,3 +1275,127 @@ target "default" {}
12741275
require.Empty(t, md.Default.BuildRef)
12751276
require.Len(t, md.Default.ResultJSON.Warnings, 3)
12761277
}
1278+
1279+
func testBakeCheckCallOutput(t *testing.T, sb integration.Sandbox) {
1280+
t.Run("check for warning count msg in check without warnings", func(t *testing.T) {
1281+
dockerfile := []byte(`
1282+
FROM busybox
1283+
COPY Dockerfile .
1284+
`)
1285+
bakefile := []byte(`
1286+
target "default" {}
1287+
`)
1288+
dir := tmpdir(
1289+
t,
1290+
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
1291+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
1292+
)
1293+
1294+
cmd := buildxCmd(
1295+
sb,
1296+
withDir(dir),
1297+
withArgs("bake", "--call", "check"),
1298+
)
1299+
stdout := bytes.Buffer{}
1300+
stderr := bytes.Buffer{}
1301+
cmd.Stdout = &stdout
1302+
cmd.Stderr = &stderr
1303+
require.NoError(t, cmd.Run(), stdout.String(), stderr.String())
1304+
require.Contains(t, stdout.String(), "Check complete, no warnings found.")
1305+
})
1306+
t.Run("check for warning count msg in check with single warning", func(t *testing.T) {
1307+
dockerfile := []byte(`
1308+
FROM busybox
1309+
copy Dockerfile .
1310+
`)
1311+
bakefile := []byte(`
1312+
target "default" {}
1313+
`)
1314+
dir := tmpdir(
1315+
t,
1316+
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
1317+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
1318+
)
1319+
1320+
cmd := buildxCmd(
1321+
sb,
1322+
withDir(dir),
1323+
withArgs("bake", "--call", "check"),
1324+
)
1325+
stdout := bytes.Buffer{}
1326+
stderr := bytes.Buffer{}
1327+
cmd.Stdout = &stdout
1328+
cmd.Stderr = &stderr
1329+
require.Error(t, cmd.Run(), stdout.String(), stderr.String())
1330+
require.Contains(t, stdout.String(), "Check complete, 1 warning has been found!")
1331+
})
1332+
t.Run("check for warning count msg in check with multiple warnings", func(t *testing.T) {
1333+
dockerfile := []byte(`
1334+
FROM busybox
1335+
copy Dockerfile .
1336+
1337+
FROM busybox as base
1338+
COPY Dockerfile .
1339+
`)
1340+
bakefile := []byte(`
1341+
target "default" {}
1342+
`)
1343+
dir := tmpdir(
1344+
t,
1345+
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
1346+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
1347+
)
1348+
1349+
cmd := buildxCmd(
1350+
sb,
1351+
withDir(dir),
1352+
withArgs("bake", "--call", "check"),
1353+
)
1354+
stdout := bytes.Buffer{}
1355+
stderr := bytes.Buffer{}
1356+
cmd.Stdout = &stdout
1357+
cmd.Stderr = &stderr
1358+
require.Error(t, cmd.Run(), stdout.String(), stderr.String())
1359+
require.Contains(t, stdout.String(), "Check complete, 2 warnings have been found!")
1360+
})
1361+
t.Run("check for warnings with multiple build targets", func(t *testing.T) {
1362+
dockerfile1 := []byte(`
1363+
FROM busybox
1364+
copy Dockerfile .
1365+
`)
1366+
dockerfile2 := []byte(`
1367+
FROM busybox
1368+
copy Dockerfile .
1369+
1370+
FROM busybox as base
1371+
COPY Dockerfile .
1372+
`)
1373+
bakefile := []byte(`
1374+
target "first" {
1375+
dockerfile = "Dockerfile.first"
1376+
}
1377+
target "second" {
1378+
dockerfile = "Dockerfile.second"
1379+
}
1380+
`)
1381+
dir := tmpdir(
1382+
t,
1383+
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
1384+
fstest.CreateFile("Dockerfile.first", dockerfile1, 0600),
1385+
fstest.CreateFile("Dockerfile.second", dockerfile2, 0600),
1386+
)
1387+
1388+
cmd := buildxCmd(
1389+
sb,
1390+
withDir(dir),
1391+
withArgs("bake", "--call", "check", "first", "second"),
1392+
)
1393+
stdout := bytes.Buffer{}
1394+
stderr := bytes.Buffer{}
1395+
cmd.Stdout = &stdout
1396+
cmd.Stderr = &stderr
1397+
require.Error(t, cmd.Run(), stdout.String(), stderr.String())
1398+
require.Contains(t, stdout.String(), "Check complete, 1 warning has been found!")
1399+
require.Contains(t, stdout.String(), "Check complete, 2 warnings have been found!")
1400+
})
1401+
}

tests/build.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){
7474
testBuildSecret,
7575
testBuildDefaultLoad,
7676
testBuildCall,
77+
testCheckCallOutput,
7778
}
7879

7980
func testBuild(t *testing.T, sb integration.Sandbox) {
@@ -1233,6 +1234,65 @@ COPy --from=base \
12331234
})
12341235
}
12351236

1237+
func testCheckCallOutput(t *testing.T, sb integration.Sandbox) {
1238+
t.Run("check for warning count msg in check without warnings", func(t *testing.T) {
1239+
dockerfile := []byte(`
1240+
FROM busybox AS base
1241+
COPY Dockerfile .
1242+
`)
1243+
dir := tmpdir(
1244+
t,
1245+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
1246+
)
1247+
1248+
cmd := buildxCmd(sb, withArgs("build", "--call=check", dir))
1249+
stdout := bytes.Buffer{}
1250+
stderr := bytes.Buffer{}
1251+
cmd.Stdout = &stdout
1252+
cmd.Stderr = &stderr
1253+
require.NoError(t, cmd.Run(), stdout.String(), stderr.String())
1254+
require.Contains(t, stdout.String(), "Check complete, no warnings found.")
1255+
})
1256+
1257+
t.Run("check for warning count msg in check with single warning", func(t *testing.T) {
1258+
dockerfile := []byte(`
1259+
FROM busybox as base
1260+
COPY Dockerfile .
1261+
`)
1262+
dir := tmpdir(
1263+
t,
1264+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
1265+
)
1266+
1267+
cmd := buildxCmd(sb, withArgs("build", "--call=check", dir))
1268+
stdout := bytes.Buffer{}
1269+
stderr := bytes.Buffer{}
1270+
cmd.Stdout = &stdout
1271+
cmd.Stderr = &stderr
1272+
require.Error(t, cmd.Run(), stdout.String(), stderr.String())
1273+
require.Contains(t, stdout.String(), "Check complete, 1 warning has been found!")
1274+
})
1275+
1276+
t.Run("check for warning count msg in check with multiple warnings", func(t *testing.T) {
1277+
dockerfile := []byte(`
1278+
frOM busybox as base
1279+
cOpy Dockerfile .
1280+
`)
1281+
dir := tmpdir(
1282+
t,
1283+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
1284+
)
1285+
1286+
cmd := buildxCmd(sb, withArgs("build", "--call=check", dir))
1287+
stdout := bytes.Buffer{}
1288+
stderr := bytes.Buffer{}
1289+
cmd.Stdout = &stdout
1290+
cmd.Stderr = &stderr
1291+
require.Error(t, cmd.Run(), stdout.String(), stderr.String())
1292+
require.Contains(t, stdout.String(), "Check complete, 2 warnings have been found!")
1293+
})
1294+
}
1295+
12361296
func createTestProject(t *testing.T) string {
12371297
dockerfile := []byte(`
12381298
FROM busybox:latest AS base

0 commit comments

Comments
 (0)