From 8b7350579cc329ab66943d2a9d5144faa212d993 Mon Sep 17 00:00:00 2001 From: Hyuga Tsukui Date: Thu, 29 Feb 2024 21:50:43 +0900 Subject: [PATCH] x/tools/cmd/deadcode: return an end code. return an end code when an unreachable code is detected. For: #66027 --- cmd/deadcode/deadcode.go | 3 +++ cmd/deadcode/deadcode_test.go | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cmd/deadcode/deadcode.go b/cmd/deadcode/deadcode.go index e0fce428d08..6312c0097f3 100644 --- a/cmd/deadcode/deadcode.go +++ b/cmd/deadcode/deadcode.go @@ -348,6 +348,9 @@ func main() { format = *formatFlag } printObjects(format, packages) + if len(packages) > 0 { + os.Exit(1) + } } // prettyName is a fork of Function.String designed to reduce diff --git a/cmd/deadcode/deadcode_test.go b/cmd/deadcode/deadcode_test.go index f17a1227362..205c8a1a169 100644 --- a/cmd/deadcode/deadcode_test.go +++ b/cmd/deadcode/deadcode_test.go @@ -113,17 +113,23 @@ func Test(t *testing.T) { cmd.Env = append(os.Environ(), "GOPROXY=", "GO111MODULE=on") var got string if err := cmd.Run(); err != nil { - if !tc.wantErr { - t.Fatalf("deadcode failed: %v (stderr=%s)", err, cmd.Stderr) + switch err.(type) { + case *exec.ExitError: + if tc.wantErr { + got = fmt.Sprint(cmd.Stderr) + } else { + // If an unreachable code is detected, exit code 1 is notified + if cmd.ProcessState.ExitCode() != 1 { + t.Fatalf("deadcode failed: %v", err) + } + got = fmt.Sprint(cmd.Stdout) + } + default: + t.Fatalf("deadcode failed: %v", err) } - got = fmt.Sprint(cmd.Stderr) } else { - if tc.wantErr { - t.Fatalf("deadcode succeeded unexpectedly (stdout=%s)", cmd.Stdout) - } got = fmt.Sprint(cmd.Stdout) } - // Check each want directive. for str, sense := range tc.want { ok := true