From 806ccd3545b93afc5df1aed3d14a45a9387530a5 Mon Sep 17 00:00:00 2001 From: Talon Bowler Date: Sun, 11 Aug 2024 21:58:01 -0700 Subject: [PATCH] print out the number of warnings after completing a rule check Signed-off-by: Talon Bowler --- commands/build.go | 22 ++++++-- tests/bake.go | 125 ++++++++++++++++++++++++++++++++++++++++++++++ tests/build.go | 60 ++++++++++++++++++++++ 3 files changed, 202 insertions(+), 5 deletions(-) diff --git a/commands/build.go b/commands/build.go index df1145de..049f19e0 100644 --- a/commands/build.go +++ b/commands/build.go @@ -891,17 +891,29 @@ func printResult(w io.Writer, f *controllerapi.CallFunc, res map[string]string) case "subrequests.describe": return 0, printValue(w, subrequests.PrintDescribe, subrequests.SubrequestsDescribeDefinition.Version, f.Format, res) case "lint": - err := printValue(w, lint.PrintLintViolations, lint.SubrequestLintDefinition.Version, f.Format, res) - if err != nil { - return 0, err - } - lintResults := lint.LintResults{} if result, ok := res["result.json"]; ok { if err := json.Unmarshal([]byte(result), &lintResults); err != nil { return 0, err } } + + warningCount := len(lintResults.Warnings) + if f.Format != "json" && warningCount > 0 { + var warningCountMsg string + if warningCount == 1 { + warningCountMsg = "1 warning has been found!" + } else if warningCount > 1 { + warningCountMsg = fmt.Sprintf("%d warnings have been found!", warningCount) + } + fmt.Fprintf(w, "Check complete, %s\n", warningCountMsg) + } + + err := printValue(w, lint.PrintLintViolations, lint.SubrequestLintDefinition.Version, f.Format, res) + if err != nil { + return 0, err + } + if lintResults.Error != nil { // Print the error message and the source // Normally, we would use `errdefs.WithSource` to attach the source to the diff --git a/tests/bake.go b/tests/bake.go index c89d290d..bafd4ba6 100644 --- a/tests/bake.go +++ b/tests/bake.go @@ -59,6 +59,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){ testBakeCallCheckFlag, testBakeCallMetadata, testBakeMultiPlatform, + testBakeCheckCallOutput, } func testBakePrint(t *testing.T, sb integration.Sandbox) { @@ -1274,3 +1275,127 @@ target "default" {} require.Empty(t, md.Default.BuildRef) require.Len(t, md.Default.ResultJSON.Warnings, 3) } + +func testBakeCheckCallOutput(t *testing.T, sb integration.Sandbox) { + t.Run("check for warning count msg in check without warnings", func(t *testing.T) { + dockerfile := []byte(` +FROM busybox +COPY Dockerfile . + `) + bakefile := []byte(` + target "default" {} + `) + dir := tmpdir( + t, + fstest.CreateFile("docker-bake.hcl", bakefile, 0600), + fstest.CreateFile("Dockerfile", dockerfile, 0600), + ) + + cmd := buildxCmd( + sb, + withDir(dir), + withArgs("bake", "--call", "check"), + ) + stdout := bytes.Buffer{} + stderr := bytes.Buffer{} + cmd.Stdout = &stdout + cmd.Stderr = &stderr + require.NoError(t, cmd.Run(), stdout.String(), stderr.String()) + require.Contains(t, stdout.String(), "Check complete, no warnings found.") + }) + t.Run("check for warning count msg in check with single warning", func(t *testing.T) { + dockerfile := []byte(` +FROM busybox +copy Dockerfile . + `) + bakefile := []byte(` + target "default" {} + `) + dir := tmpdir( + t, + fstest.CreateFile("docker-bake.hcl", bakefile, 0600), + fstest.CreateFile("Dockerfile", dockerfile, 0600), + ) + + cmd := buildxCmd( + sb, + withDir(dir), + withArgs("bake", "--call", "check"), + ) + stdout := bytes.Buffer{} + stderr := bytes.Buffer{} + cmd.Stdout = &stdout + cmd.Stderr = &stderr + require.Error(t, cmd.Run(), stdout.String(), stderr.String()) + require.Contains(t, stdout.String(), "Check complete, 1 warning has been found!") + }) + t.Run("check for warning count msg in check with multiple warnings", func(t *testing.T) { + dockerfile := []byte(` +FROM busybox +copy Dockerfile . + +FROM busybox as base +COPY Dockerfile . + `) + bakefile := []byte(` + target "default" {} + `) + dir := tmpdir( + t, + fstest.CreateFile("docker-bake.hcl", bakefile, 0600), + fstest.CreateFile("Dockerfile", dockerfile, 0600), + ) + + cmd := buildxCmd( + sb, + withDir(dir), + withArgs("bake", "--call", "check"), + ) + stdout := bytes.Buffer{} + stderr := bytes.Buffer{} + cmd.Stdout = &stdout + cmd.Stderr = &stderr + require.Error(t, cmd.Run(), stdout.String(), stderr.String()) + require.Contains(t, stdout.String(), "Check complete, 2 warnings have been found!") + }) + t.Run("check for warnings with multiple build targets", func(t *testing.T) { + dockerfile1 := []byte(` +FROM busybox +copy Dockerfile . + `) + dockerfile2 := []byte(` +FROM busybox +copy Dockerfile . + +FROM busybox as base +COPY Dockerfile . + `) + bakefile := []byte(` +target "first" { + dockerfile = "Dockerfile.first" +} +target "second" { + dockerfile = "Dockerfile.second" +} + `) + dir := tmpdir( + t, + fstest.CreateFile("docker-bake.hcl", bakefile, 0600), + fstest.CreateFile("Dockerfile.first", dockerfile1, 0600), + fstest.CreateFile("Dockerfile.second", dockerfile2, 0600), + ) + + cmd := buildxCmd( + sb, + withDir(dir), + withArgs("bake", "--call", "check", "first", "second"), + ) + stdout := bytes.Buffer{} + stderr := bytes.Buffer{} + cmd.Stdout = &stdout + cmd.Stderr = &stderr + require.Error(t, cmd.Run(), stdout.String(), stderr.String()) + require.Contains(t, stdout.String(), "Check complete, 1 warning has been found!") + require.Contains(t, stdout.String(), "Check complete, 2 warnings have been found!") + }) +} diff --git a/tests/build.go b/tests/build.go index 54792cb5..3517c926 100644 --- a/tests/build.go +++ b/tests/build.go @@ -74,6 +74,7 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){ testBuildSecret, testBuildDefaultLoad, testBuildCall, + testCheckCallOutput, } func testBuild(t *testing.T, sb integration.Sandbox) { @@ -1233,6 +1234,65 @@ COPy --from=base \ }) } +func testCheckCallOutput(t *testing.T, sb integration.Sandbox) { + t.Run("check for warning count msg in check without warnings", func(t *testing.T) { + dockerfile := []byte(` +FROM busybox AS base +COPY Dockerfile . + `) + dir := tmpdir( + t, + fstest.CreateFile("Dockerfile", dockerfile, 0600), + ) + + cmd := buildxCmd(sb, withArgs("build", "--call=check", dir)) + stdout := bytes.Buffer{} + stderr := bytes.Buffer{} + cmd.Stdout = &stdout + cmd.Stderr = &stderr + require.NoError(t, cmd.Run(), stdout.String(), stderr.String()) + require.Contains(t, stdout.String(), "Check complete, no warnings found.") + }) + + t.Run("check for warning count msg in check with single warning", func(t *testing.T) { + dockerfile := []byte(` +FROM busybox as base +COPY Dockerfile . + `) + dir := tmpdir( + t, + fstest.CreateFile("Dockerfile", dockerfile, 0600), + ) + + cmd := buildxCmd(sb, withArgs("build", "--call=check", dir)) + stdout := bytes.Buffer{} + stderr := bytes.Buffer{} + cmd.Stdout = &stdout + cmd.Stderr = &stderr + require.Error(t, cmd.Run(), stdout.String(), stderr.String()) + require.Contains(t, stdout.String(), "Check complete, 1 warning has been found!") + }) + + t.Run("check for warning count msg in check with multiple warnings", func(t *testing.T) { + dockerfile := []byte(` +frOM busybox as base +cOpy Dockerfile . + `) + dir := tmpdir( + t, + fstest.CreateFile("Dockerfile", dockerfile, 0600), + ) + + cmd := buildxCmd(sb, withArgs("build", "--call=check", dir)) + stdout := bytes.Buffer{} + stderr := bytes.Buffer{} + cmd.Stdout = &stdout + cmd.Stderr = &stderr + require.Error(t, cmd.Run(), stdout.String(), stderr.String()) + require.Contains(t, stdout.String(), "Check complete, 2 warnings have been found!") + }) +} + func createTestProject(t *testing.T) string { dockerfile := []byte(` FROM busybox:latest AS base