Skip to content

Commit

Permalink
refactor: simplify the mutator run in the correct folder test (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini authored Sep 28, 2022
1 parent 772cc33 commit a8fbacc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 46 deletions.
23 changes: 11 additions & 12 deletions internal/engine/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -202,6 +201,7 @@ func TestMutatorTestExecution(t *testing.T) {
const expectedTimeout = 10 * time.Second

type commandHolder struct {
cmd *exec.Cmd
command string
args []string
timeout time.Duration
Expand Down Expand Up @@ -455,11 +455,8 @@ func TestMutatorRunInTheCorrectFolder(t *testing.T) {
executor.Start(w)
wg.Wait()

workerName := fmt.Sprintf("%s-%d", w.Name, w.ID)
rootDir, _ := wdDealer.Get(workerName)
execDir := filepath.Join(rootDir, callingDir)
if mut.Workdir() != execDir {
t.Errorf("expected working dir to be %s, got %s", execDir, mut.Workdir())
if mut.Workdir() != holder.cmd.Dir {
t.Errorf("expected working dir to be %s, got %s", holder.cmd.Dir, mut.Workdir())
}
})
}
Expand All @@ -479,15 +476,17 @@ func fakeExecCommandSuccessWithHolder(got *commandHolder) execContext {
dl, _ := ctx.Deadline()
got.m.Lock()
defer got.m.Unlock()
if got != nil {
got.command = command
got.args = args
got.timeout = time.Until(dl)
}

cs := []string{"-test.run=TestCoverageProcessSuccess", "--", command}
cs = append(cs, args...)
cmd := getCmd(ctx, cs)

got.cmd = cmd
got.command = command
got.args = args
got.timeout = time.Until(dl)

return getCmd(ctx, cs)
return cmd
}
}

Expand Down
38 changes: 4 additions & 34 deletions internal/engine/stubs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,49 +77,19 @@ func filenameFromFixture(fix string) string {

type dealerStub struct {
t *testing.T

mutex *sync.RWMutex
cacheDirs map[string]string
}

func newWdDealerStub(t *testing.T) *dealerStub {
t.Helper()

return &dealerStub{
t: t,
mutex: &sync.RWMutex{},
cacheDirs: make(map[string]string),
}
}

func (d *dealerStub) Get(idf string) (string, error) {
dir, ok := d.fromCache(idf)
if ok {
return dir, nil
}

tmpDir := d.t.TempDir()
d.setCache(idf, tmpDir)

return tmpDir, nil

}

func (d *dealerStub) fromCache(key string) (string, bool) {
d.mutex.RLock()
defer d.mutex.RUnlock()
dir, ok := d.cacheDirs[key]

return dir, ok
return &dealerStub{t: t}
}

func (d *dealerStub) setCache(key, value string) {
d.mutex.Lock()
defer d.mutex.Unlock()
d.cacheDirs[key] = value
func (d dealerStub) Get(_ string) (string, error) {
return d.t.TempDir(), nil
}

func (*dealerStub) Clean() {}
func (dealerStub) Clean() {}

type executorDealerStub struct {
gotMutants []mutator.Mutator
Expand Down

0 comments on commit a8fbacc

Please sign in to comment.