Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions cmd/go-mutesting/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"crypto/md5"
"fmt"
"go/ast"
Expand All @@ -17,6 +18,7 @@ import (
"regexp"
"strings"
"syscall"
"time"

"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -191,7 +193,12 @@ MUTATOR:

tmpDir, err := os.MkdirTemp("", "go-mutesting-")
if err != nil {
panic(err)
return exitError("Could not create tmp dir: %v", err)
}
if !opts.General.DoNotRemoveTmpFolder {
defer func() {
_ = os.RemoveAll(tmpDir)
}()
}
console.Verbose(opts, "Save mutations into %q", tmpDir)

Expand Down Expand Up @@ -225,15 +232,15 @@ MUTATOR:

err = os.MkdirAll(tmpDir+"/"+filepath.Dir(file), 0755)
if err != nil {
panic(err)
return exitError("Could not create dir for mutation file: %v", err)
}

tmpFile := tmpDir + "/" + file

originalFile := fmt.Sprintf("%s.original", tmpFile)
err = osutil.CopyFile(file, originalFile)
if err != nil {
panic(err)
return exitError("Could not copy original file %q: %v", file, err)
}
console.Debug(opts, "Save original into %q", originalFile)

Expand All @@ -255,14 +262,6 @@ MUTATOR:
}
}

if !opts.General.DoNotRemoveTmpFolder {
err = os.RemoveAll(tmpDir)
if err != nil {
panic(err)
}
console.Debug(opts, "Remove %q", tmpDir)
}

report.Calculate()

if !opts.Exec.NoExec {
Expand Down Expand Up @@ -515,7 +514,14 @@ func mutateExec(

console.Debug(opts, "Execute %q for mutation", opts.Exec.Exec)

execCommand := exec.Command(execs[0], execs[1:]...)
timeout := opts.Exec.Timeout
if timeout == 0 {
timeout = 10
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()

execCommand := exec.CommandContext(ctx, execs[0], execs[1:]...)

execCommand.Stderr = os.Stderr
execCommand.Stdout = os.Stdout
Expand All @@ -532,21 +538,16 @@ func mutateExec(
execCommand.Env = append(execCommand.Env, "TEST_RECURSIVE=true")
}

err := execCommand.Start()
if err != nil {
panic(err)
}

// TODO timeout here

err = execCommand.Wait()
err := execCommand.Run()

if err == nil {
execExitCode = 0
} else if ctx.Err() == context.DeadlineExceeded {
execExitCode = 2
} else if e, ok := err.(*exec.ExitError); ok {
execExitCode = e.Sys().(syscall.WaitStatus).ExitStatus()
} else {
panic(err)
return exitError("Exec command failed unexpectedly: %v", err)
}

return execExitCode
Expand Down