From 4f4b49af7b361d505253caeb928f1a8cfacd3449 Mon Sep 17 00:00:00 2001 From: Muhammad Saboor Date: Sun, 17 May 2026 16:37:09 +0500 Subject: [PATCH] fix missing diff in json and html reports when exec is provided --- cmd/go-mutesting/main.go | 38 +++++++++++++++++------------------ cmd/go-mutesting/main_test.go | 3 +++ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/cmd/go-mutesting/main.go b/cmd/go-mutesting/main.go index 5749905..fd1c575 100644 --- a/cmd/go-mutesting/main.go +++ b/cmd/go-mutesting/main.go @@ -425,32 +425,32 @@ func mutateExec( execs []string, mutant *models.Mutant, ) (execExitCode int) { - if len(execs) == 0 { - console.Debug(opts, "Execute built-in exec command for mutation") - - diff, err := exec.Command("diff", "--label=Original", "--label=New", "-u", file, mutationFile).CombinedOutput() - - startLine := parser.FindOriginalStartLine(diff) - mutant.Mutator.OriginalStartLine = startLine - - if err == nil { - execExitCode = 0 - } else if e, ok := err.(*exec.ExitError); ok { - execExitCode = e.Sys().(syscall.WaitStatus).ExitStatus() + execExitCode = 0 + diff, err := exec.Command("diff", "--label=Original", "--label=New", "-u", file, mutationFile).CombinedOutput() + if err != nil { + if e, ok := err.(*exec.ExitError); ok { + execExitCode = e.ExitCode() } else { panic(err) } - if execExitCode != 0 && execExitCode != 1 { - fmt.Printf("%s\n", diff) + } - panic("Could not execute diff on mutation file") - } + if execExitCode != 0 && execExitCode != 1 { + fmt.Printf("%s\n", diff) + panic("Could not execute diff on mutation file") + } + + mutant.Mutator.OriginalStartLine = parser.FindOriginalStartLine(diff) + mutant.Diff = string(diff) + + if len(execs) == 0 { + console.Debug(opts, "Execute built-in exec command for mutation") defer func() { _ = os.Rename(file+".tmp", file) }() - err = os.Rename(file, file+".tmp") + err := os.Rename(file, file+".tmp") if err != nil { panic(err) } @@ -480,8 +480,6 @@ func mutateExec( fmt.Printf("%s\n", test) } - mutant.Diff = string(diff) - switch execExitCode { case 0: // Tests passed -> FAIL if !opts.Config.SilentMode { @@ -532,7 +530,7 @@ func mutateExec( execCommand.Env = append(execCommand.Env, "TEST_RECURSIVE=true") } - err := execCommand.Start() + err = execCommand.Start() if err != nil { panic(err) } diff --git a/cmd/go-mutesting/main_test.go b/cmd/go-mutesting/main_test.go index fc5fc61..7734855 100644 --- a/cmd/go-mutesting/main_test.go +++ b/cmd/go-mutesting/main_test.go @@ -125,6 +125,9 @@ func TestMainJSONReport(t *testing.T) { for i := 0; i < len(mutationReport.Escaped); i++ { assert.Contains(t, mutationReport.Escaped[i].ProcessOutput, "FAIL") } + for i := 0; i < len(mutationReport.Escaped); i++ { + assert.Greater(t, len(mutationReport.Escaped[i].Diff), 0) + } for i := 0; i < len(mutationReport.Killed); i++ { assert.Contains(t, mutationReport.Killed[i].ProcessOutput, "PASS") }